aboutsummaryrefslogtreecommitdiffstats
path: root/docs/reference.md
blob: 595a0a4de7ac87087885fbaa53f9253e8bb24f73 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# Reference

This section provides detailed reference information for Bolt's commands,
configuration options, and supported databases.

## Database Compatibility

Bolt supports the following databases:

- **PostgreSQL** - 13+
- **MySQL** - 5.7+
- **Microsoft SQL Server** - 2017+
- **SQLite3** - All recent versions. We explicitly test against the latest
  version.

## Commands

### `bolt new`

Create a new database migration.

```bash
bolt new [-message|-m] <message>
```

**Options:**

- `-message <message>` - Message to use for the migration (default: "autogenerated")
- `-m <message>` - Alias for `-message`

**Example:**

```bash
bolt new -m "create users table"
```

### `bolt up`

Apply migrations against the database.

```bash
bolt up [-version|-v] <version>
```

**Options:**

- `-version <version>` - The version to upgrade up to and including (optional)
- `-v <version>` - Alias for `-version`

**Examples:**

```bash
# Apply all pending migrations
bolt up

# Apply migrations up to and including version 20240316145038
bolt up -v 20240316145038
```

### `bolt down`

Downgrade migrations against the database.

```bash
bolt down [-version|-v] <version>
```

**Options:**

- `-version <version>` - The version to downgrade down to and including (optional)
- `-v <version>` - Alias for `-version`

**Examples:**

```bash
# Revert the most recent migration
bolt down

# Revert migrations down to and including version 20240316145038
bolt down -v 20240316145038
```

### `bolt status`

List the database migrations and their statuses.

```bash
bolt status
```

**Example output:**

The migrations will be ordered in the same in the same order that they would be
executed.

```
Version           Message               Applied
20240316145038    my_first_migration    X
20240317120000    add_email_index
```

### `bolt version`

Show the current version of Bolt.

```bash
bolt version
```

## Configuration

Bolt can be configured using a `bolt.toml` configuration file and environment
variables. Environment variables take precedence over configuration file
settings if both are utilized.

### Configuration File

Bolt searches for a `bolt.toml` file in your current working directory or any
parent directory.

**Complete example:**

```toml
[migrations]
# The directory where your migration scripts are located.
# Defaults to "migrations". This directory is relative to
# the current working directory. You may also use an absolute
# path.
directory_path = "migrations"

# The migration versioning style you prefer. Supported options
# are "timestamp" and "sequential". Defaults to "timestamp".
version_style = "timestamp"

[database]
# The DSN (Data Source Name) for connecting to your database.
dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=disable"

# The name of the database driver to use to connect to
# the database. Either "postgresql", "mysql", "mssql", or "sqlite3".
driver = "postgresql"

# The name of the database table to create for managing
# the applied migration versions. Defaults to "bolt_migrations".
migrations_table = "bolt_migrations"
```

### Environment Variables

All configuration file settings have corresponding environment variables:

| Environment Variable | Configuration Key | Description |
|---------------------|------------------|-------------|
| `BOLT_MIGRATIONS_DIR_PATH` | `migrations.directory_path` | Path to migrations directory |
| `BOLT_MIGRATIONS_VERSION_STYLE` | `migrations.version_style` | Versioning style (`timestamp` or `sequential`) |
| `BOLT_DB_DSN` | `database.dsn` | Database connection string |
| `BOLT_DB_DRIVER` | `database.driver` | Database driver name |
| `BOLT_DB_MIGRATIONS_TABLE` | `database.migrations_table` | Name of migrations tracking table |

**Example:**

```bash
export BOLT_DB_DSN="postgresql://user:password@localhost:5432/mydb?sslmode=disable"
export BOLT_DB_DRIVER="postgresql"
export BOLT_MIGRATIONS_DIR_PATH="db/migrations"
```

## Database Connection (DSN)

Bolt uses DSN (Data Source Name) for database connections. This allows you to
use any connection format supported by the database driver.

### What is a DSN?

A DSN (Data Source Name) is a string that contains all the information needed
to connect to a database. It includes the database type, connection parameters,
and optional driver-specific options.

### PostgreSQL

