This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
68 lines (53 loc) · 1.87 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
58
59
60
61
62
63
64
65
66
67
require 'rubygems'
require 'bundler/setup'
require 'sinatra'
require 'active_record'
require 'net/http'
namespace :db do
task :parse_config do
YAML::load(File.open('config/database.yml'))[settings.environment.to_s].each do |key, value|
set "db_#{key}".to_sym, value
end
end
desc "Get the db file from a remote server"
task :fetch do
puts "Downloading the database dump"
FileUtils.mkdir_p("db")
Net::HTTP.start("postalcodes.s3.amazonaws.com") do |http|
resp = http.get("/mysql_sql.txt")
open("db/mysql_sql.txt", "w") { |file| file.write(resp.body) }
end
end
task :establish_base_connection => [:parse_config] do
ActiveRecord::Base.establish_connection(
adapter: settings.db_adapter,
host: settings.db_host,
username: settings.db_username,
password: settings.db_password
)
end
task :establish_specific_connection => [:parse_config] do
ActiveRecord::Base.establish_connection(
adapter: settings.db_adapter,
host: settings.db_host,
database: settings.db_database,
username: settings.db_username,
password: settings.db_password
)
end
desc "Create the database"
task :create => [:establish_base_connection] do
puts "Creating database #{settings.db_database}"
ActiveRecord::Base.connection.create_database(settings.db_database)
end
desc "Drop the database"
task :drop => [:establish_base_connection] do
puts "Destroying database #{settings.db_database}"
ActiveRecord::Base.connection.drop_database(settings.db_database)
end
desc "Create the database, load the schema, and initialize with the seed data"
task :setup => [:fetch, :create, :establish_specific_connection] do
puts "Importing data into the database..."
sh "mysql -u#{settings.db_username} -p#{settings.db_password} #{settings.db_database} < db/mysql_sql.txt"
end
end