actual_db_schema 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e85ea0679fbf525eeab14a9b9db7d3bfb49cf514b554427db12d1e784e8c55d9
4
- data.tar.gz: 87e02334c37e8d10cae7c05f2aa5df474157100074458d0b41d90b6583ac839c
3
+ metadata.gz: ba9797cc49da84c28977ef23e93bb8f76a34176614fa12deb3f2e7787dcb880e
4
+ data.tar.gz: 502f59630d933d643df14de7e50c72c84b4fe00bf8ff0034bb01c604e8d2c343
5
5
  SHA512:
6
- metadata.gz: '083c8a127fc6350f67904dbc7e4295dacd7e924620e2aa9ce7e515fa4b5a300454c9fa8e4f50905f6f626a66b83f24dc10fb10753ff94d048ec986bbea8f73a4'
7
- data.tar.gz: b125355516facc2111e9b48effc33d9eb2946e57c2e75ab2966477357769bf199ff3dd20c3fc1c6765d5a25bccba02aa7b27d3d57688a3781a326368f7b80dce
6
+ metadata.gz: d0e6ad5b607a3ff67243b5e2ec8d8fa6863068f0e63118de3a2dbda3922892e5c73e036c1c0da7693a7ab427fe923fe82b214adb8640352848c87545f1e8862d
7
+ data.tar.gz: 8a47aec7467625c6e68e8d35fd2f5035863d8556e7da9e6cce13931d27a06acae823e2beb186286cc62eab0c636e80af866ffcac0e1fab8c3667c89fc76b962c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,9 @@
1
- ## [Unreleased]
1
+ ## [0.2.0] - 2022-10-19
2
+
3
+ - Catch exceptions about irreversible migrations and show a warning
4
+ - Namespace all patches into gem module
5
+ - Fix typo in a module name with a patch
6
+ - Use guard clause
2
7
 
3
8
  ## [0.1.0] - 2022-10-16
4
9
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actual_db_schema (0.1.0)
4
+ actual_db_schema (0.2.0)
5
5
  activerecord
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # ActualDbSchema
2
2
 
3
- Keep DB schema consistent while switching between branches with no additional actions.
3
+ Keep Rails DB schema consistent while switching between branches with no additional actions.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,7 @@ And then execute:
18
18
 
19
19
  ## Usage
20
20
 
21
- TL;TR: Just run `rails db:migrate` inside the current branch.
21
+ TLTR; Just run `rails db:migrate` inside the current branch.
22
22
 
23
23
  In **branch A** I add a mandatory (not null) field into DB via migration and run it.
24
24
  Then I switch to another **branch B**. This branch's code is not aware of that field.
@@ -26,12 +26,20 @@ As the result, the code is failing with an error "null value provided for non-nu
26
26
  Moreover, a DB rake task generates a diff on `schema.rb` that's not relevant to this branch.
27
27
  I can switch to **branch A** and roll back the migration, but I need to remember that branch or waste time on it.
28
28
 
