Skip to content

Commit 6fec777

Browse files
committed
Fix issue with rails 7 when fetching database name
1 parent 5aae7f1 commit 6fec777

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

lib/lazy_migrate/client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def run
2323
catch(:done) do
2424
on_done = -> { throw :done }
2525

26-
prompt.ok("\nDatabase: #{ActiveRecord::Base.connection_config[:database]}\n")
26+
prompt.ok("\nDatabase: #{database_name}\n")
2727

2828
select_migration_prompt(on_done: on_done, migrator_adapter: migrator_adapter)
2929
end
@@ -34,6 +34,16 @@ def run
3434

3535
private
3636

37+
def database_name
38+
if ActiveRecord::Base.respond_to?(:connection_db_config)
39+
# Rails 7
40+
ActiveRecord::Base.connection_db_config.database
41+
else
42+
# Rails 6-
43+
ActiveRecord::Base.connection_config[:database]
44+
end
45+
end
46+
3747
def prompt
3848
TTY::Prompt.new(active_color: :bright_green)
3949
end

spec/lazy_migrate/client_spec.rb

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# typed: false
2+
# frozen_string_literal: true
3+
4+
require 'rails_helper'
5+
require 'lazy_migrate/migration'
6+
7+
RSpec.describe LazyMigrate::Client do
8+
let(:rails_root) { Rails.root }
9+
10+
let(:create_books_migration_status) {
11+
LazyMigrate::Migration.new(status: "up", version: 20200804231712, name: "Create books", has_file: true, current: false)
12+
}
13+
14+
let(:add_author_migration_status_status) { 'up' }
15+
let(:add_author_migration_status) {
16+
LazyMigrate::Migration.new(status: add_author_migration_status_status, version: 20200804234040, name: "Add book author", has_file: true, current: false)
17+
}
18+
19+
let(:add_page_count_migration_status) {
20+
LazyMigrate::Migration.new(status: "down", version: 20200804234057, name: "Add book page count", has_file: true, current: false)
21+
}
22+
23+
let(:add_rating_migration_status) {
24+
LazyMigrate::Migration.new(status: "up", version: 20200804234111, name: "Add book rating", has_file: true, current: true)
25+
}
26+
27+
let(:migrations) {
28+
[
29+
add_rating_migration_status,
30+
add_page_count_migration_status,
31+
add_author_migration_status,
32+
create_books_migration_status,
33+
]
34+
}
35+
36+
let(:migrator_adapter) { LazyMigrate::MigratorAdapterFactory.create_migrator_adapter }
37+
38+
let(:new_version) { 30900804234040 }
39+
40+
def find_support_folder
41+
File.join(File.dirname(File.dirname(__FILE__)), 'support')
42+
end
43+
44+
before do
45+
# prepare the db directory
46+
support_folder = find_support_folder
47+
db_dir = ActiveRecord::Tasks::DatabaseTasks.db_dir
48+
49+
schema_filename = File.join(db_dir, 'schema.rb')
50+
FileUtils.cp(File.join(support_folder, 'mock_schema.rb'), schema_filename)
51+
52+
migrate_dir = File.join(db_dir, 'migrate')
53+
FileUtils.rm_rf(migrate_dir)
54+
FileUtils.cp_r(File.join(support_folder, 'mock_migrations/default/.'), migrate_dir)
55+
56+
ActiveRecord::Migration.drop_table(:books) if ActiveRecord::Base.connection.table_exists?(:books)
57+
ActiveRecord::Migration.create_table "books", force: :cascade do |t|
58+
t.string "name"
59+
t.datetime "created_at", null: false
60+
t.datetime "updated_at", null: false
61+
end
62+
63+
ActiveRecord::SchemaMigration.delete_all
64+
65+
migrations.sort { |m| m.version }.each do |migration|
66+
if migration.status == 'up'
67+
ActiveRecord::SchemaMigration.create(version: migration.version)
68+
end
69+
end
70+
end
71+
72+
after do
73+
db_dir = ActiveRecord::Tasks::DatabaseTasks.db_dir
74+
75+
FileUtils.rm(File.join(db_dir, 'schema.rb'))
76+
FileUtils.rm_rf(File.join(db_dir, 'migrate'))
77+
end
78+
79+
describe '.database_name' do
80+
it "returns without error" do
81+
expect { described_class.send(:database_name) }.not_to raise_error
82+
end
83+
end
84+
end

0 commit comments

Comments
 (0)