Permalink
Browse files

Improvements for SQLite rake task.

* Use NOT IN in SQL query
* Quote table names propertly
* Use array form of command invocation
  • Loading branch information...
guilleiguaran committed May 14, 2017
1 parent 0b921f5 commit f98c97d37996b648a27c80166f0fc60ac94f76e2
Showing with 18 additions and 7 deletions.
  1. +18 −7 activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
@@ -36,18 +36,18 @@ def charset
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 @@ def configuration
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

0 comments on commit f98c97d

Please sign in to comment.