29
- This code changes the standard migration behavior to save all run migrations inside `tmp/migrations` folder.
30
- Every run of schema dump (that's a dependency of `db:migrate` task as well) rolls back the "unknown" migrations
31
- for the current branch looking into the `tmp/migrations` folder.
29
+ Some example of this error:
32
30
 
33
- Using this gem you need to run `rails db:migrate` in the current branch and it will actualize the DB schema.
34
- You will never have wrongly generated `schema.rb`.
31
+ ActiveRecord::NotNullViolation:
32
+ PG::NotNullViolation: ERROR: null value in column "log" of relation "check_results" violates not-null constraint
33
+ DETAIL: Failing row contains (8, 46, success, 2022-10-16 21:47:21.07212, 2022-10-16 21:47:21.07212, null).
34
+
35
+ This gem saves all run migrations inside `tmp/migrations` folder.
36
+ Every run of schema dump (that's a dependency of `db:migrate` task as well) rolls back the "unknown" for the current branch migrations
37
+ looking into the `tmp/migrations` folder.
38
+
39
+ You just need to run `rails db:migrate` in the current branch to actualize the DB schema. With its hand, you will never have wrongly generated `schema.rb`.
40
+
41
+ > **Warning**
42
+ > This solution implies that all migrations are reversible. The cases with irreversible migrations should be solved manually. At the moment, these migrations are ignored by the gem and you will see a warning if some migrations can't roll back automatically.
35
43
 
36
44
  ## Development
37
45
 
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = <<~DESC
13
13
  Switching between branches with migrations and running them can make your DB inconsistent
14
14
  and not working in another branch if not roll the migration back.
15
- Install this gem and forget about that issue by running the standard rake db:migrate."
15
+ Install this gem and forget about that issue by running the standard rake db:migrate.
16
16
  DESC
17
17
  spec.homepage = "https://github.com/widefix/actual_db_schema"
18
18
  spec.license = "MIT"
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActualDbSchema
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/tasks/db.rake CHANGED
@@ -1,32 +1,35 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- if Rails.env.development?
4
- require "active_record/migration"
3
+ return unless Rails.env.development?
5
4
 
6
- def migrated_folder
7
- Rails.root.join("tmp", "migrated").tap { |folder| FileUtils.mkdir_p(folder) }
8
- end
5
+ require "active_record/migration"
6
+
7
+ def migrated_folder
8
+ Rails.root.join("tmp", "migrated").tap { |folder| FileUtils.mkdir_p(folder) }
9
+ end
9
10
 
10
- def migration_filename(fullpath)
11
- fullpath.split("/").last
11
+ def migration_filename(fullpath)
12
+ fullpath.split("/").last
13
+ end
14
+
15
+ # All patches are namespaced into this module
16
+ module ActualDbSchema
17
+ class << self
18
+ attr_accessor :failed
12
19
  end
13
20
 
21
+ self.failed = []
22
+
14
23
  # Track migrated migrations inside the tmp folder
15
24
  module MigrationProxyPatch
16
25
  def migrate(direction)
17
- if direction == :up
18
- FileUtils.copy(filename, migrated_folder.join(basename))
19
- else
20
- FileUtils.rm(migrated_folder.join(basename))
21
- end
22
26
  super(direction)
27
+ FileUtils.copy(filename, migrated_folder.join(basename)) if direction == :up
23
28
  end
24
29
  end
25
30
 
26
- ActiveRecord::MigrationProxy.prepend(MigrationProxyPatch)
27
-
28
31
  # Run only one migration that's being rolled back
29
- module MigratorPath
32
+ module MigratorPatch
30
33
  def runnable
31
34
  migration = migrations.first # there is only one migration, because we pass only one here
32
35
  ran?(migration) ? [migration] : []
@@ -38,8 +41,12 @@ if Rails.env.development?
38
41
  def rollback_branches
39
42
  migrations.each do |migration|
40
43
  migrator = ActiveRecord::Migrator.new(:down, [migration], schema_migration, migration.version)
41
- migrator.extend(MigratorPath)
44
+ migrator.extend(ActualDbSchema::MigratorPatch)
42
45
  migrator.migrate
46
+ rescue StandardError => e
47
+ raise unless e.message.include?("ActiveRecord::IrreversibleMigration")
48
+
49
+ ActualDbSchema.failed << migration
43
50
  end
44
51
  end
45
52
 
@@ -54,17 +61,27 @@ if Rails.env.development?
54
61
  other_branches_files.reject { migration_filename(_1).in?(current_branch_file_names) }
55
62
  end
56
63
  end
64
+ end
57
65
 
58
- namespace :db do
59
- desc "Rollback migrations that were run inside not a merged branch."
60
- task rollback_branches: :load_config do
61
- ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches")
66
+ ActiveRecord::MigrationProxy.prepend(ActualDbSchema::MigrationProxyPatch)
62
67
 
63
- context = ActiveRecord::Base.connection.migration_context
64
- context.extend(MigrationContextPatch)
65
- context.rollback_branches
66
- end
68
+ namespace :db do
69
+ desc "Rollback migrations that were run inside not a merged branch."
70
+ task rollback_branches: :load_config do
71
+ ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches")
67
72
 
68
- task _dump: :rollback_branches
73
+ context = ActiveRecord::Base.connection.migration_context
74
+ context.extend(ActualDbSchema::MigrationContextPatch)
75
+ context.rollback_branches
76
+ if ActualDbSchema.failed.any?
77
+ puts ""
78
+ puts "[ActualDbSchema] Irreversible migrations were found from other branches. Roll them back or fix manually:"
79
+ puts ""
80
+ puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n")
81
+ puts ""
82
+ end
83
+ # raise ActualDbSchema.failed.inspect
69
84
  end
85
+
86
+ task _dump: :rollback_branches
70
87
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: actual_db_schema
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Kaleshka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-16 00:00:00.000000000 Z
11
+ date: 2022-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -27,7 +27,7 @@ dependencies:
27
27
  description: |
28
28
  Switching between branches with migrations and running them can make your DB inconsistent
29
29
  and not working in another branch if not roll the migration back.
30
- Install this gem and forget about that issue by running the standard rake db:migrate."
30
+ Install this gem and forget about that issue by running the standard rake db:migrate.
31
31
  email:
32
32
  - ka8725@gmail.com
33
33
  executables: []