actual_db_schema 0.5.0 → 0.7.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 +9 -0
- data/Gemfile.lock +1 -1
- data/README.md +38 -22
- data/lib/actual_db_schema/commands/base.rb +33 -0
- data/lib/actual_db_schema/commands/list.rb +78 -0
- data/lib/actual_db_schema/commands/rollback.rb +22 -0
- data/lib/actual_db_schema/git.rb +12 -0
- data/lib/actual_db_schema/patches/migration_proxy.rb +1 -1
- data/lib/actual_db_schema/store.rb +43 -0
- data/lib/actual_db_schema/version.rb +1 -1
- data/lib/actual_db_schema.rb +7 -0
- data/lib/tasks/db.rake +5 -17
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 957b11aa8112a41c4cc875a2c34510e651c9f41b94ab76c86b765f3f89501a27
|
4
|
+
data.tar.gz: b1d2c2a81647bb18b6a1b3bce71691ff3cac4cd809585a4830bc332a3b135df5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9fc19c47a665af2efc5bc1e5e1d37cf8335f11d5f7c3e248e67cfb5d044d69fd626890b31e539f8f0e0fc3f63bdd36ecd3ce624383c42a1320bed9632a037242
|
7
|
+
data.tar.gz: d1af7d177e1dfbd9f7e69e502e5e55d7b73906c0d63b6a3c125ff4af311994feb3b1c5a97ba9eca321a7a8e2aa791016ee3e8de7baffda598f6eb5dcabb10ade
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
## [0.7.0] - 2024-01-18
|
2
|
+
|
3
|
+
- db:phantom_migrations displays the branch in which the phantion migration was run
|
4
|
+
|
5
|
+
## [0.6.0] - 2024-01-03
|
6
|
+
|
7
|
+
- Added db:phantom_migrations task to display phantom migrations
|
8
|
+
- Updated README
|
9
|
+
|
1
10
|
## [0.5.0] - 2023-11-06
|
2
11
|
|
3
12
|
- Rails 7.1 support added
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,37 @@
|
|
2
2
|
|
3
3
|
# ActualDbSchema
|
4
4
|
|
5
|
-
|
5
|
+
Does switching between branches in your Rails app mess up the DB schema?
|
6
|
+
|
7
|
+
Keep the DB schema actual across branches in your Rails project. Just install `actual_db_schema` gem and run `db:migrate` in branches as usual. It automatically rolls back the *phantom migrations* (non-relevant to the current branch). No additional steps are needed.
|
8
|
+
|
9
|
+
## Why ActualDbSchema
|
10
|
+
|
11
|
+
Still not clear why it's needed? To grasp the purpose of this gem and the issue it addresses, review the problem definition outlined below.
|
12
|
+
|
13
|
+
### The problem definition
|
14
|
+
|
15
|
+
Imagine you're working on **branch A**. You add a not-null column to a database table with a migration. You run the migration. Then you switch to **branch B**. The code in **branch B** isn't aware of this newly added field. When it tries to write data to the table, it fails with an error `null value provided for non-null field`. Why? The existing code is writing a null value into the column with a not-null constraint.
|
16
|
+
|
17
|
+
Here's an example of this error:
|
18
|
+
|
19
|
+
ActiveRecord::NotNullViolation:
|
20
|
+
PG::NotNullViolation: ERROR: null value in column "log" of relation "check_results" violates not-null constraint
|
21
|
+
DETAIL: Failing row contains (8, 46, success, 2022-10-16 21:47:21.07212, 2022-10-16 21:47:21.07212, null).
|
22
|
+
|
23
|
+
Furthermore, the `db:migrate` task on **branch B** generates an irrelevant diff on the `schema.rb` file, reflecting the new column added in **branch A**.
|
24
|
+
|
25
|
+
To fix this, you need to switch back to **branch A**, find the migration that added the problematic field, and roll it back. We'll call it a *phantom migration*. It's a pain, especially if you have a lot of branches in your project because you have to remember which branch the *phantom migration* is in and then manually roll it back.
|
26
|
+
|
27
|
+
With `actual_db_schema` gem you don't need to care about that anymore. It saves you time by handling all this dirty work behind the scenes automatically.
|
28
|
+
|
29
|
+
### How it solves the issue
|
30
|
+
|
31
|
+
This gem stores all run migrations with their code in the `tmp/migrations` folder. Whenever you perform a schema dump, it rolls back the *phantom migrations*.
|
32
|
+
|
33
|
+
The *phantom migrations* list is the difference between the migrations you've executed (in the `tmp/migrations` folder) and the current ones (in the `db/migrate` folder).
|
34
|
+
|
35
|
+
Therefore, all you do is run rails `db:migrate` in your current branch. `actual_db_schema` will ensure the DB schema is up-to-date. You'll never have an inaccurate `schema.rb` file again.
|
6
36
|
|
7
37
|
## Installation
|
8
38
|
|
@@ -20,34 +50,20 @@ And then execute:
|
|
20
50
|
|
21
51
|
## Usage
|
22
52
|
|
23
|
-
|
24
|
-
|
25
|
-
In **branch A** I add a mandatory (not null) field into DB via migration and run it.
|
26
|
-
Then I switch to another **branch B**. This branch's code is not aware of that field.
|
27
|
-
As the result, the code is failing with an error "null value provided for non-null field".
|
28
|
-
Moreover, a DB rake task generates a diff on `schema.rb` that's not relevant to this branch.
|
29
|
-
I can switch to **branch A** and roll back the migration, but I need to remember that branch or waste time on it.
|
30
|
-
|
31
|
-
Some example of this error:
|
32
|
-
|
33
|
-
ActiveRecord::NotNullViolation:
|
34
|
-
PG::NotNullViolation: ERROR: null value in column "log" of relation "check_results" violates not-null constraint
|
35
|
-
DETAIL: Failing row contains (8, 46, success, 2022-10-16 21:47:21.07212, 2022-10-16 21:47:21.07212, null).
|
36
|
-
|
37
|
-
This gem saves all run migrations inside `tmp/migrations` folder.
|
38
|
-
Every run of schema dump (that's a dependency of `db:migrate` task as well) rolls back the "unknown" for the current branch migrations
|
39
|
-
looking into the `tmp/migrations` folder.
|
53
|
+
Just run `rails db:migrate` inside the current branch.
|
40
54
|
|
41
|
-
|
55
|
+
> [!WARNING]
|
56
|
+
> This solution implies that all migrations are reversible. The irreversible migrations should be solved manually. At the moment, the gem ignores them. You will see warnings in the terminal for each irreversible migrations.
|
42
57
|
|
43
|
-
|
44
|
-
|
58
|
+
The gem offers the following rake tasks that can be manually run according to your preferences:
|
59
|
+
- `rails db:rollback_branches` - run it to manually rolls back phantom migrations.
|
60
|
+
- `rails db:phantom_migrations` - displays a list of phantom migrations.
|
45
61
|
|
46
62
|
## Development
|
47
63
|
|
48
64
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
49
65
|
|
50
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, update the CHANGELOG, make the commit, push, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
51
67
|
|
52
68
|
## Contributing
|
53
69
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActualDbSchema
|
4
|
+
module Commands
|
5
|
+
# Base class for all commands
|
6
|
+
class Base
|
7
|
+
def call
|
8
|
+
unless ActualDbSchema.config.fetch(:enabled, true)
|
9
|
+
raise "ActualDbSchema is disabled. Set ActualDbSchema.config[:enabled] = true to enable it."
|
10
|
+
end
|
11
|
+
|
12
|
+
if ActiveRecord::Migration.current_version >= 6
|
13
|
+
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches")
|
14
|
+
end
|
15
|
+
|
16
|
+
call_impl
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def call_impl
|
22
|
+
raise NotImplementedError
|
23
|
+
end
|
24
|
+
|
25
|
+
def context
|
26
|
+
@context ||=
|
27
|
+
ActiveRecord::Base.connection.migration_context.tap do |c|
|
28
|
+
c.extend(ActualDbSchema::Patches::MigrationContext)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActualDbSchema
|
4
|
+
module Commands
|
5
|
+
# Shows the list of phantom migrations
|
6
|
+
class List < Base
|
7
|
+
private
|
8
|
+
|
9
|
+
def call_impl
|
10
|
+
preambule
|
11
|
+
table
|
12
|
+
end
|
13
|
+
|
14
|
+
def indexed_phantom_migrations
|
15
|
+
@indexed_phantom_migrations ||= context.migrations.index_by { |m| m.version.to_s }
|
16
|
+
end
|
17
|
+
|
18
|
+
def preambule
|
19
|
+
puts "\nPhantom migrations\n\n"
|
20
|
+
puts "Below is a list of irrelevant migrations executed in unmerged branches."
|
21
|
+
puts "To bring your database schema up to date, the migrations marked as \"up\" should be rolled back."
|
22
|
+
puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n"
|
23
|
+
puts header.join(" ")
|
24
|
+
puts "-" * separator_width
|
25
|
+
end
|
26
|
+
|
27
|
+
def separator_width
|
28
|
+
header.map(&:length).sum + (header.size - 1) * 2
|
29
|
+
end
|
30
|
+
|
31
|
+
def header
|
32
|
+
@header ||=
|
33
|
+
[
|
34
|
+
"Status".center(8),
|
35
|
+
"Migration ID".ljust(14),
|
36
|
+
"Branch".ljust(branch_column_width),
|
37
|
+
"Migration File".ljust(16)
|
38
|
+
]
|
39
|
+
end
|
40
|
+
|
41
|
+
def table
|
42
|
+
context.migrations_status.each do |status, version|
|
43
|
+
line = line_for(status, version)
|
44
|
+
puts line if line
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def line_for(status, version)
|
49
|
+
migration = indexed_phantom_migrations[version]
|
50
|
+
return unless migration
|
51
|
+
|
52
|
+
[
|
53
|
+
status.center(8),
|
54
|
+
version.to_s.ljust(14),
|
55
|
+
branch_for(version).ljust(branch_column_width),
|
56
|
+
migration.filename.gsub("#{Rails.root}/", "")
|
57
|
+
].join(" ")
|
58
|
+
end
|
59
|
+
|
60
|
+
def branch_for(version)
|
61
|
+
metadata.fetch(version, {})[:branch] || "unknown"
|
62
|
+
end
|
63
|
+
|
64
|
+
def metadata
|
65
|
+
@metadata ||= ActualDbSchema::Store.instance.read
|
66
|
+
end
|
67
|
+
|
68
|
+
def longest_branch_name
|
69
|
+
@longest_branch_name ||=
|
70
|
+
metadata.values.map { |v| v[:branch] }.compact.max_by(&:length) || "unknown"
|
71
|
+
end
|
72
|
+
|
73
|
+
def branch_column_width
|
74
|
+
longest_branch_name.length
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActualDbSchema
|
4
|
+
module Commands
|
5
|
+
# Rolls back all phantom migrations
|
6
|
+
class Rollback < Base
|
7
|
+
private
|
8
|
+
|
9
|
+
def call_impl
|
10
|
+
context.rollback_branches
|
11
|
+
|
12
|
+
return if ActualDbSchema.failed.empty?
|
13
|
+
|
14
|
+
puts ""
|
15
|
+
puts "[ActualDbSchema] Irreversible migrations were found from other branches. Roll them back or fix manually:"
|
16
|
+
puts ""
|
17
|
+
puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n")
|
18
|
+
puts ""
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -6,7 +6,7 @@ module ActualDbSchema
|
|
6
6
|
module MigrationProxy
|
7
7
|
def migrate(direction)
|
8
8
|
super(direction)
|
9
|
-
|
9
|
+
ActualDbSchema::Store.instance.write(filename) if direction == :up
|
10
10
|
end
|
11
11
|
end
|
12
12
|
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActualDbSchema
|
4
|
+
# Stores the migrated files into the tmp folder
|
5
|
+
class Store
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
Item = Struct.new(:version, :timestamp, :branch)
|
9
|
+
|
10
|
+
def write(filename)
|
11
|
+
basename = File.basename(filename)
|
12
|
+
FileUtils.copy(filename, folder.join(basename))
|
13
|
+
record_metadata(filename)
|
14
|
+
end
|
15
|
+
|
16
|
+
def read
|
17
|
+
return {} unless File.exist?(store_file)
|
18
|
+
|
19
|
+
CSV.read(store_file).map { |line| Item.new(*line) }.index_by(&:version)
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
def record_metadata(filename)
|
25
|
+
version = File.basename(filename).scan(/(\d+)_.*\.rb/).first.first
|
26
|
+
CSV.open(store_file, "a") do |csv|
|
27
|
+
csv << [
|
28
|
+
version,
|
29
|
+
Time.current.iso8601,
|
30
|
+
Git.current_branch
|
31
|
+
]
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def folder
|
36
|
+
ActualDbSchema.migrated_folder
|
37
|
+
end
|
38
|
+
|
39
|
+
def store_file
|
40
|
+
folder.join("metadata.csv")
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/lib/actual_db_schema.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "active_record/migration"
|
4
|
+
require "csv"
|
5
|
+
require_relative "actual_db_schema/git"
|
6
|
+
require_relative "actual_db_schema/store"
|
4
7
|
require_relative "actual_db_schema/version"
|
5
8
|
require_relative "actual_db_schema/patches/migration_proxy"
|
6
9
|
require_relative "actual_db_schema/patches/migrator"
|
7
10
|
require_relative "actual_db_schema/patches/migration_context"
|
8
11
|
|
12
|
+
require_relative "actual_db_schema/commands/base"
|
13
|
+
require_relative "actual_db_schema/commands/rollback"
|
14
|
+
require_relative "actual_db_schema/commands/list"
|
15
|
+
|
9
16
|
# The main module definition
|
10
17
|
module ActualDbSchema
|
11
18
|
raise NotImplementedError, "ActualDbSchema is only supported in Rails" unless defined?(Rails)
|
data/lib/tasks/db.rake
CHANGED
@@ -3,24 +3,12 @@
|
|
3
3
|
namespace :db do
|
4
4
|
desc "Rollback migrations that were run inside not a merged branch."
|
5
5
|
task rollback_branches: :load_config do
|
6
|
-
|
7
|
-
|
8
|
-
end
|
9
|
-
|
10
|
-
if ActiveRecord::Migration.current_version >= 6
|
11
|
-
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches")
|
12
|
-
end
|
6
|
+
ActualDbSchema::Commands::Rollback.new.call
|
7
|
+
end
|
13
8
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
if ActualDbSchema.failed.any?
|
18
|
-
puts ""
|
19
|
-
puts "[ActualDbSchema] Irreversible migrations were found from other branches. Roll them back or fix manually:"
|
20
|
-
puts ""
|
21
|
-
puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n")
|
22
|
-
puts ""
|
23
|
-
end
|
9
|
+
desc "List all phantom migrations - non-relevant migrations that were run inside not a merged branch."
|
10
|
+
task phantom_migrations: :load_config do
|
11
|
+
ActualDbSchema::Commands::List.new.call
|
24
12
|
end
|
25
13
|
|
26
14
|
task _dump: :rollback_branches
|
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.7.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:
|
11
|
+
date: 2024-01-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -119,9 +119,14 @@ files:
|
|
119
119
|
- gemfiles/rails.7.0.gemfile
|
120
120
|
- gemfiles/rails.7.1.gemfile
|
121
121
|
- lib/actual_db_schema.rb
|
122
|
+
- lib/actual_db_schema/commands/base.rb
|
123
|
+
- lib/actual_db_schema/commands/list.rb
|
124
|
+
- lib/actual_db_schema/commands/rollback.rb
|
125
|
+
- lib/actual_db_schema/git.rb
|
122
126
|
- lib/actual_db_schema/patches/migration_context.rb
|
123
127
|
- lib/actual_db_schema/patches/migration_proxy.rb
|
124
128
|
- lib/actual_db_schema/patches/migrator.rb
|
129
|
+
- lib/actual_db_schema/store.rb
|
125
130
|
- lib/actual_db_schema/version.rb
|
126
131
|
- lib/railtie.rb
|
127
132
|
- lib/tasks/db.rake
|