actual_db_schema 0.7.3 → 0.7.5

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: ca3780aca89ac715ade8441154b10a78b258514b3daa1d23db1ede9d37d63108
4
- data.tar.gz: cb4ce56d44d5a5c61d20220ab2ead11830db440438e1dd433d0774cdf23aef97
3
+ metadata.gz: a49295fefc3c066ebb4ba22ea2304a25013963ce7daf614d97032ec249d2769a
4
+ data.tar.gz: bb870c91a0896aaf35d304869f2f6af2d9dc190e8d8694ff9a56b56d8c693bc0
5
5
  SHA512:
6
- metadata.gz: bf59099adf9a660b934fc65c3573460c4034e59876f0770691019799601217b6eeee29b9f3eda43d1ea837380d6da2bb2930819b81dbc9eb334c6bc2f4e4777c
7
- data.tar.gz: 8c9f16c0bf8f0571f163bcf07a73fe4b62dc4c031897d2d156e234aac3d4ca749a6380a41e983c5dec722d61909b0aaf09caf1f3b2303f885bc76385da6b1039
6
+ metadata.gz: ecffe8659acb2add81d97c36418ae6b7f5bb65c053f44856e4da3da9a4c91911e0016bb3fa8c5fbbd463816c45c1fdef63a5ae58fa2b2b4fdb864a47c1946f3a
7
+ data.tar.gz: cb823af551359b10b97dd3e0d3f51966c076f95ee8b16fcc9d2b1d335348301e17185a8df2e83b62c0a8887caea841b9d8b9df37446de5a36912038a4d518441
data/Appraisals CHANGED
@@ -6,3 +6,9 @@
6
6
  gem "activesupport", "~> #{version}.0"
7
7
  end
8
8
  end
9
+
10
+ appraise "rails.edge" do
11
+ gem "rails", ">= 7.2.0.beta"
12
+ gem "activerecord", ">= 7.2.0.beta"
13
+ gem "activesupport", ">= 7.2.0.beta"
14
+ end
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ ## [0.7.5] - 2024-06-20
2
+ - Added db:rollback_migrations:manual task to manually rolls back phantom migrations one by one
3
+
4
+ ## [0.7.4] - 2024-06-06
5
+ - Rails 7.2 support added
6
+ - Rails 6.0 support added
7
+
1
8
  ## [0.7.3] - 2024-04-06
2
9
  - add multipe databases support
3
10
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actual_db_schema (0.7.3)
4
+ actual_db_schema (0.7.5)
5
5
  activerecord (>= 6.0.0)
6
6
  activesupport (>= 6.0.0)
7
7
  csv
@@ -133,6 +133,8 @@ GEM
133
133
  net-smtp (0.4.0)
134
134
  net-protocol
135
135
  nio4r (2.5.9)
136
+ nokogiri (1.15.4-arm64-darwin)
137
+ racc (~> 1.4)
136
138
  nokogiri (1.15.4-x86_64-darwin)
137
139
  racc (~> 1.4)
138
140
  nokogiri (1.15.4-x86_64-linux)
@@ -204,6 +206,7 @@ GEM
204
206
  parser (>= 3.2.1.0)
205
207
  ruby-progressbar (1.13.0)
206
208
  ruby2_keywords (0.0.5)
209
+ sqlite3 (1.6.8-arm64-darwin)
207
210
  sqlite3 (1.6.8-x86_64-darwin)
208
211
  sqlite3 (1.6.8-x86_64-linux)
209
212
  stringio (3.0.8)
@@ -219,6 +222,7 @@ GEM
219
222
  zeitwerk (2.6.12)
220
223
 
221
224
  PLATFORMS
225
+ arm64-darwin-23
222
226
  x86_64-darwin-20
223
227
  x86_64-darwin-22
224
228
  x86_64-linux
data/README.md CHANGED
@@ -52,15 +52,35 @@ If you cannot commit changes to the repo or Gemfile, consider the local Gemfile
52
52
 
53
53
  ## Usage
54
54
 
