reversibility_checker 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c8716ded2cae0032c7edbbed7c8984b81d713d87fc6db20e65e78b127b96d492
4
- data.tar.gz: ed5c4a55d4d0a22e56455b5ef97f1081f4825f84de998ddcc8a61f307d82bbb5
3
+ metadata.gz: 4b91654dea3e5c95c23d07864caadb872035768bf3138133e56e06ddce86e910
4
+ data.tar.gz: 4f7b994b1386b66fdfedb7970459d29ce521d1a6a962a96442939126e622fd28
5
5
  SHA512:
6
- metadata.gz: 864547946fd5688164ba9f629d85518b8a48f93d9b3a56e5c711e58ec763306ca255fee03ec86ea65323e6c29845bfca70552de1ab71354be7516d6d50eb7e78
7
- data.tar.gz: fa59da40b51345a7a53342e3a55e1fb1c0138ec71fe3e9a5f3fa8b316183a32d33870ad9cd5cb8d9af303bf4fbbab21751b948b4320b025024cee196e0f92142
6
+ metadata.gz: 997f9a560d5606c4ff4a3269c9381be065610fe20c685cc3e6155a0cf86adf18823af3aaf2ad85d7a1e601af469f5103a537df8a8ad0ea346c9b7fcf879696b7
7
+ data.tar.gz: '088af56d236e3636ea9ce5e05f2811b6b2d49adec4dd7db500aadc89e7ff134ddfe95892276d73cbb2795745f973d1835cce33ebb767bb37116e1eaa4343216e'
data/CHANGELOG.md ADDED
@@ -0,0 +1,18 @@
1
+ # Changelog
2
+
3
+ ## [Unreleased]
4
+
5
+ ## [[0.2.0]](https://github.com/naoty/reversibility_checker/compare/v0.1.1...v0.2.0) - 2018-10-21
6
+ ### Added
7
+ * Support `CURRENT_VERSION` environment variable to specify a current schema version before migration.
8
+
9
+ ### Changed
10
+ * Check the reversibility of each migrations.
11
+
12
+ ## [[0.1.1]](https://github.com/naoty/reversibility_checker/compare/v0.1.0...v0.1.1) - 2018-10-19
13
+ ### Added
14
+ * Support sql schema format.
15
+
16
+ ## [0.1.0] - 2018-10-17
17
+ ### Added
18
+ * Add fundamental features.
data/README.md CHANGED
@@ -16,6 +16,12 @@ $ rails db:migrate:check_reversibility
16
16
 
17
17
  This task checks diffs between a current schema and a schema which migrated and rollbacked. If there are diffs, it will print the diffs and exit with exit status `1`.
18
18
 
19
+ ```bash
20
+ $ rails db:migrate:check_reversibility CURRENT_VERSION=20181020120000
21
+ ```
22
+
23
+ `CURRENT_VERSION` environment variable specifies a current schema version before migration. If it isn't passed, a current schema version will be the one of local database.
24
+
19
25
  ## Example
20
26
 
21
27
  if you create a following migration file,
@@ -36,6 +42,7 @@ when you run `db:migrate:check_reversibility` task, this task will run `db:migra
36
42
 
37
43
  ```bash
38
44
  $ rails db:migrate:check_reversibility
45
+ == +20181020041241 ============================================================
39
46
  # This file is auto-generated from the current state of the database. Instead
40
47
  # of editing this file, please use the migrations feature of Active Record to
41
48
  # incrementally modify your database, and then regenerate this schema definition.
@@ -59,4 +66,5 @@ $ rails db:migrate:check_reversibility
59
66
  end
60
67
 
61
68
  end
