aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/tasks
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2012-06-21 01:35:26 +0900
committerkennyj <kennyj@gmail.com>2012-06-21 01:35:26 +0900
commit19911959d9a8fa5341f61b028cb489e6ecc2c339 (patch)
tree539f3aedfa8167aced878adba05845e46c796f86 /activerecord/lib/active_record/tasks
parent4f700edcc094406155785c693363801d0277792c (diff)
downloadrails-19911959d9a8fa5341f61b028cb489e6ecc2c339.tar.gz
rails-19911959d9a8fa5341f61b028cb489e6ecc2c339.tar.bz2
rails-19911959d9a8fa5341f61b028cb489e6ecc2c339.zip
Refactor db:structure:load task.
Diffstat (limited to 'activerecord/lib/active_record/tasks')
-rw-r--r--activerecord/lib/active_record/tasks/database_tasks.rb6
-rw-r--r--activerecord/lib/active_record/tasks/mysql_database_tasks.rb8
-rw-r--r--activerecord/lib/active_record/tasks/postgresql_database_tasks.rb5
-rw-r--r--activerecord/lib/active_record/tasks/sqlite_database_tasks.rb5
4 files changed, 24 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/tasks/database_tasks.rb b/activerecord/lib/active_record/tasks/database_tasks.rb
index d53fe307c2..999b2ebc85 100644
--- a/activerecord/lib/active_record/tasks/database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/database_tasks.rb
@@ -66,6 +66,12 @@ module ActiveRecord
class_for_adapter(configuration['adapter']).new(*arguments).structure_dump(filename)
end
+ def structure_load(*arguments)
+ configuration = arguments.first
+ filename = arguments.delete_at 1
+ class_for_adapter(configuration['adapter']).new(*arguments).structure_load(filename)
+ end
+
private
def class_for_adapter(adapter)
diff --git a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
index 052ab60e81..b39cd2282f 100644
--- a/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/mysql_database_tasks.rb
@@ -49,6 +49,14 @@ module ActiveRecord
File.open(filename, "w:utf-8") { |f| f << ActiveRecord::Base.connection.structure_dump }
end
+ def structure_load(filename)
+ establish_connection(configuration)
+ connection.execute('SET foreign_key_checks = 0')
+ IO.read(filename).split("\n\n").each do |table|
+ connection.execute(table)
+ end
+ end
+
private
def configuration
diff --git a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
index dabc69b9cd..a210392e53 100644
--- a/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/postgresql_database_tasks.rb
@@ -48,6 +48,11 @@ module ActiveRecord
File.open(filename, "a") { |f| f << "SET search_path TO #{ActiveRecord::Base.connection.schema_search_path};\n\n" }
end
+ def structure_load(filename)
+ set_psql_env
+ Kernel.system("psql -f #{filename} #{configuration['database']}")
+ end
+
private
def configuration
diff --git a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
index 68f0c304a8..da01058a82 100644
--- a/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
+++ b/activerecord/lib/active_record/tasks/sqlite_database_tasks.rb
@@ -36,6 +36,11 @@ module ActiveRecord
`sqlite3 #{dbfile} .schema > #{filename}`
end
+ def structure_load(filename)
+ dbfile = configuration['database']
+ `sqlite3 #{dbfile} < "#{filename}"`
+ end
+
private
def configuration