**Driver:** [pgx](https://github.com/jackc/pgx)

[DSN Documentation](https://github.com/jackc/pgx/blob/v5.7.6/pgxpool/pool.go#L352)

**Format:**
```
postgresql://username:password@hostname:port/database?option1=value1&option2=value2
```

**Examples:**

```toml
# Basic connection
dsn = "postgresql://user:password@localhost:5432/mydb"

# With SSL disabled
dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=disable"

# With multiple options
dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=require&connect_timeout=10&application_name=bolt"

# Using Unix socket
dsn = "postgresql://user:password@/mydb?host=/var/run/postgresql"
```

### MySQL

**Driver:** [go-sql-driver/mysql](https://github.com/go-sql-driver/mysql)

[DSN Documentation](https://github.com/go-sql-driver/mysql#dsn-data-source-name)

**Format:**
```
username:password@tcp(hostname:port)/database?option1=value1&option2=value2
```

**Examples:**

```toml
# Basic connection
dsn = "user:password@tcp(localhost:3306)/mydb"

# With parse time and charset
dsn = "user:password@tcp(localhost:3306)/mydb?parseTime=true&charset=utf8mb4"

# With connection timeout and TLS
dsn = "user:password@tcp(localhost:3306)/mydb?timeout=10s&tls=true"

# Using Unix socket
dsn = "user:password@unix(/tmp/mysql.sock)/mydb"
```

### SQL Server (MSSQL)

**Driver:** [microsoft/go-mssqldb](https://github.com/microsoft/go-mssqldb)

[DSN Documentation](https://github.com/microsoft/go-mssqldb#connection-parameters-and-dsn)

**Format:**
```
sqlserver://username:password@hostname:port?database=database&option1=value1&option2=value2
```

**Examples:**

```toml
# Basic connection
dsn = "sqlserver://user:password@localhost:1433?database=mydb"

# With encryption and timeout
dsn = "sqlserver://user:password@localhost:1433?database=mydb&encrypt=disable&connection+timeout=30"

# With instance name
dsn = "sqlserver://user:password@localhost\\SQLEXPRESS?database=mydb"

# Using Windows authentication
dsn = "sqlserver://localhost:1433?database=mydb&trusted_connection=yes"
```

### SQLite

**Driver:** [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)

[DSN Documentation](https://github.com/mattn/go-sqlite3#connection-string)

**Format:**
```
/path/to/database.db?option1=value1&option2=value2
```

**Examples:**

```toml
# Basic file-based database
dsn = "/path/to/mydatabase.db"

# In-memory database
dsn = ":memory:"

# With read-only mode
dsn = "/path/to/mydatabase.db?mode=ro"

# With foreign keys enabled
dsn = "/path/to/mydatabase.db?_foreign_keys=on"
```

## Migration Script Directives

Bolt supports several directives that can be added to migration files to
control behavior.

### `-- depends:`

Declares dependencies on other migrations. Dependencies determine execution order.

**Syntax:**
```sql
-- depends: version1, version2, version3
```

**Format Rules:**

- Multiple versions are separated by commas
- Whitespace around commas is optional (`version1,version2` and `version1, version2` are equivalent)
- The directive is case-insensitive (`-- DEPENDS:`, `-- depends:`, `-- Depends:` all work)
- Version numbers must exactly match existing migration version numbers

**Examples:**

No dependencies (root migration):
```sql
-- migrate:up
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255)
);

-- migrate:down
DROP TABLE users;
```

Single dependency:
```sql
-- depends: 20240316145038
-- migrate:up
CREATE TABLE posts (
    id SERIAL PRIMARY KEY,
    user_id INTEGER NOT NULL REFERENCES users(id)
);

-- migrate:down
DROP TABLE posts;
```

Multiple dependencies:
```sql
-- depends: 20240316145038, 20240317120000, 20240318093000
-- migrate:up
CREATE TABLE comments (
    id SERIAL PRIMARY KEY,
    post_id INTEGER NOT NULL REFERENCES posts(id),
    user_id INTEGER NOT NULL REFERENCES users(id)
);

-- migrate:down
DROP TABLE comments;
```

**Execution Order:**

Migrations are executed in topological order:
1. Migrations with no dependencies run first
2. Once a migration completes, migrations that depend only on completed
   migrations become eligible
3. When multiple migrations are eligible simultaneously, they execute in
   version order (timestamp or sequential) for deterministic behavior

**Automatic Population:**

When you create a new migration with `bolt new`, Bolt automatically populates
the `-- depends:` directive with current "leaf" migrations (migrations that
no other migration depends on).

First migration in a project:
```bash
bolt new -m "create users"
# Generated file has no -- depends: line
```

Second migration:
```bash
bolt new -m "create posts"
# Generated file has: -- depends: 20240316145038
```

After parallel migrations merge:
```bash
# If migrations A and B both exist and neither depends on the other
bolt new -m "create feature needing both"
# Generated file has: -- depends: <version_A>, <version_B>
```

### `-- migrate:up` and `-- migrate:down`

These directives separate the upgrade and downgrade portions of your migration.

**Syntax:**
```sql
-- migrate:up [option1] [option2]
-- migrate:down [option1] [option2]
```

**Example:**
```sql
-- migrate:up
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(255) NOT NULL
);

-- migrate:down
DROP TABLE users;
```

### Available Options

Options can be added to the `-- migrate:up` or `-- migrate:down` directives.

#### `transaction:false`

Execute the migration script without a transaction. By default, every
migration script is executed within a transaction.

**Example:**

```sql
-- migrate:up transaction:false
CREATE INDEX CONCURRENTLY idx_users_email ON users(email);

-- migrate:down transaction:false
DROP INDEX CONCURRENTLY idx_users_email;
```

## Version Styles

Bolt supports two migration versioning styles:

### Timestamp (Default)

Migrations are versioned using timestamps (e.g., `20240316145038`).

**Configuration:**
```toml
[migrations]
version_style = "timestamp"
```

### Sequential

Migrations are versioned using incrementing integers (e.g., `001`, `002`, `003`).

**Configuration:**
```toml
[migrations]
version_style = "sequential"
```