Skip to content

Commit

Permalink
feat: better logging
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
vladfaust committed Jan 28, 2018
1 parent b1e2c8a commit 329b024
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,29 +101,36 @@ migrator.next_version # => 1
migrator.previous_version # => nil
migrator.up
# => INFO -- : Migrating up to version 0 → 1
# => INFO -- : Successfully migrated from version 0 to 1 in 37.602ms
migrator.current_version # => 1
migrator.previous_version # => 0
migrator.down
# => INFO -- : Migrating down to version 1 → 0
# => INFO -- : Successfully migrated from version 1 to 0 in 27.027ms
migrator.current_version # => 0
migrator.to(10)
# => INFO -- : Migrating up to version 0 → 1 → 2 → 10
# => INFO -- : Successfully migrated from version 0 to 10 in 62.214ms
migrator.current_version # => 10
migrator.next_version # => nil
migrator.redo
# => INFO -- : Migrating down to version 10 → 2 → 1 → 0
# => INFO -- : Successfully migrated from version 10 to 0 in 30.006ms
# => INFO -- : Migrating up to version 0 → 1 → 2 → 10
# => INFO -- : Successfully migrated from version 0 to 10 in 72.877ms
migrator.current_version # => 10
migrator.reset
# => INFO -- : Migrating down to version 10 → 2 → 1 → 0
# => INFO -- : Successfully migrated from version 10 to 0 in 28.958ms
migrator.current_version # => 0
migrator.to_latest
# => INFO -- : Migrating up to version 0 → 1 → 2 → 10
# => INFO -- : Successfully migrated from version 0 to 10 in 39.189ms
migrator.current_version # => 10
```
Expand All @@ -147,11 +154,7 @@ Usage:

```
$ cake db:migrate
DEBUG -- : CREATE TABLE IF NOT EXISTS version (version INT NOT NULL)
DEBUG -- : SELECT COUNT(version) FROM version
DEBUG -- : SELECT version FROM version
DEBUG -- : Applied versions: 1, 2, 10
DEBUG -- : SELECT version FROM version
INFO -- : Migrating up to version 0 → 1 → 2 → 10
INFO -- : Successfully migrated from version 0 to 10 in 33.46ms
```

Expand Down
19 changes: 17 additions & 2 deletions src/migrate/migrator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ module Migrate
table: @table,
}

@logger.try &.debug(query)
@db.scalar(query).as(Int32 | Int64)
end

Expand Down Expand Up @@ -102,7 +101,21 @@ module Migrate
end
end

@logger.try &.debug("Applied versions: #{applied_versions.map(&.to_s).join(", ")}")
case direction
when Direction::Up
@logger.try &.info("Migrating up to version #{applied_versions.dup.unshift(current.to_i64).map(&.to_s).join("")}")
when Direction::Down
# Add previous version to the list of applied versions,
# turning "10 → 2" into "10 → 2 → 1"
versions = applied_versions.dup.tap do |v|
index = all_versions.index(v[0])
if index && index > 0
v.unshift(all_versions[index - 1])
end
end

@logger.try &.info("Migrating down to version #{versions.reverse.map(&.to_s).join("")}")
end

applied_files = migrations.select do |filename|
applied_versions.includes?(MIGRATION_FILE_REGEX.match(filename).not_nil!["version"].to_i)
Expand All @@ -123,9 +136,11 @@ module Migrate

@db.transaction do |tx|
queries.each do |query|
@logger.try &.debug(query)
tx.connection.exec(query)
end

@logger.try &.debug(update_version_query(target_version))
tx.connection.exec(update_version_query(target_version))
end

Expand Down

0 comments on commit 329b024

Please sign in to comment.