aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuillermo Iguaran <guilleiguaran@gmail.com>2017-05-14 12:15:35 -0500
committerGuillermo Iguaran <guilleiguaran@gmail.com>2017-05-15 18:19:12 -0500
commitf98c97d37996b648a27c80166f0fc60ac94f76e2 (patch)
tree79121672326e8ed673dc0a950256d463ef5ac8c8
parent0b921f5d3ebaed95dcc0d8304f054a9ccf672141 (diff)
downloadrails-f98c97d37996b648a27c80166f0fc60ac94f76e2.tar.gz
rails-f98c97d37996b648a27c80166f0fc60ac94f76e2.tar.bz2
rails-f98c97d37996b648a27c80166f0fc60ac94f76e2.zip
Improvements for SQLite rake task.
* Use NOT IN in SQL query * Quote table names propertly * Use array form of command invocation
-rw-r--r--activerecord/lib/active_record/tasks/sqlite_database_tasks.rb25
1 files changed, 18 insertions, 7 deletions
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