From e0604f75007b1630de80b206aed3a8ab41001ffd Mon Sep 17 00:00:00 2001 From: bogdanvlviv Date: Wed, 26 Apr 2017 03:08:26 +0300 Subject: Refactor AR::Tasks::DatabaseTasks::migrate Set consistent type cast ENV["VERBOSE"]: ENV["VERBOSE"] is true if it not equal "false" --- activerecord/lib/active_record/tasks/database_tasks.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 46fa8a70a3..45110a79cd 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -164,7 +164,7 @@ module ActiveRecord def migrate raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty? - verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true + verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil scope = ENV["SCOPE"] verbose_was, Migration.verbose = Migration.verbose, verbose -- cgit v1.2.3 From f73b845610931ddf03c756d804982776a3cc69a4 Mon Sep 17 00:00:00 2001 From: Akira Matsuda Date: Thu, 11 May 2017 17:40:41 +0900 Subject: Defer loading each DB Tasks class from AR DatabaseTasks Because we don't need to load tasks for DBs that we don't use for the current app. Also, these Tasks classes load AR::Base in their class level, and so immediately kick :active_record on_load hooks. This used to happen when we were loading tasks, not when we run a task. --- activerecord/lib/active_record/tasks/database_tasks.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb index 45110a79cd..c53dee43d7 100644 --- a/activerecord/lib/active_record/tasks/database_tasks.rb +++ b/activerecord/lib/active_record/tasks/database_tasks.rb @@ -71,9 +71,9 @@ module ActiveRecord @tasks[pattern] = task end - register_task(/mysql/, ActiveRecord::Tasks::MySQLDatabaseTasks) - register_task(/postgresql/, ActiveRecord::Tasks::PostgreSQLDatabaseTasks) - register_task(/sqlite/, ActiveRecord::Tasks::SQLiteDatabaseTasks) + register_task(/mysql/, 'ActiveRecord::Tasks::MySQLDatabaseTasks') + register_task(/postgresql/, 'ActiveRecord::Tasks::PostgreSQLDatabaseTasks') + register_task(/sqlite/, 'ActiveRecord::Tasks::SQLiteDatabaseTasks') def db_dir @db_dir ||= Rails.application.config.paths["db"].first @@ -288,11 +288,11 @@ module ActiveRecord private def class_for_adapter(adapter) - key = @tasks.keys.detect { |pattern| adapter[pattern] } - unless key + _key, task = @tasks.each_pair.detect { |pattern, _task| adapter[pattern] } + unless task raise DatabaseNotSupported, "Rake tasks not supported by '#{adapter}' adapter" end - @tasks[key] + task.is_a?(String) ? task.constantize : task end def each_current_configuration(environment) -- cgit v1.2.3 From be1dd45200b1f21aec5b151d78ef402df9fa28e4 Mon Sep 17 00:00:00 2001 From: Rusty Geldmacher Date: Fri, 12 May 2017 11:40:31 -0400 Subject: Respect `ignore_tables` in Postgres structure dump When using `sql` as the schema format, or even just doing `rake db:structure:dump`, it would be good to respect the list of ignored tables that has been configured. --- activerecord/lib/active_record/tasks/postgresql_database_tasks.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb index f1af90c1e8..7f1a768d8b 100644 --- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb @@ -66,6 +66,12 @@ module ActiveRecord "--schema=#{part.strip}" end end + + ignore_tables = ActiveRecord::SchemaDumper.ignore_tables + if ignore_tables.any? + args += ignore_tables.flat_map { |table| ["-T", table] } + end + args << configuration["database"] run_cmd("pg_dump", args, "dumping") remove_sql_header_comments(filename) -- cgit v1.2.3 From 486562fa955f2cbd574e7f4099fdf69a49a8ce20 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Sat, 13 May 2017 21:35:22 -0500 Subject: Respect 'ignore_tables' in MySQL structure dump --- activerecord/lib/active_record/tasks/mysql_database_tasks.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb index c05f0a8fbb..541165b3d1 100644 --- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb @@ -60,6 +60,12 @@ module ActiveRecord args.concat(["--routines"]) args.concat(["--skip-comments"]) args.concat(Array(extra_flags)) if extra_flags + + ignore_tables = ActiveRecord::SchemaDumper.ignore_tables + if ignore_tables.any? + args += ignore_tables.map { |table| "--ignore-table=#{configuration['database']}.#{table}" } + end + args.concat(["#{configuration['database']}"]) run_cmd("mysqldump", args, "dumping") -- cgit v1.2.3 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 --- activerecord/lib/active_record/tasks/sqlite_database_tasks.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'activerecord/lib/active_record/tasks') 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) -- cgit v1.2.3 From f98c97d37996b648a27c80166f0fc60ac94f76e2 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Sun, 14 May 2017 12:15:35 -0500 Subject: Improvements for SQLite rake task. * Use NOT IN in SQL query * Quote table names propertly * Use array form of command invocation --- .../active_record/tasks/sqlite_database_tasks.rb | 25 ++++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) (limited to 'activerecord/lib/active_record/tasks') diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb index 5542e5585c..7043d2f0c8 100644 --- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb +++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb @@ -36,18 +36,18 @@ module ActiveRecord end def structure_dump(filename, extra_flags) - dbfile = configuration["database"] - flags = extra_flags.join(" ") if extra_flags + args = [] + args.concat(Array(extra_flags)) if extra_flags + args << configuration["database"] 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}` + condition = ignore_tables.map { |table| connection.quote_table_name(table) }.join(", ") + args << "SELECT sql FROM sqlite_master WHERE tbl_name NOT IN (#{condition}) ORDER BY tbl_name, type DESC, name" else - `sqlite3 #{flags} #{dbfile} .schema > #{filename}` + args << ".schema" end + run_cmd("sqlite3", args, filename) end def structure_load(filename, extra_flags) @@ -65,6 +65,17 @@ module ActiveRecord def root @root end + + def run_cmd(cmd, args, out) + fail run_cmd_error(cmd, args) unless Kernel.system(cmd, *args, out: out) + end + + def run_cmd_error(cmd, args) + msg = "failed to execute:\n" + msg << "#{cmd} #{args.join(' ')}\n\n" + msg << "Please check the output above for any errors and make sure that `#{cmd}` is installed in your PATH and has proper permissions.\n\n" + msg + end end end end -- cgit v1.2.3