-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
57 lines (50 loc) · 2.06 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
require 'rubygems'
require 'bundler/setup'
require 'active_record'
require 'yaml'
namespace :db do
desc 'Migrate the database'
task :migrate do
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.migration_context.migrate
end
Rake::Task["db:schema"].invoke
end
desc 'Rollback the database'
task :rollback do
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
ActiveRecord::Base.connection_pool.with_connection do |connection|
connection.migration_context.rollback
end
Rake::Task["db:schema"].invoke
end
desc 'Create a db/schema.rb file that is portable against any DB supported by AR'
task :schema do
connection_details = YAML::load(File.open('config/database.yml'))
ActiveRecord::Base.establish_connection(connection_details)
require 'active_record/schema_dumper'
filename = "db/schema.rb"
File.open(filename, "w:utf-8") do |file|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
end
desc 'Create the database'
task :create do
connection_details = YAML::load(File.open('config/database.yml'))
# admin_connection = connection_details.merge({'database'=> 'postgres',
# 'schema_search_path'=> 'public'})
ActiveRecord::Base.establish_connection(connection_details)
ActiveRecord::Base.connection.create_database(connection_details.fetch('database'))
end
desc 'Drop the database'
task :drop do
connection_details = YAML::load(File.open('config/database.yml'))
# admin_connection = connection_details.merge({'database'=> 'postgres',
# 'schema_search_path'=> 'public'})
ActiveRecord::Base.establish_connection(connection_details)
ActiveRecord::Base.connection.drop_database(connection_details.fetch('database'))
end
end