You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**IMPORTANT:** In a project developed by more than one person there is a small probability of migrations incosistency - e.g. two developers can create conflicting migrations, and the developer that created his migration later gets it merged to the repository first.
14
+
Keep an eye on such cases (and be even more careful when cherry picking).
15
+
16
+
Consider making your migrations idempotent - we can run the same sql code twice in a row with the same result. This makes our migrations more robust. On the other hand, it causes slightly less control over database schema - e.g. let's say you forgot to drop the table in down migration. You run down migration - the table is still there. When you run up migration again - `CREATE TABLE` would return an error, helping you find an issue in down migration, while `CREATE TABLE IF NOT EXISTS` would not. Use those conditions wisely.
17
+
18
+
In case you would like to run several commands/queries in one migration, you should wrap them in a transaction (if your database supports it).
19
+
This way if one of commands fails, our database will remain unchanged.
20
+
21
+
## Run migrations
22
+
Run your migrations through the CLI or your app and check if they applied expected changes.
23
+
Just to give you an idea:
24
+
```
25
+
migrate -database YOUR_DATBASE_URL -path PATH_TO_YOUR_MIGRATIONS up
26
+
```
27
+
28
+
Just add the code to your app and you're ready to go!
29
+
30
+
Before commiting your migrations you should run your migrations up, down, and then up again to see if migrations are working properly both ways.
31
+
(e.g. if you created a table in a migration but reverse migration did not delete it, you will encounter an error when running the forward migration again)
32
+
It's also worth checking your migrations in a separate, containerized environment. You can find some tools in the end of this document.
33
+
34
+
**IMPORTANT:** If you would like to run multiple instances of your app on different machines be sure to use a database that supports locking when running migrations. Otherwise you may encounter issues.
If there were no errors, we should have two files available under `db/migrations` folder:
23
24
- 000001_create_users_table.down.sql
24
25
- 000001_create_users_table.up.sql
25
-
Note the `sql` extension that we provided.
26
26
27
-
**IMPORTANT:** In a project developed by more than one person there is a small probability of migrations incosistency - e.g. two developers can create conflicting migrations, and the developer that created his migration later gets it merged to the repository first.
28
-
Keep an eye on such cases (and be even more careful when cherry picking).
27
+
Note the `sql` extension that we provided.
29
28
30
29
In the `.up.sql` file let's create the table:
31
30
```
@@ -40,10 +39,46 @@ And in the `.down.sql` let's delete it:
40
39
```
41
40
DROP TABLE IF EXISTS users;
42
41
```
43
-
By adding `IF EXISTS/IF NOT EXISTS` we are making migrations idempotent - we can run the same sql code twice in a row with the same result. This makes our migrations more robust. On the other hand, it causes slightly less control over database schema - e.g. let's say you forgot to drop the table in down migration. You run down migration - the table is still there. When you run up migration again - `CREATE TABLE` would return an error, helping you find an issue in down migration, while `CREATE TABLE IF NOT EXISTS` would not. Use those conditions wisely.
42
+
By adding `IF EXISTS/IF NOT EXISTS` we are making migrations idempotent - you can read more about idempotency in [getting started](GETTING_STARTED.md#create-migrations)
44
43
45
-
In case you would like to run several commands/queries in one migration, you should wrap them in a transaction (if your database supports it).
46
-
In our postgres example it would be (migration up):
44
+
## Run migrations
45
+
```
46
+
migrate -database ${POSTGRESQL_URL} -path db/migrations up
47
+
```
48
+
Let's check if the table was created properly by running `psql example -c "\d users"`.
Great! Now let's check if running reverse migration also works:
81
-
```
82
-
migrate -database ${POSTGRESQL_URL} -path db/migrations down
83
-
```
84
-
Make sure to check if your database changed as expected in this case as well.
85
-
86
-
**IMPORTANT:** Before commiting your migrations you should run your migrations up, down, and then up again to see if migrations are working properly both ways.
87
-
(e.g. if you created a table in a migration but reverse migration did not delete it, you will encounter an error when running the forward migration again)
88
-
It's also worth checking your migrations in a separate, containerized environment. You can find some tools in the end of this tutorial.
89
124
90
125
## Optional: Run migrations within your Go app
91
126
Here is a very simple app running migrations for the above configuration:
@@ -110,15 +145,4 @@ func main() {
110
145
}
111
146
}
112
147
```
113
-
You can find details [here](README.md#use-in-your-go-project)
114
-
115
-
Just add the code to your app and you're ready to go!
116
-
117
-
**IMPORTANT:** If you would like to run multiple instances of your app on different machines be sure to use a database that supports locking when running migrations. Otherwise you may encounter issues.
118
-
119
-
## Further reading:
120
-
-[Best practices](MIGRATIONS.md)
121
-
-[FAQ](FAQ.md)
122
-
- Tools for testing your migrations in a container:
123
-
- https://github.com/dhui/dktest
124
-
- https://github.com/ory/dockertest
148
+
You can find details [here](README.md#use-in-your-go-project)
0 commit comments