From ab412a2445cc09f7e17bd78a8ea7f40184c62a2a Mon Sep 17 00:00:00 2001 From: Andrei Kaleshka Date: Tue, 30 Apr 2024 18:22:02 +0200 Subject: [PATCH] multiple db support --- lib/actual_db_schema/commands/base.rb | 4 ---- lib/actual_db_schema/commands/list.rb | 6 ++++++ lib/actual_db_schema/store.rb | 24 +++++++++++++++++++----- test/dummy_app/db/secondary_schema.rb | 14 ++++++++++++++ test/rake_task_test.rb | 8 ++++---- test/test_helper.rb | 27 ++++++++++++++++++--------- 6 files changed, 61 insertions(+), 22 deletions(-) create mode 100644 test/dummy_app/db/secondary_schema.rb diff --git a/lib/actual_db_schema/commands/base.rb b/lib/actual_db_schema/commands/base.rb index 016d06e..bd43026 100644 --- a/lib/actual_db_schema/commands/base.rb +++ b/lib/actual_db_schema/commands/base.rb @@ -9,10 +9,6 @@ def call raise "ActualDbSchema is disabled. Set ActualDbSchema.config[:enabled] = true to enable it." end - if ActiveRecord::Migration.current_version >= 6 - ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches") - end - call_impl end diff --git a/lib/actual_db_schema/commands/list.rb b/lib/actual_db_schema/commands/list.rb index 482ccbb..900da17 100644 --- a/lib/actual_db_schema/commands/list.rb +++ b/lib/actual_db_schema/commands/list.rb @@ -34,6 +34,7 @@ def header "Status".center(8), "Migration ID".ljust(14), "Branch".ljust(branch_column_width), + "Configuration".ljust(16), "Migration File".ljust(16) ] end @@ -53,6 +54,7 @@ def line_for(status, version) status.center(8), version.to_s.ljust(14), branch_for(version).ljust(branch_column_width), + configuration_for(version).ljust(16), migration.filename.gsub("#{Rails.root}/", "") ].join(" ") end @@ -61,6 +63,10 @@ def branch_for(version) metadata.fetch(version, {})[:branch] || "unknown" end + def configuration_for(version) + metadata.fetch(version, {})[:configuration] || "default" + end + def metadata @metadata ||= ActualDbSchema::Store.instance.read end diff --git a/lib/actual_db_schema/store.rb b/lib/actual_db_schema/store.rb index 3cacb89..bd86777 100644 --- a/lib/actual_db_schema/store.rb +++ b/lib/actual_db_schema/store.rb @@ -5,12 +5,15 @@ module ActualDbSchema class Store include Singleton - Item = Struct.new(:version, :timestamp, :branch) + Item = Struct.new(:version, :timestamp, :branch, :configuration) def write(filename) basename = File.basename(filename) - FileUtils.copy(filename, folder.join(basename)) - record_metadata(filename) + configuration = db_config_from_filename(filename) + destination = folder.join(*[configuration, basename].compact) + + FileUtils.copy(filename, destination) + record_metadata(filename, configuration) end def read @@ -21,13 +24,14 @@ def read private - def record_metadata(filename) + def record_metadata(filename, configuration) version = File.basename(filename).scan(/(\d+)_.*\.rb/).first.first CSV.open(store_file, "a") do |csv| csv << [ version, Time.current.iso8601, - Git.current_branch + Git.current_branch, + configuration ] end end @@ -39,5 +43,15 @@ def folder def store_file folder.join("metadata.csv") end + + def db_config_from_filename(filename) + subfolder = filename.split("/")[-3..-2] + + if subfolder == %w[db migrate] + nil # default configuration + else + subfolder.last + end + end end end diff --git a/test/dummy_app/db/secondary_schema.rb b/test/dummy_app/db/secondary_schema.rb new file mode 100644 index 0000000..f23614c --- /dev/null +++ b/test/dummy_app/db/secondary_schema.rb @@ -0,0 +1,14 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema[7.1].define(version: 2013_09_06_111513) do +end diff --git a/test/rake_task_test.rb b/test/rake_task_test.rb index ced50fb..283a468 100644 --- a/test/rake_task_test.rb +++ b/test/rake_task_test.rb @@ -146,10 +146,10 @@ def run_task ActualDbSchema::Git.stub(:current_branch, "fix-bug") do prepare_phantom_migrations run_task - assert_match(/ Status Migration ID Branch Migration File/, TestingState.output) - assert_match(/---------------------------------------------------/, TestingState.output) - assert_match(%r{ up 20130906111511 fix-bug tmp/migrated/20130906111511_first.rb}, TestingState.output) - assert_match(%r{ up 20130906111512 fix-bug tmp/migrated/20130906111512_second.rb}, TestingState.output) + assert_match(/ Status Migration ID Branch Configuration Migration File/, TestingState.output) + assert_match(/---------------------------------------------------------------------/, TestingState.output) + assert_match(%r{ up 20130906111511 fix-bug default tmp/migrated/20130906111511_first.rb}, TestingState.output) + assert_match(%r{ up 20130906111512 fix-bug default tmp/migrated/20130906111512_second.rb}, TestingState.output) end end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 06f2a87..e7e551c 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,20 +17,24 @@ def initialize Rails.application = FakeApplication.new -db_config = { - adapter: "sqlite3", - database: "tmp/test.sqlite3" -} -ActiveRecord::Tasks::DatabaseTasks.database_configuration = { test: db_config } -ActiveRecord::Base.establish_connection(**db_config) - -ActualDbSchema.config[:enabled] = true - class TestingState class << self attr_accessor :up, :down, :output end + def self.db_config + { + "primary" => { + adapter: "sqlite3", + database: "tmp/primary.sqlite3" + }, + "secondary" => { + adapter: "sqlite3", + database: "tmp/secondary.sqlite3" + } + } + end + def self.reset self.up = [] self.down = [] @@ -40,6 +44,11 @@ def self.reset reset end +ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config } +ActiveRecord::Base.establish_connection(TestingState.db_config["primary"]) + +ActualDbSchema.config[:enabled] = true + module Kernel alias original_puts puts