Skip to content

Commit 01a828c

Browse files
authored
Merge pull request #3 from danielmorell/docs
Added some more documentation
2 parents 024389b + c62a45f commit 01a828c

File tree

2 files changed

+43
-23
lines changed

2 files changed

+43
-23
lines changed

README.md

Lines changed: 42 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
1-
# SQLX Migrator (SQLXM)
1+
# sqlx migrator (sqlxm)
22

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.
45

56
## Features
67

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
89
will run each migration once.
910

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.
1217

1318
## Best Practices
1419

1520
**Run on startup.** For most applications the best time to run your DB migrations is on start up.
1621

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+
1727
## Installation
1828

1929
It is not too complicated.
@@ -24,11 +34,11 @@ $ go get github.com/danielmorell/sqlxm
2434

2535
## Basic Usage
2636

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.
2838

2939
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.
3242
3. `Migrator.Run()` takes all the previous migrations added with `Migrator.AddMigration()` and makes sure they have been
3343
applied to database or applies them.
3444

@@ -59,8 +69,11 @@ func main() {
5969

6070
// Add a few migrations
6171
xm.AddMigration(
72+
// A human-readable key for the migration
6273
"create_user_table",
74+
// A comment to explain what the migration does
6375
"Add the initial user table",
76+
// The migration SQL statement
6477
`CREATE TABLE users (
6578
id SERIAL
6679
CONSTRAINT users_pk PRIMARY KEY,
@@ -105,8 +118,8 @@ func main() {
105118

106119
### Migration Hashing
107120

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.
110123
This keeps the code you used to run past migrations and the current state of your database from getting out of sync.
111124

112125
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.
116129

117130
### Safe Mode
118131

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.
121135

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()`.
124140

125141
### Hash Repair
126142

127143
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.
130146

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
132148
update the hash of previous migrations.
133149

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
135151
integrity of the migration source with the already run migration.
136152

137153
### Backends
@@ -144,11 +160,15 @@ integrity of the migration source with the already run migration.
144160

145161
You can easily write your own backend by implementing the `Backend` interface from the `sqlxm/backends` package.
146162

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
149165
backend that you registered.
150166

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.
152172

153173
## Testing
154174

@@ -159,7 +179,7 @@ Because a database connection is required to run tests, I recommend using Docker
159179
**1. Create `.env` File**
160180

161181
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.
163183

164184
**2. Start Docker Containers**
165185

sqlxm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var registeredBackends = map[string]backends.Backend{
5050
"sqlite": &backends.SQLite{},
5151
}
5252

53-
// RegisterBackend adds a new DB Backend to SQLXM for Migrator to use to run
53+
// RegisterBackend adds a new DB Backend to sqlxm for Migrator to use to run
5454
// queries. A backend handles peculiarities in SQL dialects and can help
5555
// abstract alternate implementations.
5656
func RegisterBackend(key string, backend backends.Backend) error {

0 commit comments

Comments
 (0)