Skip to content

Commit

Permalink
allow a single prefix for mysqldump
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg Rychlewski authored and Greg Rychlewski committed Jul 21, 2023
1 parent cfb3dad commit 7170fbf
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 109 deletions.
39 changes: 13 additions & 26 deletions integration_test/myxql/storage_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -146,57 +146,44 @@ defmodule Ecto.Integration.StorageTest do
end

test "structure dump and load with migrations table" do
default_db = "ecto_test"
num = @base_migration + System.unique_integer([:positive])
:ok = Ecto.Migrator.up(PoolRepo, num, Migration, log: false)
{:ok, path} = Ecto.Adapters.MyXQL.structure_dump(tmp_path(), TestRepo.config())
contents = File.read!(path)
assert contents =~ "INSERT INTO `ecto_test`.`schema_migrations` (version) VALUES (#{num})"
assert contents =~ "Database: #{default_db}"
assert contents =~ "INSERT INTO `schema_migrations` (version) VALUES (#{num})"
end

test "dumps structure and schema_migration records from multiple prefixes" do
# Create the test_schema schema
test "raises when attempting to dump multiple prefixes" do
create_database()
prefix = params()[:database]

# Run migrations
version = @base_migration + System.unique_integer([:positive])
:ok = Ecto.Migrator.up(PoolRepo, version, Migration, log: false)
:ok = Ecto.Migrator.up(PoolRepo, version, Migration, log: false, prefix: prefix)

config = Keyword.put(TestRepo.config(), :dump_prefixes, ["ecto_test", prefix])
{:ok, path} = Ecto.Adapters.MyXQL.structure_dump(tmp_path(), config)
contents = File.read!(path)
msg = "cannot dump multiple prefixes with MySQL. Please run the command separately for each prefix."

assert contents =~ "Current Database: `#{prefix}`"
assert contents =~ "Current Database: `ecto_test`"
assert contents =~ "CREATE TABLE `schema_migrations`"
assert contents =~ ~s[INSERT INTO `#{prefix}`.`schema_migrations` (version) VALUES (#{version})]
assert contents =~ ~s[INSERT INTO `ecto_test`.`schema_migrations` (version) VALUES (#{version})]
assert_raise ArgumentError, msg, fn ->
Ecto.Adapters.MyXQL.structure_dump(tmp_path(), config)
end
after
drop_database()
end

test "dumps structure and schema_migration records only from queried prefix" do
# Create the test_schema schema
# Create the storage_mgt database
create_database()
prefix = params()[:database]

# Run migrations
version = @base_migration + System.unique_integer([:positive])
:ok = Ecto.Migrator.up(PoolRepo, version, Migration, log: false)
:ok = Ecto.Migrator.up(PoolRepo, version, Migration, log: false, prefix: prefix)

config = Keyword.put(TestRepo.config(), :dump_prefixes, ["ecto_test"])
config = Keyword.put(TestRepo.config(), :dump_prefixes, [prefix])
{:ok, path} = Ecto.Adapters.MyXQL.structure_dump(tmp_path(), config)
contents = File.read!(path)

refute contents =~ "Current Database: `#{prefix}`"
assert contents =~ "Current Database: `ecto_test`"
assert contents =~ "CREATE TABLE `schema_migrations`"
refute contents =~ ~s[INSERT INTO `#{prefix}`.`schema_migrations` (version) VALUES (#{version})]
assert contents =~ ~s[INSERT INTO `ecto_test`.`schema_migrations` (version) VALUES (#{version})]
after
drop_database()
refute contents =~ "USE `#{prefix}`"
assert contents =~ "Database: #{prefix}"
assert contents =~ "INSERT INTO `schema_migrations` (version) VALUES (#{version})"
end

defp strip_timestamp(dump) do
Expand Down
Loading

0 comments on commit 7170fbf

Please sign in to comment.