aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md13
-rw-r--r--README.md76
-rw-r--r--docs/contributing.md33
-rw-r--r--docs/explanation.md351
-rw-r--r--docs/how-to.md101
-rw-r--r--docs/index.md44
-rw-r--r--docs/installation.md9
-rw-r--r--docs/reference.md192
-rw-r--r--docs/tutorial.md67
-rw-r--r--go.mod2
-rw-r--r--go.sum4
-rw-r--r--internal/bolttest/mock_migration_dependency_graph.go65
-rw-r--r--internal/bolttest/mock_migration_executor.go26
-rw-r--r--internal/bolttest/mock_migration_query_service.go31
-rw-r--r--internal/models/migration.go19
-rw-r--r--internal/models/migration_test.go2
-rw-r--r--internal/repositories/migration_fs_repo.go49
-rw-r--r--internal/repositories/migration_fs_repo_test.go10
-rw-r--r--internal/services/migration.go252
-rw-r--r--internal/services/migration_dependency_graph.go191
-rw-r--r--internal/services/migration_dependency_graph_test.go377
-rw-r--r--internal/services/migration_executor.go66
-rw-r--r--internal/services/migration_executor_test.go230
-rw-r--r--internal/services/migration_query_service.go90
-rw-r--r--internal/services/migration_query_service_test.go309
-rw-r--r--internal/services/migration_test.go771
-rw-r--r--internal/sqlparse/sqlparse.go71
-rw-r--r--internal/sqlparse/sqlparse_test.go105
-rw-r--r--scripts/lib/test.sh1
29 files changed, 2601 insertions, 956 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index df79c15..303ca26 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
+### Added
+
+- **Migration dependencies**: Migrations can now declare dependencies on other
+ migrations using `-- depends: version1, version2` at the top of the migration file.
+ These dependencies determine execution order. New migrations automatically depend
+ on current leaf migrations.
+
+Migration to dependencies: If you don't have _any_ '-- depends:' directives,
+all migrations will be treated as 'leaf' node migrations and simply execute in
+version order as before. To start using dependencies, you should backport them
+to all existing dependencies before doing so.
+
### Changed
- Updated Go to version 1.24.0.
@@ -16,6 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `github.com/mattn/go-sqlite3` from v1.14.22 to v1.14.32
- `github.com/microsoft/go-mssqldb` from v1.7.0 to v1.9.3
- Updated `github.com/BurntSushi/toml` from v1.3.2 to v1.5.0.
+- Migration execution order is now determined by dependencies rather than version number.
### Removed
diff --git a/README.md b/README.md
index bf43aec..a843666 100644
--- a/README.md
+++ b/README.md
@@ -5,84 +5,22 @@
![Version](https://img.shields.io/badge/version-0.11.0-blue)
[![Build Status](https://builds.sr.ht/~eugenetriguba/bolt/commits/main.svg)](https://builds.sr.ht/~eugenetriguba/bolt/commits/main?)
-Bolt is a command-line tool designed to simplify and streamline your database migration process.
-
-It is distributed as a standalone binary, making it easy to run anywhere without having to setup any tooling, and it is language independent. It doesn't matter what programming language you're using, you can use Bolt for any project you have.
-
-## Features
-
-- Schema migrations are written in plain SQL
-- Migrations can be versioned sequentially or by creation timestamp
-- All migrations run in transactions by default
-- Supports up and down migrations and jumping to any particular schema version
-- Supports PostgreSQL, MySQL, SQL Server, and SQLite3
-
-## Quick Start
-
-### Install
-
-```bash
-# Using Go
-go install git.sr.ht/~eugenetriguba/bolt/cmd/bolt@v0.11.0
-
-# Using Homebrew
-brew tap eugenetriguba/bolt https://git.sr.ht/~eugenetriguba/homebrew-bolt
-brew install bolt
-```
-
-### Create a migration
-
-```bash
-bolt new -m "create users table"
-```
-
-### Write your migration
-
-Edit the generated SQL file in `migrations/`:
-
-```sql
--- migrate:up
-CREATE TABLE users (
- id SERIAL PRIMARY KEY,
- name VARCHAR(255) NOT NULL,
- email VARCHAR(255) NOT NULL,
- created_at TIMESTAMP NOT NULL DEFAULT NOW()
-);
-
--- migrate:down
-DROP TABLE users;
-```
-
-### Configure and apply
-
-Create `bolt.toml`:
-
-```toml
-[database]
-dsn = "postgresql://user:password@localhost:5432/mydb?sslmode=disable"
-driver = "postgresql"
-```
-
-Apply the migration:
-
-```bash
-bolt up
-```
+Bolt is a command-line tool designed to simplify and streamline your
+database migration process.
## Documentation
-For complete documentation, visit **[bolt.eugenetriguba.com](https://bolt.eugenetriguba.com/)**
+For the full documentation, visit **[bolt.eugenetriguba.com](https://bolt.eugenetriguba.com/)**
- [Installation Guide](https://bolt.eugenetriguba.com/installation/)
- [Tutorial](https://bolt.eugenetriguba.com/tutorial/)
- [How-To Guides](https://bolt.eugenetriguba.com/how-to/)
- [Reference](https://bolt.eugenetriguba.com/reference/)
- [Explanation](https://bolt.eugenetriguba.com/explanation/)
-
-## Contributing
-
-For information on how to contribute to Bolt, see the [Contributing Guide](https://bolt.eugenetriguba.com/contributing/).
+- [Contributing](https://bolt.eugenetriguba.com/contributing/)
## License
-MIT License - see [LICENSE](LICENSE) file for details.
+MIT License
+
+See [LICENSE](./LICENSE) file for details.
diff --git a/docs/contributing.md b/docs/contributing.md
index 109b04a..418958f 100644
--- a/docs/contributing.md
+++ b/docs/contributing.md
@@ -1,8 +1,7 @@
# Contributing to Bolt
-Thank you for your interest in contributing to Bolt! This document provides guidelines and instructions for contributing to the project.
-
-## Getting Started
+Thank you for your interest in contributing to Bolt! This document provides
+guidelines and instructions for contributing to the project.
Bolt is hosted on [SourceHut](https://sr.ht/~eugenetriguba/Bolt/). If you're new
to SourceHut, see the [git.sr.ht quickstart](https://man.sr.ht/git.sr.ht/).
@@ -14,22 +13,16 @@ You do not need an account on SourceHut to contribute.
Bolt uses an [email-based workflow](https://git-send-email.io/) for
accepting contributions.
-- Patches should be submitted via email to my [public-inbox mailing
-list](https://lists.sr.ht/~eugenetriguba/public-inbox) with the subject
-prefix `[PATCH bolt]`.
-- General questions and discussions about the bolt project can be sent to my
-[public-inbox mailing list](https://lists.sr.ht/~eugenetriguba/public-inbox).
-- Bug reports can be submitted to my [public-inbox mailing
-list](https://lists.sr.ht/~eugenetriguba/public-inbox).
-
-For general questions, discussions, or bug reports, please make it obvious in
-the subject line it is for the bolt project.
+- All questions, discussions, requests, or patches should be submitted via
+ email to my [public-inbox mailing list](https://lists.sr.ht/~eugenetriguba/public-inbox).
+ For patches, use the subject prefix `[PATCH bolt]`. For anything else, please
+ make it obvious it is for the bolt project.
## Development Setup
### Prerequisites
-- Go 1.23.0 or later
+- Go 1.24.0 or later
- Docker (for running database integration tests)
- git
@@ -73,3 +66,15 @@ Run tests for a specific database:
./scripts/test.sh mssql
./scripts/test.sh sqlite3
```
+
+### Running Linters/Formatters
+
+To lint:
+```bash
+./scripts/lint.sh
+```
+
+To format:
+```bash
+./scripts/format.sh
+```
diff --git a/docs/explanation.md b/docs/explanation.md
index aa6b9a7..15eb729 100644
--- a/docs/explanation.md
+++ b/docs/explanation.md
@@ -1,29 +1,38 @@
# Explanation
-This section provides deeper explanations of how Bolt works internally and the design decisions behind it.
+This section provides deeper explanations of how Bolt works internally and the
+design decisions behind it.
## How Are Migration Scripts Executed?
-Bolt executes upgrade and downgrade migration scripts in a transaction by default. This ensures that if any errors occur during the execution of a migration script, the transaction will be rolled back and the migration will be marked as failed. Bolt will then exit with an error code and output the error to standard error.
+Bolt executes upgrade and downgrade migration scripts in a transaction by
+default. This ensures that if any errors occur during the execution of a
+migration script, the transaction will be rolled back and the migration will be
+marked as failed. Bolt will then exit with an error code and output the error to
+standard error.
### Transaction Behavior
When you run a migration:
-1. Bolt opens a database connection
-2. Begins a transaction
-3. Executes the SQL statements in the migration script
-4. If successful, commits the transaction and records the migration as applied
-5. If an error occurs, rolls back the transaction and exits with an error
+1. Bolt opens a database connection.
+2. Begins a transaction.
+3. Executes the SQL statements in the migration script.
+4. If successful, commits the transaction and records the migration as applied.
+5. If an error occurs, rolls back the transaction and exits with an error.
### Database-Specific Considerations
!!! warning "Auto-commit DDL Statements"
- Some databases, particularly MySQL, auto-commit certain DDL statements even within a transaction. This means that if a migration fails partway through, some changes may have already been committed. Always test your migrations and have a backup strategy.
+ Some databases, i.e. MySQL, auto-commit certain DDL statements even
+ within a transaction. This means that if a migration fails partway through,
+ some changes may have already been committed.
### Opting Out of Transactions
-For operations that cannot run within a transaction (like `CREATE INDEX CONCURRENTLY` in PostgreSQL), you can opt out using the `transaction:false` option:
+For operations that cannot run within a transaction (like `CREATE INDEX
+CONCURRENTLY` in PostgreSQL), you can opt out using the `transaction:false`
+option:
```sql
-- migrate:up transaction:false
@@ -31,203 +40,250 @@ For operations that cannot run within a transaction (like `CREATE INDEX CONCURRE
## How Does Bolt Know What Migrations Have Been Applied?
-Bolt keeps track of which migrations have been applied by creating a tracking table in your database (by default named `bolt_migrations`).
+Bolt keeps track of which migrations have been applied by creating a tracking
+table in your database (by default named `bolt_migrations`).
### Migrations Tracking Table
The tracking table has a simple structure with a single column:
-- `version` - Stores the version identifier of each applied migration
+- `version` - Stores the version identifier of each applied migration.
When you run `bolt status`, Bolt:
-1. Reads all migration files from your migrations directory
-2. Queries the tracking table for applied versions
-3. Compares the two lists to show you which migrations are pending or applied
+1. Reads all migration files from your migrations directory.
+2. Queries the tracking table for applied versions.
+3. Compares the two lists to show you which migrations are pending or applied.
+ Any pending migrations will be shown in the order they would be executed.
### Why Not Use Migration Names?
-Bolt uses only the version portion of the migration filename (e.g., `20240316145038` from `20240316145038_create_users.sql`) for tracking. The descriptive message portion is purely for human readability and is not stored in the database.
+Bolt only uses the version portion of the migration filename (e.g.,
+`20240316145038` from `20240316145038_create_users.sql`) for tracking.
+
+This allows the descriptive message portion to be purely for human readability.
+It is not stored in the database.
## How Are Migrations Applied?
When you run `bolt up`, the following process occurs:
-1. **Discovery**: Bolt scans your migrations directory for all `.sql` files
-2. **Parsing**: Each filename is parsed to extract the version and message
+1. **Discovery**: Bolt scans your migrations directory for all `.sql` files.
+2. **Parsing**: Each filename is parsed to extract the version and message, and
+ each file is parsed to extract dependencies.
3. **Comparison**: Bolt queries the tracking table to determine which migrations
- haven't been applied
-4. **Sorting**: Pending migrations are sorted by version (oldest first)
+ haven't been applied.
+4. **Ordering**: Pending migrations are ordered based on their dependencies to
+ ensure each migration runs after its dependencies.
5. **Execution**: For each pending migration:
- - The file is read and parsed to extract the `-- migrate:up` portion
- - A transaction is started (unless `transaction:false` is specified)
- - The SQL is executed
- - The version is inserted into the tracking table
- - The transaction is committed
+ - The file is read and parsed to extract the `-- migrate:up` portion.
+ - All dependencies are verified to be applied.
+ - The transaction is started (unless `transaction:false` is specified)
+ - The SQL is executed
+ - The version is inserted into the tracking table
+ - The transaction is committed
6. **Reporting**: Success or failure is reported for each migration. If any
migrations fail to be applied, the process halts and no more migrations are
applied.
-### Applying to a Specific Version
+### Applying Up to a Specific Version
-When you specify a target version with `bolt up -v <version>`:
-
-- Only migrations with versions less than or equal to the target are considered
-- Migrations are still applied in order from oldest to newest
-- The process stops after applying the target version (or before if an error occurs)
+When you specify a target version with `bolt up -v <version>`, only migrations up
+to and including the target version are considered.
## How Are Migrations Reverted?
When you run `bolt down`, the following process occurs:
-1. **Discovery**: Bolt scans your migrations directory for all `.sql` files
-2. **Query**: Bolt queries the tracking table for applied migrations
-3. **Matching**: Applied versions are matched against local migration files
-4. **Sorting**: Applied migrations are sorted by version (newest first for reverting)
-5. **Execution**: For the most recent applied migration (or down to a target version):
+1. **Discovery**: Bolt scans your migrations directory for all `.sql` files.
+2. **Query**: Bolt queries the tracking table for applied migrations.
+3. **Matching**: Applied versions are matched against local migration files.
+4. **Ordering**: Applied migrations are ordered in reverse dependency order.
+ (migrations that depend on others are reverted first)
+5. **Execution**: For the most recent applied migration (or down to a target
+ version):
- - The file is read and parsed to extract the `-- migrate:down` portion
+ - The file is read and parsed to extract the `-- migrate:down` portion.
+ - Bolt verifies that no other applied migrations depend on this one
- A transaction is started (unless `transaction:false` is specified)
- The SQL is executed
- The version is removed from the tracking table
- The transaction is committed
-6. **Reporting**: Success or failure is reported for each migration. If any
+5. **Reporting**: Success or failure is reported for each migration. If any
migrations fail to be reverted, the process halts and no more migrations are
applied.
-
### Reverting to a Specific Version
-When you specify a target version with `bolt down -v <version>`:
-
-- All migrations with versions greater than or equal to the target are reverted
-- Migrations are reverted in order from newest to oldest
-- The process stops after reverting the target version
+When you specify a target version with `bolt down -v <version>`, all migrations
+down to and including the target version are reverted.
### Missing Migration Files
!!! danger "Missing Migration Files"
- If a migration has been applied (its version is in the tracking table) but the migration file no longer exists locally, Bolt cannot revert it automatically. You'll need to either:
+ If a migration has been applied (its version is in the tracking table) but
+ the migration file no longer exists locally, Bolt cannot revert it
+ automatically. You'll need to either:
- - Restore the missing migration file
- - Manually revert the changes and remove the version from the tracking table
+ - Restore the missing migration file.
+ - Manually revert the changes and remove the version from the tracking
+ table.
- This is one reason why migration files should be committed to version control and never deleted.
+ Generally, migration files should be committed to version control and not deleted.
-## What are Migration Version Styles?
+## How Do Migration Dependencies Work?
-Whenever you create a migration with `bolt new`, it's prefixed with a "version". This version is used by Bolt to:
+Migration dependencies solve a fundamental problem in collaborative database
+development: how do many developers create migrations on different branches
+without conflicts once merged?
-- Uniquely identify each migration
-- Determine the order in which migrations should be applied or reverted
-- Track which migrations have been applied
+### The Problem with Version-Based Ordering
-Bolt supports two version styles:
+Traditionally, migration tools order migrations by version number (timestamp or
+sequential).
-### Timestamp Versions
+#### Sequential
-Format: `YYYYMMDDHHMMSS` (e.g., `20240316145038`)
+- Developer A creates migration `002` for feature X on branch A.
+- Developer B creates migration `002` for feature Y on branch B.
+- When branches merge, there's a filename conflict that one of them will need
+ to resolve.
-The version represents the timestamp when the migration was created:
+#### Timestamp
-- `2024` - Year
-- `03` - Month
-- `16` - Day
-- `14` - Hour (24-hour format)
-- `50` - Minute
-- `38` - Second
+- Developer A creates migration `20251013214743` for feature X on branch A.
+- Developer B creates migration `20251013214943` for feature Y on branch B.
+- Developer B merges first and applies their migration successfully.
+- Developer A merges second and applies their migration successfully.
+- Everything works in this environment.
-**Trade-offs:**
+However, when a fresh database is set up (staging, production, new developer):
+- Migrations run in **timestamp** order: A's migration runs first, then B's
+- This is the *opposite* order from the original development environment
+- If B's migration depends on A's changes, the fresh setup fails, even though
+ the original environment worked fine.
-- **Pros:**
- - Avoids filename conflicts when multiple developers create migrations on different branches
- - Embeds creation time in the version number
-- **Cons:**
- - Migration order may not match merge order (e.g., if a migration with an earlier timestamp is merged after one with a later timestamp, it will run out of chronological order)
- - Longer, harder to remember version numbers
+While timestamp-versioned migrations generally won't run into filename
+conflicts, they don't respect the actual dependencies between migrations
+or the order they were validated in.
-**Best for:** Teams working on multiple branches simultaneously where avoiding Git merge conflicts is prioritized over strict chronological ordering.
+### The Dependency Solution
-### Sequential Versions
+Instead of relying solely on version numbers, migrations explicitly declare
+which other migrations they depend on. This creates a dependency graph where
+Bolt can determine the correct execution order.
-Format: Zero-padded integers (e.g., `001`, `002`, `003`)
+The key insight is that execution order should be based on what migrations need
+to run before others, not when they were created.
-The version is simply an incrementing counter.
+### Automatic Dependency Population
-**Trade-offs:**
+Managing these migration dependencies is low-friction because Bolt automatically
+populating them on creation. When you create a new migration, Bolt finds all
+"leaf" migrations (migrations that nothing else depends on) and makes your new
+migration depend on them.
-- **Pros:**
- - Simple, easy-to-remember version numbers
- - Clear sequential ordering intention
-- **Cons:**
- - Multiple developers creating migrations on different branches will create filename conflicts (e.g., both create `003_...sql`), requiring manual resolution during merge
- - Forces explicit coordination between developers
+This means:
-**Best for:** Single-developer projects or teams with strict coordination where you want simple version numbers and explicit conflict resolution.
+- The first migration in a project has no dependencies.
+- The second migration automatically depends on the first.
+- If developers create migrations in parallel branches, a subsequent migration
+ will depend on both when merged.
-## Why can't I change between version styles?
+You'll get a dependency chain by default but can manually edit dependencies for
+more complex scenarios.
-Switching between version styles after you have existing migrations is not supported for several important reasons:
+### How Execution Order is Determined
-### Sorting Ambiguity
+Bolt uses topological sorting to determine execution order. Migrations are
+modeled as a directed graph:
-Bolt needs to sort migrations to determine the correct application order. Consider this scenario:
+- Each migration is a node.
+- Dependencies are edges pointing from a migration to what it depends on.
+- Bolt finds an ordering where every migration runs after its dependencies.
-```
-001_first_migration.sql
-002_second_migration.sql
-20240316145038_third_migration.sql
-```
+When multiple migrations have all their dependencies satisfied, Bolt uses
+version number as a tiebreaker for deterministic ordering.
-How should these be sorted? As strings:
+### Validation and Safety
-- `001` comes before `002` comes before `20240316145038` ✓
+Bolt validates the dependency graph before execution:
-But what if you had:
-```
-001_first_migration.sql
-20240316145038_second_migration.sql
-002_third_migration.sql
-```
+**Before applying**:
+
+- Verifies that all dependencies exist and have been applied.
+- Detects circular dependencies (A depends on B, B depends on A).
+
+**Before reverting**: Ensures no other applied migrations depend on the
+migration being reverted. This prevents breaking the dependency chain and
+leaving the database in an inconsistent state.
+
+### Why This Matters
-Now the string sorting would be:
+Dependencies enable:
-- `001`, then `002`, then `20240316145038`
+- **Parallel development**: Multiple developers can create migrations
+ simultaneously without version conflicts.
+- **Flexible merging**: Migrations can be merged in any order as long as
+ dependencies are correct.
+- **Explicit ordering**: The dependency graph makes execution order explicit
+ and reviewable.
+- **Safer operations**: Bolt prevents invalid apply/revert operations that would
+ violate dependencies
-But the chronological creation order was:
+## What are Migration Version Styles?
+
+Whenever you create a migration with `bolt new`, it's prefixed with a "version".
+This version is used by Bolt to:
-- `001`, then `20240316145038`, then `002`
+- Uniquely identify each migration.
+- Name the migration file.
+- Track which migrations have been applied.
-This creates ambiguity about the intended order.
+Bolt supports two version styles.
-### Parsing Complexity
+### Timestamp Versions (Default)
-Bolt parses version numbers according to the configured version style:
+Format: `YYYYMMDDHHMMSS` (e.g., `20240316145038`)
-- **Timestamp style**: Expects exactly 14 digits in `YYYYMMDDHHMMSS` format
-- **Sequential style**: Expects an integer (any length)
+The version represents the timestamp when the migration was created:
+
+- `2024` - Year
+- `03` - Month
+- `16` - Day
+- `14` - Hour (24-hour format)
+- `50` - Minute
+- `38` - Second
-If you mix styles, Bolt cannot reliably parse version numbers, leading to potential errors or incorrect ordering.
+### Sequential Versions
-### Manual Migration Path
+Format: Zero-padded integers (e.g., `001`, `002`, `003`)
-If you absolutely need to change version styles:
+The version is simply an incrementing counter.
-1. Export your current migration state
-2. Rename all migration files to use the new version style
-3. Update all version entries in the tracking table to match
-4. Update your `bolt.toml` or environment variables
-5. Verify the migrations are correctly ordered with `bolt status`
+## Can I switch between version styles?
-!!! warning
- This is an advanced operation and should be done carefully.
+Yes, _if_ you do the work to migrate all the way to one version style or the
+other. There is no automated tooling support for that.
+
+In a mixed state? Kind of. It's not well supported or recommended. Some aspects
+of using Bolt would not work properly if your migration file versions are in a
+mixed state. Some examples:
+
+- If you were using timestamp versions and switched to sequential versions, the
+ next migration would simply be the highest timestamp version incremented by 1.
+
+- If you were using sequential versions and switched to timestamp versions, the
+ sorting would error out because Bolt wouldn't be able to parse the sequential
+ versions as timestamps.
## How is the migration message used?
-When you create a migration with `bolt new -m "create users table"`, the resulting file is named:
+When you create a migration with `bolt new -m "create users table"`, the
+resulting file is named:
```
20240316145038_create_users_table.sql
@@ -235,50 +291,39 @@ When you create a migration with `bolt new -m "create users table"`, the resulti
The format is: `<version>_<message>.sql`
-### Purpose of the Message
-
-The message serves several purposes:
-
-1. **Human readability**: Makes it easy to understand what a migration does without opening the file
-2. **File organization**: Helps you quickly find migrations in your editor or file browser
-3. **Code review**: Makes git diffs and pull requests more understandable
-4. **Documentation**: Serves as lightweight documentation of your schema changes
-
-### What the Message is NOT Used For
-
-- **Tracking**: Bolt uses only the version portion for tracking applied migrations
-- **Ordering**: The message has no impact on migration order
-- **Uniqueness**: Two migrations can have the same message (though different versions)
+The message is purely for human understanding of the migration. It is not used
+by Bolt for any purpose.
### Message Formatting
When you provide a message:
-- Spaces are converted to underscores
-- Special characters may be sanitized
-- The message becomes part of the filename
+
+- Spaces are converted to underscores.
+- Extra leading or trailing spaces are trimmed.
+- The message is lowercased.
Example:
+
```bash
-bolt new -m "add index to users table"
+bolt new -m " add INDEX to users table "
# Creates: 20240316145038_add_index_to_users_table.sql
```
## What restrictions are there on the custom migration table?
-While you can configure the database table that Bolt uses for tracking applied migrations, there are restrictions on the name.
+While you can configure the database table that Bolt uses for tracking applied
+migrations, there are restrictions on the name.
### Naming Restrictions
-The migrations table name must:
-
-- Contain only alphanumeric characters and underscores
-- Follow your database's table naming rules
+The migrations table name must contain only alphanumeric characters and
+underscores.
Valid examples:
- `bolt_migrations` (default)
- `my_custom_migrations`
-- `migrations_v2`
+- `migrations_v2` -
- `app_schema_versions`
Invalid examples:
@@ -298,21 +343,15 @@ migrations_table = "myschema.migrations"
**Schema support:**
-- ✓ **PostgreSQL**: Full support for schemas
-- ✓ **SQL Server**: Full support for schemas
-- ✗ **MySQL**: Does not support schemas (use databases instead)
-- ✗ **SQLite**: Does not support schemas
-
-### Why These Restrictions?
-
-These restrictions exist to:
-
-- Ensure cross-database compatibility
-- Prevent SQL injection risks
-- Avoid parsing ambiguities
-- Follow database best practices
+- **PostgreSQL**: Full support for schemas
+- **SQL Server**: Full support for schemas
+- **MySQL**: Does not support schemas
+- **SQLite**: Does not support schemas
### Existing Table Handling
!!! info
- If you change the `migrations_table` setting after migrations have already been applied, Bolt will look for the new table name. This effectively resets your migration state unless you manually copy the data from the old table to the new one.
+ If you change the `migrations_table` setting after migrations have already
+ been applied, Bolt will look for the new table name. This effectively resets
+ your migration state unless you manually copy the data from the old table to
+ the new one.
diff --git a/docs/how-to.md b/docs/how-to.md
index 5d59a39..0d5c370 100644
--- a/docs/how-to.md
+++ b/docs/how-to.md
@@ -4,9 +4,12 @@ This section contains guides for specific tasks you may want to accomplish with
## How to execute a migration script without a transaction
-By default, Bolt executes all migrations within a transaction. However, some SQL commands cannot be executed within a transaction (for example, certain DDL operations in some databases).
+By default, Bolt executes all migrations within a transaction. However, some
+SQL commands cannot be executed within a transaction (for example, certain
+DDL operations in some databases).
-To execute a migration without a transaction, add a `transaction:false` option to the migration directives:
+To execute a migration without a transaction, add a `transaction:false` option
+to the migration directives:
```sql
-- migrate:up transaction:false
@@ -17,7 +20,65 @@ DROP INDEX CONCURRENTLY idx_users_email;
```
!!! note
- The `transaction:false` option can be applied independently to `-- migrate:up` and `-- migrate:down`. You can have one run in a transaction and the other not, depending on your needs.
+ The `transaction:false` option can be applied independently to
+ `-- migrate:up` and `-- migrate:down`. You can have one run in
+ a transaction and the other not, depending on your needs.
+
+## How to make a migration depend on a specific migrations
+
+By default, Bolt automatically manages dependencies when you create a new
+migration. To manually control which migrations must run first, add a
+`-- depends:` directive at the top of the migration file.
+
+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 (comma-separated):
+
+```sql
+-- depends: 20240316145038, 20240317120000
+-- 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;
+```
+
+Bolt ensures all listed migrations are applied before this one runs.
+
+## How to determine migration execution order
+
+Use `bolt status` to see migrations in the order they'll be executed:
+
+```bash
+bolt status
+```
+
+Output:
+```
+Version Message Applied
+20240316145038 create_users X
+20240317120000 create_posts X
+20240318093000 add_comments
+```
+
+Migrations are listed in dependency order. Applied migrations show an `X` in
+the Applied column.
## How to jump to a specific migration version
@@ -45,37 +106,29 @@ This will revert all applied migrations down to and including version `202403161
## How to use environment variables for configuration
-Instead of using a `bolt.toml` file, you can configure Bolt using environment variables. This is particularly useful for CI/CD environments or when you want to keep sensitive information out of configuration files.
-
-### Available environment variables
-
-- `BOLT_MIGRATIONS_DIR_PATH` - The directory where your migration scripts are located
-- `BOLT_MIGRATIONS_VERSION_STYLE` - The migration versioning style (`timestamp` or `sequential`)
-- `BOLT_DB_DSN` - The database connection string (DSN)
-- `BOLT_DB_DRIVER` - The database driver (`postgresql`, `mysql`, `mssql`, or `sqlite3`)
-- `BOLT_DB_MIGRATIONS_TABLE` - The name of the migrations tracking table
-
-### Example
-
```bash
export BOLT_DB_DSN="postgresql://user:password@localhost:5432/mydb?sslmode=disable"
export BOLT_DB_DRIVER="postgresql"
bolt up
```
+See the [reference](./reference.md#environment-variables) for the complete list
+of environment variables supported.
+
!!! tip
- Environment variables take precedence over `bolt.toml` settings. You can use a mix of both, with environment variables overriding file-based configuration when needed.
+ Environment variables take precedence over `bolt.toml` settings. You can
+ use a mix of both, with environment variables overriding file-based
+ configuration when needed.
## How to use a custom migrations table name
-By default, Bolt uses a table named `bolt_migrations` to track applied migrations. You can customize this table name in your configuration.
+By default, Bolt uses a table named `bolt_migrations` to track applied
+migrations. You can customize this table name in your configuration.
### In bolt.toml
```toml
[database]
-dsn = "postgresql://user:password@localhost:5432/mydb"
-driver = "postgresql"
migrations_table = "my_custom_migrations"
```
@@ -92,12 +145,11 @@ export BOLT_DB_MIGRATIONS_TABLE="my_custom_migrations"
migrations_table = "myschema.migrations"
```
-!!! warning "Restrictions"
- The migrations table name must only contain alphanumeric characters or underscores (e.g., `my_custom_table`). It may contain one dot (`.`) if specifying a schema, but custom schemas are only supported with PostgreSQL and MSSQL.
-
## How to use sequential versioning instead of timestamps
-By default, Bolt uses timestamp-based versioning for migrations (e.g., `20240316145038`). You can switch to sequential versioning (e.g., `001`, `002`, `003`) for new projects.
+By default, Bolt uses timestamp-based versioning for migrations
+(e.g., `20240316145038`). You can switch to sequential versioning
+(e.g., `001`, `002`, `003`) for new projects.
### In bolt.toml
@@ -113,4 +165,5 @@ export BOLT_MIGRATIONS_VERSION_STYLE="sequential"
```
!!! danger "Important"
- You cannot change version styles once you have existing migrations. Mixing timestamp and sequential versions is not supported. See the [Explanation](explanation.md#why-cant-i-change-between-version-styles) section for more details.
+ You shouldn't change version styles once you have existing migrations.
+ Mixing timestamp and sequential versions is not well supported.
diff --git a/docs/index.md b/docs/index.md
index 91b8891..b524074 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -5,37 +5,32 @@
[![GoDoc](https://godoc.org/git.sr.ht/~eugenetriguba/bolt?status.svg)](https://godoc.org/git.sr.ht/~eugenetriguba/bolt)
[![Go Report Card](https://goreportcard.com/badge/git.sr.ht/~eugenetriguba/bolt)](https://goreportcard.com/report/git.sr.ht/~eugenetriguba/bolt)
-Bolt is a command-line tool designed to simplify and streamline your database migration process.
+Bolt is a command-line tool designed to simplify and streamline your database
+migration process.
-It is distributed as a standalone binary, making it easy to run anywhere without having to setup any tooling, and it is language independent. It doesn't matter what programming language you're using, you can use Bolt for any project you have.
+It is distributed as a standalone binary, making it easy to run anywhere
+without having to setup any tooling, and it is language independent. It
+doesn't matter what programming language you're using, you can use Bolt
+for any project you have.
## Features
-- **Plain SQL migrations** - Schema migrations are written in plain SQL
-- **Flexible versioning** - Migrations can be versioned sequentially or by creation timestamp
-- **Transactional by default** - All migrations run in transactions by default
-- **Bidirectional migrations** - Supports up and down migrations and jumping to any particular schema version
-- **Multi-database support** - Supports PostgreSQL, MySQL, SQL Server, and SQLite3
+- Schema migrations are written in plain SQL
+- Migrations can be versioned sequentially or by creation timestamp
+- All migrations run in transactions by default
+- Supports up and down migrations and jumping to any particular schema version
+- Supports PostgreSQL, MySQL, SQL Server, and SQLite3
## Quick Start
### Install Bolt
-#### Go install
-
```bash
go install git.sr.ht/~eugenetriguba/bolt/cmd/bolt@v0.11.0
```
This will install Bolt to your `$GOBIN` directory. Make sure that `$GOBIN` is in your `$PATH`.
-#### Homebrew
-
-```bash
-brew tap eugenetriguba/bolt https://git.sr.ht/~eugenetriguba/homebrew-bolt
-brew install bolt
-```
-
### Create your first migration
```bash
@@ -78,13 +73,10 @@ bolt up
## Next Steps
-- Follow the complete [Tutorial](tutorial.md) for a detailed walkthrough
-- Check out [How-To Guides](how-to.md) for specific tasks
-- Browse the [Reference](reference.md) for commands and configuration options
-- Read [Explanations](explanation.md) to understand how Bolt works internally
-
-## Getting Help
-
-- Read the [documentation](https://bolt.eugenetriguba.com)
-- Check the [issue tracker](https://todo.sr.ht/~eugenetriguba/bolt)
-- View the [source code](https://git.sr.ht/~eugenetriguba/bolt)
+- Read the [installation guide](./installation.md) for all installation options.
+- Follow the [Tutorial](tutorial.md) for a detailed walkthrough.
+- Check out [How-To Guides](how-to.md) for specific tasks.
+- Browse the [Reference](reference.md) for commands and configuration options.
+- Read [Explanations](explanation.md) to understand how Bolt works internally.
+- See [Contributing](contributing.md) if you're interesting in contributing to
+ Bolt.
diff --git a/docs/installation.md b/docs/installation.md
index c45b439..24dc82a 100644
--- a/docs/installation.md
+++ b/docs/installation.md
@@ -1,6 +1,7 @@
# Installation
-Bolt is distributed as a standalone binary that can be installed through several methods.
+Bolt is distributed as a standalone binary that can be installed through
+several methods.
## Go install
@@ -10,7 +11,8 @@ If you have Go installed, you can install Bolt using `go install`:
go install git.sr.ht/~eugenetriguba/bolt/cmd/bolt@v0.11.0
```
-This will install Bolt to your `$GOBIN` directory. Make sure that `$GOBIN` is in your `$PATH`.
+This will install Bolt to your `$GOBIN` directory. Make sure that `$GOBIN` is
+in your `$PATH`.
## Homebrew
@@ -23,7 +25,8 @@ brew install bolt
## Verifying Installation
-After installation, verify that Bolt is correctly installed by checking the version:
+After installation, verify that Bolt is correctly installed by checking the
+version:
```bash
bolt version
diff --git a/docs/reference.md b/docs/reference.md
index ac3147d..595a0a4 100644
--- a/docs/reference.md
+++ b/docs/reference.md
@@ -1,15 +1,17 @@
# Reference
-This section provides detailed reference information for Bolt's commands, configuration options, and supported databases.
+This section provides detailed reference information for Bolt's commands,
+configuration options, and supported databases.
## Database Compatibility
Bolt supports the following databases:
-- **PostgreSQL** - All recent versions
-- **MySQL** - All recent versions
-- **Microsoft SQL Server** - All recent versions
-- **SQLite3** - All recent versions
+- **PostgreSQL** - 13+
+- **MySQL** - 5.7+
+- **Microsoft SQL Server** - 2017+
+- **SQLite3** - All recent versions. We explicitly test against the latest
+ version.
## Commands
@@ -88,6 +90,9 @@ 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
@@ -104,11 +109,14 @@ bolt version
## Configuration
-Bolt can be configured using either a `bolt.toml` configuration file or environment variables. Environment variables take precedence over configuration file settings.
+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.
+Bolt searches for a `bolt.toml` file in your current working directory or any
+parent directory.
**Complete example:**
@@ -122,9 +130,6 @@ directory_path = "migrations"
# The migration versioning style you prefer. Supported options
# are "timestamp" and "sequential". Defaults to "timestamp".
-#
-# Note: It is not supported to change migration version styles
-# i.e. you can't have a mix of sequential and timestamp migrations.
version_style = "timestamp"
[database]
@@ -162,15 +167,20 @@ 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.
+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.
+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://pkg.go.dev/github.com/jackc/pgx/v5#hdr-Connection_String_Parameters)
+**Driver:** [pgx](https://github.com/jackc/pgx)
+
+[DSN Documentation](https://github.com/jackc/pgx/blob/v5.7.6/pgxpool/pool.go#L352)
**Format:**
```
@@ -195,7 +205,9 @@ 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)
+**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:**
```
@@ -220,7 +232,9 @@ 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)
+**Driver:** [microsoft/go-mssqldb](https://github.com/microsoft/go-mssqldb)
+
+[DSN Documentation](https://github.com/microsoft/go-mssqldb#connection-parameters-and-dsn)
**Format:**
```
@@ -245,7 +259,9 @@ 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)
+**Driver:** [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3)
+
+[DSN Documentation](https://github.com/mattn/go-sqlite3#connection-string)
**Format:**
```
@@ -268,30 +284,132 @@ dsn = "/path/to/mydatabase.db?mode=ro"
dsn = "/path/to/mydatabase.db?_foreign_keys=on"
```
-### Security Notes
+## 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
+```
-!!! warning "Security Best Practices"
- - Never commit DSN strings containing passwords to version control
- - Use environment variables for sensitive connection information
- - For PostgreSQL and MySQL, always use SSL/TLS in production (`sslmode=require` for PostgreSQL, `tls=true` for MySQL)
- - Use dedicated database users with minimal required permissions for migrations
+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>
+```
-## Script Execution Options
+### `-- migrate:up` and `-- migrate:down`
-Bolt provides options to customize how migration scripts are executed.
+These directives separate the upgrade and downgrade portions of your migration.
-Options are added to the `-- migrate:up` or `-- migrate:down` comments:
+**Syntax:**
+```sql
+-- migrate:up [option1] [option2]
+-- migrate:down [option1] [option2]
+```
+**Example:**
```sql
--- migrate:up <option1> <option2>
--- migrate:down <option1> <option2>
+-- 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.
+Execute the migration script without a transaction. By default, every
+migration script is executed within a transaction.
**Example:**
@@ -303,11 +421,6 @@ CREATE INDEX CONCURRENTLY idx_users_email ON users(email);
DROP INDEX CONCURRENTLY idx_users_email;
```
-**When to use:**
-- PostgreSQL `CREATE INDEX CONCURRENTLY` operations
-- Operations that cannot run within a transaction in your database
-- When you need to handle transactions manually within your script
-
## Version Styles
Bolt supports two migration versioning styles:
@@ -316,11 +429,6 @@ Bolt supports two migration versioning styles:
Migrations are versioned using timestamps (e.g., `20240316145038`).
-**Advantages:**
-- Works well in teams where multiple developers create migrations simultaneously
-- Timestamp indicates when the migration was created
-- Less likely to have version conflicts
-
**Configuration:**
```toml
[migrations]
@@ -331,16 +439,8 @@ version_style = "timestamp"
Migrations are versioned using incrementing integers (e.g., `001`, `002`, `003`).
-**Advantages:**
-- Simpler, more readable version numbers
-- Easier to reference in conversation ("migration 5")
-- Works well for solo developers or small teams with linear development
-
**Configuration:**
```toml
[migrations]
version_style = "sequential"
```
-
-!!! danger "Important"
- You cannot change version styles once you have existing migrations. Mixing version styles is not supported. See [Why can't I change between version styles?](explanation.md#why-cant-i-change-between-version-styles) for more details.
diff --git a/docs/tutorial.md b/docs/tutorial.md
index d163f23..147a6f6 100644
--- a/docs/tutorial.md
+++ b/docs/tutorial.md
@@ -1,6 +1,8 @@
# Tutorial
-In this tutorial, we'll guide you through the basics of using Bolt by running through how you can create a migration, apply it, revert it, and see the status of your migrations.
+In this tutorial, we'll guide you through the basics of using Bolt by running
+through how you can create a migration, apply it, revert it, and see the
+status of your migrations.
## Prerequisites
@@ -22,7 +24,8 @@ Output:
Created migration 20240316145038 - my first migration.
```
-Upon completion, a migrations directory will be created with the following structure:
+Upon completion, a migrations directory will be created with the following
+structure:
```bash
migrations
@@ -31,11 +34,14 @@ migrations
1 directory, 1 file
```
-Bolt automatically creates the migrations directory and your migration scripts inside it.
+Bolt automatically creates the migrations directory and your migration scripts
+inside it.
## Writing your migration scripts
-Bolt utilizes plain SQL for migration scripts. Each migration file contains a comment for the upgrade script portion with `-- migrate:up` and a comment for the downgrade script with `-- migrate:down`.
+Bolt utilizes plain SQL for migration scripts. Each migration file contains a
+comment for the upgrade script portion with `-- migrate:up` and a comment for
+the downgrade script with `-- migrate:down`.
1. Open up `20240316145038_my_first_migration.sql`
@@ -86,7 +92,8 @@ docker run \
## Configuring Bolt
-Bolt can be configured with a `bolt.toml` file or environment variables. For simplicity, we'll use a `bolt.toml` file in this tutorial.
+Bolt can be configured with a `bolt.toml` file and environment variables. For
+simplicity, we'll use a `bolt.toml` file in this tutorial.
1. Create a `bolt.toml` file:
@@ -130,7 +137,8 @@ Version Message Applied
20240316145038 my_first_migration X
```
-This command displays the migration's version, name, and whether the migration was applied or not.
+This command displays the migration's version, name, and whether the migration
+was applied or not.
## Verifying the Migration
@@ -171,7 +179,8 @@ Successfully reverted migration 20240316145038_my_first_migration in 6.283667ms!
## Verifying Migration Reversion
-Reconnect to the database container and check that the users table has been removed:
+Reconnect to the database container and check that the users table has been
+removed:
```bash
docker exec -it bolt-postgres psql -U bolt_user -d bolt_tutorial_db
@@ -186,6 +195,50 @@ bolt_tutorial_db=# \dt
(1 row)
```
+## Creating a migration with dependencies
+
+Let's create another migration that depends on the first one:
+
+```bash
+bolt new -m "create posts table"
+```
+
+Open the new migration file. You'll see that Bolt automatically added
+a dependency on the previous migration:
+
+```sql
+-- depends: 20240316145038
+-- migrate:up
+
+-- migrate:down
+```
+
+Add the migration SQL:
+
+```sql
+-- depends: 20240316145038
+-- migrate:up
+CREATE TABLE posts (
+ id SERIAL PRIMARY KEY,
+ user_id INTEGER NOT NULL REFERENCES users(id),
+ title VARCHAR(255) NOT NULL,
+ content TEXT NOT NULL,
+ created_at TIMESTAMP NOT NULL DEFAULT NOW()
+);
+
+-- migrate:down
+DROP TABLE posts;
+```
+
+Apply the new migration:
+
+```bash
+bolt up
+```
+
+The dependency ensures that the `users` table exists before creating the
+`posts` table with its foreign key reference.
+
## Next Steps
Congratulations! You've learned the core features of Bolt.
diff --git a/go.mod b/go.mod
index 513ef89..e194055 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module git.sr.ht/~eugenetriguba/bolt
go 1.24.0
require (
- git.sr.ht/~eugenetriguba/checkmate v0.4.0
+ git.sr.ht/~eugenetriguba/checkmate v0.5.0
git.sr.ht/~eugenetriguba/envelope v0.2.0
git.sr.ht/~eugenetriguba/subcmd v0.2.0
github.com/BurntSushi/toml v1.5.0
diff --git a/go.sum b/go.sum
index 14663b1..b563fff 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
-git.sr.ht/~eugenetriguba/checkmate v0.4.0 h1:9BokIJjo5BF44q3pS4Kcuo7aooOrQCin1dBBQ5n+aOs=
-git.sr.ht/~eugenetriguba/checkmate v0.4.0/go.mod h1:Pxv1PKcBSbOqmoEOZxOBJVv9dHUGU+zcwuwfnGqZEQk=
+git.sr.ht/~eugenetriguba/checkmate v0.5.0 h1:6AuloFbKgXCsEA7h4mOa8sdGaepKHSlegzHY9QMPmXU=
+git.sr.ht/~eugenetriguba/checkmate v0.5.0/go.mod h1:Pxv1PKcBSbOqmoEOZxOBJVv9dHUGU+zcwuwfnGqZEQk=
git.sr.ht/~eugenetriguba/envelope v0.2.0 h1:+vq3MENb2Xo1K7D/jZJcOHB9dBTbdHOEvzKoRtWyQpc=
git.sr.ht/~eugenetriguba/envelope v0.2.0/go.mod h1:DcP27+64+elmsPbHqA90L/z3zkiyguRAk3wfKVBtnA8=
git.sr.ht/~eugenetriguba/subcmd v0.2.0 h1:F85wNm6IdsM29XruFZpfU89UVYrqtxffmRuOaBgayak=
diff --git a/internal/bolttest/mock_migration_dependency_graph.go b/internal/bolttest/mock_migration_dependency_graph.go
new file mode 100644
index 0000000..1d73ca4
--- /dev/null
+++ b/internal/bolttest/mock_migration_dependency_graph.go
@@ -0,0 +1,65 @@
+package bolttest
+
+import "git.sr.ht/~eugenetriguba/bolt/internal/models"
+
+type MockMigrationDependencyGraph struct {
+ TopologicalSortReturnValue TopologicalSortReturnValue
+ TopologicalSortCallCount int
+ ReverseTopologicalSortReturnValue ReverseTopologicalSortReturnValue
+ ReverseTopologicalSortCallCount int
+ CheckDependenciesAppliedReturnValue CheckDependenciesAppliedReturnValue
+ CheckDependenciesAppliedCallCount int
+ CheckNoDependentsAppliedReturnValue CheckNoDependentsAppliedReturnValue
+ CheckNoDependentsAppliedCallCount int
+ FindLeafNodesReturnValue FindLeafNodesReturnValue
+ FindLeafNodesCallCount int
+}
+
+type TopologicalSortReturnValue struct {
+ Migrations []*models.Migration
+ Err error
+}
+
+type ReverseTopologicalSortReturnValue = TopologicalSortReturnValue
+
+type CheckDependenciesAppliedReturnValue struct {
+ Err error
+}
+
+type CheckNoDependentsAppliedReturnValue = CheckDependenciesAppliedReturnValue
+
+type FindLeafNodesReturnValue struct {
+ LeafNodes []string
+ Err error
+}
+
+func (g *MockMigrationDependencyGraph) TopologicalSort(migrations []*models.Migration) ([]*models.Migration, error) {
+ g.TopologicalSortCallCount++
+ return g.TopologicalSortReturnValue.Migrations, g.TopologicalSortReturnValue.Err
+}
+
+func (g *MockMigrationDependencyGraph) ReverseTopologicalSort(migrations []*models.Migration) ([]*models.Migration, error) {
+ g.ReverseTopologicalSortCallCount++
+ return g.ReverseTopologicalSortReturnValue.Migrations, g.ReverseTopologicalSortReturnValue.Err
+}
+
+func (g *MockMigrationDependencyGraph) CheckDependenciesApplied(
+ migration *models.Migration,
+ allMigrations []*models.Migration,
+) error {
+ g.CheckDependenciesAppliedCallCount++
+ return g.CheckDependenciesAppliedReturnValue.Err
+}
+
+func (g *MockMigrationDependencyGraph) CheckNoDependentsApplied(
+ migration *models.Migration,
+ allMigrations []*models.Migration,
+) error {
+ g.CheckNoDependentsAppliedCallCount++
+ return g.CheckNoDependentsAppliedReturnValue.Err
+}
+
+func (g *MockMigrationDependencyGraph) FindLeafNodes(localMigrations map[string]*models.Migration) ([]string, error) {
+ g.FindLeafNodesCallCount++
+ return g.FindLeafNodesReturnValue.LeafNodes, g.FindLeafNodesReturnValue.Err
+}
diff --git a/internal/bolttest/mock_migration_executor.go b/internal/bolttest/mock_migration_executor.go
new file mode 100644
index 0000000..74eda4f
--- /dev/null
+++ b/internal/bolttest/mock_migration_executor.go
@@ -0,0 +1,26 @@
+package bolttest
+
+import "git.sr.ht/~eugenetriguba/bolt/internal/models"
+
+type MockMigrationExecutor struct {
+ ApplyReturnValue ExecutorApplyReturnValue
+ ApplyCallCount int
+ RevertReturnValue ExecutorRevertReturnValue
+ RevertCallCount int
+}
+
+type ExecutorApplyReturnValue struct {
+ Err error
+}
+
+type ExecutorRevertReturnValue = ExecutorApplyReturnValue
+
+func (e *MockMigrationExecutor) Apply(migration *models.Migration) error {
+ e.ApplyCallCount++
+ return e.ApplyReturnValue.Err
+}
+
+func (e *MockMigrationExecutor) Revert(migration *models.Migration) error {
+ e.RevertCallCount++
+ return e.RevertReturnValue.Err
+}
diff --git a/internal/bolttest/mock_migration_query_service.go b/internal/bolttest/mock_migration_query_service.go
new file mode 100644
index 0000000..9200929
--- /dev/null
+++ b/internal/bolttest/mock_migration_query_service.go
@@ -0,0 +1,31 @@
+package bolttest
+
+import "git.sr.ht/~eugenetriguba/bolt/internal/models"
+
+type MockMigrationQueryService struct {
+ ListMigrationsReturnValue QueryListMigrationsReturnValue
+ ListMigrationsCallCount int
+ CombineMigrationsReturnValue QueryCombineMigrationsReturnValue
+ CombineMigrationsCallCount int
+}
+
+type QueryListMigrationsReturnValue struct {
+ Migrations []*models.Migration
+ Err error
+}
+
+type QueryCombineMigrationsReturnValue = QueryListMigrationsReturnValue
+
+func (s *MockMigrationQueryService) ListMigrations(order interface{}) ([]*models.Migration, error) {
+ s.ListMigrationsCallCount++
+ return s.ListMigrationsReturnValue.Migrations, s.ListMigrationsReturnValue.Err
+}
+
+func (s *MockMigrationQueryService) CombineMigrations(
+ localMigrations map[string]*models.Migration,
+ appliedMigrations map[string]*models.Migration,
+ order interface{},
+) ([]*models.Migration, error) {
+ s.CombineMigrationsCallCount++
+ return s.CombineMigrationsReturnValue.Migrations, s.CombineMigrationsReturnValue.Err
+}
diff --git a/internal/models/migration.go b/internal/models/migration.go
index ed48073..baf5de9 100644
--- a/internal/models/migration.go
+++ b/internal/models/migration.go
@@ -7,9 +7,10 @@ import (
)
type Migration struct {
- Version string
- Message string
- Applied bool
+ Version string
+ Message string
+ Applied bool
+ Dependencies []string
}
func NewTimestampMigration(version time.Time, message string) *Migration {
@@ -19,16 +20,18 @@ func NewTimestampMigration(version time.Time, message string) *Migration {
version.Year(), version.Month(), version.Day(),
version.Hour(), version.Minute(), version.Second(),
),
- Message: message,
- Applied: false,
+ Message: message,
+ Applied: false,
+ Dependencies: []string{},
}
}
func NewSequentialMigration(version uint64, message string) *Migration {
return &Migration{
- Version: fmt.Sprintf("%03d", version),
- Message: message,
- Applied: false,
+ Version: fmt.Sprintf("%03d", version),
+ Message: message,
+ Applied: false,
+ Dependencies: []string{},
}
}
diff --git a/internal/models/migration_test.go b/internal/models/migration_test.go
index aae1cba..d7c1d6d 100644
--- a/internal/models/migration_test.go
+++ b/internal/models/migration_test.go
@@ -16,6 +16,7 @@ func TestNewTimestampMigration(t *testing.T) {
check.Equal(t, m.Version, "20200101000000")
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
+ check.DeepEqual(t, m.Dependencies, []string{})
}
func TestNewSequentialMigration(t *testing.T) {
@@ -30,6 +31,7 @@ func TestNewSequentialMigration(t *testing.T) {
check.Equal(t, m.Version, "100")
check.Equal(t, m.Message, "test message")
check.Equal(t, m.Applied, false)
+ check.DeepEqual(t, m.Dependencies, []string{})
}
func TestName(t *testing.T) {
diff --git a/internal/repositories/migration_fs_repo.go b/internal/repositories/migration_fs_repo.go
index 3923b69..b300dad 100644
--- a/internal/repositories/migration_fs_repo.go
+++ b/internal/repositories/migration_fs_repo.go
@@ -69,7 +69,17 @@ func (mr migrationFsRepo) Create(migration *models.Migration) error {
if err != nil {
return fmt.Errorf("unable to create file at %s: %w", newMigrationPath, err)
}
- _, err = file.WriteString("-- migrate:up\n\n-- migrate:down\n")
+ defer file.Close()
+
+ var template strings.Builder
+ if len(migration.Dependencies) > 0 {
+ template.WriteString("-- depends: ")
+ template.WriteString(strings.Join(migration.Dependencies, ", "))
+ template.WriteString("\n")
+ }
+ template.WriteString("-- migrate:up\n\n-- migrate:down\n")
+
+ _, err = file.WriteString(template.String())
if err != nil {
return fmt.Errorf("unable to write template to new migration script: %w", err)
}
@@ -84,7 +94,7 @@ func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
var migrations = make(map[string]*models.Migration, 0)
for _, entry := range entries {
- migration, err := dirEntryToMigration(entry)
+ migration, err := mr.dirEntryToMigration(entry)
if err != nil {
return nil, err
}
@@ -96,14 +106,13 @@ func (mr migrationFsRepo) List() (map[string]*models.Migration, error) {
migration.Version,
)
}
-
migrations[migration.Version] = migration
}
return migrations, nil
}
-func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
+func (mr migrationFsRepo) dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
parts := strings.SplitN(entry.Name(), "_", 2)
if len(parts) != 2 {
return nil, fmt.Errorf(
@@ -114,11 +123,35 @@ func dirEntryToMigration(entry fs.DirEntry) (*models.Migration, error) {
}
version := parts[0]
message := strings.TrimSuffix(parts[1], filepath.Ext(parts[1])) // Remove .sql
- return &models.Migration{
+ migration := &models.Migration{
Version: version,
Message: message,
Applied: false,
- }, nil
+ }
+
+ scriptPath := filepath.Join(mr.migrationsDirPath, entry.Name())
+ dependencies, err := mr.parseDependenciesFromFile(scriptPath)
+ if err != nil {
+ return nil, fmt.Errorf("unable to parse dependencies from %s: %w", entry.Name(), err)
+ }
+ migration.Dependencies = dependencies
+
+ return migration, nil
+}
+
+func (mr migrationFsRepo) parseDependenciesFromFile(scriptPath string) ([]string, error) {
+ scriptContents, err := mr.readScriptContents(scriptPath)
+ if err != nil {
+ return nil, err
+ }
+
+ sqlParser := sqlparse.NewSqlParser()
+ migration, err := sqlParser.Parse(strings.NewReader(scriptContents))
+ if err != nil {
+ return nil, err
+ }
+
+ return migration.Dependencies, nil
}
func (mr migrationFsRepo) ReadUpgradeScript(
@@ -155,7 +188,8 @@ func (mr migrationFsRepo) getMigrationScripts(
)
}
sqlParser := sqlparse.NewSqlParser()
- return sqlParser.Parse(strings.NewReader(scriptContents))
+ migration, err := sqlParser.Parse(strings.NewReader(scriptContents))
+ return migration.UpgradeScript, migration.DowngradeScript, err
}
func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error) {
@@ -163,6 +197,5 @@ func (mr migrationFsRepo) readScriptContents(scriptPath string) (string, error)
if err != nil {
return "", err
}
-
return string(contents), nil
}
diff --git a/internal/repositories/migration_fs_repo_test.go b/internal/repositories/migration_fs_repo_test.go
index 7625c06..facfd34 100644
--- a/internal/repositories/migration_fs_repo_test.go
+++ b/internal/repositories/migration_fs_repo_test.go
@@ -55,11 +55,17 @@ func TestCreate_SuccessfullyCreated(t *testing.T) {
repo, err := repositories.NewMigrationFsRepo(&migrationsConfig)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "add users table")
+ migration.Dependencies = append(migration.Dependencies, "001")
err = repo.Create(migration)
assert.Nil(t, err)
- assertFileExists(t, filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name())))
+ path := filepath.Join(tempDir, fmt.Sprintf("%s.sql", migration.Name()))
+ assertFileExists(t, path)
+ contents, err := os.ReadFile(path)
+ assert.Nil(t, err)
+ assert.Equal(t, "-- depends: 001\n-- migrate:up\n\n-- migrate:down\n", string(contents))
+
}
func TestCreate_FailsToCreateMigration(t *testing.T) {
@@ -162,10 +168,12 @@ func TestList_Success(t *testing.T) {
time.Date(2020, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_1",
)
+ migration1.Dependencies = []string{"001", "002"}
migration2 := models.NewTimestampMigration(
time.Date(2022, 10, 12, 1, 1, 1, 1, time.UTC),
"migration_2",
)
+ migration2.Dependencies = []string{"003"}
err = repo.Create(migration1)
assert.Nil(t, err)
repo.Create(migration2)
diff --git a/internal/services/migration.go b/internal/services/migration.go
index ddfb8c2..ba43ff7 100644
--- a/internal/services/migration.go
+++ b/internal/services/migration.go
@@ -2,7 +2,6 @@ package services
import (
"fmt"
- "sort"
"strconv"
"time"
@@ -12,11 +11,17 @@ import (
"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
)
+// MigrationService provides the primary interface for migration operations.
+// It coordinates with the lower-level components to apply, revert, create,
+// and list out migrations while also managing presentation concerns like
+// output and timing.
type MigrationService struct {
- dbRepo repositories.MigrationDBRepo
- fsRepo repositories.MigrationFsRepo
- cfg configloader.Config
- outputter output.Outputter
+ executor *MigrationExecutor
+ queryService *MigrationQueryService
+ depGraph *MigrationDependencyGraph
+ fsRepo repositories.MigrationFsRepo
+ cfg configloader.Config
+ outputter output.Outputter
}
func NewMigrationService(
@@ -25,29 +30,32 @@ func NewMigrationService(
cfg configloader.Config,
outputter output.Outputter,
) MigrationService {
+ depGraph := NewMigrationDependencyGraph(cfg)
+ executor := NewMigrationExecutor(dbRepo, fsRepo)
+ queryService := NewMigrationQueryService(dbRepo, fsRepo, depGraph)
return MigrationService{
- dbRepo: dbRepo,
- fsRepo: fsRepo,
- cfg: cfg,
- outputter: outputter,
+ executor: executor,
+ queryService: queryService,
+ depGraph: depGraph,
+ fsRepo: fsRepo,
+ cfg: cfg,
+ outputter: outputter,
}
}
-type sortOrder int
-
-const (
- SortOrderDesc sortOrder = iota
- SortOrderAsc
-)
-
func (ms MigrationService) ApplyAllMigrations() error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
return err
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
+ if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -62,6 +70,14 @@ func (ms MigrationService) ApplyAllMigrations() error {
return nil
}
+func getMigrationsByVersion(migrations []*models.Migration) map[string]*models.Migration {
+ result := make(map[string]*models.Migration)
+ for _, migration := range migrations {
+ result[migration.Version] = migration
+ }
+ return result
+}
+
func (ms MigrationService) ApplyUpToVersion(version string) error {
migrations, err := ms.ListMigrations(SortOrderAsc)
if err != nil {
@@ -79,16 +95,21 @@ func (ms MigrationService) ApplyUpToVersion(version string) error {
return fmt.Errorf("migration with version %s does not exist", version)
}
if targetMigration.Applied {
- // Assumption: If the target migration is applied, all migrations before
- // it must also be applied.
+ // TODO: Check not only that the target migration is applied, but also
+ // all prior migrations.
return fmt.Errorf(
"migration with version %s is already applied, nothing to apply",
version,
)
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if !migration.Applied {
+ if err := ms.depGraph.CheckDependenciesApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.ApplyMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -111,19 +132,9 @@ func (ms MigrationService) ApplyMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Applying migration %s..", migration.Name()))
startTime := time.Now()
- upgradeScript, err := ms.fsRepo.ReadUpgradeScript(migration)
- if err != nil {
- return fmt.Errorf("unable to read upgrade script: %w", err)
- }
-
- if upgradeScript.Options.UseTransaction {
- err = ms.dbRepo.ApplyWithTx(upgradeScript.Contents, migration)
- } else {
- err = ms.dbRepo.Apply(upgradeScript.Contents, migration)
- }
-
+ err := ms.executor.Apply(migration)
if err != nil {
- return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err)
+ return err
}
ms.outputter.Output(
@@ -143,8 +154,13 @@ func (ms MigrationService) RevertAllMigrations() error {
return err
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
+ if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -176,16 +192,21 @@ func (ms MigrationService) RevertDownToVersion(version string) error {
return fmt.Errorf("migration with version %s does not exist", version)
}
if !targetMigration.Applied {
- // Assumption: Every migration from the latest down to the target
- // migration hasn't been applied.
+ // TODO: Check not only that the target migration is reverted, but also
+ // all prior migrations.
return fmt.Errorf(
"migration with version %s isn't applied, nothing to revert",
version,
)
}
+ migrationsByVersion := getMigrationsByVersion(migrations)
for _, migration := range migrations {
if migration.Applied {
+ if err := ms.depGraph.CheckNoDependentsApplied(migration, migrationsByVersion); err != nil {
+ return err
+ }
+
err := ms.RevertMigration(migration)
if err != nil {
return fmt.Errorf(
@@ -208,17 +229,7 @@ func (ms MigrationService) RevertMigration(migration *models.Migration) error {
ms.outputter.Output(fmt.Sprintf("Reverting migration %s..", migration.Name()))
startTime := time.Now()
- downgradeScript, err := ms.fsRepo.ReadDowngradeScript(migration)
- if err != nil {
- return fmt.Errorf("unable to read downgrade script: %w", err)
- }
-
- if downgradeScript.Options.UseTransaction {
- err = ms.dbRepo.RevertWithTx(downgradeScript.Contents, migration)
- } else {
- err = ms.dbRepo.Revert(downgradeScript.Contents, migration)
- }
-
+ err := ms.executor.Revert(migration)
if err != nil {
return err
}
@@ -247,160 +258,61 @@ func (ms MigrationService) CreateMigration(message string) (*models.Migration, e
migration = models.NewSequentialMigration(currentVersion+1, message)
}
- err := ms.fsRepo.Create(migration)
+ leafNodes, err := ms.getMigrationLeafNodes()
if err != nil {
return nil, err
}
- ms.outputter.Output(
- fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
- )
- return migration, nil
-}
-
-func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
- var currentVersion uint64 = 0
+ migration.Dependencies = leafNodes
- localMigrations, err := ms.fsRepo.List()
+ err = ms.fsRepo.Create(migration)
if err != nil {
- return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
+ return nil, err
}
-
- migrations, err := ms.combineMigrations(
- localMigrations,
- map[string]*models.Migration{},
- SortOrderDesc,
+ ms.outputter.Output(
+ fmt.Sprintf("Created migration %s - %s.", migration.Version, migration.Message),
)
- if err != nil {
- return 0, err
- }
-
- if len(migrations) > 0 {
- latestMigration := migrations[0]
- currentVersion, err = strconv.ParseUint(latestMigration.Version, 10, 64)
- if err != nil {
- return 0, err
- }
- }
-
- return currentVersion, nil
+ return migration, nil
}
-func (ms MigrationService) ListMigrations(order sortOrder) ([]*models.Migration, error) {
- // Assumption: All migration versions that have been applied
- // to the database exist locally in the list of filesystem migrations.
- // This would hold true unless someone decided to apply a migration and,
- // for whatever reason, delete it later from their filesystem. If there is
- // an applied migration that doesn't exist locally, it would not be shown
- // here.
+func (ms MigrationService) getMigrationLeafNodes() ([]string, error) {
localMigrations, err := ms.fsRepo.List()
if err != nil {
- return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
+ return nil, fmt.Errorf("unable to list migrations for finding leaf nodes: %w", err)
}
- appliedMigrations, err := ms.dbRepo.List()
+ leafNodes, err := ms.depGraph.FindLeafNodes(localMigrations)
if err != nil {
- return nil, fmt.Errorf(
- "unable to list out applied migrations from remote db: %w",
- err,
- )
+ return nil, fmt.Errorf("unable to find leaf nodes for dependencies: %w", err)
}
+ return leafNodes, nil
- return ms.combineMigrations(localMigrations, appliedMigrations, order)
}
-func (ms MigrationService) combineMigrations(
- localMigrations map[string]*models.Migration,
- appliedMigrations map[string]*models.Migration,
- order sortOrder,
-) ([]*models.Migration, error) {
- migrations := make([]*models.Migration, 0)
- for _, localMigration := range localMigrations {
- _, ok := appliedMigrations[localMigration.Version]
- if ok {
- localMigration.Applied = true
- }
- migrations = append(migrations, localMigration)
- }
+func (ms MigrationService) getCurrentSequentialMigrationVersion() (uint64, error) {
+ var maxVersion uint64 = 0
- err := ms.sortMigrations(migrations, order)
+ localMigrations, err := ms.fsRepo.List()
if err != nil {
- return nil, err
+ return 0, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
}
- return migrations, err
-}
-
-func (ms MigrationService) sortMigrations(
- migrations []*models.Migration,
- order sortOrder,
-) error {
- sortErrs := make([]error, 0)
- sort.Slice(migrations, func(i, j int) bool {
- var comparison bool
- var err error
-
- if ms.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential {
- comparison, err = ms.compareSequentialMigrations(
- migrations[i],
- migrations[j],
- order,
+ for _, migration := range localMigrations {
+ version, err := strconv.ParseUint(migration.Version, 10, 64)
+ if err != nil {
+ return 0, fmt.Errorf(
+ "migration %s has invalid version for sequential style: %w",
+ migration.Name(),
+ err,
)
- } else {
- comparison, err = ms.compareTimestampMigrations(migrations[i], migrations[j], order)
}
-
- if err != nil {
- sortErrs = append(sortErrs, err)
+ if version > maxVersion {
+ maxVersion = version
}
-
- return comparison
- })
- if len(sortErrs) != 0 {
- return fmt.Errorf("unable to sort migrations: %v", sortErrs)
- }
- return nil
-}
-
-func (ms MigrationService) compareSequentialMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := strconv.ParseInt(m1.Version, 10, 64)
- if err != nil {
- return false, err
- }
-
- m2Version, err := strconv.ParseInt(m2.Version, 10, 64)
- if err != nil {
- return false, err
}
- if order == SortOrderAsc {
- return m1Version < m2Version, nil
- } else {
- return m1Version > m2Version, nil
- }
+ return maxVersion, nil
}
-func (ms MigrationService) compareTimestampMigrations(
- m1 *models.Migration,
- m2 *models.Migration,
- order sortOrder,
-) (bool, error) {
- m1Version, err := time.Parse("20060102150405", m1.Version)
- if err != nil {
- return false, err
- }
-
- m2Version, err := time.Parse("20060102150405", m2.Version)
- if err != nil {
- return false, err
- }
-
- if order == SortOrderAsc {
- return m1Version.Before(m2Version), nil
- } else {
- return m1Version.After(m2Version), nil
- }
+func (ms MigrationService) ListMigrations(order SortOrder) ([]*models.Migration, error) {
+ return ms.queryService.ListMigrations(order)
}
diff --git a/internal/services/migration_dependency_graph.go b/internal/services/migration_dependency_graph.go
new file mode 100644
index 0000000..3f0e417
--- /dev/null
+++ b/internal/services/migration_dependency_graph.go
@@ -0,0 +1,191 @@
+package services
+
+import (
+ "fmt"
+ "slices"
+ "sort"
+ "strconv"
+ "time"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+)
+
+// MigrationDependencyGraph handles all dependency graph operations for migrations,
+// including topological sorting, dependency validation, and graph analysis.
+type MigrationDependencyGraph struct {
+ cfg configloader.Config
+}
+
+func NewMigrationDependencyGraph(cfg configloader.Config) *MigrationDependencyGraph {
+ return &MigrationDependencyGraph{cfg: cfg}
+}
+
+// TopologicalSort performs a topological sort on migrations based on their dependencies.
+// It uses Kahn's algorithm to detect cycles and order migrations. When multiple migrations
+// have the same topological level, they are sorted by version for determinism. `migrations`
+// is expected to be a map of version -> Migration.
+func (g *MigrationDependencyGraph) TopologicalSort(migrations map[string]*models.Migration) ([]*models.Migration, error) {
+ graph := make(map[string][]*models.Migration)
+ inDegree := make(map[string]int)
+
+ // To start, we need to build the graph and in-degree count.
+ for _, m := range migrations {
+ for _, dep := range m.Dependencies {
+ if _, exists := migrations[dep]; !exists {
+ return nil, fmt.Errorf(
+ "migration %s depends on non-existent migration %s",
+ m.Version, dep,
+ )
+ }
+ graph[dep] = append(graph[dep], m)
+ inDegree[m.Version]++
+ }
+ }
+
+ // Next step of Kahn's algorithm is to find the vertices with in-degree of 0
+ // Often, this will simply be one migration (the first migration), but there
+ // could be multiple independent migrations which would result in multiple
+ // starting points.
+ queue := make([]*models.Migration, 0)
+ for _, m := range migrations {
+ if inDegree[m.Version] == 0 {
+ queue = append(queue, m)
+ }
+ }
+ // Within each topological level, we want to sort the migrations by the version
+ // for determinism, so it's easier to reason about how migrations will execute.
+ g.sortByVersion(queue)
+
+ result := make([]*models.Migration, 0)
+ for len(queue) > 0 {
+ current := queue[0]
+ queue = queue[1:]
+ result = append(result, current)
+
+ newlyAvailable := make([]*models.Migration, 0)
+ for _, neighbor := range graph[current.Version] {
+ inDegree[neighbor.Version]--
+ if inDegree[neighbor.Version] == 0 {
+ newlyAvailable = append(newlyAvailable, neighbor)
+ }
+ }
+
+ g.sortByVersion(newlyAvailable)
+ queue = append(queue, newlyAvailable...)
+ g.sortByVersion(queue)
+ }
+
+ if len(result) != len(migrations) {
+ return nil, fmt.Errorf("circular dependency detected in migrations")
+ }
+
+ return result, nil
+}
+
+// ReverseTopologicalSort returns migrations in reverse topological order (for reverting).
+func (g *MigrationDependencyGraph) ReverseTopologicalSort(migrations map[string]*models.Migration) ([]*models.Migration, error) {
+ sorted, err := g.TopologicalSort(migrations)
+ if err != nil {
+ return nil, err
+ }
+ reversed := make([]*models.Migration, len(sorted))
+ for i, m := range sorted {
+ reversed[len(sorted)-1-i] = m
+ }
+ return reversed, nil
+}
+
+// sortByVersion sorts migrations by version number deterministically
+func (g *MigrationDependencyGraph) sortByVersion(migrations []*models.Migration) {
+ sort.Slice(migrations, func(i, j int) bool {
+ if g.cfg.Migrations.VersionStyle == configloader.VersionStyleSequential {
+ v1, _ := strconv.ParseInt(migrations[i].Version, 10, 64)
+ v2, _ := strconv.ParseInt(migrations[j].Version, 10, 64)
+ return v1 < v2
+ }
+ // Note: If not sequential, we assume timestamp style.
+ t1, _ := time.Parse("20060102150405", migrations[i].Version)
+ t2, _ := time.Parse("20060102150405", migrations[j].Version)
+ return t1.Before(t2)
+ })
+}
+
+// CheckDependenciesApplied verifies that all dependencies of a migration have been applied.
+func (g *MigrationDependencyGraph) CheckDependenciesApplied(
+ migration *models.Migration,
+ allMigrations map[string]*models.Migration,
+) error {
+ for _, depVersion := range migration.Dependencies {
+ dep, exists := allMigrations[depVersion]
+ if !exists {
+ return fmt.Errorf(
+ "cannot apply migration %s: dependency %s does not exist",
+ migration.Version, depVersion,
+ )
+ }
+ if !dep.Applied {
+ return fmt.Errorf(
+ "cannot apply migration %s: dependency %s has not been applied",
+ migration.Version, depVersion,
+ )
+ }
+ }
+
+ return nil
+}
+
+// CheckNoDependentsApplied verifies that no applied migrations depend on this migration.
+func (g *MigrationDependencyGraph) CheckNoDependentsApplied(
+ migration *models.Migration,
+ allMigrations map[string]*models.Migration,
+) error {
+ for _, m := range allMigrations {
+ if m.Version == migration.Version {
+ continue
+ }
+ if m.Applied && slices.Contains(m.Dependencies, migration.Version) {
+ return fmt.Errorf(
+ "cannot revert migration %s: migration %s still applied and depends on it",
+ migration.Version, m.Version,
+ )
+ }
+ }
+
+ return nil
+}
+
+// FindLeafNodes finds all migrations that no other migration depends on. These are
+// the "leaf" nodes in the dependency graph. If there are circular dependency in the
+// migration graph, an error is returned.
+func (g *MigrationDependencyGraph) FindLeafNodes(localMigrations map[string]*models.Migration) ([]string, error) {
+ if len(localMigrations) == 0 {
+ return []string{}, nil
+ }
+
+ // Validate there are no circular dependencies by building
+ // out the dependency graph via topological sort.
+ _, err := g.TopologicalSort(localMigrations)
+ if err != nil {
+ return nil, err
+ }
+
+ // What are all the migrations that have dependencies?
+ dependedUpon := make(map[string]bool)
+ for _, migration := range localMigrations {
+ for _, dep := range migration.Dependencies {
+ dependedUpon[dep] = true
+ }
+ }
+
+ // What are all the migrations that no other migration depends on?
+ // These are the leaf nodes.
+ leafNodes := make([]string, 0)
+ for _, migration := range localMigrations {
+ if !dependedUpon[migration.Version] {
+ leafNodes = append(leafNodes, migration.Version)
+ }
+ }
+
+ return leafNodes, nil
+}
diff --git a/internal/services/migration_dependency_graph_test.go b/internal/services/migration_dependency_graph_test.go
new file mode 100644
index 0000000..29d36db
--- /dev/null
+++ b/internal/services/migration_dependency_graph_test.go
@@ -0,0 +1,377 @@
+package services
+
+import (
+ "testing"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/checkmate/assert"
+ "git.sr.ht/~eugenetriguba/checkmate/check"
+)
+
+func TestTopologicalSort_ThreeMigrationsWithDependencies(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "003": {Version: "003", Dependencies: []string{"001", "002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "001": {Version: "001", Dependencies: []string{}},
+ }
+
+ sorted, err := graph.TopologicalSort(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(sorted), 3)
+ assert.Equal(t, sorted[0].Version, "001")
+ assert.Equal(t, sorted[1].Version, "002")
+ assert.Equal(t, sorted[2].Version, "003")
+}
+
+func TestTopologicalSort_CircularDependency(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{"002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ }
+
+ _, err := graph.TopologicalSort(migrations)
+
+ assert.ErrorContains(t, err, "circular dependency detected")
+}
+
+func TestTopologicalSort_NonExistentDependency(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{"002"}},
+ }
+
+ _, err := graph.TopologicalSort(migrations)
+
+ assert.ErrorContains(t, err, "depends on non-existent migration")
+}
+
+func TestTopologicalSort_EmptyList(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{}
+
+ sorted, err := graph.TopologicalSort(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(sorted), 0)
+}
+
+func TestTopologicalSort_DiamondDependency(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ // Diamond: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001"}},
+ "004": {Version: "004", Dependencies: []string{"002", "003"}},
+ }
+
+ sorted, err := graph.TopologicalSort(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(sorted), 4)
+ assert.Equal(t, sorted[0].Version, "001")
+ // 002 and 003 can be in either order (both depend only on 001)
+ // But our implementation sorts deterministically
+ assert.Equal(t, sorted[1].Version, "002")
+ assert.Equal(t, sorted[2].Version, "003")
+ assert.Equal(t, sorted[3].Version, "004")
+}
+
+func TestTopologicalSort_TimestampVersionStyle(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "20250101120000": {Version: "20250101120000", Dependencies: []string{}},
+ "20250101130000": {Version: "20250101130000", Dependencies: []string{"20250101120000"}},
+ "20250101110000": {Version: "20250101110000", Dependencies: []string{}},
+ }
+
+ sorted, err := graph.TopologicalSort(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(sorted), 3)
+ // Should be sorted by timestamp among peers
+ assert.Equal(t, sorted[0].Version, "20250101110000")
+ assert.Equal(t, sorted[1].Version, "20250101120000")
+ assert.Equal(t, sorted[2].Version, "20250101130000")
+}
+
+func TestReverseTopologicalSort(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001", "002"}},
+ }
+
+ reversed, err := graph.ReverseTopologicalSort(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(reversed), 3)
+ assert.Equal(t, reversed[0].Version, "003")
+ assert.Equal(t, reversed[1].Version, "002")
+ assert.Equal(t, reversed[2].Version, "001")
+}
+
+func TestReverseTopologicalSort_CircularDependency(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{"002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ }
+
+ _, err := graph.ReverseTopologicalSort(migrations)
+
+ assert.ErrorContains(t, err, "circular dependency detected")
+}
+
+func TestCheckDependenciesApplied_AllApplied(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: true, Dependencies: []string{"001"}},
+ "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations)
+
+ assert.Nil(t, err)
+}
+
+func TestCheckDependenciesApplied_DependencyNotApplied(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: false, Dependencies: []string{"001"}},
+ "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations)
+
+ assert.ErrorContains(t, err, "cannot apply migration 003")
+ assert.ErrorContains(t, err, "dependency 002 has not been applied")
+}
+
+func TestCheckDependenciesApplied_DependencyDoesNotExist(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckDependenciesApplied(allMigrations["003"], allMigrations)
+
+ assert.ErrorContains(t, err, "cannot apply migration 003")
+ assert.ErrorContains(t, err, "dependency 002 does not exist")
+}
+
+func TestCheckDependenciesApplied_NoDependencies(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false, Dependencies: []string{}},
+ }
+
+ err := graph.CheckDependenciesApplied(allMigrations["001"], allMigrations)
+
+ assert.Nil(t, err)
+}
+
+func TestCheckNoDependentsApplied_NoDependents(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: true, Dependencies: []string{"001"}},
+ "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations)
+
+ assert.Nil(t, err)
+}
+
+func TestCheckNoDependentsApplied_HasAppliedDependent(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: true, Dependencies: []string{"001"}},
+ "003": {Version: "003", Applied: true, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations)
+
+ assert.ErrorContains(t, err, "cannot revert migration 002")
+ assert.ErrorContains(t, err, "migration 003 still applied and depends on it")
+}
+
+func TestCheckNoDependentsApplied_HasUnappliedDependent(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: true, Dependencies: []string{"001"}},
+ "003": {Version: "003", Applied: false, Dependencies: []string{"001", "002"}},
+ }
+
+ err := graph.CheckNoDependentsApplied(allMigrations["002"], allMigrations)
+
+ assert.Nil(t, err)
+}
+
+func TestCheckNoDependentsApplied_RootMigration(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ allMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true, Dependencies: []string{}},
+ "002": {Version: "002", Applied: true, Dependencies: []string{"001"}},
+ }
+
+ err := graph.CheckNoDependentsApplied(allMigrations["001"], allMigrations)
+
+ assert.ErrorContains(t, err, "cannot revert migration 001")
+ assert.ErrorContains(t, err, "migration 002 still applied and depends on it")
+}
+
+func TestFindLeafNodes_NoMigrations(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+
+ leafNodes, err := graph.FindLeafNodes(map[string]*models.Migration{})
+
+ assert.Nil(t, err)
+ assert.DeepEqual(t, leafNodes, []string{})
+}
+
+func TestFindLeafNodes_SingleMigration(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ }
+
+ leafNodes, err := graph.FindLeafNodes(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(leafNodes), 1)
+ assert.Contains(t, leafNodes, "001")
+}
+
+func TestFindLeafNodes_LinearChain(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"002"}},
+ }
+
+ leafNodes, err := graph.FindLeafNodes(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(leafNodes), 1)
+ assert.Contains(t, leafNodes, "003")
+}
+
+func TestFindLeafNodes_ParallelBranches(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001"}},
+ }
+
+ leafNodes, err := graph.FindLeafNodes(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(leafNodes), 2)
+ assert.Contains(t, leafNodes, "002")
+ assert.Contains(t, leafNodes, "003")
+}
+
+func TestFindLeafNodes_ComplexGraph(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{})
+ // Diamond shape: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004
+ // Plus independent branch: 005
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001"}},
+ "004": {Version: "004", Dependencies: []string{"002", "003"}},
+ "005": {Version: "005", Dependencies: []string{}},
+ }
+
+ leafNodes, err := graph.FindLeafNodes(migrations)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(leafNodes), 2)
+ assert.Contains(t, leafNodes, "004")
+ assert.Contains(t, leafNodes, "005")
+}
+
+func TestSortByVersion_Sequential(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ migrations := []*models.Migration{
+ {Version: "003"},
+ {Version: "001"},
+ {Version: "002"},
+ }
+
+ graph.sortByVersion(migrations)
+
+ check.Equal(t, migrations[0].Version, "001")
+ check.Equal(t, migrations[1].Version, "002")
+ check.Equal(t, migrations[2].Version, "003")
+}
+
+func TestSortByVersion_Timestamp(t *testing.T) {
+ graph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ })
+ migrations := []*models.Migration{
+ {Version: "20250101130000"},
+ {Version: "20250101110000"},
+ {Version: "20250101120000"},
+ }
+
+ graph.sortByVersion(migrations)
+
+ check.Equal(t, migrations[0].Version, "20250101110000")
+ check.Equal(t, migrations[1].Version, "20250101120000")
+ check.Equal(t, migrations[2].Version, "20250101130000")
+}
diff --git a/internal/services/migration_executor.go b/internal/services/migration_executor.go
new file mode 100644
index 0000000..e9f827e
--- /dev/null
+++ b/internal/services/migration_executor.go
@@ -0,0 +1,66 @@
+package services
+
+import (
+ "fmt"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/bolt/internal/repositories"
+)
+
+// MigrationExecutor handles the core execution logic for applying and reverting migrations.
+type MigrationExecutor struct {
+ dbRepo repositories.MigrationDBRepo
+ fsRepo repositories.MigrationFsRepo
+}
+
+func NewMigrationExecutor(
+ dbRepo repositories.MigrationDBRepo,
+ fsRepo repositories.MigrationFsRepo,
+) *MigrationExecutor {
+ return &MigrationExecutor{
+ dbRepo: dbRepo,
+ fsRepo: fsRepo,
+ }
+}
+
+// Apply applies a migration by reading the upgrade script and executing it.
+// It automatically handles transaction wrapping based on the script options.
+func (e *MigrationExecutor) Apply(migration *models.Migration) error {
+ upgradeScript, err := e.fsRepo.ReadUpgradeScript(migration)
+ if err != nil {
+ return fmt.Errorf("unable to read upgrade script: %w", err)
+ }
+
+ if upgradeScript.Options.UseTransaction {
+ err = e.dbRepo.ApplyWithTx(upgradeScript.Contents, migration)
+ } else {
+ err = e.dbRepo.Apply(upgradeScript.Contents, migration)
+ }
+
+ if err != nil {
+ return fmt.Errorf("unable to apply migration %s: %w", migration.Name(), err)
+ }
+
+ return nil
+}
+
+// Revert reverts a migration by reading the downgrade script and executing it.
+// It automatically handles transaction wrapping based on the script options.
+func (e *MigrationExecutor) Revert(migration *models.Migration) error {
+ downgradeScript, err := e.fsRepo.ReadDowngradeScript(migration)
+ if err != nil {
+ return fmt.Errorf("unable to read downgrade script: %w", err)
+ }
+
+ if downgradeScript.Options.UseTransaction {
+ err = e.dbRepo.RevertWithTx(downgradeScript.Contents, migration)
+ } else {
+ err = e.dbRepo.Revert(downgradeScript.Contents, migration)
+ }
+
+ if err != nil {
+ return err
+ }
+
+ return nil
+}
diff --git a/internal/services/migration_executor_test.go b/internal/services/migration_executor_test.go
new file mode 100644
index 0000000..00e7402
--- /dev/null
+++ b/internal/services/migration_executor_test.go
@@ -0,0 +1,230 @@
+package services
+
+import (
+ "errors"
+ "testing"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/bolt/internal/sqlparse"
+ "git.sr.ht/~eugenetriguba/checkmate/assert"
+)
+
+func TestExecutorApply_WithTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+}
+
+func TestExecutorApply_WithoutTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE tmp(id INT PRIMARY KEY);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyReturnValue: bolttest.ApplyReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
+ assert.Equal(t, migrationDbRepo.ApplyCallCount, 1)
+}
+
+func TestExecutorApply_ReadUpgradeScriptErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{},
+ Err: expectedErr,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorApply_ApplyWithTxErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: true,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorApply_ApplyErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: false,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Apply(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_WithTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "DROP TABLE tmp;",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
+}
+
+func TestExecutorRevert_WithoutTransaction(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "DROP TABLE tmp;",
+ Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertReturnValue: bolttest.RevertReturnValue{
+ Err: nil,
+ },
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 1)
+}
+
+func TestExecutorRevert_ReadDowngradeScriptErr(t *testing.T) {
+ expectedErr := errors.New("error!")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{},
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_RevertWithTxErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: true,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestExecutorRevert_RevertErr(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Options: sqlparse.ExecutionOptions{
+ UseTransaction: false,
+ },
+ },
+ Err: nil,
+ },
+ }
+ expectedErr := errors.New("error!")
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr},
+ }
+
+ executor := NewMigrationExecutor(migrationDbRepo, migrationFsRepo)
+ err := executor.Revert(&models.Migration{})
+
+ assert.ErrorIs(t, err, expectedErr)
+}
diff --git a/internal/services/migration_query_service.go b/internal/services/migration_query_service.go
new file mode 100644
index 0000000..f505727
--- /dev/null
+++ b/internal/services/migration_query_service.go
@@ -0,0 +1,90 @@
+package services
+
+import (
+ "fmt"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/bolt/internal/repositories"
+)
+
+type SortOrder int
+
+const (
+ SortOrderDesc SortOrder = iota
+ SortOrderAsc
+)
+
+// MigrationQueryService handles querying and listing migrations from
+// both the filesystem and database sources.
+type MigrationQueryService struct {
+ dbRepo repositories.MigrationDBRepo
+ fsRepo repositories.MigrationFsRepo
+ depGraph *MigrationDependencyGraph
+}
+
+func NewMigrationQueryService(
+ dbRepo repositories.MigrationDBRepo,
+ fsRepo repositories.MigrationFsRepo,
+ depGraph *MigrationDependencyGraph,
+) *MigrationQueryService {
+ return &MigrationQueryService{
+ dbRepo: dbRepo,
+ fsRepo: fsRepo,
+ depGraph: depGraph,
+ }
+}
+
+// ListMigrations retrieves and combines migrations from both the filesystem and database,
+// returning them in the specified order (topological).
+func (s *MigrationQueryService) ListMigrations(order SortOrder) ([]*models.Migration, error) {
+ // Assumption: All migration versions that have been applied
+ // to the database exist locally in the list of filesystem migrations.
+ // This would hold true unless someone decided to apply a migration and,
+ // for whatever reason, delete it later from their filesystem. If there is
+ // an applied migration that doesn't exist locally, it would not be shown
+ // here.
+ localMigrations, err := s.fsRepo.List()
+ if err != nil {
+ return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
+ }
+
+ appliedMigrations, err := s.dbRepo.List()
+ if err != nil {
+ return nil, fmt.Errorf(
+ "unable to list out applied migrations from remote db: %w",
+ err,
+ )
+ }
+
+ return s.CombineMigrations(localMigrations, appliedMigrations, order)
+}
+
+// CombineMigrations combines local filesystem migrations with applied database migrations,
+// marking migrations as applied where appropriate and sorting them in the specified order.
+func (s *MigrationQueryService) CombineMigrations(
+ localMigrations map[string]*models.Migration,
+ appliedMigrations map[string]*models.Migration,
+ order SortOrder,
+) ([]*models.Migration, error) {
+ migrations := make(map[string]*models.Migration, 0)
+ for _, localMigration := range localMigrations {
+ _, ok := appliedMigrations[localMigration.Version]
+ if ok {
+ localMigration.Applied = true
+ }
+ migrations[localMigration.Version] = localMigration
+ }
+
+ var sorted []*models.Migration
+ var err error
+ if order == SortOrderAsc {
+ sorted, err = s.depGraph.TopologicalSort(migrations)
+ } else {
+ sorted, err = s.depGraph.ReverseTopologicalSort(migrations)
+ }
+ if err != nil {
+ return nil, err
+ }
+
+ return sorted, nil
+}
diff --git a/internal/services/migration_query_service_test.go b/internal/services/migration_query_service_test.go
new file mode 100644
index 0000000..af48777
--- /dev/null
+++ b/internal/services/migration_query_service_test.go
@@ -0,0 +1,309 @@
+package services
+
+import (
+ "errors"
+ "testing"
+
+ "git.sr.ht/~eugenetriguba/bolt/internal/bolttest"
+ "git.sr.ht/~eugenetriguba/bolt/internal/configloader"
+ "git.sr.ht/~eugenetriguba/bolt/internal/models"
+ "git.sr.ht/~eugenetriguba/checkmate/assert"
+)
+
+func TestListMigrations_NoMigrations(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 0)
+}
+
+func TestListMigrations_OneLocalMigration(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 1)
+ assert.Equal(t, migrations[0].Version, "001")
+ assert.Equal(t, migrations[0].Applied, false)
+}
+
+func TestListMigrations_LocalAndAppliedMigrationMerged(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ },
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 1)
+ assert.Equal(t, migrations[0].Version, "001")
+ assert.Equal(t, migrations[0].Applied, true)
+}
+
+func TestListMigrations_AppliedWithoutLocalNotShown(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ },
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 0)
+}
+
+func TestListMigrations_SortedTopologicallyAsc(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "003": {Version: "003", Dependencies: []string{"001", "002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "001": {Version: "001", Dependencies: []string{}},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 3)
+ assert.Equal(t, migrations[0].Version, "001")
+ assert.Equal(t, migrations[1].Version, "002")
+ assert.Equal(t, migrations[2].Version, "003")
+}
+
+func TestListMigrations_SortedTopologicallyDesc(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001", "002"}},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderDesc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 3)
+ assert.Equal(t, migrations[0].Version, "003")
+ assert.Equal(t, migrations[1].Version, "002")
+ assert.Equal(t, migrations[2].Version, "001")
+}
+
+func TestListMigrations_FsRepoListError(t *testing.T) {
+ expectedErr := errors.New("fs repo error")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{})
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.ErrorIs(t, err, expectedErr)
+ var expectedMigrations []*models.Migration
+ assert.DeepEqual(t, migrations, expectedMigrations)
+}
+
+func TestListMigrations_DbRepoListError(t *testing.T) {
+ expectedErr := errors.New("db repo error")
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{})
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ migrations, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.ErrorIs(t, err, expectedErr)
+ var expectedMigrations []*models.Migration
+ assert.DeepEqual(t, migrations, expectedMigrations)
+}
+
+func TestListMigrations_CircularDependency(t *testing.T) {
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{"002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: nil,
+ },
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{})
+ queryService := NewMigrationQueryService(migrationDbRepo, migrationFsRepo, depGraph)
+
+ _, err := queryService.ListMigrations(SortOrderAsc)
+
+ assert.ErrorContains(t, err, "circular dependency detected")
+}
+
+func TestCombineMigrations_MarksAppliedCorrectly(t *testing.T) {
+ localMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false, Dependencies: []string{}},
+ "002": {Version: "002", Applied: false, Dependencies: []string{"001"}},
+ }
+ appliedMigrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ }
+
+ depGraph := NewMigrationDependencyGraph(configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ })
+ queryService := NewMigrationQueryService(nil, nil, depGraph)
+
+ migrations, err := queryService.CombineMigrations(localMigrations, appliedMigrations, SortOrderAsc)
+
+ assert.Nil(t, err)
+ assert.Equal(t, len(migrations), 2)
+
+ // Find migrations by version (order is topological)
+ var m001, m002 *models.Migration
+ for _, m := range migrations {
+ if m.Version == "001" {
+ m001 = m
+ } else if m.Version == "002" {
+ m002 = m
+ }
+ }
+
+ assert.Equal(t, m001.Applied, true)
+ assert.Equal(t, m002.Applied, false)
+}
diff --git a/internal/services/migration_test.go b/internal/services/migration_test.go
index 09d33a3..c931dc9 100644
--- a/internal/services/migration_test.go
+++ b/internal/services/migration_test.go
@@ -18,7 +18,7 @@ func TestListMigrations(t *testing.T) {
localFilesystemMigrations []*models.Migration
remoteAppliedMigrations []*models.Migration
cfg configloader.Config
- sortOrder sortOrder
+ sortOrder SortOrder
expectedMigrations []*models.Migration
}
@@ -85,16 +85,12 @@ func TestListMigrations(t *testing.T) {
sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{},
},
- // Ensure that migrations are sorted in asc order for sequential migrations
+ // Ensure that migrations are sorted by dependencies (topological order)
{
localFilesystemMigrations: []*models.Migration{
- {Version: "20000"},
- {Version: "10000"},
- {Version: "1010"},
- {Version: "1009"},
- {Version: "190"},
- {Version: "110"},
- {Version: "001"},
+ {Version: "003", Dependencies: []string{"001", "002"}},
+ {Version: "002", Dependencies: []string{"001"}},
+ {Version: "001", Dependencies: []string{}},
},
remoteAppliedMigrations: []*models.Migration{},
cfg: configloader.Config{
@@ -104,25 +100,17 @@ func TestListMigrations(t *testing.T) {
},
sortOrder: SortOrderAsc,
expectedMigrations: []*models.Migration{
- {Version: "001"},
- {Version: "110"},
- {Version: "190"},
- {Version: "1009"},
- {Version: "1010"},
- {Version: "10000"},
- {Version: "20000"},
+ {Version: "001", Dependencies: []string{}},
+ {Version: "002", Dependencies: []string{"001"}},
+ {Version: "003", Dependencies: []string{"001", "002"}},
},
},
- // Ensure that migrations are sorted in desc order for sequential migrations
+ // Ensure that migrations are sorted in reverse topological order for desc
{
localFilesystemMigrations: []*models.Migration{
- {Version: "001"},
- {Version: "110"},
- {Version: "190"},
- {Version: "1009"},
- {Version: "1010"},
- {Version: "10000"},
- {Version: "20000"},
+ {Version: "001", Dependencies: []string{}},
+ {Version: "002", Dependencies: []string{"001"}},
+ {Version: "003", Dependencies: []string{"001", "002"}},
},
remoteAppliedMigrations: []*models.Migration{},
cfg: configloader.Config{
@@ -132,69 +120,9 @@ func TestListMigrations(t *testing.T) {
},
sortOrder: SortOrderDesc,
expectedMigrations: []*models.Migration{
- {Version: "20000"},
- {Version: "10000"},
- {Version: "1010"},
- {Version: "1009"},
- {Version: "190"},
- {Version: "110"},
- {Version: "001"},
- },
- },
- // Ensure that migrations are sorted in asc order for timestamp migrations
- {
- localFilesystemMigrations: []*models.Migration{
- {Version: "20080509220905"},
- {Version: "20080509220405"},
- {Version: "20080509150405"},
- {Version: "20080502150405"},
- {Version: "20080102150405"},
- {Version: "20070102150405"},
- {Version: "20060102150405"},
- },
- remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleTimestamp,
- },
- },
- sortOrder: SortOrderAsc,
- expectedMigrations: []*models.Migration{
- {Version: "20060102150405"},
- {Version: "20070102150405"},
- {Version: "20080102150405"},
- {Version: "20080502150405"},
- {Version: "20080509150405"},
- {Version: "20080509220405"},
- {Version: "20080509220905"},
- },
- },
- // Ensure that migrations are sorted in desc order for timestamp migrations
- {
- localFilesystemMigrations: []*models.Migration{
- {Version: "20060102150405"},
- {Version: "20070102150405"},
- {Version: "20080102150405"},
- {Version: "20080502150405"},
- {Version: "20080509150405"},
- {Version: "20080509220405"},
- {Version: "20080509220905"},
- },
- remoteAppliedMigrations: []*models.Migration{},
- cfg: configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleTimestamp,
- },
- },
- sortOrder: SortOrderDesc,
- expectedMigrations: []*models.Migration{
- {Version: "20080509220905"},
- {Version: "20080509220405"},
- {Version: "20080509150405"},
- {Version: "20080502150405"},
- {Version: "20080102150405"},
- {Version: "20070102150405"},
- {Version: "20060102150405"},
+ {Version: "003", Dependencies: []string{"001", "002"}},
+ {Version: "002", Dependencies: []string{"001"}},
+ {Version: "001", Dependencies: []string{}},
},
},
}
@@ -235,127 +163,6 @@ func TestListMigrations(t *testing.T) {
}
}
-func TestListMigrations_FsRepoListError(t *testing.T) {
- expectedErr := errors.New("fs repo error")
- migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: expectedErr,
- },
- }
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: nil,
- },
- }
-
- svc := NewMigrationService(
- migrationDbRepo,
- migrationFsRepo,
- configloader.Config{},
- bolttest.NullOutputter{},
- )
- migrations, err := svc.ListMigrations(SortOrderAsc)
-
- assert.ErrorIs(t, err, expectedErr)
- var expectedMigrations []*models.Migration
- assert.DeepEqual(t, migrations, expectedMigrations)
-}
-
-func TestListMigrations_DbRepoListError(t *testing.T) {
- expectedErr := errors.New("db repo error")
- migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: nil,
- },
- }
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: expectedErr,
- },
- }
-
- svc := NewMigrationService(
- migrationDbRepo,
- migrationFsRepo,
- configloader.Config{},
- bolttest.NullOutputter{},
- )
- migrations, err := svc.ListMigrations(SortOrderAsc)
-
- assert.ErrorIs(t, err, expectedErr)
- var expectedMigrations []*models.Migration
- assert.DeepEqual(t, migrations, expectedMigrations)
-}
-
-func TestListMigrations_ParseErrDuringSort(t *testing.T) {
- type test struct {
- m1 models.Migration
- m2 models.Migration
- versionStyle configloader.VersionStyle
- }
-
- tests := []test{
- {
- m1: models.Migration{Version: "abc"},
- m2: models.Migration{Version: "123"},
- versionStyle: configloader.VersionStyleSequential,
- },
- {
- m1: models.Migration{Version: "123"},
- m2: models.Migration{Version: "abc"},
- versionStyle: configloader.VersionStyleSequential,
- },
- {
- m1: models.Migration{Version: "20060102150405"},
- m2: models.Migration{Version: "abc"},
- versionStyle: configloader.VersionStyleTimestamp,
- },
- {
- m1: models.Migration{Version: "abc"},
- m2: models.Migration{Version: "20060102150405"},
- versionStyle: configloader.VersionStyleTimestamp,
- },
- }
-
- for _, tc := range tests {
- migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: map[string]*models.Migration{
- tc.m1.Version: {Version: tc.m1.Version},
- tc.m2.Version: {Version: tc.m2.Version},
- },
- Err: nil,
- },
- }
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: nil,
- },
- }
-
- svc := NewMigrationService(
- migrationDbRepo,
- migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: tc.versionStyle,
- },
- },
- bolttest.NullOutputter{},
- )
- migrations, err := svc.ListMigrations(SortOrderAsc)
-
- assert.ErrorContains(t, err, "unable to sort migrations")
- var expectedMigrations []*models.Migration
- assert.DeepEqual(t, migrations, expectedMigrations)
- }
-}
-
func TestCreateMigration_VersionStyleTimestamp(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
CreateReturnValue: bolttest.CreateReturnValue{
@@ -580,140 +387,169 @@ func TestCreateMigration_SequentialVersionIncrementsParsingErr(t *testing.T) {
assert.Nil(t, migration)
}
-func TestApplyMigration_WithTransaction(t *testing.T) {
+func TestCreateMigration_FirstMigrationNoDependencies(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);",
- Options: sqlparse.ExecutionOptions{UseTransaction: true},
- },
- Err: nil,
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
},
- }
-
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ CreateReturnValue: bolttest.CreateReturnValue{
Err: nil,
},
}
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.ApplyMigration(&models.Migration{})
+ migration, err := svc.CreateMigration("first migration")
assert.Nil(t, err)
- assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 1)
- assert.Equal(t, migrationDbRepo.ApplyCallCount, 0)
+ assert.Equal(t, len(migration.Dependencies), 0)
}
-func TestApplyMigration_WithoutTransaction(t *testing.T) {
+func TestCreateMigration_SecondMigrationDependsOnFirst(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Contents: "CREATE TABLE tmp(id INT PRIMARY KEY, id2 INT, id3 INT);",
- Options: sqlparse.ExecutionOptions{UseTransaction: false},
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
},
Err: nil,
},
- }
-
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ApplyReturnValue: bolttest.ApplyReturnValue{
+ CreateReturnValue: bolttest.CreateReturnValue{
Err: nil,
},
}
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.ApplyMigration(&models.Migration{})
+ migration, err := svc.CreateMigration("second migration")
assert.Nil(t, err)
- assert.Equal(t, migrationDbRepo.ApplyWithTxCallCount, 0)
- assert.Equal(t, migrationDbRepo.ApplyCallCount, 1)
+ assert.Equal(t, len(migration.Dependencies), 1)
+ assert.Contains(t, migration.Dependencies, "001")
}
-func TestApplyMigration_ReadUpgradeScriptErr(t *testing.T) {
- expectedErr := errors.New("error!")
+func TestCreateMigration_LinearChainDependsOnLatest(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
- Script: sqlparse.MigrationScript{},
- Err: expectedErr,
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"002"}},
+ },
+ Err: nil,
+ },
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
},
}
-
migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.ApplyMigration(&models.Migration{})
+ migration, err := svc.CreateMigration("fourth migration")
- assert.ErrorIs(t, err, expectedErr)
+ assert.Nil(t, err)
+ assert.Equal(t, len(migration.Dependencies), 1)
+ assert.Contains(t, migration.Dependencies, "003")
}
-func TestApplyMigration_ApplyWithTxErr(t *testing.T) {
+func TestCreateMigration_ParallelBranchesDependsOnBoth(t *testing.T) {
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Options: sqlparse.ExecutionOptions{
- UseTransaction: true,
- },
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001"}},
},
Err: nil,
},
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
}
- expectedErr := errors.New("error!")
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{Err: expectedErr},
- }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.ApplyMigration(&models.Migration{})
+ migration, err := svc.CreateMigration("merge migration")
- assert.ErrorIs(t, err, expectedErr)
+ assert.Nil(t, err)
+ assert.Equal(t, len(migration.Dependencies), 2)
+ assert.Contains(t, migration.Dependencies, "002")
+ assert.Contains(t, migration.Dependencies, "003")
}
-func TestApplyMigration_ApplyErr(t *testing.T) {
+func TestCreateMigration_ComplexGraphMultipleLeaves(t *testing.T) {
+ // Diamond shape: 001 -> 002, 001 -> 003, 002 -> 004, 003 -> 004
+ // Plus independent branch: 005
+ // Leaf nodes are 004 and 005
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Options: sqlparse.ExecutionOptions{
- UseTransaction: false,
- },
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ "003": {Version: "003", Dependencies: []string{"001"}},
+ "004": {Version: "004", Dependencies: []string{"002", "003"}},
+ "005": {Version: "005", Dependencies: []string{}},
},
Err: nil,
},
+ CreateReturnValue: bolttest.CreateReturnValue{
+ Err: nil,
+ },
}
- expectedErr := errors.New("error!")
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- ApplyReturnValue: bolttest.ApplyReturnValue{Err: expectedErr},
- }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.ApplyMigration(&models.Migration{})
+ migration, err := svc.CreateMigration("new migration")
- assert.ErrorIs(t, err, expectedErr)
+ assert.Nil(t, err)
+ assert.Equal(t, len(migration.Dependencies), 2)
+ assert.Contains(t, migration.Dependencies, "004")
+ assert.Contains(t, migration.Dependencies, "005")
}
func TestApplyAllMigrations_ListMigrationsErr(t *testing.T) {
@@ -918,163 +754,242 @@ func TestApplyUpToVersion_AppliesMigrations(t *testing.T) {
assert.Equal(t, migrationFsRepo.ReadUpgradeScriptCallCount, 2)
}
-func TestRevertMigration_WithTransaction(t *testing.T) {
+func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) {
+ expectedErr := errors.New("error!")
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Contents: "DROP TABLE tmp;",
- Options: sqlparse.ExecutionOptions{UseTransaction: true},
- },
- Err: nil,
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
},
}
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{},
+ bolttest.NullOutputter{},
+ )
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{
- Err: nil,
+ err := svc.RevertDownToVersion("001")
+
+ assert.ErrorIs(t, err, expectedErr)
+}
+
+func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ }
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
},
}
svc := NewMigrationService(
- migrationDbRepo,
+ &bolttest.MockMigrationDBRepo{},
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertMigration(&models.Migration{})
+ err := svc.RevertDownToVersion("002")
- assert.Nil(t, err)
- assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 1)
- assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
+ assert.ErrorContains(t, err, "migration with version 002 does not exist")
}
-func TestRevertMigration_WithoutTransaction(t *testing.T) {
- migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Contents: "DROP TABLE tmp;",
- Options: sqlparse.ExecutionOptions{UseTransaction: false},
- },
- Err: nil,
- },
+func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
}
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- RevertReturnValue: bolttest.RevertReturnValue{
- Err: nil,
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
},
}
svc := NewMigrationService(
- migrationDbRepo,
+ &bolttest.MockMigrationDBRepo{},
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertMigration(&models.Migration{})
+ err := svc.RevertDownToVersion("001")
- assert.Nil(t, err)
- assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
- assert.Equal(t, migrationDbRepo.RevertCallCount, 1)
+ assert.ErrorContains(t, err, "migration with version 001 isn't applied")
}
-func TestRevertMigration_ReadUpgradeScriptErr(t *testing.T) {
+func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ }
expectedErr := errors.New("error!")
migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Script: sqlparse.MigrationScript{},
- Err: expectedErr,
+ Err: expectedErr,
},
}
- migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
- migrationDbRepo,
+ &bolttest.MockMigrationDBRepo{},
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertMigration(&models.Migration{})
+ err := svc.RevertDownToVersion("001")
assert.ErrorIs(t, err, expectedErr)
}
-func TestRevertMigration_RevertWithTxErr(t *testing.T) {
+func TestRevertDownToVersion_RevertsMigrations(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ "002": {Version: "002", Applied: true},
+ "003": {Version: "003", Applied: false},
+ }
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Options: sqlparse.ExecutionOptions{
- UseTransaction: true,
- },
- },
- Err: nil,
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
},
}
+ svc := NewMigrationService(
+ &bolttest.MockMigrationDBRepo{},
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.RevertDownToVersion("001")
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2)
+}
+
+func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) {
expectedErr := errors.New("error!")
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- RevertWithTxReturnValue: bolttest.RevertWithTxReturnValue{Err: expectedErr},
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: nil,
+ Err: expectedErr,
+ },
}
svc := NewMigrationService(
- migrationDbRepo,
+ &bolttest.MockMigrationDBRepo{},
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertMigration(&models.Migration{})
+ err := svc.RevertAllMigrations()
assert.ErrorIs(t, err, expectedErr)
}
-func TestRevertMigration_RevertErr(t *testing.T) {
+func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: false},
+ "002": {Version: "002", Applied: false},
+ }
migrationFsRepo := &bolttest.MockMigrationFsRepo{
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Script: sqlparse.MigrationScript{
- Options: sqlparse.ExecutionOptions{
- UseTransaction: false,
- },
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
},
- Err: nil,
},
+ bolttest.NullOutputter{},
+ )
+
+ err := svc.RevertAllMigrations()
+
+ assert.Nil(t, err)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
+}
+
+func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) {
+ migrations := map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
}
expectedErr := errors.New("error!")
- migrationDbRepo := &bolttest.MockMigrationDBRepo{
- RevertReturnValue: bolttest.RevertReturnValue{Err: expectedErr},
+ migrationFsRepo := &bolttest.MockMigrationFsRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: migrations,
+ Err: nil,
+ },
+ ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
+ Err: expectedErr,
+ },
}
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleSequential,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.RevertMigration(&models.Migration{})
+ err := svc.RevertAllMigrations()
assert.ErrorIs(t, err, expectedErr)
+ assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
+ assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
}
-func TestRevertDownToVersion_ListMigrationsErr(t *testing.T) {
- expectedErr := errors.New("error!")
+func TestCreateMigration_FsRepoListErrForLeafNodes(t *testing.T) {
+ // Test fsRepo.List error when finding leaf nodes (line 280)
+ // Using timestamp version style so we only call List once (for leaf nodes)
+ expectedErr := errors.New("fs error finding leaf nodes")
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: nil,
Err: expectedErr,
},
}
+
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.RevertDownToVersion("001")
+ migration, err := svc.CreateMigration("new migration")
assert.ErrorIs(t, err, expectedErr)
+ assert.ErrorContains(t, err, "unable to list migrations for finding leaf nodes")
+ assert.Nil(t, migration)
}
-func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) {
+func TestApplyAllMigrations_CheckDependenciesAppliedErr(t *testing.T) {
+ // Test CheckDependenciesApplied error (line 66)
+ // Migration 002 depends on 001, but 001 doesn't exist
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: true},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false},
}
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
@@ -1082,21 +997,29 @@ func TestRevertDownToVersion_TargetMigrationNotFound(t *testing.T) {
Err: nil,
},
}
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
svc := NewMigrationService(
- &bolttest.MockMigrationDBRepo{},
+ migrationDbRepo,
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertDownToVersion("002")
+ err := svc.ApplyAllMigrations()
- assert.ErrorContains(t, err, "migration with version 002 does not exist")
+ assert.ErrorContains(t, err, "non-existent migration 001")
}
-func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) {
+func TestApplyUpToVersion_CheckDependenciesAppliedErr(t *testing.T) {
+ // Test CheckDependenciesApplied error in ApplyUpToVersion (line 116)
+ // Migration 002 depends on 001, but 001 doesn't exist
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: false},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false},
}
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
@@ -1104,49 +1027,62 @@ func TestRevertDownToVersion_TargetMigrationNotApplied(t *testing.T) {
Err: nil,
},
}
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ }
svc := NewMigrationService(
- &bolttest.MockMigrationDBRepo{},
+ migrationDbRepo,
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertDownToVersion("001")
+ err := svc.ApplyUpToVersion("002")
- assert.ErrorContains(t, err, "migration with version 001 isn't applied")
+ assert.ErrorContains(t, err, "non-existent migration 001")
}
-func TestRevertDownToVersion_RevertMigrationErr(t *testing.T) {
+func TestRevertAllMigrations_CheckNoDependentsAppliedErr(t *testing.T) {
+ // Test CheckNoDependentsApplied error (line 173)
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: true},
+ "001": {Version: "001", Dependencies: []string{}, Applied: true},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: true},
}
- expectedErr := errors.New("error!")
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: migrations,
Err: nil,
},
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Err: expectedErr,
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ "002": {Version: "002", Applied: true},
+ },
+ Err: nil,
},
}
svc := NewMigrationService(
- &bolttest.MockMigrationDBRepo{},
+ migrationDbRepo,
migrationFsRepo,
configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertDownToVersion("001")
+ err := svc.RevertAllMigrations()
- assert.ErrorIs(t, err, expectedErr)
+ assert.ErrorContains(t, err, "cannot revert migration")
}
-func TestRevertDownToVersion_RevertsMigrations(t *testing.T) {
+func TestRevertDownToVersion_CheckNoDependentsAppliedErr(t *testing.T) {
+ // Test CheckNoDependentsApplied error in RevertDownToVersion (line 223)
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: true},
- "002": {Version: "002", Applied: true},
- "003": {Version: "003", Applied: false},
+ "001": {Version: "001", Dependencies: []string{}, Applied: true},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: true},
}
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
@@ -1154,102 +1090,135 @@ func TestRevertDownToVersion_RevertsMigrations(t *testing.T) {
Err: nil,
},
}
- svc := NewMigrationService(
- &bolttest.MockMigrationDBRepo{},
- migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Applied: true},
+ "002": {Version: "002", Applied: true},
},
+ Err: nil,
},
+ }
+ svc := NewMigrationService(
+ migrationDbRepo,
+ migrationFsRepo,
+ configloader.Config{},
bolttest.NullOutputter{},
)
err := svc.RevertDownToVersion("001")
- assert.Nil(t, err)
- assert.Equal(t, migrationFsRepo.ReadDowngradeScriptCallCount, 2)
+ assert.ErrorContains(t, err, "cannot revert migration")
}
-func TestRevertAllMigrations_ListMigrationsErr(t *testing.T) {
- expectedErr := errors.New("error!")
+func TestCreateMigration_FindLeafNodesErr(t *testing.T) {
+ // Test FindLeafNodes error when existing migrations have circular dependency (line 285)
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
- Migrations: nil,
- Err: expectedErr,
+ Migrations: map[string]*models.Migration{
+ "001": {Version: "001", Dependencies: []string{"002"}},
+ "002": {Version: "002", Dependencies: []string{"001"}},
+ },
+ Err: nil,
},
}
svc := NewMigrationService(
&bolttest.MockMigrationDBRepo{},
migrationFsRepo,
- configloader.Config{},
+ configloader.Config{
+ Migrations: configloader.MigrationsConfig{
+ VersionStyle: configloader.VersionStyleTimestamp,
+ },
+ },
bolttest.NullOutputter{},
)
- err := svc.RevertAllMigrations()
+ migration, err := svc.CreateMigration("new migration")
- assert.ErrorIs(t, err, expectedErr)
+ assert.NotNil(t, err)
+ assert.ErrorContains(t, err, "circular dependency")
+ assert.Nil(t, migration)
}
-func TestRevertAllMigrations_RevertsNothingIfNoneApplied(t *testing.T) {
+func TestApplyAllMigrations_DependencyNotYetAppliedInLoop(t *testing.T) {
+ // Test that CheckDependenciesApplied fails when a dependency was applied earlier
+ // in the same ApplyAllMigrations call but the Applied field wasn't updated
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: false},
- "002": {Version: "002", Applied: false},
+ "001": {Version: "001", Dependencies: []string{}, Applied: false},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false},
}
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: migrations,
Err: nil,
},
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE test (id INT);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ Err: nil,
+ },
}
- migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
+ configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertAllMigrations()
+ err := svc.ApplyAllMigrations()
- assert.Nil(t, err)
- assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
- assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
+ // This should fail because after applying 001, its Applied field in the list
+ // is still false, so CheckDependenciesApplied for 002 will fail
+ assert.ErrorContains(t, err, "dependency 001 has not been applied")
}
-func TestRevertAllMigrations_RevertMigrationErr(t *testing.T) {
+func TestApplyUpToVersion_DependencyNotYetAppliedInLoop(t *testing.T) {
+ // Same test for ApplyUpToVersion
migrations := map[string]*models.Migration{
- "001": {Version: "001", Applied: true},
+ "001": {Version: "001", Dependencies: []string{}, Applied: false},
+ "002": {Version: "002", Dependencies: []string{"001"}, Applied: false},
}
- expectedErr := errors.New("error!")
migrationFsRepo := &bolttest.MockMigrationFsRepo{
ListReturnValue: bolttest.ListReturnValue{
Migrations: migrations,
Err: nil,
},
- ReadDowngradeScriptReturnValue: bolttest.ReadDowngradeScriptReturnValue{
- Err: expectedErr,
+ ReadUpgradeScriptReturnValue: bolttest.ReadUpgradeScriptReturnValue{
+ Script: sqlparse.MigrationScript{
+ Contents: "CREATE TABLE test (id INT);",
+ Options: sqlparse.ExecutionOptions{UseTransaction: true},
+ },
+ Err: nil,
+ },
+ }
+ migrationDbRepo := &bolttest.MockMigrationDBRepo{
+ ListReturnValue: bolttest.ListReturnValue{
+ Migrations: map[string]*models.Migration{},
+ Err: nil,
+ },
+ ApplyWithTxReturnValue: bolttest.ApplyWithTxReturnValue{
+ Err: nil,
},
}
- migrationDbRepo := &bolttest.MockMigrationDBRepo{}
svc := NewMigrationService(
migrationDbRepo,
migrationFsRepo,
- configloader.Config{
- Migrations: configloader.MigrationsConfig{
- VersionStyle: configloader.VersionStyleSequential,
- },
- },
+ configloader.Config{},
bolttest.NullOutputter{},
)
- err := svc.RevertAllMigrations()
+ err := svc.ApplyUpToVersion("002")
- assert.ErrorIs(t, err, expectedErr)
- assert.Equal(t, migrationDbRepo.RevertCallCount, 0)
- assert.Equal(t, migrationDbRepo.RevertWithTxCallCount, 0)
+ assert.ErrorContains(t, err, "dependency 001 has not been applied")
}
diff --git a/internal/sqlparse/sqlparse.go b/internal/sqlparse/sqlparse.go
index 90ebc2f..a43d354 100644
--- a/internal/sqlparse/sqlparse.go
+++ b/internal/sqlparse/sqlparse.go
@@ -8,7 +8,13 @@ import (
)
type SqlParser interface {
- Parse(reader io.Reader) (MigrationScript, MigrationScript, error)
+ Parse(reader io.Reader) (Migration, error)
+}
+
+type Migration struct {
+ UpgradeScript MigrationScript
+ DowngradeScript MigrationScript
+ Dependencies []string
}
type MigrationScript struct {
@@ -29,6 +35,7 @@ const (
const (
upgradeScriptDeliminator = "-- migrate:up"
downgradeScriptDeliminator = "-- migrate:down"
+ dependsDirective = "-- depends:"
transactionOptionName = "transaction"
)
@@ -40,12 +47,11 @@ func NewSqlParser() SqlParser {
return &sqlParser{}
}
-// Parse parses out the migration's upgrade and downgrade scripts
-// and any custom execution options with those scripts. It returns
-// the upgrade script, downgrade script, and any error if one occurred.
-func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript, error) {
- upgradeScript := MigrationScript{}
- downgradeScript := MigrationScript{}
+// Parse parses out the migration's upgrade and downgrade scripts,
+// any custom execution options with those scripts, and migration dependencies.
+// It returns the upgrade script, downgrade script, dependencies, and any error if one occurred.
+func (sp *sqlParser) Parse(reader io.Reader) (Migration, error) {
+ migration := Migration{Dependencies: []string{}}
scanner := bufio.NewScanner(reader)
currentSection := unknownSection
@@ -55,25 +61,30 @@ func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript,
if strings.HasPrefix(loweredLine, upgradeScriptDeliminator) {
currentSection = upgradeScriptSection
- upgradeScript.Options = parseExecutionOptions(line)
+ migration.UpgradeScript.Options = parseExecutionOptions(line)
} else if strings.HasPrefix(loweredLine, downgradeScriptDeliminator) {
currentSection = downgradeScriptSection
- downgradeScript.Options = parseExecutionOptions(line)
+ migration.DowngradeScript.Options = parseExecutionOptions(line)
} else if currentSection == upgradeScriptSection {
- upgradeScript.Contents += line + "\n"
+ migration.UpgradeScript.Contents += line + "\n"
} else if currentSection == downgradeScriptSection {
- downgradeScript.Contents += line + "\n"
+ migration.DowngradeScript.Contents += line + "\n"
+ }
+
+ if strings.HasPrefix(loweredLine, dependsDirective) {
+ migration.Dependencies = append(migration.Dependencies, parseDependencies(line)...)
}
+
}
if err := scanner.Err(); err != nil {
- return MigrationScript{}, MigrationScript{}, fmt.Errorf(
+ return Migration{}, fmt.Errorf(
"parsing sql file encountered an error: %w",
err,
)
}
- return upgradeScript, downgradeScript, nil
+ return migration, nil
}
// parseExecutionOptions extracts the execution options for a
@@ -81,10 +92,10 @@ func (sp *sqlParser) Parse(reader io.Reader) (MigrationScript, MigrationScript,
func parseExecutionOptions(line string) ExecutionOptions {
options := ExecutionOptions{UseTransaction: true}
- parts := strings.Split(line, " ")
- for _, part := range parts {
+ parts := strings.SplitSeq(line, " ")
+ for part := range parts {
part = strings.ToLower(part)
- if strings.Contains(part, "transaction:") {
+ if strings.Contains(part, fmt.Sprintf("%s:", transactionOptionName)) {
option := strings.Split(part, ":")[1]
options.UseTransaction = option != "false"
}
@@ -92,3 +103,31 @@ func parseExecutionOptions(line string) ExecutionOptions {
return options
}
+
+// parseDependencies extracts migration dependencies from a depends directive.
+// Expected format: -- depends: 001, 002, 003
+func parseDependencies(line string) []string {
+ dependencies := []string{}
+
+ loweredLine := strings.ToLower(line)
+ prefixIdx := strings.Index(loweredLine, dependsDirective)
+ if prefixIdx == -1 {
+ return dependencies
+ }
+
+ depsStr := line[prefixIdx+len(dependsDirective):]
+ depsStr = strings.TrimSpace(depsStr)
+ if depsStr == "" {
+ return dependencies
+ }
+
+ deps := strings.SplitSeq(depsStr, ",")
+ for dep := range deps {
+ dep = strings.TrimSpace(dep)
+ if dep != "" {
+ dependencies = append(dependencies, dep)
+ }
+ }
+
+ return dependencies
+}
diff --git a/internal/sqlparse/sqlparse_test.go b/internal/sqlparse/sqlparse_test.go
index 5cbbc07..20ba72f 100644
--- a/internal/sqlparse/sqlparse_test.go
+++ b/internal/sqlparse/sqlparse_test.go
@@ -182,12 +182,12 @@ func TestSqlParser_Parse(t *testing.T) {
for _, tc := range testCases {
sqlParser := sqlparse.NewSqlParser()
- upgradeScript, downgradeScript, err := sqlParser.Parse(
+ migration, err := sqlParser.Parse(
strings.NewReader(tc.migration),
)
assert.Nil(t, err)
- check.DeepEqual(t, upgradeScript, tc.expectedUpgradeScript)
- check.DeepEqual(t, downgradeScript, tc.expectedDowngradeScript)
+ check.DeepEqual(t, migration.UpgradeScript, tc.expectedUpgradeScript)
+ check.DeepEqual(t, migration.DowngradeScript, tc.expectedDowngradeScript)
}
}
@@ -215,7 +215,104 @@ func TestSqlParserParseError(t *testing.T) {
ErrCond: " ",
}
sqlParser := sqlparse.NewSqlParser()
- _, _, err := sqlParser.Parse(reader)
+ _, err := sqlParser.Parse(reader)
assert.NotNil(t, err)
assert.ErrorContains(t, err, "unwanted input encountered")
}
+
+func TestSqlParser_ParseDependencies(t *testing.T) {
+ testCases := []struct {
+ name string
+ migration string
+ expectedDependencies []string
+ }{
+ {
+ name: "no dependencies",
+ migration: "-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{},
+ },
+ {
+ name: "single dependency",
+ migration: "-- depends: 001\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001"},
+ },
+ {
+ name: "multiple dependencies",
+ migration: "-- depends: 001, 002, 003\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001", "002", "003"},
+ },
+ {
+ name: "multiple dependencies without spaces",
+ migration: "-- depends: 001,002,003\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001", "002", "003"},
+ },
+ {
+ name: "case insensitive depends",
+ migration: "-- DEPENDS: 001, 002\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001", "002"},
+ },
+ {
+ name: "mixed case depends",
+ migration: "-- Depends: 001\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001"},
+ },
+ {
+ name: "empty depends directive",
+ migration: "-- depends: \n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{},
+ },
+ {
+ name: "depends with trailing comma",
+ migration: "-- depends: 001, 002,\n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001", "002"},
+ },
+ {
+ name: "depends with extra whitespace",
+ migration: "-- depends: 001 , 002 , 003 \n-- migrate:up\nCREATE TABLE users(id int);",
+ expectedDependencies: []string{"001", "002", "003"},
+ },
+ {
+ name: "timestamp version dependencies",
+ migration: "-- depends: 20240316145038, 20240317120000\n-- migrate:up\nCREATE TABLE posts(id int);",
+ expectedDependencies: []string{"20240316145038", "20240317120000"},
+ },
+ {
+ name: "multiple depends directives",
+ migration: `-- depends: 001
+-- depends: 002
+-- migrate:up
+CREATE TABLE users(id int);`,
+ expectedDependencies: []string{"001", "002"},
+ },
+ {
+ name: "depends before up and down",
+ migration: `-- depends: 001, 002
+-- migrate:up
+CREATE TABLE users(id int);
+-- migrate:down
+DROP TABLE users;`,
+ expectedDependencies: []string{"001", "002"},
+ },
+ {
+ name: "depends anywhere in file",
+ migration: `-- depends: 004
+-- migrate:up
+-- depends: 003
+CREATE TABLE users(id int);
+-- migrate:down
+-- depends: 002
+DROP TABLE users;
+-- depends: 001`,
+ expectedDependencies: []string{"004", "003", "002", "001"},
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ sqlParser := sqlparse.NewSqlParser()
+ migration, err := sqlParser.Parse(strings.NewReader(tc.migration))
+ assert.Nil(t, err)
+ check.DeepEqual(t, migration.Dependencies, tc.expectedDependencies)
+ })
+ }
+}
diff --git a/scripts/lib/test.sh b/scripts/lib/test.sh
index 6eae9a1..939f822 100644
--- a/scripts/lib/test.sh
+++ b/scripts/lib/test.sh
@@ -145,6 +145,7 @@ test_run_go_tests() {
cmd="BOLT_DB_DSN='$dsn' BOLT_DB_DRIVER='$db_type' BOLT_DB_MIGRATIONS_TABLE='bolt_migrations' $cmd"
fi
+ log_debug "Running command: $cmd"
if eval "$cmd"; then
log_success "Tests completed successfully!"
if [[ -n "$coverage_file" && -f "$coverage_file" ]]; then