2011-12-07 11:34:29 -05:00
|
|
|
require "isolation/abstract_unit"
|
|
|
|
|
|
|
|
module ApplicationTests
|
|
|
|
module RakeTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class RakeMigrationsTest < ActiveSupport::TestCase
|
2011-12-07 11:34:29 -05:00
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf("#{app_path}/config/environments")
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
2011-12-09 14:53:55 -05:00
|
|
|
|
|
|
|
test 'running migrations with given scope' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`rails generate model user username:string password:string`
|
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
app_file "db/migrate/01_a_migration.bukkits.rb", <<-MIGRATION
|
|
|
|
class AMigration < ActiveRecord::Migration
|
|
|
|
end
|
|
|
|
MIGRATION
|
|
|
|
|
|
|
|
output = `rake db:migrate SCOPE=bukkits`
|
|
|
|
assert_no_match(/create_table\(:users\)/, output)
|
|
|
|
assert_no_match(/CreateUsers/, output)
|
|
|
|
assert_no_match(/add_column\(:users, :email, :string\)/, output)
|
2011-12-09 14:53:55 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/AMigration: migrated/, output)
|
2011-12-09 14:53:55 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
output = `rake db:migrate SCOPE=bukkits VERSION=0`
|
|
|
|
assert_no_match(/drop_table\(:users\)/, output)
|
|
|
|
assert_no_match(/CreateUsers/, output)
|
|
|
|
assert_no_match(/remove_column\(:users, :email\)/, output)
|
2011-12-09 14:53:55 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/AMigration: reverted/, output)
|
|
|
|
end
|
2011-12-09 14:53:55 -05:00
|
|
|
end
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2011-12-07 11:34:29 -05:00
|
|
|
test 'model and migration generator with change syntax' do
|
2011-12-09 15:19:49 -05:00
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string`
|
|
|
|
|
|
|
|
output = `rake db:migrate`
|
|
|
|
assert_match(/create_table\(:users\)/, output)
|
|
|
|
assert_match(/CreateUsers: migrated/, output)
|
|
|
|
assert_match(/add_column\(:users, :email, :string\)/, output)
|
|
|
|
assert_match(/AddEmailToUsers: migrated/, output)
|
|
|
|
|
|
|
|
output = `rake db:rollback STEP=2`
|
2012-12-22 12:10:40 -05:00
|
|
|
assert_match(/drop_table\(:users\)/, output)
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/CreateUsers: reverted/, output)
|
2012-12-22 12:10:40 -05:00
|
|
|
assert_match(/remove_column\(:users, :email, :string\)/, output)
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/AddEmailToUsers: reverted/, output)
|
2011-12-09 15:19:49 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'migration status when schema migrations table is not present' do
|
2014-05-06 09:20:29 -04:00
|
|
|
output = Dir.chdir(app_path){ `rake db:migrate:status 2>&1` }
|
2011-12-09 15:19:49 -05:00
|
|
|
assert_equal "Schema migrations table does not exist yet.\n", output
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'test migration status' do
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string;
|
|
|
|
rake db:migrate`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
output = `rake db:migrate:status`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{14}\s+Add email to users/, output)
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:rollback STEP=1`
|
|
|
|
output = `rake db:migrate:status`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/down\s+\d{14}\s+Add email to users/, output)
|
|
|
|
end
|
2011-12-09 15:19:49 -05:00
|
|
|
end
|
|
|
|
|
2012-02-17 10:56:57 -05:00
|
|
|
test 'migration status without timestamps' do
|
|
|
|
add_to_config('config.active_record.timestamped_migrations = false')
|
|
|
|
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string;
|
|
|
|
rake db:migrate`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
output = `rake db:migrate:status`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{3,}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:rollback STEP=1`
|
|
|
|
output = `rake db:migrate:status`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{3,}\s+Create users/, output)
|
|
|
|
assert_match(/down\s+\d{3,}\s+Add email to users/, output)
|
|
|
|
end
|
2012-02-17 10:56:57 -05:00
|
|
|
end
|
|
|
|
|
2011-12-09 15:19:49 -05:00
|
|
|
test 'test migration status after rollback and redo' do
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string;
|
|
|
|
rake db:migrate`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
output = `rake db:migrate:status`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{14}\s+Add email to users/, output)
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:rollback STEP=2`
|
|
|
|
output = `rake db:migrate:status`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/down\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/down\s+\d{14}\s+Add email to users/, output)
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:migrate:redo`
|
|
|
|
output = `rake db:migrate:status`
|
2011-12-09 15:19:49 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{14}\s+Add email to users/, output)
|
|
|
|
end
|
2011-12-09 15:19:49 -05:00
|
|
|
end
|
2012-02-17 10:56:57 -05:00
|
|
|
|
|
|
|
test 'migration status after rollback and redo without timestamps' do
|
|
|
|
add_to_config('config.active_record.timestamped_migrations = false')
|
|
|
|
|
|
|
|
Dir.chdir(app_path) do
|
2012-03-08 17:32:41 -05:00
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string;
|
|
|
|
rake db:migrate`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
output = `rake db:migrate:status`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{3,}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:rollback STEP=2`
|
|
|
|
output = `rake db:migrate:status`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/down\s+\d{3,}\s+Create users/, output)
|
|
|
|
assert_match(/down\s+\d{3,}\s+Add email to users/, output)
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
`rake db:migrate:redo`
|
|
|
|
output = `rake db:migrate:status`
|
2012-02-17 10:56:57 -05:00
|
|
|
|
2012-03-08 17:32:41 -05:00
|
|
|
assert_match(/up\s+\d{3,}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{3,}\s+Add email to users/, output)
|
|
|
|
end
|
2012-02-17 10:56:57 -05:00
|
|
|
end
|
2014-02-05 02:32:38 -05:00
|
|
|
|
|
|
|
test 'schema generation when dump_schema_after_migration is set' do
|
|
|
|
add_to_config('config.active_record.dump_schema_after_migration = false')
|
|
|
|
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate`
|
|
|
|
|
|
|
|
assert !File.exist?("db/schema.rb")
|
|
|
|
end
|
|
|
|
|
|
|
|
add_to_config('config.active_record.dump_schema_after_migration = true')
|
|
|
|
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`rails generate model author name:string;
|
|
|
|
bundle exec rake db:migrate`
|
|
|
|
|
|
|
|
structure_dump = File.read("db/schema.rb")
|
|
|
|
assert_match(/create_table "authors"/, structure_dump)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'default schema generation after migration' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`rails generate model book title:string;
|
|
|
|
bundle exec rake db:migrate`
|
|
|
|
|
|
|
|
structure_dump = File.read("db/schema.rb")
|
|
|
|
assert_match(/create_table "books"/, structure_dump)
|
|
|
|
end
|
|
|
|
end
|
2014-06-27 11:28:12 -04:00
|
|
|
|
|
|
|
test 'test migration status migrated file is deleted' do
|
|
|
|
Dir.chdir(app_path) do
|
|
|
|
`rails generate model user username:string password:string;
|
|
|
|
rails generate migration add_email_to_users email:string;
|
|
|
|
rake db:migrate
|
|
|
|
rm db/migrate/*email*.rb`
|
|
|
|
|
|
|
|
output = `rake db:migrate:status`
|
|
|
|
File.write('test.txt', output)
|
|
|
|
|
|
|
|
assert_match(/up\s+\d{14}\s+Create users/, output)
|
|
|
|
assert_match(/up\s+\d{14}\s+\** NO FILE \**/, output)
|
|
|
|
end
|
|
|
|
end
|
2011-12-07 11:34:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|