1
- # SQLX Migrator (SQLXM )
1
+ # sqlx migrator (sqlxm )
2
2
3
- SQLXM runs a set of migrations against a DB.
3
+ sqlxm runs a set of migrations against a DB. It is designed to work with the popular
4
+ [ sqlx] ( https://github.com/jmoiron/sqlx ) package by Jason Moiron.
4
5
5
6
## Features
6
7
7
- ** Idempotent** One of the critical things required in DB schema changes is ensuring the changes are made once. SQLXM
8
+ ** Idempotent** One of the critical things required in DB schema changes is ensuring the changes are made once. sqlxm
8
9
will run each migration once.
9
10
10
- ** Data Migrations** Because it is common for schema changes to also require data changes, SQLXM will let you run any SQL
11
- query you want. You read that right. If you can write a SQL query SQLXM can run it.
11
+ ** Data Migrations** Because it is common for schema changes to also require data changes, sqlxm will let you run any SQL
12
+ query you want. You read that right. If you can write a SQL query sqlxm can run it.
13
+
14
+ ** Migration Integrity** The accidental editing of SQL migration statements can introduce subtle bugs into an
15
+ application. To prevent this sqlxm validates the integrity of every previously run query against its current
16
+ version using a checksum.
12
17
13
18
## Best Practices
14
19
15
20
** Run on startup.** For most applications the best time to run your DB migrations is on start up.
16
21
22
+ ** Don't remove old migrations.** As your application ages and changes the number of migrations will grow. Since they
23
+ only need to be run once on your production database it may be tempting to prune some of them. However, it is
24
+ recommended that you keep them. This makes creating new development, testing, or staging environments reproducible,
25
+ deterministic and consistent with production.
26
+
17
27
## Installation
18
28
19
29
It is not too complicated.
@@ -24,11 +34,11 @@ $ go get github.com/danielmorell/sqlxm
24
34
25
35
## Basic Usage
26
36
27
- There are three basic functions that set up SQLX Migrator and start running your migrations.
37
+ There are three basic functions that set up sqlx migrator and start running your migrations.
28
38
29
39
1 . ` sqlxm.New() ` creates a new ` sqlxm.Migrator ` instance that can be used to track and run migrations.
30
- 2 . ` Migrator.AddMigration() ` creates a new migration to run and keep track of. Migrations are run in the order they
31
- are added.
40
+ 2 . ` Migrator.AddMigration() ` creates a new migration to run and keep track of. Migrations are run in the order they are
41
+ added.
32
42
3 . ` Migrator.Run() ` takes all the previous migrations added with ` Migrator.AddMigration() ` and makes sure they have been
33
43
applied to database or applies them.
34
44
@@ -59,8 +69,11 @@ func main() {
59
69
60
70
// Add a few migrations
61
71
xm.AddMigration (
72
+ // A human-readable key for the migration
62
73
" create_user_table" ,
74
+ // A comment to explain what the migration does
63
75
" Add the initial user table" ,
76
+ // The migration SQL statement
64
77
` CREATE TABLE users (
65
78
id SERIAL
66
79
CONSTRAINT users_pk PRIMARY KEY,
@@ -105,8 +118,8 @@ func main() {
105
118
106
119
### Migration Hashing
107
120
108
- SQLXM creates a hash of the migration statement and arguments. This ensures that any change to the migration query
109
- itself or any arguments will not go unnoticed. This hash is used as a checksum to validate previously run migrations.
121
+ sqlxm creates a hash of the migration statement and arguments. This ensures that any change to the migration query
122
+ itself or any arguments will not go unnoticed. This hash is used as a checksum to validate previously run migrations.
110
123
This keeps the code you used to run past migrations and the current state of your database from getting out of sync.
111
124
112
125
You may be wondering what the rational is for doing this. It would be too easy to accidentally, change a column from
@@ -116,22 +129,25 @@ returning `NULL` when it is not expected.
116
129
117
130
### Safe Mode
118
131
119
- For the most part it is recommended that you run migrations in ** safe mode** . It works a bit like a compile error in
120
- Go. If there is a potential to create an error or unknown state SQLXM will stop the migration.
132
+ For the most part it is recommended that you run migrations in ** safe mode** . You do this by simply calling the
133
+ ` Migrator.Run() ` method. It works a bit like a compile error in Go. If there is a potential to create an error or
134
+ unknown state sqlxm will stop the migration.
121
135
122
- SQLXM does this by checking the hash stored in the database with the hash of the migration. If the hash check fails,
123
- the migration stops and a hash mismatch error is returned.
136
+ sqlxm does this by checking the hash stored in the database with the hash of the migration. If the hash check fails, the
137
+ migration stops and a hash mismatch error is returned.
138
+
139
+ If you want to run the migrations in ** unsafe mode** , you can do so by calling ` Migrator.RunUnsafe() ` .
124
140
125
141
### Hash Repair
126
142
127
143
There are times when non-substantive changes (like indentation) may be made to a migration query. * For the most part,
128
- changing migration queries is a bad idea and should be avoided.* But it is recognized that ` alter table ` and `ALTER
129
- TABLE` do the same thing but produce a different hash.
144
+ changing migration queries is a bad idea and should be avoided.* But it is recognized that ` alter table `
145
+ and ` ALTER TABLE` do the same thing but produce a different hash.
130
146
131
- In a scenario where you need to update the hash of the migration, you can use the ` Migrator.RepairHash() ` method to
147
+ In a scenario where you need to update the hash of the migration, you can use the ` Migrator.RepairHash() ` method to
132
148
update the hash of previous migrations.
133
149
134
- ** Note:** safe mode will not prevent you from writing ` DROP TABLE users ` as a migration. It simply validates the
150
+ ** Note:** safe mode will not prevent you from writing ` DROP TABLE users ` as a migration. It simply validates the
135
151
integrity of the migration source with the already run migration.
136
152
137
153
### Backends
@@ -144,11 +160,15 @@ integrity of the migration source with the already run migration.
144
160
145
161
You can easily write your own backend by implementing the ` Backend ` interface from the ` sqlxm/backends ` package.
146
162
147
- You will need to register your custom backend by calling the ` RegisterBackend() ` function. Then you can tell your
148
- ` Migrator ` instance to use that backend by calling the ` Migrator.UseBackend() ` method and passing in the key for the
163
+ You will need to register your custom backend by calling the ` RegisterBackend() ` function. Then you can tell your
164
+ ` Migrator ` instance to use that backend by calling the ` Migrator.UseBackend() ` method and passing in the key for the
149
165
backend that you registered.
150
166
151
- Note you cannot overwrite an existing backend. However, you can simply specify a new key.
167
+ Note: you cannot overwrite an existing backend. However, you can simply specify a new key.
168
+
169
+ If you use one of the common database drivers for a DBMS with a pre-build backend, sqlxm should automatically know
170
+ what backend to use. This helps reduce the boilerplate needed to run migrations. However, if you are using a special
171
+ database driver you can always call ` Migrator.UseBackend() ` to specify the backend you want to use.
152
172
153
173
## Testing
154
174
@@ -159,7 +179,7 @@ Because a database connection is required to run tests, I recommend using Docker
159
179
** 1. Create ` .env ` File**
160
180
161
181
Copy the ` .sample.env ` file to ` .env ` and make any needed changes to the values. This ` .env ` file will be read by both
162
- Docker and the SQLXM tests.
182
+ Docker and the sqlxm tests.
163
183
164
184
** 2. Start Docker Containers**
165
185
0 commit comments