69
+ == -20181019134724 ============================================================
62
70
  ```
@@ -17,4 +17,27 @@ module ReversibilityChecker
17
17
  file.read
18
18
  end
19
19
  end
20
+
21
+ def self.current_version(config)
22
+ return ENV["CURRENT_VERSION"].to_i unless ENV["CURRENT_VERSION"].nil?
23
+
24
+ ActiveRecord::Base.establish_connection(config)
25
+ version = ActiveRecord::Migrator.current_version
26
+ ActiveRecord::Base.remove_connection
27
+
28
+ return version
29
+ end
30
+
31
+ def self.target_versions(config, current_version)
32
+ ActiveRecord::Base.establish_connection(config)
33
+ migration_context = ActiveRecord::Base.connection.migration_context
34
+ ActiveRecord::Base.remove_connection
35
+
36
+ versions = ActiveRecord::Tasks::DatabaseTasks.migrations_paths.flat_map do |migrations_path|
37
+ Dir["#{migrations_path}/*.rb"].map do |filepath|
38
+ migration_context.parse_migration_filename(filepath).first.to_i
39
+ end
40
+ end
41
+ versions.select { |version| version > current_version }.sort
42
+ end
20
43
  end
@@ -8,12 +8,10 @@ namespace :db do
8
8
  require "active_record/schema_dumper"
9
9
 
10
10
  config = ActiveRecord::Base.configurations.fetch(Rails.env)
11
+ current_version = ReversibilityChecker.current_version(config)
12
+ target_versions = ReversibilityChecker.target_versions(config, current_version)
11
13
  migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
12
14
 
13
- ActiveRecord::Base.establish_connection(config)
14
- current_version = ActiveRecord::Migrator.current_version
15
- ActiveRecord::Base.remove_connection
16
-
17
15
  # Use a temporary database
18
16
  config["database"] += "_tmp"
19
17
 
@@ -21,28 +19,38 @@ namespace :db do
21
19
  $stdout = Tempfile.new
22
20
 
23
21
  ActiveRecord::Tasks::DatabaseTasks.create(config)
24
- at_exit { ActiveRecord::Tasks::DatabaseTasks.drop(config) }
25
-
26
22
  ActiveRecord::Base.establish_connection(config)
27
- ActiveRecord::Base.connection.migration_context.up(current_version)
28
23
 
29
- # Take a snapshot of the temporary schema
30
- current_schema = ReversibilityChecker.dump(config)
24
+ at_exit {
25
+ ActiveRecord::Tasks::DatabaseTasks.drop(config)
26
+ ActiveRecord::Base.remove_connection
27
+ }
31
28
 
32
- # Migrate a temporary schema upto latest and rollback to current version
33
- ActiveRecord::Base.connection.migration_context.up
34
- ActiveRecord::Base.connection.migration_context.down(current_version)
29
+ reversible = true
30
+ base_version = current_version
35
31
 
36
- # Take a snapshot again
37
- rollbacked_schema = ReversibilityChecker.dump(config)
32
+ target_versions.each do |target_version|
33
+ ActiveRecord::Base.connection.migration_context.up(base_version)
34
+ base_schema = ReversibilityChecker.dump(config)
38
35
 
39
- # Compare two snapshots
40
- diff = Diffy::Diff.new(current_schema, rollbacked_schema)
36
+ ActiveRecord::Base.connection.migration_context.up(target_version)
37
+ ActiveRecord::Base.connection.migration_context.down(base_version)
38
+ rollbacked_schema = ReversibilityChecker.dump(config)
41
39
 
42
- if diff.count > 0
43
- Object::STDOUT.puts diff.to_s(:color)
44
- exit 1
40
+ diff = Diffy::Diff.new(base_schema, rollbacked_schema)
41
+
42
+ if diff.count > 0
43
+ Object::STDOUT.puts "== +#{target_version} ============================================================"
44
+ Object::STDOUT.puts diff.to_s(:color)
45
+ Object::STDOUT.puts "== -#{base_version} ============================================================"
46
+ Object::STDOUT.puts ""
47
+ reversible = false
48
+ end
49
+
50
+ base_version = target_version
45
51
  end
52
+
53
+ exit 1 unless reversible
46
54
  end
47
55
  end
48
56
  end
@@ -1,3 +1,3 @@
1
1
  module ReversibilityChecker
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reversibility_checker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naoto Kaneko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-19 00:00:00.000000000 Z
11
+ date: 2018-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -74,6 +74,7 @@ extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
76
  - ".gitignore"
77
+ - CHANGELOG.md
77
78
  - Gemfile
78
79
  - Gemfile.lock
79
80
  - LICENSE.txt