-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootstrap-db.rake
More file actions
193 lines (151 loc) · 7.53 KB
/
bootstrap-db.rake
File metadata and controls
193 lines (151 loc) · 7.53 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require File.dirname(__FILE__) + '/../bootstrap_helpers.rb'
namespace :db do
include BootstrapHelpers
desc "Reset application to past SQL point"
task :reset_app => :environment do
puts "===================================================\n"
puts "== Rake - db:reset_app ==\n"
puts "===================================================\n"
#Check for any bootstrap sql file to load
bootstrap_location = File.join(RAILS_ROOT, 'db', 'bootstrap')
sql_file_location = ENV['file'] || File.join(bootstrap_location, 'bootstrap_data.sql')
if File.exists?(sql_file_location)
puts "Dropping database tables...\n"
run_rake('db:drop')
puts "Creating database tables...\n"
run_rake('db:create')
run_rake('db:automigrate') if Object.const_defined?(:DataMapper)
puts "Loading database dump...\n"
run_rake('db:database_load')
puts "\n***** Database Loaded... *****\n\n"
if Object.const_defined?(:ActiveRecord) #'nicety' for displaying some common errors
migrator = ActiveRecord::Migrator.new(:up, "db/migrate")
if migrator && migrator.current_migration && migrator.current_migration.filename
last_migration_file = migrator.current_migration.filename
puts "** Loading migrations since #{last_migration_file} -->"
else
puts "** Error - Unable to find the current migration file. Most likely cause is the database has migrations loaded that are beyond the code base. Please update the codebase."
puts "** Attempting to run migration tasks."
end
end
run_rake('db:migrate')
else
raise "Error - Unable to find sql file to load : #{sql_file_location}"
end
puts "===================================================\n"
puts "== Rake - db:reset_app completed ==\n"
puts "===================================================\n"
end
desc "Dump the current database to an SQL file"
task :database_dump do
load 'config/environment.rb' unless Object.const_defined?(:ActiveRecord)
config = load_config
raise_common_errors(config)
passed_file = ENV['file']
passed_filename = ENV['bootstrap'] == true ? 'bootstrap_data.sql' : ENV['file_name']
sql_root = File.join(RAILS_ROOT, 'db', 'bootstrap')
ignore_tables = ENV['ignore_tables'].split(',') if ENV['ignore_tables'].present?
passed_params = ENV['additional_params'].split(',') if ENV['additional_params'].present?
display = ENV['display']
if passed_file.blank?
sql_filename = passed_filename || "#{RAILS_ENV}_database_dump.sql"
sql_path = File.join(sql_root,sql_filename)
else
sql_filename = File.basename(ENV['file'])
sql_path = ENV['file']
end
#Create directories if they don't exist
Dir.mkdir sql_root if !File.exists?(sql_root)
puts "Generating SQL Dump of Database - #{sql_path}"
case config[RAILS_ENV]["adapter"]
when 'mysql'
#mysqldump --help
default_sql_attrs = "-q --add-drop-table --add-locks --extended-insert --lock-tables --single-transaction"
if ignore_tables.present?
ignore_tables.each do |table_name|
default_sql_attrs += " --ignore-table=#{config[RAILS_ENV]["database"]}.#{table_name.strip}"
end
end
if passed_params.present?
passed_params.each do |param|
default_sql_attrs += " #{param}"
end
end
password_attrs = " -p#{config[RAILS_ENV]["password"]}" if config[RAILS_ENV]["password"].present?
#--all-tablespaces
display_and_execute("mysqldump #{default_sql_attrs} -h #{config[RAILS_ENV]["host"]} -u #{config[RAILS_ENV]["username"]}#{password_attrs.to_s} #{config[RAILS_ENV]["database"]} > \"#{sql_path}\"", display)
when 'postgres', 'postgresql'
#pg_dumpall --help
default_sql_attrs = "-i --clean --inserts --column-inserts --no-owner --no-privileges"
if ignore_tables.present?
ignore_tables.each do |table_name|
default_sql_attrs += " --exclude-table=#{config[RAILS_ENV]["database"]}.#{table_name.strip}"
end
end
if passed_params.present?
passed_params.each do |param|
default_sql_attrs += " #{param}"
end
end
display_and_execute("pg_dumpall #{default_sql_attrs} --host=#{config[RAILS_ENV]["host"]} --port=#{config[RAILS_ENV]["port"] || 5432} --username=#{config[RAILS_ENV]["username"]} --file=\"#{sql_path}\" --database=#{config[RAILS_ENV]["database"]}", display)
else
raise "Error : Task not supported by '#{config[RAILS_ENV]['adapter']}'"
end
puts "SQL Dump completed --> #{sql_path}"
end
desc "Loads the SQL dump into the current environment"
task :database_load do
load 'config/environment.rb' unless Object.const_defined?(:ActiveRecord)
config = load_config
raise_common_errors(config)
display = ENV['display']
sql_filename = ENV['file'] || File.join('db', 'bootstrap','bootstrap_data.sql')
puts "No file location passed. Loading bootstrap defaults..." if ENV['file'].blank?
puts "Attempting to load data... #{sql_filename}"
sql_path = File.join(RAILS_ROOT, sql_filename)
raise "Unable to find SQL file to load at location - #{sql_path}" if !File.exists?(sql_path)
puts "Importing Database SQL..."
case config[RAILS_ENV]["adapter"]
when 'mysql'
password_attrs = " -p#{config[RAILS_ENV]["password"]}" if config[RAILS_ENV]["password"].present?
display_and_execute("mysql -f -h #{config[RAILS_ENV]["host"]} -u #{config[RAILS_ENV]["username"]}#{password_attrs.to_s} #{config[RAILS_ENV]["database"]} < \"#{sql_path}\"", display)
when 'postgres', 'postgresql'
default_sql_attrs = "--single-transaction"
display_and_execute("psql #{default_sql_attrs} --host=#{config[RAILS_ENV]["host"]} --port=#{config[RAILS_ENV]["port"] || 5432} --dbname=#{config[RAILS_ENV]["database"]} --username=#{config[RAILS_ENV]["username"]} < \"#{sql_path}\"", display)
else
raise "Task not supported by '#{config[RAILS_ENV]['adapter']}'"
end
puts "Database load completed..."
end
desc "Backup the mysql db to a set number of dump files"
task :database_backup do
load 'config/environment.rb' unless Object.const_defined?(:ActiveRecord)
config = load_config
raise_common_errors(config)
backup_numbers = ENV['total_backups'].present? ? ENV['total_backups'].to_i : 5
backup_location = ENV['backup_location'] || File.join(RAILS_ROOT, 'db', 'backup')
#Create directories if they don't exist
Dir.mkdir backup_location if !File.exists?(backup_location)
puts "No 'total_backups' passed. Setting maximum backup files : #{backup_numbers}..." if ENV['total_backups'].blank?
puts "No 'backup_location' passed. Saving to : #{backup_location}..." if ENV['backup_location'].blank?
#Check for current releases
Dir.chdir(backup_location) do
#Delete records
sorted_files = Dir.glob("*.sql").sort_by {|f| test(?M, f)}
puts " -- Found #{sorted_files.size} backup files"
if sorted_files.size >= backup_numbers
puts "Cleaning up backups..."
sorted_files.reverse.each_with_index do |file, i|
if i >= (backup_numbers - 1)
FileUtils.rm( file )
end
end
end
end
sql_filename = "#{Time.now.strftime("%Y%m%d").to_s}_#{Time.now.strftime("%H%M%S")}.sql"
puts "Attempting to dump data... #{sql_filename}"
ENV['file'] = File.join(backup_location, sql_filename)
run_rake('db:database_dump')
puts "Backup complete."
end
end