Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiple db support #53

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ GEM
marcel (1.0.2)
mini_mime (1.1.5)
minitest (5.20.0)
minitest-around (0.5.0)
minitest (~> 5.0)
mutex_m (0.1.2)
net-imap (0.4.4)
date
Expand Down Expand Up @@ -230,6 +232,7 @@ DEPENDENCIES
appraisal
debug
minitest (~> 5.0)
minitest-around
rails
rake
rubocop (~> 1.21)
Expand Down
1 change: 1 addition & 0 deletions actual_db_schema.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Gem::Specification.new do |spec|

spec.add_development_dependency "appraisal"
spec.add_development_dependency "debug"
spec.add_development_dependency "minitest-around"
spec.add_development_dependency "rails"
spec.add_development_dependency "sqlite3"

Expand Down
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
Empty file.
9 changes: 9 additions & 0 deletions test/dummy_app/db/migrate_secondary/20130906111511_first.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class First < ActiveRecord::Migration[6.0]
def up
TestingState.up << :first
end

def down
TestingState.down << :first
end
end
9 changes: 9 additions & 0 deletions test/dummy_app/db/migrate_secondary/20130906111512_second.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Second < ActiveRecord::Migration[6.0]
def up
TestingState.up << :second
end

def down
TestingState.down << :second
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
93 changes: 93 additions & 0 deletions test/rake_task_secondary_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

require "test_helper"

describe "multiple databases support" do
let(:utils) do
TestUtils.new(migrations_path: "db/migrate_secondary", migrated_path: "tmp/migrated_migrate_secondary")
end

around do |block|
original_db_config = ActiveRecord::Base.connection_db_config
original = ActiveRecord::Tasks::DatabaseTasks.database_configuration
ActiveRecord::Tasks::DatabaseTasks.database_configuration = {
test: TestingState.db_config.fetch(:secondary)
}
ActiveRecord::Base.establish_connection(TestingState.db_config.fetch(:secondary))
utils.cleanup
block.call
ensure
ActiveRecord::Base.establish_connection(original_db_config)
ActiveRecord::Tasks::DatabaseTasks.database_configuration = original
end

describe "db:rollback_branches" do
it "creates the tmp/migrated_migrate_secondary folder" do
refute File.exist?(utils.app_file("tmp/migrated_migrate_secondary"))
utils.run_migrations
assert File.exist?(utils.app_file("tmp/migrated_migrate_secondary"))
end

it "migrates the migrations" do
assert_empty utils.applied_migrations
utils.run_migrations
assert_equal %w[20130906111511 20130906111512], utils.applied_migrations
end

it "keeps migrated migrations in tmp/migrated folder" do
utils.run_migrations
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files
end

it "rolls back the migrations in the reversed order" do
utils.prepare_phantom_migrations
assert_empty TestingState.down
utils.run_migrations
assert_equal %i[second first], TestingState.down
end

describe "with irreversible migration" do
before do
utils.define_migration_file("20130906111513_irreversible.rb", <<~RUBY)
class Irreversible < ActiveRecord::Migration[6.0]
def up
TestingState.up << :irreversible
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
RUBY
end

it "keeps track of the irreversible migrations" do
utils.prepare_phantom_migrations
assert_equal %i[first second irreversible], TestingState.up
assert_empty ActualDbSchema.failed
utils.run_migrations
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
end
end
end

describe "db:phantom_migrations" do
it "shows the list of phantom migrations" do
ActualDbSchema::Git.stub(:current_branch, "fix-bug") do
utils.prepare_phantom_migrations
Rake::Task["db:phantom_migrations"].invoke
Rake::Task["db:phantom_migrations"].reenable
assert_match(/ Status Migration ID Branch Migration File/, TestingState.output)
assert_match(/---------------------------------------------------/, TestingState.output)
assert_match(
%r{ up 20130906111511 fix-bug tmp/migrated_migrate_secondary/20130906111511_first.rb},
TestingState.output
)
assert_match(
%r{ up 20130906111512 fix-bug tmp/migrated_migrate_secondary/20130906111512_second.rb},
TestingState.output
)
end
end
end
end
29 changes: 20 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require "rails/all"
require "actual_db_schema"
require "minitest/autorun"
require "minitest/around/spec"
require "debug"
require "support/test_utils"

Expand All @@ -18,20 +19,25 @@ 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",
migrations_paths: Rails.root.join("db", "migrate_secondary").to_s
}
}
end

def self.reset
self.up = []
self.down = []
Expand All @@ -41,6 +47,11 @@ def self.reset
reset
end

ActiveRecord::Tasks::DatabaseTasks.database_configuration = { test: TestingState.db_config.fetch(:primary) }
ActiveRecord::Base.establish_connection(**TestingState.db_config.fetch(:primary))

ActualDbSchema.config[:enabled] = true

module Kernel
alias original_puts puts

Expand Down
Loading