55
- Just run `rails db:migrate` inside the current branch.
55
+ Just run `rails db:migrate` inside the current branch. It will roll back all phantom migrations for all configured databases in your `database.yml.`
56
56
 
57
57
  > [!WARNING]
58
58
  > 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.
59
59
 
60
60
  The gem offers the following rake tasks that can be manually run according to your preferences:
61
61
  - `rails db:rollback_branches` - run it to manually rolls back phantom migrations.
62
+ - `rails db:rollback_branches:manual` - run it to manually rolls back phantom migrations one by one.
62
63
  - `rails db:phantom_migrations` - displays a list of phantom migrations.
63
64
 
65
+ ## Disabling Automatic Rollback
66
+
67
+ By default, the automatic rollback of migrations is enabled. If you prefer to perform manual rollbacks, you can disable the automatic rollback in two ways:
68
+
69
+ ### 1. Using Environment Variable
70
+
71
+ Set the environment variable `ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED` to `true`:
72
+
73
+ ```sh
74
+ export ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED=true
75
+ ```
76
+
77
+ ### 2. Using Initializer
78
+ Add the following line to your initializer file (`config/initializers/actual_db_schema.rb`):
79
+
80
+ ```ruby
81
+ ActualDbSchema.config[:auto_rollback_disabled] = true
82
+ ```
83
+
64
84
  ## Development
65
85
 
66
86
  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.
@@ -75,6 +95,29 @@ To release a new version do the following in the order:
75
95
  - make the commit and push;
76
96
  - run `bundle exec rake release`. This 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).
77
97
 
98
+ ### Running Tests with Specific Rails Versions
99
+
100
+ The following versions can be specifically tested using Appraisal
101
+ - 6.0
102
+ - 6.1
103
+ - 7.0
104
+ - 7.1
105
+ - edge
106
+
107
+ To run tests with a specific version of Rails using Appraisal:
108
+ - Run all tests with Rails 6.0:
109
+ ```sh
110
+ bundle exec appraisal rails.6.0 rake test
111
+ ```
112
+ - Run tests for a specific file:
113
+ ```sh
114
+ bundle exec appraisal rails.6.0 rake test TEST=test/rake_task_test.rb
115
+ ```
116
+ - Run a specific test:
117
+ ```sh
118
+ bundle exec appraisal rails.6.0 rake test TEST=test/rake_task_test.rb TESTOPTS="--name=/db::db:rollback_branches#test_0003_keeps/"
119
+ ```
120
+
78
121
  ## Contributing
79
122
 
80
123
  Bug reports and pull requests are welcome on GitHub at https://github.com/widefix/actual_db_schema. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/widefix/actual_db_schema/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file was generated by Appraisal
4
+
5
+ source "https://rubygems.org"
6
+
7
+ gem "activerecord", ">= 7.2.0.beta"
8
+ gem "activesupport", ">= 7.2.0.beta"
9
+ gem "minitest", "~> 5.0"
10
+ gem "rake"
11
+ gem "rubocop", "~> 1.21"
12
+ gem "rails", ">= 7.2.0.beta"
13
+ gem "sqlite3"
14
+
15
+ gemspec path: "../"
@@ -19,10 +19,19 @@ module ActualDbSchema
19
19
  end
20
20
 
21
21
  def context
22
- @context ||=
23
- ActiveRecord::Base.connection.migration_context.tap do |c|
24
- c.extend(ActualDbSchema::Patches::MigrationContext)
25
- end
22
+ @context ||= fetch_migration_context.tap do |c|
23
+ c.extend(ActualDbSchema::Patches::MigrationContext)
24
+ end
25
+ end
26
+
27
+ def fetch_migration_context
28
+ ar_version = Gem::Version.new(ActiveRecord::VERSION::STRING)
29
+ if ar_version >= Gem::Version.new("7.2.0") ||
30
+ (ar_version >= Gem::Version.new("7.1.0") && ar_version.prerelease?)
31
+ ActiveRecord::Base.connection_pool.migration_context
32
+ else
33
+ ActiveRecord::Base.connection.migration_context
34
+ end
26
35
  end
27
36
  end
