Skip to content

Commit

Permalink
multiple db support
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed Apr 30, 2024
1 parent 83a0b54 commit ab412a2
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 22 deletions.
4 changes: 0 additions & 4 deletions lib/actual_db_schema/commands/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/actual_db_schema/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
24 changes: 19 additions & 5 deletions lib/actual_db_schema/store.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
14 changes: 14 additions & 0 deletions test/dummy_app/db/secondary_schema.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 18 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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

Expand Down

0 comments on commit ab412a2

Please sign in to comment.