actual_db_schema 0.7.4 → 0.7.5

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: 7bae784cf88c546a852fea372e8bcbf1356008bc36c9e63c6ff0e18fb1c06bd6
4
- data.tar.gz: 0fc13227fd92ecb46f7d10f16d900477115a2ca960c701a2080d19690f1631d3
3
+ metadata.gz: a49295fefc3c066ebb4ba22ea2304a25013963ce7daf614d97032ec249d2769a
4
+ data.tar.gz: bb870c91a0896aaf35d304869f2f6af2d9dc190e8d8694ff9a56b56d8c693bc0
5
5
  SHA512:
6
- metadata.gz: 226db7dc680bb706a73048c1fec3e52685a1e36ac5d5e2cef82511080234e1cc6b789cb5266ea101669462205823ec565da9b059cc7d4b0752fc4f600f0cfbe2
7
- data.tar.gz: 47c936d8bb2cddf627dbb04ab8d3993e71a960363d4472c9ef9fa4b2102b89789fea7257333b6b91e38ffe1a288c52b5e72ef619377c97276684a0f46dd04ee1
6
+ metadata.gz: ecffe8659acb2add81d97c36418ae6b7f5bb65c053f44856e4da3da9a4c91911e0016bb3fa8c5fbbd463816c45c1fdef63a5ae58fa2b2b4fdb864a47c1946f3a
7
+ data.tar.gz: cb823af551359b10b97dd3e0d3f51966c076f95ee8b16fcc9d2b1d335348301e17185a8df2e83b62c0a8887caea841b9d8b9df37446de5a36912038a4d518441
data/CHANGELOG.md CHANGED
@@ -1,3 +1,6 @@
1
+ ## [0.7.5] - 2024-06-20
2
+ - Added db:rollback_migrations:manual task to manually rolls back phantom migrations one by one
3
+
1
4
  ## [0.7.4] - 2024-06-06
2
5
  - Rails 7.2 support added
3
6
  - Rails 6.0 support added
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- actual_db_schema (0.7.4)
4
+ actual_db_schema (0.7.5)
5
5
  activerecord (>= 6.0.0)
6
6
  activesupport (>= 6.0.0)
7
7
  csv
data/README.md CHANGED
@@ -59,8 +59,28 @@ Just run `rails db:migrate` inside the current branch. It will roll back all pha
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.
@@ -88,12 +108,15 @@ To run tests with a specific version of Rails using Appraisal:
88
108
  - Run all tests with Rails 6.0:
89
109
  ```sh
90
110
  bundle exec appraisal rails.6.0 rake test
111
+ ```
91
112
  - Run tests for a specific file:
92
113
  ```sh
93
114
  bundle exec appraisal rails.6.0 rake test TEST=test/rake_task_test.rb
115
+ ```
94
116
  - Run a specific test:
95
117
  ```sh
96
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
+ ```
97
120
 
98
121
  ## Contributing
99
122
 
@@ -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.4"
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
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.4
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-06-15 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