28
37
  end
@@ -19,11 +19,20 @@ module ActualDbSchema
19
19
  puts "\nPhantom migrations\n\n"
20
20
  puts "Below is a list of irrelevant migrations executed in unmerged branches."
21
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"
22
+ database_path = db_config[:database]
23
+ puts "\ndatabase: #{database_path}\n\n"
23
24
  puts header.join(" ")
24
25
  puts "-" * separator_width
25
26
  end
26
27
 
28
+ def db_config
29
+ if ActiveRecord::Base.respond_to?(:connection_db_config)
30
+ ActiveRecord::Base.connection_db_config.configuration_hash
31
+ else
32
+ ActiveRecord::Base.connection_config
33
+ end
34
+ end
35
+
27
36
  def separator_width
28
37
  header.map(&:length).sum + (header.size - 1) * 2
29
38
  end
@@ -4,10 +4,15 @@ module ActualDbSchema
4
4
  module Commands
5
5
  # Rolls back all phantom migrations
6
6
  class Rollback < Base
7
+ def initialize(manual_mode: false)
8
+ @manual_mode = manual_mode || manual_mode_default?
9
+ super()
10
+ end
11
+
7
12
  private
8
13
 
9
14
  def call_impl
10
- context.rollback_branches
15
+ context.rollback_branches(manual_mode: @manual_mode)
11
16
 
12
17
  return if ActualDbSchema.failed.empty?
13
18
 
@@ -17,6 +22,10 @@ module ActualDbSchema
17
22
  puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n")
18
23
  puts ""
19
24
  end
25
+
26
+ def manual_mode_default?
27
+ ActualDbSchema.config[:auto_rollback_disabled]
28
+ end
20
29
  end
21
30
  end
22
31
  end
@@ -4,15 +4,14 @@ module ActualDbSchema
4
4
  module Patches
5
5
  # Add new command to roll back the phantom migrations
6
6
  module MigrationContext
7
- def rollback_branches
7
+ def rollback_branches(manual_mode: false)
8
8
  migrations.reverse_each do |migration|
9
- migrator = down_migrator_for(migration)
10
- migrator.extend(ActualDbSchema::Patches::Migrator)
11
- migrator.migrate
12
- rescue StandardError => e
13
- raise unless e.message.include?("ActiveRecord::IrreversibleMigration")
9
+ next unless status_up?(migration)
14
10
 
15
- ActualDbSchema.failed << migration
11
+ show_info_for(migration) if manual_mode
12
+ migrate(migration) if !manual_mode || user_wants_rollback?
13
+ rescue StandardError => e
14
+ handle_migration_error(e, migration)
16
15
  end
17
16
  end
18
17
 
@@ -36,6 +35,55 @@ module ActualDbSchema
36
35
  current_branch_file_names = current_branch_files.map { |f| ActualDbSchema.migration_filename(f) }
37
36
  other_branches_files.reject { |f| ActualDbSchema.migration_filename(f).in?(current_branch_file_names) }
38
37
  end
38
+
39
+ def status_up?(migration)
40
+ migrations_status.any? do |status, version|
41
+ status == "up" && version.to_s == migration.version.to_s
42
+ end
43
+ end
44
+
45
+ def user_wants_rollback?
46
+ print "\nRollback this migration? [y,n] "
47
+ answer = $stdin.gets.chomp.downcase
48
+ answer[0] == "y"
49
+ end
50
+
51
+ def show_info_for(migration)
52
+ puts "\n[ActualDbSchema] A phantom migration was found and is about to be rolled back."
53
+ puts "Please make a decision from the options below to proceed.\n\n"
54
+ puts "Branch: #{branch_for(migration.version.to_s)}"
55
+ puts "Database: #{db_config[:database]}"
56
+ puts "Version: #{migration.version}\n\n"
57
+ puts File.read(migration.filename)
58
+ end
59
+
60
+ def handle_migration_error(error, migration)
61
+ raise unless error.message.include?("ActiveRecord::IrreversibleMigration")
62
+
63
+ ActualDbSchema.failed << migration
64
+ end
65
+
66
+ def migrate(migration)
67
+ migrator = down_migrator_for(migration)
68
+ migrator.extend(ActualDbSchema::Patches::Migrator)
69
+ migrator.migrate
70
+ end
71
+
72
+ def db_config
73
+ @db_config ||= if ActiveRecord::Base.respond_to?(:connection_db_config)
74
+ ActiveRecord::Base.connection_db_config.configuration_hash
75
+ else
76
+ ActiveRecord::Base.connection_config
77
+ end
78
+ end
79
+
80
+ def branch_for(version)
81
+ metadata.fetch(version, {})[:branch] || "unknown"
82
+ end
83
+
84
+ def metadata
85
+ @metadata ||= ActualDbSchema::Store.instance.read
86
+ end
39
87
  end
