28 Commits

Author SHA1 Message Date
jdb 4d38011455 Add status command to show the current state of a database wrt. migrations. 0.4.1 2025-09-01 21:41:41 -05:00
jdb f5943a69f0 Allow going down while in an inconsistent state.
Previously we prevent the user migrating the database in either
direction if there were missing or out-of-order migrations. However,
going down should always be safe (assuming proper down scripts were
written) and often going down is the proper way to resolve out-of-order
migrations. Going down should always be done thoughtfully (it is highly
likely to lead to data loss), and really should not be done in a
production setting.

However, in a development environment, it is not unusual to get into
states where you have missing scripts. For example, if there are test
scripts that apply test data to a development environment on top of the
formal application schema, you may need to back out the test migrations
before creating new application migrations to avoid getting into an
"inconsistent state." Consider the following migrations:

- 1-initial-schema
- 2-add-feature-x
- T1-test-user-data

If you then add *3-add-feature-y*, you will  be in an inconsistent
state, as the expected ordering from db_migrate's point of view will be:

- 1-initial-schema
- 2-add-feature-x
- 3-add-feature-y
- T1-test-user-data

With the previous behavior db_migrate refuses to do anything and you
must manually back-out the test migrations before applying the new
migration. With the new behavior you can easily go down to back out the
test migrations automatically before going back up with the new migrations.

As an aside:

Another way to avoid this is to name test migrations in such a way that
they stay inline with the changes they are built on top of:

- 1-initial-schema
- 1.1-test-user-data
- 2-add-feature-x
- 2.1-feature-x-test-data

But sometimes the previous pattern is preferable, as it allows you to
have test migrations that evolve and match the "current state of test
data" rather than a developer needing to layer the contents of multiple
migrations to get a clear picture of the test data. This same issue
applies to non-test migrations, but db_migrate exists to solve the
use-case where you generally prefer to have traversible, immutable
layers that are used to manage production database schema migrations
downtown.
0.4.0
2025-08-13 08:12:35 -05:00
jdb 2dbe3ea07c Update for Nim 2.x 0.3.2 2024-12-28 11:07:50 -06:00
jdb 9acbc27710 Add debug logging for migration diff. 0.3.1 2021-08-07 23:54:10 -05:00
jdb 7cf53a4702 Allow multiple SQL directories (to support, for example, test configurations). 0.3.0 2021-07-19 22:57:47 -05:00
jdb 6837e5448b Update for Nim 1.4.x+ 0.2.8 2021-07-03 22:00:14 -05:00
jdb daf3a8dad0 Rename migrationsDir sqlDir. 2020-09-01 16:30:50 -05:00
Jonathan Bernard 4e771345ea Add more info to the version string. 0.2.7 2018-07-01 10:36:13 -05:00
jdb e26b6eb01c Fix argument parsing bug. 0.2.6 2018-06-01 01:23:23 -05:00
Jonathan Bernard ad1540ca20 Add timestamp to migrations.run_at. 2017-02-11 20:19:39 -06:00
Jonathan Bernard e35a5177ef Add env configuration, bug in SQL parsing for Nim db_migrate.
* The Nim binary now recognizes the following environment and allows them to
  override configured values:

  - `DATABASE_DRIVER`: overrides the `driver` config value, selects which
    kind of database we expect to connect to.
  - `MIGRATIONS_DIR`: overrides the `sqlDir` config value, sets the path to
    the directory containing the migration SQL files.
  - `DATABASE_URL`: overrides the `connectionString` config value, supplies
    connection parameters to the database.
  - `DBM_LOG_LEVEL`: overrides the `logLevel` config value, sets the logging
    level of db_migrate.

* Fixes a bug in the parsing of migration files. Previously the file was split
  on `;` characters and chunks that started with `--` were ignored as comments.
  This was wrong in the common case where a block starts with a comment but
  then contains SQL further. Such a block was being ignored. The new behavior
  is to consider each line and build up queries that way.

* Fixes a logging bug when parsing the configuration. If there was an that
  prevented the configuration from loading this in turn prevented logging from
  being setup correctly, and so the error was not logged. Now errors that may
  occur before logging is setup are hard-coded to be logged to STDERR.

* Changes the logic for creating the `migrations` table to check before
  performing the query that creates the table. This is to avoid continually
  printing messages about skipping this creation when the table already exists
  (which is the normal case). This change is PostgreSQL-specific, but of course
  the entire tool is currently PostgreSQL-specific.
2017-02-11 20:00:37 -06:00
Jonathan Bernard 9f38da0043 Fixed typo in groovy docopt. 2017-02-09 11:22:43 -06:00
Jonathan Bernard c933d6ac2b Separated statement running logic in Groovy implementation to allow reuse. 0.2.3 2016-05-17 21:35:57 -05:00
Jonathan Bernard b49b648358 Added JDB Labs Maven repo. 2016-04-21 07:29:13 -05:00
Jonathan Bernard 37c1c01ffc Added maven build plugin. 2016-04-17 17:40:44 -05:00
Jonathan Bernard 6bf2b86592 Groovy impl: added verbosity levels. Tested and fixed create/up/down. 0.2.2 2016-04-11 21:08:16 -05:00
Jonathan Bernard 9b3e7b4d26 Incrementing version for Nim and Groovy implementations. 2016-04-11 19:49:29 -05:00
Jonathan Bernard 5b9de9b10f Added initial, untested groovy implementation. 2016-04-11 17:16:30 -05:00
Jonathan Bernard 109a667fd5 Added Gradle build file. 2016-04-10 21:02:09 -05:00
Jonathan Bernard 462da00dd3 Restructured to accomodate being more than just a Nim implementation. 2016-04-10 21:01:53 -05:00
Jonathan Bernard 7242a13e51 Added better logging when database connection fails. 0.2.1 2016-02-10 03:25:14 -06:00
Jonathan Bernard 395bf0d35e Change line feeds to unix. 0.2.0 2016-02-07 07:38:27 -06:00
Jonathan Bernard 02e326e661 Added logging, implemented diff, up, and down. 2016-02-07 07:37:06 -06:00
Jonathan Bernard efc5dd2ed9 Added UP command (not working). 2016-02-06 23:28:35 -06:00
Jonathan Bernard 3963a27a66 Increment version number (not released). 2016-02-06 19:28:51 -06:00
Jonathan Bernard 9e0b264cae Added stubs for up, down, init. Starting on connection creation (BROKEN). 2016-01-31 12:27:37 -06:00
Jonathan Bernard bccd0439f7 Add error handling. 2016-01-31 11:54:37 -06:00
Jonathan Bernard 66bb8d9d39 Initial commit: create verb only. 0.1.0 2016-01-31 00:18:21 -06:00