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 +4 -4
- data/CHANGELOG.md +6 -1
- data/Gemfile.lock +1 -1
- data/README.md +15 -7
- data/actual_db_schema.gemspec +1 -1
- data/lib/actual_db_schema/version.rb +1 -1
- data/lib/tasks/db.rake +42 -25
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ba9797cc49da84c28977ef23e93bb8f76a34176614fa12deb3f2e7787dcb880e
|
4
|
+
data.tar.gz: 502f59630d933d643df14de7e50c72c84b4fe00bf8ff0034bb01c604e8d2c343
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0e6ad5b607a3ff67243b5e2ec8d8fa6863068f0e63118de3a2dbda3922892e5c73e036c1c0da7693a7ab427fe923fe82b214adb8640352848c87545f1e8862d
|
7
|
+
data.tar.gz: 8a47aec7467625c6e68e8d35fd2f5035863d8556e7da9e6cce13931d27a06acae823e2beb186286cc62eab0c636e80af866ffcac0e1fab8c3667c89fc76b962c
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
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
|
-
|
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
|
-
|
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
|
-
|
34
|
-
|
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
|
|
data/actual_db_schema.gemspec
CHANGED
@@ -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"
|
data/lib/tasks/db.rake
CHANGED
@@ -1,32 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
require "active_record/migration"
|
3
|
+
return unless Rails.env.development?
|
5
4
|
|
6
|
-
|
7
|
-
|
8
|
-
|
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
|
-
|
11
|
-
|
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
|
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(
|
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
|
-
|
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
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
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
|
-
|
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.
|
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-
|
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: []
|