actual_db_schema 0.7.9 → 0.8.0
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 +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +2 -2
- data/README.md +34 -0
- data/actual_db_schema.gemspec +12 -0
- data/lib/actual_db_schema/commands/rollback.rb +44 -34
- data/lib/actual_db_schema/failed_migration.rb +1 -1
- data/lib/actual_db_schema/git_hooks.rb +184 -0
- data/lib/actual_db_schema/output_formatter.rb +18 -0
- data/lib/actual_db_schema/patches/migration_context.rb +36 -2
- data/lib/actual_db_schema/version.rb +1 -1
- data/lib/actual_db_schema.rb +4 -1
- data/lib/tasks/actual_db_schema.rake +28 -0
- metadata +16 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 43a6d3a4c89ea984fe7b4cbc6160a9f459951bac207d2592b609ae0a7606fd76
|
4
|
+
data.tar.gz: ac2dcb733bb87bdf8dcf5daa250eb386e0ac15f99297d0feb53815e308fb3915
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1bd9b3d3ed5145aacc2d0a481742dc3a45e707257dc3290de245ca4e6ce8db26aaad75619bb0212273aedc6dc30c833e0e604d84c5a1711ea0d1bf8d0cef0dc2
|
7
|
+
data.tar.gz: bbc183547eeb23566c326405519120af7e4e1aea2525ade5c7f8711985d90b6d3d142322bcc19b955a1605f58074420c28657646a16be8c667d47aeae2531ebc
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,9 @@
|
|
1
|
+
## [0.8.0] - 2024-12-30
|
2
|
+
- Enhanced Console Visibility: Automatically rolled-back phantom migrations now provide clearer and more visible logs in the console
|
3
|
+
- Git Hooks for Branch Management: Introduced hooks that automatically rollback phantom migrations after checking out a branch. Additionally, the schema migration rake task can now be executed automatically upon branch checkout
|
4
|
+
- Temporary Folder Cleanup: Rolled-back phantom migrations are now automatically deleted from the temporary folder after rollback
|
5
|
+
- Acronym Support in Phantom Migration Names: Resolved an issue where phantom migrations with acronyms in their names, defined in other branches, couldn't be rolled back automatically. These are now handled seamlessly
|
6
|
+
|
1
7
|
## [0.7.9] - 2024-09-07
|
2
8
|
- Don't stop if a phantom migration rollback fails
|
3
9
|
- Improve failed rollback of phantom migrations report
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
actual_db_schema (0.
|
4
|
+
actual_db_schema (0.8.0)
|
5
5
|
activerecord
|
6
6
|
activesupport
|
7
7
|
csv
|
@@ -93,7 +93,7 @@ GEM
|
|
93
93
|
concurrent-ruby (1.2.2)
|
94
94
|
connection_pool (2.4.1)
|
95
95
|
crass (1.0.6)
|
96
|
-
csv (3.3.
|
96
|
+
csv (3.3.2)
|
97
97
|
date (3.3.3)
|
98
98
|
debug (1.8.0)
|
99
99
|
irb (>= 1.5.0)
|
data/README.md
CHANGED
@@ -110,6 +110,40 @@ Add the following line to your initializer file (`config/initializers/actual_db_
|
|
110
110
|
ActualDbSchema.config[:auto_rollback_disabled] = true
|
111
111
|
```
|
112
112
|
|
113
|
+
## Automatic Phantom Migration Rollback On Branch Switch
|
114
|
+
|
115
|
+
By default, the automatic rollback of migrations on branch switch is disabled. If you prefer to automatically rollback phantom migrations whenever you switch branches with `git checkout`, you can enable it in two ways:
|
116
|
+
|
117
|
+
### 1. Using Environment Variable
|
118
|
+
|
119
|
+
Set the environment variable `ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED` to `true`:
|
120
|
+
|
121
|
+
```sh
|
122
|
+
export ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED=true
|
123
|
+
```
|
124
|
+
|
125
|
+
### 2. Using Initializer
|
126
|
+
Add the following line to your initializer file (`config/initializers/actual_db_schema.rb`):
|
127
|
+
|
128
|
+
```ruby
|
129
|
+
ActualDbSchema.config[:git_hooks_enabled] = true
|
130
|
+
```
|
131
|
+
|
132
|
+
### Installing the Post-Checkout Hook
|
133
|
+
After enabling Git hooks in your configuration, run the rake task to install the post-checkout hook:
|
134
|
+
|
135
|
+
```sh
|
136
|
+
rake actual_db_schema:install_git_hooks
|
137
|
+
```
|
138
|
+
|
139
|
+
This task will prompt you to choose one of the three options:
|
140
|
+
|
141
|
+
1. Rollback phantom migrations with `db:rollback_branches`
|
142
|
+
2. Migrate up to the latest schema with `db:migrate`
|
143
|
+
3. Skip installing git hook
|
144
|
+
|
145
|
+
Based on your selection, a post-checkout hook will be installed or updated in your `.git/hooks` folder.
|
146
|
+
|
113
147
|
## Development
|
114
148
|
|
115
149
|
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.
|
data/actual_db_schema.gemspec
CHANGED
@@ -44,6 +44,18 @@ Gem::Specification.new do |spec|
|
|
44
44
|
spec.add_development_dependency "rails"
|
45
45
|
spec.add_development_dependency "sqlite3"
|
46
46
|
|
47
|
+
spec.post_install_message = <<~MSG
|
48
|
+
Thank you for installing ActualDbSchema!
|
49
|
+
|
50
|
+
To enable automatic rollback of phantom migrations when switching branches, follow these steps:
|
51
|
+
1. Set `export ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED=true` in your environment, OR
|
52
|
+
add `ActualDbSchema.config[:git_hooks_enabled] = true` to your initializer.
|
53
|
+
2. Run `rake actual_db_schema:install_git_hooks` to install the post-checkout Git hook.
|
54
|
+
|
55
|
+
For more information, see the README.
|
56
|
+
|
57
|
+
MSG
|
58
|
+
|
47
59
|
# For more information and examples about making a new gem, check out our
|
48
60
|
# guide at: https://bundler.io/guides/creating_gem.html
|
49
61
|
end
|
@@ -4,11 +4,8 @@ module ActualDbSchema
|
|
4
4
|
module Commands
|
5
5
|
# Rolls back all phantom migrations
|
6
6
|
class Rollback < Base
|
7
|
-
|
8
|
-
|
9
|
-
green: 32,
|
10
|
-
yellow: 33
|
11
|
-
}.freeze
|
7
|
+
include ActualDbSchema::OutputFormatter
|
8
|
+
include ActionView::Helpers::TextHelper
|
12
9
|
|
13
10
|
def initialize(context, manual_mode: false)
|
14
11
|
@manual_mode = manual_mode || manual_mode_default?
|
@@ -18,50 +15,63 @@ module ActualDbSchema
|
|
18
15
|
private
|
19
16
|
|
20
17
|
def call_impl
|
21
|
-
context.rollback_branches(manual_mode: @manual_mode)
|
18
|
+
rolled_back = context.rollback_branches(manual_mode: @manual_mode)
|
22
19
|
|
23
|
-
return
|
20
|
+
return unless rolled_back
|
24
21
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
22
|
+
ActualDbSchema.failed.empty? ? print_success : print_error
|
23
|
+
end
|
24
|
+
|
25
|
+
def print_success
|
26
|
+
puts colorize("[ActualDbSchema] All phantom migrations rolled back successfully! 🎉", :green)
|
27
|
+
end
|
28
|
+
|
29
|
+
def print_error
|
30
|
+
header_message = <<~HEADER
|
31
|
+
#{ActualDbSchema.failed.count} phantom migration(s) could not be rolled back automatically.
|
32
|
+
|
33
|
+
Try these steps to fix and move forward:
|
34
|
+
1. Ensure the migrations are reversible (define #up and #down methods or use #reversible).
|
35
|
+
2. If the migration references code or tables from another branch, restore or remove them.
|
36
|
+
3. Once fixed, run `rails db:migrate` again.
|
37
|
+
|
38
|
+
Below are the details of the problematic migrations:
|
39
|
+
HEADER
|
40
|
+
|
41
|
+
print_error_summary("#{header_message}\n#{failed_migrations_list}")
|
30
42
|
end
|
31
43
|
|
32
44
|
def failed_migrations_list
|
33
45
|
ActualDbSchema.failed.map.with_index(1) do |failed, index|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
\t\t#{exception.inspect.gsub("\n", "\n\t ")}
|
41
|
-
MSG
|
42
|
-
end
|
46
|
+
<<~MIGRATION
|
47
|
+
#{colorize("Migration ##{index}:", :yellow)}
|
48
|
+
File: #{failed.short_filename}
|
49
|
+
Branch: #{failed.branch}
|
50
|
+
MIGRATION
|
51
|
+
end.join("\n")
|
43
52
|
end
|
44
53
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
48
|
-
|
54
|
+
def print_error_summary(content)
|
55
|
+
width = 100
|
56
|
+
indent = 4
|
57
|
+
gem_name = "ActualDbSchema"
|
58
|
+
|
59
|
+
puts colorize("╔═ [#{gem_name}] #{"═" * (width - gem_name.length - 5)}╗", :red)
|
60
|
+
print_wrapped_content(content, width, indent)
|
61
|
+
puts colorize("╚#{"═" * width}╝", :red)
|
49
62
|
end
|
50
63
|
|
51
|
-
def
|
52
|
-
|
53
|
-
|
54
|
-
|
64
|
+
def print_wrapped_content(content, width, indent)
|
65
|
+
usable_width = width - indent - 4
|
66
|
+
wrapped_content = word_wrap(content, line_width: usable_width)
|
67
|
+
wrapped_content.each_line do |line|
|
68
|
+
puts "#{" " * indent}#{line.chomp}"
|
69
|
+
end
|
55
70
|
end
|
56
71
|
|
57
72
|
def manual_mode_default?
|
58
73
|
ActualDbSchema.config[:auto_rollback_disabled]
|
59
74
|
end
|
60
|
-
|
61
|
-
def colorize(text, color)
|
62
|
-
code = UNICODE_COLORS.fetch(color, 37)
|
63
|
-
"\e[#{code}m#{text}\e[0m"
|
64
|
-
end
|
65
75
|
end
|
66
76
|
end
|
67
77
|
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
module ActualDbSchema
|
6
|
+
# Handles the installation of a git post-checkout hook that rolls back phantom migrations when switching branches
|
7
|
+
class GitHooks # rubocop:disable Metrics/ClassLength
|
8
|
+
include ActualDbSchema::OutputFormatter
|
9
|
+
|
10
|
+
POST_CHECKOUT_MARKER_START = "# >>> BEGIN ACTUAL_DB_SCHEMA"
|
11
|
+
POST_CHECKOUT_MARKER_END = "# <<< END ACTUAL_DB_SCHEMA"
|
12
|
+
|
13
|
+
POST_CHECKOUT_HOOK_ROLLBACK = <<~BASH
|
14
|
+
#{POST_CHECKOUT_MARKER_START}
|
15
|
+
# ActualDbSchema post-checkout hook (ROLLBACK)
|
16
|
+
# Runs db:rollback_branches on branch checkout.
|
17
|
+
|
18
|
+
if [ -f ./bin/rails ]; then
|
19
|
+
if [ -n "$ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED" ]; then
|
20
|
+
GIT_HOOKS_ENABLED="$ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED"
|
21
|
+
else
|
22
|
+
GIT_HOOKS_ENABLED=$(./bin/rails runner "puts ActualDbSchema.config[:git_hooks_enabled]" 2>/dev/null)
|
23
|
+
fi
|
24
|
+
|
25
|
+
if [ "$GIT_HOOKS_ENABLED" == "true" ]; then
|
26
|
+
./bin/rails db:rollback_branches
|
27
|
+
fi
|
28
|
+
fi
|
29
|
+
#{POST_CHECKOUT_MARKER_END}
|
30
|
+
BASH
|
31
|
+
|
32
|
+
POST_CHECKOUT_HOOK_MIGRATE = <<~BASH
|
33
|
+
#{POST_CHECKOUT_MARKER_START}
|
34
|
+
# ActualDbSchema post-checkout hook (MIGRATE)
|
35
|
+
# Runs db:migrate on branch checkout.
|
36
|
+
|
37
|
+
if [ -f ./bin/rails ]; then
|
38
|
+
if [ -n "$ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED" ]; then
|
39
|
+
GIT_HOOKS_ENABLED="$ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED"
|
40
|
+
else
|
41
|
+
GIT_HOOKS_ENABLED=$(./bin/rails runner "puts ActualDbSchema.config[:git_hooks_enabled]" 2>/dev/null)
|
42
|
+
fi
|
43
|
+
|
44
|
+
if [ "$GIT_HOOKS_ENABLED" == "true" ]; then
|
45
|
+
./bin/rails db:migrate
|
46
|
+
fi
|
47
|
+
fi
|
48
|
+
#{POST_CHECKOUT_MARKER_END}
|
49
|
+
BASH
|
50
|
+
|
51
|
+
def initialize(strategy: :rollback)
|
52
|
+
@strategy = strategy
|
53
|
+
end
|
54
|
+
|
55
|
+
def install_post_checkout_hook
|
56
|
+
return unless git_hooks_enabled?
|
57
|
+
return unless hooks_directory_present?
|
58
|
+
|
59
|
+
if File.exist?(hook_path)
|
60
|
+
handle_existing_hook
|
61
|
+
else
|
62
|
+
create_new_hook
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def hook_code
|
69
|
+
@strategy == :migrate ? POST_CHECKOUT_HOOK_MIGRATE : POST_CHECKOUT_HOOK_ROLLBACK
|
70
|
+
end
|
71
|
+
|
72
|
+
def hooks_dir
|
73
|
+
@hooks_dir ||= Rails.root.join(".git", "hooks")
|
74
|
+
end
|
75
|
+
|
76
|
+
def hook_path
|
77
|
+
@hook_path ||= hooks_dir.join("post-checkout")
|
78
|
+
end
|
79
|
+
|
80
|
+
def git_hooks_enabled?
|
81
|
+
return true if ActualDbSchema.config[:git_hooks_enabled]
|
82
|
+
|
83
|
+
puts colorize("[ActualDbSchema] Git hooks are disabled in configuration. Skipping installation.", :gray)
|
84
|
+
end
|
85
|
+
|
86
|
+
def hooks_directory_present?
|
87
|
+
return true if Dir.exist?(hooks_dir)
|
88
|
+
|
89
|
+
puts colorize("[ActualDbSchema] .git/hooks directory not found. Please ensure this is a Git repository.", :gray)
|
90
|
+
end
|
91
|
+
|
92
|
+
def handle_existing_hook
|
93
|
+
return update_hook if markers_exist?
|
94
|
+
return install_hook if safe_install?
|
95
|
+
|
96
|
+
show_manual_install_instructions
|
97
|
+
end
|
98
|
+
|
99
|
+
def create_new_hook
|
100
|
+
contents = <<~BASH
|
101
|
+
#!/usr/bin/env bash
|
102
|
+
|
103
|
+
#{hook_code}
|
104
|
+
BASH
|
105
|
+
|
106
|
+
write_hook_file(contents)
|
107
|
+
print_success
|
108
|
+
end
|
109
|
+
|
110
|
+
def markers_exist?
|
111
|
+
contents = File.read(hook_path)
|
112
|
+
contents.include?(POST_CHECKOUT_MARKER_START) && contents.include?(POST_CHECKOUT_MARKER_END)
|
113
|
+
end
|
114
|
+
|
115
|
+
def update_hook
|
116
|
+
contents = File.read(hook_path)
|
117
|
+
new_contents = replace_marker_contents(contents)
|
118
|
+
|
119
|
+
if new_contents == contents
|
120
|
+
message = "[ActualDbSchema] post-checkout git hook already contains the necessary code. Nothing to update."
|
121
|
+
puts colorize(message, :gray)
|
122
|
+
else
|
123
|
+
write_hook_file(new_contents)
|
124
|
+
puts colorize("[ActualDbSchema] post-checkout git hook updated successfully at #{hook_path}", :green)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
def replace_marker_contents(contents)
|
129
|
+
contents.gsub(
|
130
|
+
/#{Regexp.quote(POST_CHECKOUT_MARKER_START)}.*#{Regexp.quote(POST_CHECKOUT_MARKER_END)}/m,
|
131
|
+
hook_code.strip
|
132
|
+
)
|
133
|
+
end
|
134
|
+
|
135
|
+
def safe_install?
|
136
|
+
puts colorize("[ActualDbSchema] A post-checkout hook already exists at #{hook_path}.", :gray)
|
137
|
+
puts "Overwrite the existing hook at #{hook_path}? [y,n] "
|
138
|
+
|
139
|
+
answer = $stdin.gets.chomp.downcase
|
140
|
+
answer.start_with?("y")
|
141
|
+
end
|
142
|
+
|
143
|
+
def install_hook
|
144
|
+
contents = File.read(hook_path)
|
145
|
+
new_contents = <<~BASH
|
146
|
+
#{contents.rstrip}
|
147
|
+
|
148
|
+
#{hook_code}
|
149
|
+
BASH
|
150
|
+
|
151
|
+
write_hook_file(new_contents)
|
152
|
+
print_success
|
153
|
+
end
|
154
|
+
|
155
|
+
def show_manual_install_instructions
|
156
|
+
puts colorize("[ActualDbSchema] You can follow these steps to manually install the hook:", :yellow)
|
157
|
+
puts <<~MSG
|
158
|
+
|
159
|
+
1. Open the existing post-checkout hook at:
|
160
|
+
#{hook_path}
|
161
|
+
|
162
|
+
2. Insert the following lines into that file (preferably at the end or in a relevant section).
|
163
|
+
Make sure you include the #{POST_CHECKOUT_MARKER_START} and #{POST_CHECKOUT_MARKER_END} lines:
|
164
|
+
|
165
|
+
#{hook_code}
|
166
|
+
|
167
|
+
3. Ensure the post-checkout file is executable:
|
168
|
+
chmod +x #{hook_path}
|
169
|
+
|
170
|
+
4. Done! Now when you switch branches, phantom migrations will be rolled back automatically (if enabled).
|
171
|
+
|
172
|
+
MSG
|
173
|
+
end
|
174
|
+
|
175
|
+
def write_hook_file(contents)
|
176
|
+
File.open(hook_path, "w") { |file| file.write(contents) }
|
177
|
+
FileUtils.chmod("+x", hook_path)
|
178
|
+
end
|
179
|
+
|
180
|
+
def print_success
|
181
|
+
puts colorize("[ActualDbSchema] post-checkout git hook installed successfully at #{hook_path}", :green)
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActualDbSchema
|
4
|
+
# Provides functionality for formatting terminal output with colors
|
5
|
+
module OutputFormatter
|
6
|
+
UNICODE_COLORS = {
|
7
|
+
red: 31,
|
8
|
+
green: 32,
|
9
|
+
yellow: 33,
|
10
|
+
gray: 90
|
11
|
+
}.freeze
|
12
|
+
|
13
|
+
def colorize(text, color)
|
14
|
+
code = UNICODE_COLORS.fetch(color, 37)
|
15
|
+
"\e[#{code}m#{text}\e[0m"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -4,15 +4,22 @@ module ActualDbSchema
|
|
4
4
|
module Patches
|
5
5
|
# Add new command to roll back the phantom migrations
|
6
6
|
module MigrationContext
|
7
|
+
include ActualDbSchema::OutputFormatter
|
8
|
+
|
7
9
|
def rollback_branches(manual_mode: false)
|
10
|
+
rolled_back = false
|
11
|
+
|
8
12
|
phantom_migrations.reverse_each do |migration|
|
9
13
|
next unless status_up?(migration)
|
10
14
|
|
15
|
+
rolled_back = true
|
11
16
|
show_info_for(migration) if manual_mode
|
12
17
|
migrate(migration) if !manual_mode || user_wants_rollback?
|
13
18
|
rescue StandardError => e
|
14
|
-
|
19
|
+
handle_rollback_error(migration, e)
|
15
20
|
end
|
21
|
+
|
22
|
+
rolled_back
|
16
23
|
end
|
17
24
|
|
18
25
|
def phantom_migrations
|
@@ -63,7 +70,7 @@ module ActualDbSchema
|
|
63
70
|
end
|
64
71
|
|
65
72
|
def show_info_for(migration)
|
66
|
-
puts "\n[ActualDbSchema] A phantom migration was found and is about to be rolled back."
|
73
|
+
puts colorize("\n[ActualDbSchema] A phantom migration was found and is about to be rolled back.", :gray)
|
67
74
|
puts "Please make a decision from the options below to proceed.\n\n"
|
68
75
|
puts "Branch: #{branch_for(migration.version.to_s)}"
|
69
76
|
puts "Database: #{ActualDbSchema.db_config[:database]}"
|
@@ -72,9 +79,21 @@ module ActualDbSchema
|
|
72
79
|
end
|
73
80
|
|
74
81
|
def migrate(migration)
|
82
|
+
migration.name = extract_class_name(migration.filename)
|
83
|
+
|
84
|
+
message = "[ActualDbSchema] Rolling back phantom migration #{migration.version} #{migration.name} " \
|
85
|
+
"(from branch: #{branch_for(migration.version.to_s)})"
|
86
|
+
puts colorize(message, :gray)
|
87
|
+
|
75
88
|
migrator = down_migrator_for(migration)
|
76
89
|
migrator.extend(ActualDbSchema::Patches::Migrator)
|
77
90
|
migrator.migrate
|
91
|
+
File.delete(migration.filename)
|
92
|
+
end
|
93
|
+
|
94
|
+
def extract_class_name(filename)
|
95
|
+
content = File.read(filename)
|
96
|
+
content.match(/^class\s+([A-Za-z0-9_]+)\s+</)[1]
|
78
97
|
end
|
79
98
|
|
80
99
|
def branch_for(version)
|
@@ -84,6 +103,21 @@ module ActualDbSchema
|
|
84
103
|
def metadata
|
85
104
|
@metadata ||= ActualDbSchema::Store.instance.read
|
86
105
|
end
|
106
|
+
|
107
|
+
def handle_rollback_error(migration, exception)
|
108
|
+
error_message = <<~ERROR
|
109
|
+
Error encountered during rollback:
|
110
|
+
|
111
|
+
#{exception.message.gsub(/^An error has occurred, all later migrations canceled:\s*/, "").strip}
|
112
|
+
ERROR
|
113
|
+
|
114
|
+
puts colorize(error_message, :red)
|
115
|
+
ActualDbSchema.failed << FailedMigration.new(
|
116
|
+
migration: migration,
|
117
|
+
exception: exception,
|
118
|
+
branch: branch_for(migration.version.to_s)
|
119
|
+
)
|
120
|
+
end
|
87
121
|
end
|
88
122
|
end
|
89
123
|
end
|
data/lib/actual_db_schema.rb
CHANGED
@@ -9,9 +9,11 @@ require_relative "actual_db_schema/version"
|
|
9
9
|
require_relative "actual_db_schema/migration"
|
10
10
|
require_relative "actual_db_schema/failed_migration"
|
11
11
|
require_relative "actual_db_schema/migration_context"
|
12
|
+
require_relative "actual_db_schema/output_formatter"
|
12
13
|
require_relative "actual_db_schema/patches/migration_proxy"
|
13
14
|
require_relative "actual_db_schema/patches/migrator"
|
14
15
|
require_relative "actual_db_schema/patches/migration_context"
|
16
|
+
require_relative "actual_db_schema/git_hooks"
|
15
17
|
|
16
18
|
require_relative "actual_db_schema/commands/base"
|
17
19
|
require_relative "actual_db_schema/commands/rollback"
|
@@ -29,7 +31,8 @@ module ActualDbSchema
|
|
29
31
|
self.config = {
|
30
32
|
enabled: Rails.env.development?,
|
31
33
|
auto_rollback_disabled: ENV["ACTUAL_DB_SCHEMA_AUTO_ROLLBACK_DISABLED"].present?,
|
32
|
-
ui_enabled: Rails.env.development? || ENV["ACTUAL_DB_SCHEMA_UI_ENABLED"].present
|
34
|
+
ui_enabled: Rails.env.development? || ENV["ACTUAL_DB_SCHEMA_UI_ENABLED"].present?,
|
35
|
+
git_hooks_enabled: ENV["ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED"].present?
|
33
36
|
}
|
34
37
|
|
35
38
|
def self.migrated_folder
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :actual_db_schema do
|
4
|
+
desc "Install ActualDbSchema post-checkout git hook that rolls back phantom migrations when switching branches."
|
5
|
+
task :install_git_hooks do
|
6
|
+
extend ActualDbSchema::OutputFormatter
|
7
|
+
|
8
|
+
puts "Which Git hook strategy would you like to install? [1, 2, 3]"
|
9
|
+
puts " 1) Rollback phantom migrations (db:rollback_branches)"
|
10
|
+
puts " 2) Migrate up to latest (db:migrate)"
|
11
|
+
puts " 3) No hook installation (skip)"
|
12
|
+
answer = $stdin.gets.chomp
|
13
|
+
|
14
|
+
strategy =
|
15
|
+
case answer
|
16
|
+
when "1" then :rollback
|
17
|
+
when "2" then :migrate
|
18
|
+
else
|
19
|
+
:none
|
20
|
+
end
|
21
|
+
|
22
|
+
if strategy == :none
|
23
|
+
puts colorize("[ActualDbSchema] Skipping git hook installation.", :gray)
|
24
|
+
else
|
25
|
+
ActualDbSchema::GitHooks.new(strategy: strategy).install_post_checkout_hook
|
26
|
+
end
|
27
|
+
end
|
28
|
+
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.8.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: 2024-
|
11
|
+
date: 2024-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -148,13 +148,16 @@ files:
|
|
148
148
|
- lib/actual_db_schema/engine.rb
|
149
149
|
- lib/actual_db_schema/failed_migration.rb
|
150
150
|
- lib/actual_db_schema/git.rb
|
151
|
+
- lib/actual_db_schema/git_hooks.rb
|
151
152
|
- lib/actual_db_schema/migration.rb
|
152
153
|
- lib/actual_db_schema/migration_context.rb
|
154
|
+
- lib/actual_db_schema/output_formatter.rb
|
153
155
|
- lib/actual_db_schema/patches/migration_context.rb
|
154
156
|
- lib/actual_db_schema/patches/migration_proxy.rb
|
155
157
|
- lib/actual_db_schema/patches/migrator.rb
|
156
158
|
- lib/actual_db_schema/store.rb
|
157
159
|
- lib/actual_db_schema/version.rb
|
160
|
+
- lib/tasks/actual_db_schema.rake
|
158
161
|
- lib/tasks/db.rake
|
159
162
|
- sig/actual_db_schema.rbs
|
160
163
|
homepage: https://blog.widefix.com/actual-db-schema/
|
@@ -164,7 +167,16 @@ metadata:
|
|
164
167
|
homepage_uri: https://blog.widefix.com/actual-db-schema/
|
165
168
|
source_code_uri: https://github.com/widefix/actual_db_schema
|
166
169
|
changelog_uri: https://blog.widefix.com/actual-db-schema//blob/main/CHANGELOG.md
|
167
|
-
post_install_message:
|
170
|
+
post_install_message: |+
|
171
|
+
Thank you for installing ActualDbSchema!
|
172
|
+
|
173
|
+
To enable automatic rollback of phantom migrations when switching branches, follow these steps:
|
174
|
+
1. Set `export ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED=true` in your environment, OR
|
175
|
+
add `ActualDbSchema.config[:git_hooks_enabled] = true` to your initializer.
|
176
|
+
2. Run `rake actual_db_schema:install_git_hooks` to install the post-checkout Git hook.
|
177
|
+
|
178
|
+
For more information, see the README.
|
179
|
+
|
168
180
|
rdoc_options: []
|
169
181
|
require_paths:
|
170
182
|
- lib
|
@@ -184,3 +196,4 @@ signing_key:
|
|
184
196
|
specification_version: 4
|
185
197
|
summary: Keep your DB and schema.rb consistent in dev branches.
|
186
198
|
test_files: []
|
199
|
+
...
|