mirror of https://github.com/rails/rails
Add ability to ignore tables by regexp for SQL schema dumps
This commit is contained in:
parent
7d3fd6edc6
commit
a3dc851159
|
@ -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
|
||||
|
|
|
@ -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: []
|
||||
|
||||
##
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue