dbmate 1.16.0 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Adrian Macneil
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,24 +1,548 @@
1
1
  # Dbmate
2
2
 
3
- Nodejs wrapper for [amacneil/dbmate](https://github.com/amacneil/dbmate)
3
+ [![Release](https://img.shields.io/github/release/amacneil/dbmate.svg)](https://github.com/amacneil/dbmate/releases)
4
+ [![Go Report](https://goreportcard.com/badge/github.com/amacneil/dbmate)](https://goreportcard.com/report/github.com/amacneil/dbmate)
5
+ [![Reference](https://img.shields.io/badge/go.dev-reference-blue?logo=go&logoColor=white)](https://pkg.go.dev/github.com/amacneil/dbmate/v2/pkg/dbmate)
6
+
7
+ Dbmate is a database migration tool that will keep your database schema in sync across multiple developers and your production servers.
8
+
9
+ It is a standalone command line tool that can be used with Go, Node.js, Python, Ruby, PHP, or any other language or framework you are using to write database-backed applications. This is especially helpful if you are writing multiple services in different languages, and want to maintain some sanity with consistent development tools.
10
+
11
+ For a comparison between dbmate and other popular database schema migration tools, please see [Alternatives](#alternatives).
12
+
13
+ ## Table of Contents
14
+
15
+ - [Features](#features)
16
+ - [Installation](#installation)
17
+ - [Commands](#commands)
18
+ - [Command Line Options](#command-line-options)
19
+ - [Usage](#usage)
20
+ - [Connecting to the Database](#connecting-to-the-database)
21
+ - [PostgreSQL](#postgresql)
22
+ - [MySQL](#mysql)
23
+ - [SQLite](#sqlite)
24
+ - [ClickHouse](#clickhouse)
25
+ - [Creating Migrations](#creating-migrations)
26
+ - [Running Migrations](#running-migrations)
27
+ - [Rolling Back Migrations](#rolling-back-migrations)
28
+ - [Migration Options](#migration-options)
29
+ - [Waiting For The Database](#waiting-for-the-database)
30
+ - [Exporting Schema File](#exporting-schema-file)
31
+ - [Library](#library)
32
+ - [Use dbmate as a library](#use-dbmate-as-a-library)
33
+ - [Embedding migrations](#embedding-migrations)
34
+ - [Concepts](#concepts)
35
+ - [Migration files](#migration-files)
36
+ - [Schema file](#schema-file)
37
+ - [Schema migrations table](#schema-migrations-table)
38
+ - [Alternatives](#alternatives)
39
+ - [Contributing](#contributing)
40
+
41
+ ## Features
42
+
43
+ - Supports MySQL, PostgreSQL, SQLite, and ClickHouse.
44
+ - Uses plain SQL for writing schema migrations.
45
+ - Migrations are timestamp-versioned, to avoid version number conflicts with multiple developers.
46
+ - Migrations are run atomically inside a transaction.
47
+ - Supports creating and dropping databases (handy in development/test).
48
+ - Supports saving a `schema.sql` file to easily diff schema changes in git.
49
+ - Database connection URL is defined using an environment variable (`DATABASE_URL` by default), or specified on the command line.
50
+ - Built-in support for reading environment variables from your `.env` file.
51
+ - Easy to distribute, single self-contained binary.
52
+
53
+ ## Installation
54
+
55
+ **NPM**
56
+
57
+ Install using [NPM](https://www.npmjs.com/):
58
+
59
+ ```sh
60
+ $ npm install --save-dev dbmate
61
+ $ npx dbmate --help
62
+ ```
63
+
64
+ **macOS**
65
+
66
+ Install using [Homebrew](https://brew.sh/):
67
+
68
+ ```sh
69
+ $ brew install dbmate
70
+ ```
71
+
72
+ **Linux**
73
+
74
+ Install the binary directly:
75
+
76
+ ```sh
77
+ $ sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
78
+ $ sudo chmod +x /usr/local/bin/dbmate
79
+ ```
80
+
81
+ **Windows**
82
+
83
+ Install using [Scoop](https://scoop.sh)
84
+
85
+ ```pwsh
86
+ scoop install dbmate
87
+ ```
88
+
89
+ **Docker**
90
+
91
+ Docker images are published to both Docker Hub ([`amacneil/dbmate`](https://hub.docker.com/r/amacneil/dbmate)) and Github Container Registry ([`ghcr.io/amacneil/dbmate`](https://ghcr.io/amacneil/dbmate)).
92
+
93
+ Remember to set `--network=host` or see [this comment](https://github.com/amacneil/dbmate/issues/128#issuecomment-615924611) for more tips on using dbmate with docker networking):
94
+
95
+ ```sh
96
+ $ docker run --rm -it --network=host ghcr.io/amacneil/dbmate --help
97
+ ```
98
+
99
+ If you wish to create or apply migrations, you will need to use Docker's [bind mount](https://docs.docker.com/storage/bind-mounts/) feature to make your local working directory (`pwd`) available inside the dbmate container:
100
+
101
+ ```sh
102
+ $ docker run --rm -it --network=host -v "$(pwd)/db:/db" ghcr.io/amacneil/dbmate new create_users_table
103
+ ```
104
+
105
+ **Heroku**
106
+
107
+ To use dbmate on Heroku, either use the NPM method, or store the linux binary in your git repository:
108
+
109
+ ```sh
110
+ $ mkdir -p bin
111
+ $ curl -fsSL -o bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
112
+ $ chmod +x bin/dbmate
113
+ $ git add bin/dbmate
114
+ $ git commit -m "Add dbmate binary"
115
+ $ git push heroku master
116
+ $ heroku run bin/dbmate --help
117
+ ```
118
+
119
+ ## Commands
120
+
121
+ ```sh
122
+ dbmate --help # print usage help
123
+ dbmate new # generate a new migration file
124
+ dbmate up # create the database (if it does not already exist) and run any pending migrations
125
+ dbmate create # create the database
126
+ dbmate drop # drop the database
127
+ dbmate migrate # run any pending migrations
128
+ dbmate rollback # roll back the most recent migration
129
+ dbmate down # alias for rollback
130
+ dbmate status # show the status of all migrations (supports --exit-code and --quiet)
131
+ dbmate dump # write the database schema.sql file
132
+ dbmate wait # wait for the database server to become available
133
+ ```
134
+
135
+ ### Command Line Options
136
+
137
+ The following options are available with all commands. You must use command line arguments in the order `dbmate [global options] command [command options]`. Most options can also be configured via environment variables (and loaded from your `.env` file, which is helpful to share configuration between team members).
138
+
139
+ - `--url, -u "protocol://host:port/dbname"` - specify the database url directly. _(env: `DATABASE_URL`)_
140
+ - `--env, -e "DATABASE_URL"` - specify an environment variable to read the database connection URL from.
141
+ - `--migrations-dir, -d "./db/migrations"` - where to keep the migration files. _(env: `DBMATE_MIGRATIONS_DIR`)_
142
+ - `--migrations-table "schema_migrations"` - database table to record migrations in. _(env: `DBMATE_MIGRATIONS_TABLE`)_
143
+ - `--schema-file, -s "./db/schema.sql"` - a path to keep the schema.sql file. _(env: `DBMATE_SCHEMA_FILE`)_
144
+ - `--no-dump-schema` - don't auto-update the schema.sql file on migrate/rollback _(env: `DBMATE_NO_DUMP_SCHEMA`)_
145
+ - `--wait` - wait for the db to become available before executing the subsequent command _(env: `DBMATE_WAIT`)_
146
+ - `--wait-timeout 60s` - timeout for --wait flag _(env: `DBMATE_WAIT_TIMEOUT`)_
147
+
148
+ ## Usage
149
+
150
+ ### Connecting to the Database
151
+
152
+ Dbmate locates your database using the `DATABASE_URL` environment variable by default. If you are writing a [twelve-factor app](http://12factor.net/), you should be storing all connection strings in environment variables.
153
+
154
+ To make this easy in development, dbmate looks for a `.env` file in the current directory, and treats any variables listed there as if they were specified in the current environment (existing environment variables take preference, however).
155
+
156
+ If you do not already have a `.env` file, create one and add your database connection URL:
157
+
158
+ ```sh
159
+ $ cat .env
160
+ DATABASE_URL="postgres://postgres@127.0.0.1:5432/myapp_development?sslmode=disable"
161
+ ```
162
+
163
+ `DATABASE_URL` should be specified in the following format:
164
+
165
+ ```
166
+ protocol://username:password@host:port/database_name?options
167
+ ```
168
+
169
+ - `protocol` must be one of `mysql`, `postgres`, `postgresql`, `sqlite`, `sqlite3`, `clickhouse`
170
+ - `host` can be either a hostname or IP address
171
+ - `options` are driver-specific (refer to the underlying Go SQL drivers if you wish to use these)
172
+
173
+ Dbmate can also load the connection URL from a different environment variable. For example, before running your test suite, you may wish to drop and recreate the test database. One easy way to do this is to store your test database connection URL in the `TEST_DATABASE_URL` environment variable:
174
+
175
+ ```sh
176
+ $ cat .env
177
+ DATABASE_URL="postgres://postgres@127.0.0.1:5432/myapp_dev?sslmode=disable"
178
+ TEST_DATABASE_URL="postgres://postgres@127.0.0.1:5432/myapp_test?sslmode=disable"
179
+ ```
180
+
181
+ You can then specify this environment variable in your test script (Makefile or similar):
182
+
183
+ ```sh
184
+ $ dbmate -e TEST_DATABASE_URL drop
185
+ Dropping: myapp_test
186
+ $ dbmate -e TEST_DATABASE_URL --no-dump-schema up
187
+ Creating: myapp_test
188
+ Applying: 20151127184807_create_users_table.sql
189
+ ```
190
+
191
+ Alternatively, you can specify the url directly on the command line:
192
+
193
+ ```sh
194
+ $ dbmate -u "postgres://postgres@127.0.0.1:5432/myapp_test?sslmode=disable" up
195
+ ```
196
+
197
+ The only advantage of using `dbmate -e TEST_DATABASE_URL` over `dbmate -u $TEST_DATABASE_URL` is that the former takes advantage of dbmate's automatic `.env` file loading.
198
+
199
+ #### PostgreSQL
200
+
201
+ When connecting to Postgres, you may need to add the `sslmode=disable` option to your connection string, as dbmate by default requires a TLS connection (some other frameworks/languages allow unencrypted connections by default).
202
+
203
+ ```sh
204
+ DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?sslmode=disable"
205
+ ```
206
+
207
+ A `socket` or `host` parameter can be specified to connect through a unix socket (note: specify the directory only):
208
+
209
+ ```sh
210
+ DATABASE_URL="postgres://username:password@/database_name?socket=/var/run/postgresql"
211
+ ```
212
+
213
+ A `search_path` parameter can be used to specify the [current schema](https://www.postgresql.org/docs/13/ddl-schemas.html#DDL-SCHEMAS-PATH) while applying migrations, as well as for dbmate's `schema_migrations` table.
214
+ If the schema does not exist, it will be created automatically. If multiple comma-separated schemas are passed, the first will be used for the `schema_migrations` table.
215
+
216
+ ```sh
217
+ DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?search_path=myschema"
218
+ ```
219
+
220
+ ```sh
221
+ DATABASE_URL="postgres://username:password@127.0.0.1:5432/database_name?search_path=myschema,public"
222
+ ```
223
+
224
+ #### MySQL
225
+
226
+ ```sh
227
+ DATABASE_URL="mysql://username:password@127.0.0.1:3306/database_name"
228
+ ```
229
+
230
+ A `socket` parameter can be specified to connect through a unix socket:
231
+
232
+ ```sh
233
+ DATABASE_URL="mysql://username:password@/database_name?socket=/var/run/mysqld/mysqld.sock"
234
+ ```
235
+
236
+ #### SQLite
237
+
238
+ SQLite databases are stored on the filesystem, so you do not need to specify a host. By default, files are relative to the current directory. For example, the following will create a database at `./db/database.sqlite3`:
239
+
240
+ ```sh
241
+ DATABASE_URL="sqlite:db/database.sqlite3"
242
+ ```
243
+
244
+ To specify an absolute path, add a forward slash to the path. The following will create a database at `/tmp/database.sqlite3`:
245
+
246
+ ```sh
247
+ DATABASE_URL="sqlite:/tmp/database.sqlite3"
248
+ ```
249
+
250
+ #### ClickHouse
251
+
252
+ ```sh
253
+ DATABASE_URL="clickhouse://username:password@127.0.0.1:9000/database_name"
254
+ ```
255
+
256
+ [See other supported connection options](https://github.com/ClickHouse/clickhouse-go#dsn).
257
+
258
+ ### Creating Migrations
259
+
260
+ To create a new migration, run `dbmate new create_users_table`. You can name the migration anything you like. This will create a file `db/migrations/20151127184807_create_users_table.sql` in the current directory:
261
+
262
+ ```sql
263
+ -- migrate:up
264
+
265
+ -- migrate:down
266
+ ```
267
+
268
+ To write a migration, simply add your SQL to the `migrate:up` section:
269
+
270
+ ```sql
271
+ -- migrate:up
272
+ create table users (
273
+ id integer,
274
+ name varchar(255),
275
+ email varchar(255) not null
276
+ );
277
+
278
+ -- migrate:down
279
+ ```
280
+
281
+ > Note: Migration files are named in the format `[version]_[description].sql`. Only the version (defined as all leading numeric characters in the file name) is recorded in the database, so you can safely rename a migration file without having any effect on its current application state.
282
+
283
+ ### Running Migrations
284
+
285
+ Run `dbmate up` to run any pending migrations.
286
+
287
+ ```sh
288
+ $ dbmate up
289
+ Creating: myapp_development
290
+ Applying: 20151127184807_create_users_table.sql
291
+ Writing: ./db/schema.sql
292
+ ```
293
+
294
+ > Note: `dbmate up` will create the database if it does not already exist (assuming the current user has permission to create databases). If you want to run migrations without creating the database, run `dbmate migrate`.
295
+
296
+ Pending migrations are always applied in numerical order. However, dbmate does not prevent migrations from being applied out of order if they are committed independently (for example: if a developer has been working on a branch for a long time, and commits a migration which has a lower version number than other already-applied migrations, dbmate will simply apply the pending migration). See [#159](https://github.com/amacneil/dbmate/issues/159) for a more detailed explanation.
297
+
298
+ ### Rolling Back Migrations
299
+
300
+ By default, dbmate doesn't know how to roll back a migration. In development, it's often useful to be able to revert your database to a previous state. To accomplish this, implement the `migrate:down` section:
301
+
302
+ ```sql
303
+ -- migrate:up
304
+ create table users (
305
+ id integer,
306
+ name varchar(255),
307
+ email varchar(255) not null
308
+ );
309
+
310
+ -- migrate:down
311
+ drop table users;
312
+ ```
313
+
314
+ Run `dbmate rollback` to roll back the most recent migration:
315
+
316
+ ```sh
317
+ $ dbmate rollback
318
+ Rolling back: 20151127184807_create_users_table.sql
319
+ Writing: ./db/schema.sql
320
+ ```
321
+
322
+ ### Migration Options
323
+
324
+ dbmate supports options passed to a migration block in the form of `key:value` pairs. List of supported options:
325
+
326
+ - `transaction`
327
+
328
+ **transaction**
329
+
330
+ `transaction` is useful if you need to run some SQL which cannot be executed from within a transaction. For example, in Postgres, you would need to disable transactions for migrations that alter an enum type to add a value:
331
+
332
+ ```sql
333
+ -- migrate:up transaction:false
334
+ ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
335
+ ```
336
+
337
+ `transaction` will default to `true` if your database supports it.
338
+
339
+ ### Waiting For The Database
340
+
341
+ If you use a Docker development environment for your project, you may encounter issues with the database not being immediately ready when running migrations or unit tests. This can be due to the database server having only just started.
342
+
343
+ In general, your application should be resilient to not having a working database connection on startup. However, for the purpose of running migrations or unit tests, this is not practical. The `wait` command avoids this situation by allowing you to pause a script or other application until the database is available. Dbmate will attempt a connection to the database server every second, up to a maximum of 60 seconds.
344
+
345
+ If the database is available, `wait` will return no output:
346
+
347
+ ```sh
348
+ $ dbmate wait
349
+ ```
350
+
351
+ If the database is unavailable, `wait` will block until the database becomes available:
352
+
353
+ ```sh
354
+ $ dbmate wait
355
+ Waiting for database....
356
+ ```
357
+
358
+ You can also use the `--wait` flag with other commands if you sometimes see failures caused by the database not yet being ready:
359
+
360
+ ```sh
361
+ $ dbmate --wait up
362
+ Waiting for database....
363
+ Creating: myapp_development
364
+ ```
365
+
366
+ You can customize the timeout using `--wait-timeout` (default 60s). If the database is still not available, the command will return an error:
367
+
368
+ ```sh
369
+ $ dbmate --wait-timeout=5s wait
370
+ Waiting for database.....
371
+ Error: unable to connect to database: dial tcp 127.0.0.1:5432: connect: connection refused
372
+ ```
373
+
374
+ Please note that the `wait` command does not verify whether your specified database exists, only that the server is available and ready (so it will return success if the database server is available, but your database has not yet been created).
375
+
376
+ ### Exporting Schema File
377
+
378
+ When you run the `up`, `migrate`, or `rollback` commands, dbmate will automatically create a `./db/schema.sql` file containing a complete representation of your database schema. Dbmate keeps this file up to date for you, so you should not manually edit it.
379
+
380
+ It is recommended to check this file into source control, so that you can easily review changes to the schema in commits or pull requests. It's also possible to use this file when you want to quickly load a database schema, without running each migration sequentially (for example in your test harness). However, if you do not wish to save this file, you could add it to your `.gitignore`, or pass the `--no-dump-schema` command line option.
381
+
382
+ To dump the `schema.sql` file without performing any other actions, run `dbmate dump`. Unlike other dbmate actions, this command relies on the respective `pg_dump`, `mysqldump`, or `sqlite3` commands being available in your PATH. If these tools are not available, dbmate will silenty skip the schema dump step during `up`, `migrate`, or `rollback` actions. You can diagnose the issue by running `dbmate dump` and looking at the output:
383
+
384
+ ```sh
385
+ $ dbmate dump
386
+ exec: "pg_dump": executable file not found in $PATH
387
+ ```
388
+
389
+ On Ubuntu or Debian systems, you can fix this by installing `postgresql-client`, `mysql-client`, or `sqlite3` respectively. Ensure that the package version you install is greater than or equal to the version running on your database server.
390
+
391
+ > Note: The `schema.sql` file will contain a complete schema for your database, even if some tables or columns were created outside of dbmate migrations.
4
392
 
5
393
  ## Library
6
394
 
7
- ```typescript
8
- import { DbMate } from 'dbmate';
395
+ ### Use dbmate as a library
396
+
397
+ Dbmate is designed to be used as a CLI with any language or framework, but it can also be used as a library in a Go application.
398
+
399
+ Here is a simple example. Remember to import the driver you need!
400
+
401
+ ```go
402
+ package main
403
+
404
+ import (
405
+ "net/url"
406
+
407
+ "github.com/amacneil/dbmate/v2/pkg/dbmate"
408
+ _ "github.com/amacneil/dbmate/v2/pkg/driver/sqlite"
409
+ )
410
+
411
+ func main() {
412
+ u, _ := url.Parse("sqlite:foo.sqlite3")
413
+ db := dbmate.New(u)
414
+
415
+ err := db.CreateAndMigrate()
416
+ if err != nil {
417
+ panic(err)
418
+ }
419
+ }
420
+ ```
421
+
422
+ See the [reference documentation](https://pkg.go.dev/github.com/amacneil/dbmate/v2/pkg/dbmate) for more options.
423
+
424
+ ### Embedding migrations
425
+
426
+ Migrations can be embedded into your application binary using Go's [embed](https://pkg.go.dev/embed) functionality.
427
+
428
+ Use `db.FS` to specify the filesystem used for reading migrations:
429
+
430
+ ```go
431
+ package main
9
432
 
10
- // construct a dbmate instance using a database url string
11
- // see https://github.com/amacneil/dbmate#usage for more details
12
- const dbmate = new DbMate('db://user:pass@host:port/db?opt');
433
+ import (
434
+ "embed"
435
+ "fmt"
436
+ "net/url"
13
437
 
14
- // invoke up, down, drop as necessary
15
- await dbmate.up();
438
+ "github.com/amacneil/dbmate/v2/pkg/dbmate"
439
+ _ "github.com/amacneil/dbmate/v2/pkg/driver/sqlite"
440
+ )
441
+
442
+ //go:embed db/migrations/*.sql
443
+ var fs embed.FS
444
+
445
+ func main() {
446
+ u, _ := url.Parse("sqlite:foo.sqlite3")
447
+ db := dbmate.New(u)
448
+ db.FS = fs
449
+
450
+ fmt.Println("Migrations:")
451
+ migrations, err := db.FindMigrations()
452
+ if err != nil {
453
+ panic(err)
454
+ }
455
+ for _, m := range migrations {
456
+ fmt.Println(m.Version, m.FilePath)
457
+ }
458
+
459
+ fmt.Println("\nApplying...")
460
+ err = db.CreateAndMigrate()
461
+ if err != nil {
462
+ panic(err)
463
+ }
464
+ }
16
465
  ```
17
466
 
18
- ## CLI
467
+ ## Concepts
468
+
469
+ ### Migration files
19
470
 
20
- The `dbmate` cli wraps the precompiled dbmate binaries and passes all command line options. The cli wrapper will detect the appropriate binary for your system.
471
+ Migration files are very simple, and are stored in `./db/migrations` by default. You can create a new migration file named `[date]_create_users.sql` by running `dbmate new create_users`.
472
+ Here is an example:
21
473
 
22
- ## License
474
+ ```sql
475
+ -- migrate:up
476
+ create table users (
477
+ id integer,
478
+ name varchar(255),
479
+ );
23
480
 
24
- See LICENSE.md
481
+ -- migrate:down
482
+ drop table if exists users;
483
+ ```
484
+
485
+ Both up and down migrations are stored in the same file, for ease of editing. Both up and down directives are required, even if you choose not to implement the down migration.
486
+
487
+ When you apply a migration dbmate only stores the version number, not the contents, so you should always rollback a migration before modifying its contents. For this reason, you can safely rename a migration file without affecting its applied status, as long as you keep the version number intact.
488
+
489
+ ### Schema file
490
+
491
+ The schema file is written to `./db/schema.sql` by default. It is a complete dump of your database schema, including any applied migrations, and any other modifications you have made.
492
+
493
+ This file should be checked in to source control, so that you can easily compare the diff of a migration. You can use the schema file to quickly restore your database without needing to run all migrations.
494
+
495
+ ### Schema migrations table
496
+
497
+ Dbmate stores a record of each applied migration in table named `schema_migrations`. This table will be created for you automatically if it does not already exist.
498
+
499
+ The table is very simple:
500
+
501
+ ```sql
502
+ CREATE TABLE IF NOT EXISTS schema_migrations (
503
+ version VARCHAR(255) PRIMARY KEY
504
+ )
505
+ ```
506
+
507
+ You can customize the name of this table using the `--migrations-table` flag or `DBMATE_MIGRATIONS_TABLE` environment variable.
508
+
509
+ ## Alternatives
510
+
511
+ Why another database schema migration tool? Dbmate was inspired by many other tools, primarily [Active Record Migrations](http://guides.rubyonrails.org/active_record_migrations.html), with the goals of being trivial to configure, and language & framework independent. Here is a comparison between dbmate and other popular migration tools.
512
+
513
+ | | [dbmate](https://github.com/amacneil/dbmate) | [goose](https://github.com/pressly/goose) | [sql-migrate](https://github.com/rubenv/sql-migrate) | [golang-migrate](https://github.com/golang-migrate/migrate) | [activerecord](http://guides.rubyonrails.org/active_record_migrations.html) | [sequelize](http://docs.sequelizejs.com/manual/tutorial/migrations.html) | [flyway](https://flywaydb.org/) |
514
+ | ------------------------------------------------------------ | :------------------------------------------: | :---------------------------------------: | :--------------------------------------------------: | :---------------------------------------------------------: | :-------------------------------------------------------------------------: | :----------------------------------------------------------------------: | :-----------------------------: |
515
+ | **Features** |
516
+ | Plain SQL migration files | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | | :white_check_mark: |
517
+ | Support for creating and dropping databases | :white_check_mark: | | | | :white_check_mark: | | |
518
+ | Support for saving schema dump files | :white_check_mark: | | | | :white_check_mark: | | |
519
+ | Timestamp-versioned migration files | :white_check_mark: | :white_check_mark: | | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
520
+ | Custom schema migrations table | :white_check_mark: | | :white_check_mark: | | | :white_check_mark: | :white_check_mark: |
521
+ | Ability to wait for database to become ready | :white_check_mark: | | | | | | |
522
+ | Database connection string loaded from environment variables | :white_check_mark: | | | | | | :white_check_mark: |
523
+ | Automatically load .env file | :white_check_mark: | | | | | | |
524
+ | No separate configuration file | :white_check_mark: | | | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
525
+ | Language/framework independent | :white_check_mark: | :white_check_mark: | | :white_check_mark: | | | :white_check_mark: |
526
+ | **Drivers** |
527
+ | PostgreSQL | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
528
+ | MySQL | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
529
+ | SQLite | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: |
530
+ | CliсkHouse | :white_check_mark: | | | :white_check_mark: | :white_check_mark: | :white_check_mark: | |
531
+
532
+ _If you notice any inaccuracies in this table, please [propose a change](https://github.com/amacneil/dbmate/edit/main/README.md)._
533
+
534
+ ## Contributing
535
+
536
+ Dbmate is written in Go, pull requests are welcome.
537
+
538
+ Tests are run against a real database using docker-compose. To build a docker image and run the tests:
539
+
540
+ ```sh
541
+ $ make docker-all
542
+ ```
543
+
544
+ To start a development shell:
545
+
546
+ ```sh
547
+ $ make docker-sh
548
+ ```
package/bin/dbmate.js ADDED
@@ -0,0 +1,21 @@
1
+ #!/usr/bin/env node
2
+
3
+ const { arch, platform } = require("process");
4
+ const { realpathSync } = require("fs");
5
+ const { spawnSync } = require("child_process");
6
+ const { fileURLToPath } = require("url");
7
+ const { dirname } = require("path");
8
+
9
+ const packageName = `@dbmate/${platform}-${arch}`;
10
+ const binName = platform === "win32" ? "dbmate.exe" : "dbmate";
11
+ let binPath = `${packageName}/bin/${binName}`;
12
+
13
+ try {
14
+ binPath = require.resolve(binPath);
15
+ } catch (error) {
16
+ console.error(`Error: Unable to locate package ${packageName}`);
17
+ process.exit(1);
18
+ }
19
+
20
+ const child = spawnSync(binPath, process.argv.slice(2), { stdio: "inherit" });
21
+ process.exit(child.status);
package/package.json CHANGED
@@ -1,34 +1,29 @@
1
1
  {
2
2
  "name": "dbmate",
3
- "version": "1.16.0",
4
- "scripts": {
5
- "build": "tsc",
6
- "build:docs": "typedoc ./src/*.ts",
7
- "prepublish": "npm run build",
8
- "test": "jest",
9
- "prettier:check": "prettier --check 'src/**/*.ts' 'spec/**/*.ts'",
10
- "prettier:fix": "prettier --write 'src/**/*.ts' 'spec/**/*.ts'",
11
- "lint": "npm run prettier:check"
12
- },
13
- "repository": "gitlab:defunctzombie/node-dbmate",
14
- "main": "dist/index.js",
3
+ "version": "2.0.0",
4
+ "description": "A lightweight, framework-agnostic database migration tool",
5
+ "repository": "https://github.com/amacneil/dbmate",
6
+ "homepage": "https://github.com/amacneil/dbmate#readme",
7
+ "author": "Adrian Macneil",
8
+ "license": "MIT",
9
+ "keywords": [
10
+ "clickhouse",
11
+ "database",
12
+ "migration",
13
+ "mysql",
14
+ "postgres",
15
+ "schema",
16
+ "sqlite"
17
+ ],
15
18
  "bin": {
16
- "dbmate": "dist/cli.js"
19
+ "dbmate": "bin/dbmate.js"
17
20
  },
18
- "files": [
19
- "yarn.lock",
20
- "package.json",
21
- "binaries",
22
- "dist",
23
- "LICENSE.md",
24
- "README.md"
25
- ],
26
- "devDependencies": {
27
- "@types/jest": "25.1.4",
28
- "@types/node": "13.9.0",
29
- "jest": "25.1.0",
30
- "prettier": "1.19.1",
31
- "ts-jest": "25.2.1",
32
- "typescript": "3.8.3"
21
+ "optionalDependencies": {
22
+ "@dbmate/linux-x64": "2.0.0",
23
+ "@dbmate/linux-arm64": "2.0.0",
24
+ "@dbmate/linux-arm": "2.0.0",
25
+ "@dbmate/darwin-x64": "2.0.0",
26
+ "@dbmate/darwin-arm64": "2.0.0",
27
+ "@dbmate/win32-x64": "2.0.0"
33
28
  }
34
- }
29
+ }
package/LICENSE.md DELETED
@@ -1,55 +0,0 @@
1
- # Blue Oak Model License
2
-
3
- Version 1.0.0
4
-
5
- ## Purpose
6
-
7
- This license gives everyone as much permission to work with
8
- this software as possible, while protecting contributors
9
- from liability.
10
-
11
- ## Acceptance
12
-
13
- In order to receive this license, you must agree to its
14
- rules. The rules of this license are both obligations
15
- under that agreement and conditions to your license.
16
- You must not do anything with this software that triggers
17
- a rule that you cannot or will not follow.
18
-
19
- ## Copyright
20
-
21
- Each contributor licenses you to do everything with this
22
- software that would otherwise infringe that contributor's
23
- copyright in it.
24
-
25
- ## Notices
26
-
27
- You must ensure that everyone who gets a copy of
28
- any part of this software from you, with or without
29
- changes, also gets the text of this license or a link to
30
- <https://blueoakcouncil.org/license/1.0.0>.
31
-
32
- ## Excuse
33
-
34
- If anyone notifies you in writing that you have not
35
- complied with [Notices](#notices), you can keep your
36
- license by taking all practical steps to comply within 30
37
- days after the notice. If you do not do so, your license
38
- ends immediately.
39
-
40
- ## Patent
41
-
42
- Each contributor licenses you to do everything with this
43
- software that would otherwise infringe any patent claims
44
- they can license or become able to license.
45
-
46
- ## Reliability
47
-
48
- No contributor can revoke this license.
49
-
50
- ## No Liability
51
-
52
- ***As far as the law allows, this software comes as is,
53
- without any warranty or condition, and no contributor
54
- will be liable to anyone for any damages related to this
55
- software or this license, under any kind of legal claim.***
Binary file
Binary file
Binary file
Binary file
package/dist/DbMate.d.ts DELETED
@@ -1,14 +0,0 @@
1
- declare type Args = {
2
- migrationsDir?: string;
3
- };
4
- declare class DbMate {
5
- private binaryPath;
6
- private dbUrl;
7
- private args;
8
- constructor(dbUrl: string, args?: Args);
9
- up(): Promise<void>;
10
- down(): Promise<void>;
11
- drop(): Promise<void>;
12
- private buildCliArgs;
13
- }
14
- export default DbMate;
package/dist/DbMate.js DELETED
@@ -1,100 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- var child_process_1 = require("child_process");
40
- var resolve_1 = require("./resolve");
41
- var DbMate = /** @class */ (function () {
42
- function DbMate(dbUrl, args) {
43
- this.binaryPath = resolve_1.calculateBinaryPath();
44
- this.dbUrl = dbUrl;
45
- this.args = args;
46
- }
47
- DbMate.prototype.up = function () {
48
- return __awaiter(this, void 0, void 0, function () {
49
- var cmd;
50
- return __generator(this, function (_a) {
51
- cmd = this.binaryPath + " --env DB_URL " + this.buildCliArgs() + " up";
52
- child_process_1.execSync(cmd, {
53
- env: {
54
- DB_URL: this.dbUrl,
55
- },
56
- });
57
- return [2 /*return*/];
58
- });
59
- });
60
- };
61
- DbMate.prototype.down = function () {
62
- return __awaiter(this, void 0, void 0, function () {
63
- var cmd;
64
- return __generator(this, function (_a) {
65
- cmd = this.binaryPath + " --env DB_URL " + this.buildCliArgs() + " down";
66
- child_process_1.execSync(cmd, {
67
- env: {
68
- DB_URL: this.dbUrl,
69
- },
70
- });
71
- return [2 /*return*/];
72
- });
73
- });
74
- };
75
- DbMate.prototype.drop = function () {
76
- return __awaiter(this, void 0, void 0, function () {
77
- var cmd;
78
- return __generator(this, function (_a) {
79
- cmd = this.binaryPath + " --env DB_URL " + this.buildCliArgs() + " drop";
80
- child_process_1.execSync(cmd, {
81
- env: {
82
- DB_URL: this.dbUrl,
83
- },
84
- });
85
- return [2 /*return*/];
86
- });
87
- });
88
- };
89
- DbMate.prototype.buildCliArgs = function () {
90
- var _a;
91
- // additional cli args
92
- var cliArgs = [];
93
- if (((_a = this.args) === null || _a === void 0 ? void 0 : _a.migrationsDir) != undefined) {
94
- cliArgs.push("-d \"" + this.args.migrationsDir + "\"");
95
- }
96
- return cliArgs.join(' ');
97
- };
98
- return DbMate;
99
- }());
100
- exports.default = DbMate;
package/dist/cli.d.ts DELETED
@@ -1,2 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
package/dist/cli.js DELETED
@@ -1,13 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- var resolve_1 = require("./resolve");
5
- var child_process_1 = require("child_process");
6
- var binaryPath = resolve_1.calculateBinaryPath();
7
- var args = process.argv.slice(2);
8
- var ps = child_process_1.spawn(binaryPath, args, {
9
- stdio: 'inherit',
10
- });
11
- ps.on('close', function (code) {
12
- process.exit(code);
13
- });
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import DbMate from './DbMate';
2
- export { DbMate };
package/dist/index.js DELETED
@@ -1,4 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var DbMate_1 = require("./DbMate");
4
- exports.DbMate = DbMate_1.default;
package/dist/resolve.d.ts DELETED
@@ -1,3 +0,0 @@
1
- /** Return a path to the dbmate binary */
2
- declare function calculateBinaryPath(): string;
3
- export { calculateBinaryPath };
package/dist/resolve.js DELETED
@@ -1,34 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var path_1 = require("path");
4
- var fs_1 = require("fs");
5
- function archName() {
6
- switch (process.arch) {
7
- case 'x64':
8
- return 'amd64';
9
- case 'arm64':
10
- return 'arm64';
11
- default:
12
- throw new Error("Unsupported architecture " + process.arch);
13
- }
14
- }
15
- function platformName() {
16
- switch (process.platform) {
17
- case 'darwin':
18
- return 'macos';
19
- case 'linux':
20
- return 'linux';
21
- default:
22
- throw new Error("Unsupported platform: " + process.platform);
23
- }
24
- }
25
- /** Return a path to the dbmate binary */
26
- function calculateBinaryPath() {
27
- var binaryName = "dbmate-" + platformName() + "-" + archName();
28
- var binaryPath = path_1.join(__dirname, '..', 'binaries', binaryName);
29
- if (!fs_1.existsSync(binaryPath)) {
30
- throw new Error("Could not locate binary: " + binaryPath);
31
- }
32
- return binaryPath;
33
- }
34
- exports.calculateBinaryPath = calculateBinaryPath;