Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ For reference, here is the default configuration file:
A hash of the migration file is saved to the database when a migration is applied. This way, we can ensure that migrations are not modified after being applied.
If a migration has been modified, you will get an error as it probably means that you should reset your database to update a migration.

During development, you may want to edit your migrations after they have been applied to your database. In that case, you can use the `--no-hash-check` flag to
bypass hash checking when you want to apply or rollback migrations. It isn't recommended to use this flag in production, because then your migrations scripts
don't match what is in your database !

> This feature was introduced in version 3 of cigogne.
> Note that it requires Gleam >=1.9.0.
> If you use earlier versions of the library, you can use the to_v3 script to have them filled automatically.
Expand Down
12 changes: 12 additions & 0 deletions src/cigogne.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ pub fn main() {
pub fn create_engine(
config: config.Config,
) -> Result(MigrationEngine, CigogneError) {
warn_no_hash_check(config)

use #(db_data, applied) <- result.try(init_db_and_get_applied(config))
use files <- result.try(read_migrations(config))
use applied <- result.try(
Expand All @@ -160,6 +162,16 @@ pub fn create_engine(
Ok(MigrationEngine(db_data:, applied:, unapplied:, files:, config:))
}

fn warn_no_hash_check(config: config.Config) -> Nil {
case config.migrations.no_hash_check {
option.Some(True) ->
io.println(
"Warning: no_hash_check is enabled ! Do not use it in production environments as it can lead to inconsistencies between your migration files and the database.",
)
_ -> Nil
}
}

fn init_db_and_get_applied(
config: config.Config,
) -> Result(#(database.DatabaseData, List(migration.Migration)), CigogneError) {
Expand Down
2 changes: 1 addition & 1 deletion src/cigogne/internal/cli.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ fn migrations_config_decoder(
"no-hash-check",
[],
"",
"/!\\ Warning: not recommended /!\\ Disable hash checks on engine creation",
"/!\\ Warning: not recommended in production /!\\ Disable hash checks on engine creation. Can be useful during development.",
cli_lib.bool,
)

Expand Down
10 changes: 9 additions & 1 deletion src/cigogne/migration.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import cigogne/internal/utils
import gleam/bool
import gleam/int
import gleam/io
import gleam/list
import gleam/order
import gleam/result
Expand Down Expand Up @@ -170,7 +171,14 @@ pub fn match_migrations(
case match_res {
Ok(match) -> {
case no_hash_check || migration.sha256 == match.sha256 {
True -> Ok(match)
True -> {
io.println(
"Warning: Hash of file "
<> to_fullname(migration)
<> " has changed, but hash check is disabled.",
)
Ok(match)
}
False -> Error(FileHashChanged(migration |> to_fullname()))
}
}
Expand Down