From 4b49ab66425283f4b79907873b72236c7ebec0be Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Sat, 13 May 2017 23:01:13 -0500 Subject: Respect 'ignore_tables' in SQLite structure dump --- .../lib/active_record/tasks/sqlite_database_tasks.rb | 11 ++++++++++- activerecord/test/cases/tasks/sqlite_rake_test.rb | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index 1f756c2979..5542e5585c 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -38,7 +38,16 @@ module ActiveRecord def structure_dump(filename, extra_flags) dbfile = configuration["database"] flags = extra_flags.join(" ") if extra_flags - `sqlite3 #{flags} #{dbfile} .schema > #{filename}` + + ignore_tables = ActiveRecord::SchemaDumper.ignore_tables + if ignore_tables.any? + tables = `sqlite3 #{dbfile} .tables`.split - ignore_tables + condition = tables.map { |table| "tbl_name = '#{table}'" }.join(" OR ") + statement = "SELECT sql FROM sqlite_master WHERE #{condition} ORDER BY tbl_name, type DESC, name" + `sqlite3 #{flags} #{dbfile} "#{statement}" > #{filename}` + else + `sqlite3 #{flags} #{dbfile} .schema > #{filename}` + end end def structure_load(filename, extra_flags) diff --git a/activerecord/test/cases/tasks/sqlite_rake_test.rb b/activerecord/test/cases/tasks/sqlite_rake_test.rb index 0d917f3f6c..ccb3834fee 100644 --- a/activerecord/test/cases/tasks/sqlite_rake_test.rb +++ b/activerecord/test/cases/tasks/sqlite_rake_test.rb @@ -180,6 +180,9 @@ if current_adapter?(:SQLite3Adapter) "adapter" => "sqlite3", "database" => @database } + + `sqlite3 #{@database} 'CREATE TABLE bar(id INTEGER)'` + `sqlite3 #{@database} 'CREATE TABLE foo(id INTEGER)'` end def test_structure_dump @@ -189,6 +192,23 @@ if current_adapter?(:SQLite3Adapter) ActiveRecord::Tasks::DatabaseTasks.structure_dump @configuration, filename, "/rails/root" assert File.exist?(dbfile) assert File.exist?(filename) + assert_match(/CREATE TABLE foo/, File.read(filename)) + assert_match(/CREATE TABLE bar/, File.read(filename)) + ensure + FileUtils.rm_f(filename) + FileUtils.rm_f(dbfile) + end + + def test_structure_dump_with_ignore_tables + dbfile = @database + filename = "awesome-file.sql" + ActiveRecord::SchemaDumper.expects(:ignore_tables).returns(["foo"]) + + ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename, "/rails/root") + assert File.exist?(dbfile) + assert File.exist?(filename) + assert_match(/bar/, File.read(filename)) + assert_no_match(/foo/, File.read(filename)) ensure FileUtils.rm_f(filename) FileUtils.rm_f(dbfile) -- cgit v1.2.3