| 1 | | namespace :db do |
|---|
| 2 | | desc 'Creates the databases defined in config/database.yml (unless they already exist)' |
|---|
| 3 | | task :create => :environment do |
|---|
| 4 | | ActiveRecord::Base.configurations.each_value do |config| |
|---|
| | 1 | namespace :db do |
|---|
| | 2 | namespace :create do |
|---|
| | 3 | desc 'Create all the local databases defined in config/database.yml' |
|---|
| | 4 | task :all => :environment do |
|---|
| | 5 | ActiveRecord::Base.configurations.each_value do |config| |
|---|
| | 6 | create_local_database(config) |
|---|
| | 7 | end |
|---|
| | 8 | end |
|---|
| | 9 | end |
|---|
| | 10 | |
|---|
| | 11 | desc 'Create the local database defined in config/database.yml for the current RAILS_ENV' |
|---|
| | 12 | task :create => :environment do |
|---|
| | 13 | create_local_database(ActiveRecord::Base.configurations[RAILS_ENV]) |
|---|
| | 14 | end |
|---|
| | 15 | |
|---|
| | 16 | def create_local_database(config) |
|---|
| | 17 | # Only connect to local databases |
|---|
| | 18 | if config['host'] == 'localhost' || config['host'].blank? |
|---|
| 117 | | when "mysql", "oci", "oracle" |
|---|
| 118 | | ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) |
|---|
| 119 | | File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } |
|---|
| 120 | | when "postgresql" |
|---|
| 121 | | ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] |
|---|
| 122 | | ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] |
|---|
| 123 | | ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] |
|---|
| 124 | | search_path = abcs[RAILS_ENV]["schema_search_path"] |
|---|
| 125 | | search_path = "--schema=#{search_path}" if search_path |
|---|
| 126 | | `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` |
|---|
| 127 | | raise "Error dumping database" if $?.exitstatus == 1 |
|---|
| 128 | | when "sqlite", "sqlite3" |
|---|
| 129 | | dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] |
|---|
| 130 | | `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` |
|---|
| 131 | | when "sqlserver" |
|---|
| 132 | | `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` |
|---|
| 133 | | `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` |
|---|
| 134 | | when "firebird" |
|---|
| 135 | | set_firebird_env(abcs[RAILS_ENV]) |
|---|
| 136 | | db_string = firebird_db_string(abcs[RAILS_ENV]) |
|---|
| 137 | | sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" |
|---|
| 138 | | else |
|---|
| 139 | | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|
| | 133 | when "mysql", "oci", "oracle" |
|---|
| | 134 | ActiveRecord::Base.establish_connection(abcs[RAILS_ENV]) |
|---|
| | 135 | File.open("db/#{RAILS_ENV}_structure.sql", "w+") { |f| f << ActiveRecord::Base.connection.structure_dump } |
|---|
| | 136 | when "postgresql" |
|---|
| | 137 | ENV['PGHOST'] = abcs[RAILS_ENV]["host"] if abcs[RAILS_ENV]["host"] |
|---|
| | 138 | ENV['PGPORT'] = abcs[RAILS_ENV]["port"].to_s if abcs[RAILS_ENV]["port"] |
|---|
| | 139 | ENV['PGPASSWORD'] = abcs[RAILS_ENV]["password"].to_s if abcs[RAILS_ENV]["password"] |
|---|
| | 140 | search_path = abcs[RAILS_ENV]["schema_search_path"] |
|---|
| | 141 | search_path = "--schema=#{search_path}" if search_path |
|---|
| | 142 | `pg_dump -i -U "#{abcs[RAILS_ENV]["username"]}" -s -x -O -f db/#{RAILS_ENV}_structure.sql #{search_path} #{abcs[RAILS_ENV]["database"]}` |
|---|
| | 143 | raise "Error dumping database" if $?.exitstatus == 1 |
|---|
| | 144 | when "sqlite", "sqlite3" |
|---|
| | 145 | dbfile = abcs[RAILS_ENV]["database"] || abcs[RAILS_ENV]["dbfile"] |
|---|
| | 146 | `#{abcs[RAILS_ENV]["adapter"]} #{dbfile} .schema > db/#{RAILS_ENV}_structure.sql` |
|---|
| | 147 | when "sqlserver" |
|---|
| | 148 | `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /f db\\#{RAILS_ENV}_structure.sql /q /A /r` |
|---|
| | 149 | `scptxfr /s #{abcs[RAILS_ENV]["host"]} /d #{abcs[RAILS_ENV]["database"]} /I /F db\ /q /A /r` |
|---|
| | 150 | when "firebird" |
|---|
| | 151 | set_firebird_env(abcs[RAILS_ENV]) |
|---|
| | 152 | db_string = firebird_db_string(abcs[RAILS_ENV]) |
|---|
| | 153 | sh "isql -a #{db_string} > db/#{RAILS_ENV}_structure.sql" |
|---|
| | 154 | else |
|---|
| | 155 | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|
| 161 | | when "mysql" |
|---|
| 162 | | ActiveRecord::Base.establish_connection(:test) |
|---|
| 163 | | ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') |
|---|
| 164 | | IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| |
|---|
| 165 | | ActiveRecord::Base.connection.execute(table) |
|---|
| 166 | | end |
|---|
| 167 | | when "postgresql" |
|---|
| 168 | | ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] |
|---|
| 169 | | ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] |
|---|
| 170 | | ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] |
|---|
| 171 | | `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` |
|---|
| 172 | | when "sqlite", "sqlite3" |
|---|
| 173 | | dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] |
|---|
| 174 | | `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` |
|---|
| 175 | | when "sqlserver" |
|---|
| 176 | | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` |
|---|
| 177 | | when "oci", "oracle" |
|---|
| 178 | | ActiveRecord::Base.establish_connection(:test) |
|---|
| 179 | | IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| |
|---|
| 180 | | ActiveRecord::Base.connection.execute(ddl) |
|---|
| 181 | | end |
|---|
| 182 | | when "firebird" |
|---|
| 183 | | set_firebird_env(abcs["test"]) |
|---|
| 184 | | db_string = firebird_db_string(abcs["test"]) |
|---|
| 185 | | sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" |
|---|
| 186 | | else |
|---|
| 187 | | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|
| | 177 | when "mysql" |
|---|
| | 178 | ActiveRecord::Base.establish_connection(:test) |
|---|
| | 179 | ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0') |
|---|
| | 180 | IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split("\n\n").each do |table| |
|---|
| | 181 | ActiveRecord::Base.connection.execute(table) |
|---|
| | 182 | end |
|---|
| | 183 | when "postgresql" |
|---|
| | 184 | ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] |
|---|
| | 185 | ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] |
|---|
| | 186 | ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] |
|---|
| | 187 | `psql -U "#{abcs["test"]["username"]}" -f db/#{RAILS_ENV}_structure.sql #{abcs["test"]["database"]}` |
|---|
| | 188 | when "sqlite", "sqlite3" |
|---|
| | 189 | dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] |
|---|
| | 190 | `#{abcs["test"]["adapter"]} #{dbfile} < db/#{RAILS_ENV}_structure.sql` |
|---|
| | 191 | when "sqlserver" |
|---|
| | 192 | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` |
|---|
| | 193 | when "oci", "oracle" |
|---|
| | 194 | ActiveRecord::Base.establish_connection(:test) |
|---|
| | 195 | IO.readlines("db/#{RAILS_ENV}_structure.sql").join.split(";\n\n").each do |ddl| |
|---|
| | 196 | ActiveRecord::Base.connection.execute(ddl) |
|---|
| | 197 | end |
|---|
| | 198 | when "firebird" |
|---|
| | 199 | set_firebird_env(abcs["test"]) |
|---|
| | 200 | db_string = firebird_db_string(abcs["test"]) |
|---|
| | 201 | sh "isql -i db/#{RAILS_ENV}_structure.sql #{db_string}" |
|---|
| | 202 | else |
|---|
| | 203 | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|
| 195 | | when "mysql" |
|---|
| 196 | | ActiveRecord::Base.establish_connection(:test) |
|---|
| 197 | | ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) |
|---|
| 198 | | when "postgresql" |
|---|
| 199 | | ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] |
|---|
| 200 | | ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] |
|---|
| 201 | | ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] |
|---|
| 202 | | enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"] |
|---|
| 203 | | |
|---|
| 204 | | ActiveRecord::Base.clear_active_connections! |
|---|
| 205 | | `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` |
|---|
| 206 | | `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` |
|---|
| 207 | | when "sqlite","sqlite3" |
|---|
| 208 | | dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] |
|---|
| 209 | | File.delete(dbfile) if File.exist?(dbfile) |
|---|
| 210 | | when "sqlserver" |
|---|
| 211 | | dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') |
|---|
| 212 | | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` |
|---|
| 213 | | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` |
|---|
| 214 | | when "oci", "oracle" |
|---|
| 215 | | ActiveRecord::Base.establish_connection(:test) |
|---|
| 216 | | ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| |
|---|
| 217 | | ActiveRecord::Base.connection.execute(ddl) |
|---|
| 218 | | end |
|---|
| 219 | | when "firebird" |
|---|
| 220 | | ActiveRecord::Base.establish_connection(:test) |
|---|
| 221 | | ActiveRecord::Base.connection.recreate_database! |
|---|
| 222 | | else |
|---|
| 223 | | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|
| | 211 | when "mysql" |
|---|
| | 212 | ActiveRecord::Base.establish_connection(:test) |
|---|
| | 213 | ActiveRecord::Base.connection.recreate_database(abcs["test"]["database"]) |
|---|
| | 214 | when "postgresql" |
|---|
| | 215 | ENV['PGHOST'] = abcs["test"]["host"] if abcs["test"]["host"] |
|---|
| | 216 | ENV['PGPORT'] = abcs["test"]["port"].to_s if abcs["test"]["port"] |
|---|
| | 217 | ENV['PGPASSWORD'] = abcs["test"]["password"].to_s if abcs["test"]["password"] |
|---|
| | 218 | enc_option = "-E #{abcs["test"]["encoding"]}" if abcs["test"]["encoding"] |
|---|
| | 219 | |
|---|
| | 220 | ActiveRecord::Base.clear_active_connections! |
|---|
| | 221 | `dropdb -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` |
|---|
| | 222 | `createdb #{enc_option} -U "#{abcs["test"]["username"]}" #{abcs["test"]["database"]}` |
|---|
| | 223 | when "sqlite","sqlite3" |
|---|
| | 224 | dbfile = abcs["test"]["database"] || abcs["test"]["dbfile"] |
|---|
| | 225 | File.delete(dbfile) if File.exist?(dbfile) |
|---|
| | 226 | when "sqlserver" |
|---|
| | 227 | dropfkscript = "#{abcs["test"]["host"]}.#{abcs["test"]["database"]}.DP1".gsub(/\\/,'-') |
|---|
| | 228 | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{dropfkscript}` |
|---|
| | 229 | `osql -E -S #{abcs["test"]["host"]} -d #{abcs["test"]["database"]} -i db\\#{RAILS_ENV}_structure.sql` |
|---|
| | 230 | when "oci", "oracle" |
|---|
| | 231 | ActiveRecord::Base.establish_connection(:test) |
|---|
| | 232 | ActiveRecord::Base.connection.structure_drop.split(";\n\n").each do |ddl| |
|---|
| | 233 | ActiveRecord::Base.connection.execute(ddl) |
|---|
| | 234 | end |
|---|
| | 235 | when "firebird" |
|---|
| | 236 | ActiveRecord::Base.establish_connection(:test) |
|---|
| | 237 | ActiveRecord::Base.connection.recreate_database! |
|---|
| | 238 | else |
|---|
| | 239 | raise "Task not supported by '#{abcs["test"]["adapter"]}'" |
|---|