Skip to content

Commit b1d62b8

Browse files
authored
Configuration for tmp folder (#136)
1 parent 0ed25aa commit b1d62b8

File tree

10 files changed

+76
-10
lines changed

10 files changed

+76
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
/spec/reports/
88
/tmp/
99
/test/dummy_app/tmp/
10+
/test/dummy_app/custom/
1011
/test/dummy_app/db/**/*.rb
1112
.ruby-version
1213
.ruby-gemset

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ The gem offers the following rake tasks that can be manually run according to yo
7272
- `rails db:rollback_branches:manual` - run it to manually rolls back phantom migrations one by one.
7373
- `rails db:phantom_migrations` - displays a list of phantom migrations.
7474

75+
## Migrated Folder Configuration
76+
77+
By default, `actual_db_schema` stores all run migrations in the `tmp/migrated` folder. However, if you want to change this location, you can configure it in two ways:
78+
79+
### 1. Using Environment Variable
80+
81+
Set the environment variable `ACTUAL_DB_SCHEMA_MIGRATED_FOLDER` to your desired folder path:
82+
83+
```sh
84+
export ACTUAL_DB_SCHEMA_MIGRATED_FOLDER="custom/migrated"
85+
```
86+
87+
### 2. Using Initializer
88+
Add the following line to your initializer file (`config/initializers/actual_db_schema.rb`):
89+
90+
```ruby
91+
config.migrated_folder = Rails.root.join("custom", "migrated")
92+
```
93+
7594
## Accessing the UI
7695

7796
The UI for managing migrations is enabled automatically. To access the UI, simply navigate to the following URL in your web browser:

lib/actual_db_schema.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def self.migrated_folders
5959
end
6060

6161
def self.default_migrated_folder
62-
Rails.root.join("tmp", "migrated")
62+
config[:migrated_folder] || Rails.root.join("tmp", "migrated")
6363
end
6464

6565
def self.migrations_paths

lib/actual_db_schema/configuration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module ActualDbSchema
44
# Manages the configuration settings for the gem.
55
class Configuration
66
attr_accessor :enabled, :auto_rollback_disabled, :ui_enabled, :git_hooks_enabled, :multi_tenant_schemas,
7-
:console_migrations_enabled
7+
:console_migrations_enabled, :migrated_folder
88

99
def initialize
1010
@enabled = Rails.env.development?
@@ -13,6 +13,7 @@ def initialize
1313
@git_hooks_enabled = ENV["ACTUAL_DB_SCHEMA_GIT_HOOKS_ENABLED"].present?
1414
@multi_tenant_schemas = nil
1515
@console_migrations_enabled = ENV["ACTUAL_DB_SCHEMA_CONSOLE_MIGRATIONS_ENABLED"].present?
16+
@migrated_folder = ENV["ACTUAL_DB_SCHEMA_MIGRATED_FOLDER"].present?
1617
end
1718

1819
def [](key)

lib/actual_db_schema/migration.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ def sort_migrations_desc(migrations)
101101
end
102102

103103
def phantom?(migration)
104-
migration.filename.include?("/tmp/migrated")
104+
migrated_folder = ActualDbSchema.config[:migrated_folder].presence || "/tmp/migrated"
105+
migration.filename.include?(migrated_folder.to_s)
105106
end
106107

107108
def should_include?(status, migration)

lib/actual_db_schema/schema_diff.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,25 @@ def migration_changes
6363
end
6464

6565
def migrated_folders
66+
dirs = find_migrated_folders
67+
68+
if (configured_migrated_folder = ActualDbSchema.config[:migrated_folder].presence)
69+
relative_migrated_folder = configured_migrated_folder.to_s.sub(%r{\A#{Regexp.escape(Rails.root.to_s)}/?}, "")
70+
dirs << relative_migrated_folder unless dirs.include?(relative_migrated_folder)
71+
end
72+
73+
dirs.map { |dir| dir.sub(%r{\A\./}, "") }.uniq
74+
end
75+
76+
def find_migrated_folders
6677
path_parts = Pathname.new(@migrations_path).each_filename.to_a
6778
db_index = path_parts.index("db")
68-
6979
return [] unless db_index
7080

7181
base_path = db_index.zero? ? "." : File.join(*path_parts[0...db_index])
72-
dirs = Dir[File.join(base_path, "tmp", "migrated*")].select do |path|
82+
Dir[File.join(base_path, "tmp", "migrated*")].select do |path|
7383
File.directory?(path) && File.basename(path).match?(/^migrated(_[a-zA-Z0-9_-]+)?$/)
7484
end
75-
76-
dirs.map { |dir| dir.sub(%r{\A\./}, "") }
7785
end
7886

7987
def generate_diff(old_content, new_content)

lib/generators/actual_db_schema/templates/actual_db_schema.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@
2121
# If your application leverages multiple schemas for multi-tenancy, define the active schemas.
2222
# config.multi_tenant_schemas = -> { ["public", "tenant1", "tenant2"] }
2323

24-
# Enable console migrations
24+
# Enable console migrations.
2525
# config.console_migrations_enabled = true
2626
config.console_migrations_enabled = ENV["ACTUAL_DB_SCHEMA_CONSOLE_MIGRATIONS_ENABLED"].present?
27+
28+
# Define the migrated folder location.
29+
# config.migrated_folder = Rails.root.join("custom", "migrated")
30+
config.migrated_folder = Rails.root.join("tmp", "migrated")
2731
end

lib/tasks/actual_db_schema.rake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ namespace :actual_db_schema do # rubocop:disable Metrics/BlockLength
5555
end
5656

5757
desc "Show the schema.rb diff annotated with the migrations that made the changes"
58-
task :diff_schema_with_migrations, [:schema_path, :migrations_path] do |_, args|
58+
task :diff_schema_with_migrations, %i[schema_path migrations_path] => :environment do |_, args|
5959
schema_path = args[:schema_path] || "db/schema.rb"
6060
migrations_path = args[:migrations_path] || "db/migrate"
6161

test/rake_task_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,37 @@ def down
122122
assert_empty utils.migrated_files
123123
end
124124
end
125+
126+
describe "with custom migrated folder" do
127+
before do
128+
ActualDbSchema.configure { |config| config.migrated_folder = Rails.root.join("custom", "migrated") }
129+
end
130+
131+
after do
132+
utils.remove_app_dir("custom/migrated")
133+
ActualDbSchema.configure { |config| config.migrated_folder = nil }
134+
end
135+
136+
it "creates the custom migrated folder" do
137+
refute File.exist?(utils.app_file("custom/migrated"))
138+
utils.run_migrations
139+
assert File.exist?(utils.app_file("custom/migrated"))
140+
end
141+
142+
it "keeps migrated migrations in the custom migrated folder" do
143+
utils.run_migrations
144+
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files
145+
end
146+
147+
it "rolls back the migrations in the reversed order" do
148+
utils.prepare_phantom_migrations
149+
assert_empty TestingState.down
150+
utils.run_migrations
151+
assert_equal %i[second first], TestingState.down
152+
assert_match(/\[ActualDbSchema\] Rolling back phantom migration/, TestingState.output)
153+
assert_empty utils.migrated_files
154+
end
155+
end
125156
end
126157

127158
describe "db:rollback_branches:manual" do

test/support/test_utils.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ def delete_migrations_files_for(path)
207207
end
208208

209209
def migrated_files_call(prefix_name = nil)
210-
path = MIGRATED_PATHS.fetch(prefix_name&.to_sym, migrated_paths.first)
210+
migrated_path = ActualDbSchema.config[:migrated_folder].presence || migrated_paths.first
211+
path = MIGRATED_PATHS.fetch(prefix_name&.to_sym, migrated_path.to_s)
211212
Dir.glob(app_file("#{path}/*.rb")).map { |f| File.basename(f) }.sort
212213
end
213214

0 commit comments

Comments
 (0)