40
88
  end
41
89
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActualDbSchema
4
- VERSION = "0.7.3"
4
+ VERSION = "0.7.5"
5
5
  end
@@ -25,7 +25,8 @@ module ActualDbSchema
25
25
 
26
26
  self.failed = []
27
27
  self.config = {
28
- enabled: Rails.env.development?
28
+ enabled: Rails.env.development?,
29
+ auto_rollback_disabled: ENV["ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED"].present?
29
30
  }
30
31
 
31
32
  def self.migrated_folder
@@ -50,7 +51,11 @@ module ActualDbSchema
50
51
  end
51
52
 
52
53
  def self.migrations_paths
53
- ActiveRecord::Base.connection_db_config.migrations_paths
54
+ if ActiveRecord::Base.respond_to?(:connection_db_config)
55
+ ActiveRecord::Base.connection_db_config.migrations_paths
56
+ else
57
+ ActiveRecord::Base.connection_config[:migrations_paths]
58
+ end
54
59
  end
55
60
 
56
61
  def self.migration_filename(fullpath)
@@ -60,7 +65,8 @@ module ActualDbSchema
60
65
  def self.for_each_db_connection
61
66
  configs = ActiveRecord::Base.configurations.configs_for(env_name: ActiveRecord::Tasks::DatabaseTasks.env)
62
67
  configs.each do |db_config|
63
- ActiveRecord::Base.establish_connection(db_config)
68
+ config = db_config.respond_to?(:config) ? db_config.config : db_config
69
+ ActiveRecord::Base.establish_connection(config)
64
70
  yield
65
71
  end
66
72
  end
data/lib/tasks/db.rake CHANGED
@@ -9,6 +9,16 @@ namespace :db do
9
9
  end
10
10
  end
11
11
 
12
+ namespace :rollback_branches do
13
+ desc "Manually rollback phantom migrations one by one"
14
+ task manual: :load_config do
15
+ ActualDbSchema.failed = []
16
+ ActualDbSchema.for_each_db_connection do
17
+ ActualDbSchema::Commands::Rollback.new(manual_mode: true).call
18
+ end
19
+ end
20
+ end
21
+
12
22
  desc "List all phantom migrations - non-relevant migrations that were run inside not a merged branch."
13
23
  task phantom_migrations: :load_config do
14
24
  ActualDbSchema.for_each_db_connection do
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.7.3
4
+ version: 0.7.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrei Kaleshka
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-06 00:00:00.000000000 Z
11
+ date: 2024-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -131,6 +131,7 @@ files:
131
131
  - gemfiles/rails.6.1.gemfile
132
132
  - gemfiles/rails.7.0.gemfile
133
133
  - gemfiles/rails.7.1.gemfile
134
+ - gemfiles/rails.edge.gemfile
134
135
  - lib/actual_db_schema.rb
135
136
  - lib/actual_db_schema/commands/base.rb
136
137
  - lib/actual_db_schema/commands/list.rb
@@ -166,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
166
167
  - !ruby/object:Gem::Version
167
168
  version: '0'
168
169
  requirements: []
169
- rubygems_version: 3.3.3
170
+ rubygems_version: 3.3.26
170
171
  signing_key:
171
172
  specification_version: 4
172
173
  summary: Keep your DB and schema.rb consistent in dev branches.