Skip to content

Commit

Permalink
try tests fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ka8725 committed May 2, 2024
1 parent 8494615 commit 481141e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 12 deletions.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/dummy_app/db/secondary_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2013_09_06_111512) do
ActiveRecord::Schema[7.0].define(version: 2013_09_06_111512) do
end
85 changes: 85 additions & 0 deletions test/rake_task_secondary_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# 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

before do
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config["secondary"] }
ActiveRecord::Base.establish_connection(**TestingState.db_config["secondary"])
utils.cleanup
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
6 changes: 5 additions & 1 deletion test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
describe "single db" do
let(:utils) { TestUtils.new }

before { utils.cleanup }
before do
ActiveRecord::Tasks::DatabaseTasks.database_configuration = { "test" => TestingState.db_config["primary"] }
ActiveRecord::Base.establish_connection(**TestingState.db_config["primary"])
utils.cleanup
end

describe "db:rollback_branches" do
it "creates the tmp/migrated folder" do
Expand Down
18 changes: 8 additions & 10 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,22 @@ def self.reset

def self.db_config
{
primary: {
adapter: "sqlite3",
database: "tmp/primary.sqlite3"
"primary" => {
"adapter" => "sqlite3",
"database" => "tmp/primary.sqlite3",
"migrations_paths" => Rails.root.join("db", "migrate").to_s
},
secondary: {
adapter: "sqlite3",
database: "tmp/secondary.sqlite3",
migrations_paths: Rails.root.join("db", "migrate_secondary").to_s
"secondary" => {
"adapter" => "sqlite3",
"database" => "tmp/secondary.sqlite3",
"migrations_paths" => Rails.root.join("db", "migrate_secondary").to_s
}
}
end

reset
end

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

ActualDbSchema.config[:enabled] = true

module Kernel
Expand Down

0 comments on commit 481141e

Please sign in to comment.