Add ability to ignore tables by regexp for SQL schema dumps

This commit is contained in:
fatkodima 2022-05-14 02:11:47 +03:00
parent 7d3fd6edc6
commit a3dc851159
8 changed files with 40 additions and 26 deletions

View File

@ -1,3 +1,11 @@
* Add ability to ignore tables by regexp for SQL schema dumps.
```ruby
ActiveRecord::SchemaDumper.ignore_tables = [/^_/]
```
*fatkodima*
* Avoid queries when performing calculations on contradictory relations.
Previously calculations would make a query even when passed a

View File

@ -13,8 +13,7 @@ module ActiveRecord
##
# :singleton-method:
# A list of tables which should not be dumped to the schema.
# Acceptable values are strings as well as regexp if ActiveRecord.schema_format == :ruby.
# Only strings are accepted if ActiveRecord.schema_format == :sql.
# Acceptable values are strings and regexps.
cattr_accessor :ignore_tables, default: []
##

View File

@ -49,6 +49,7 @@ module ActiveRecord
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
if ignore_tables.any?
ignore_tables = connection.data_sources.select { |table| ignore_tables.any? { |pattern| pattern === table } }
args += ignore_tables.map { |table| "--ignore-table=#{db_config.database}.#{table}" }
end

View File

@ -70,6 +70,7 @@ module ActiveRecord
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
if ignore_tables.any?
ignore_tables = connection.data_sources.select { |table| ignore_tables.any? { |pattern| pattern === table } }
args += ignore_tables.flat_map { |table| ["-T", table] }
end

View File

@ -49,6 +49,7 @@ module ActiveRecord
ignore_tables = ActiveRecord::SchemaDumper.ignore_tables
if ignore_tables.any?
ignore_tables = connection.data_sources.select { |table| ignore_tables.any? { |pattern| pattern === table } }
condition = ignore_tables.map { |table| connection.quote(table) }.join(", ")
args << "SELECT sql FROM sqlite_master WHERE tbl_name NOT IN (#{condition}) ORDER BY tbl_name, type DESC, name"
else

View File

@ -336,14 +336,16 @@ if current_adapter?(:Mysql2Adapter)
def test_structure_dump_with_ignore_tables
filename = "awesome-file.sql"
ActiveRecord::SchemaDumper.stub(:ignore_tables, ["foo", "bar"]) do
assert_called_with(
Kernel,
:system,
["mysqldump", "--result-file", filename, "--no-data", "--routines", "--skip-comments", "--ignore-table=test-db.foo", "--ignore-table=test-db.bar", "test-db"],
returns: true
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
ActiveRecord::Base.connection.stub(:data_sources, ["foo", "bar", "prefix_foo", "ignored_foo"]) do
ActiveRecord::SchemaDumper.stub(:ignore_tables, [/^prefix_/, "ignored_foo"]) do
assert_called_with(
Kernel,
:system,
["mysqldump", "--result-file", filename, "--no-data", "--routines", "--skip-comments", "--ignore-table=test-db.prefix_foo", "--ignore-table=test-db.ignored_foo", "test-db"],
returns: true
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
end
end
end
end

View File

@ -428,18 +428,16 @@ if current_adapter?(:PostgreSQLAdapter)
end
def test_structure_dump_with_ignore_tables
assert_called(
ActiveRecord::SchemaDumper,
:ignore_tables,
returns: ["foo", "bar"]
) do
assert_called_with(
Kernel,
:system,
[{}, "pg_dump", "--schema-only", "--no-privileges", "--no-owner", "--file", @filename, "-T", "foo", "-T", "bar", "my-app-db"],
returns: true
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
ActiveRecord::Base.connection.stub(:data_sources, ["foo", "bar", "prefix_foo", "ignored_foo"]) do
ActiveRecord::SchemaDumper.stub(:ignore_tables, [/^prefix_/, "ignored_foo"]) do
assert_called_with(
Kernel,
:system,
[{}, "pg_dump", "--schema-only", "--no-privileges", "--no-owner", "--file", @filename, "-T", "prefix_foo", "-T", "ignored_foo", "my-app-db"],
returns: true
) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, @filename)
end
end
end
end

View File

@ -199,13 +199,17 @@ if current_adapter?(:SQLite3Adapter)
def test_structure_dump_with_ignore_tables
dbfile = @database
filename = "awesome-file.sql"
assert_called(ActiveRecord::SchemaDumper, :ignore_tables, returns: ["foo"]) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root")
ActiveRecord::Base.connection.stub(:data_sources, ["foo", "bar", "prefix_foo", "ignored_foo"]) do
ActiveRecord::SchemaDumper.stub(:ignore_tables, [/^prefix_/, "ignored_foo"]) do
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root")
end
end
assert File.exist?(dbfile)
assert File.exist?(filename)
assert_match(/bar/, File.read(filename))
assert_no_match(/foo/, File.read(filename))
contents = File.read(filename)
assert_match(/bar/, contents)
assert_no_match(/prefix_foo/, contents)
assert_no_match(/ignored_foo/, contents)
ensure
FileUtils.rm_f(filename)
FileUtils.rm_f(dbfile)