2009-06-28 07:16:14 -04:00
|
|
|
require 'generators/generators_test_helper'
|
2010-03-23 08:40:19 -04:00
|
|
|
require 'rails/generators/rails/migration/migration_generator'
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-18 18:07:11 -05:00
|
|
|
class MigrationGeneratorTest < Rails::Generators::TestCase
|
|
|
|
include GeneratorsTestHelper
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_migration
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "change_title_body_from_posts"
|
|
|
|
run_generator [migration]
|
2015-12-05 15:13:38 -05:00
|
|
|
assert_migration "db/migrate/#{migration}.rb", /class ChangeTitleBodyFromPosts < ActiveRecord::Migration\[[0-9.]+\]/
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
|
2010-04-16 01:31:15 -04:00
|
|
|
def test_migrations_generated_simultaneously
|
|
|
|
migrations = ["change_title_body_from_posts", "change_email_from_comments"]
|
|
|
|
|
|
|
|
first_migration_number, second_migration_number = migrations.collect do |migration|
|
|
|
|
run_generator [migration]
|
|
|
|
file_name = migration_file_name "db/migrate/#{migration}.rb"
|
|
|
|
|
|
|
|
File.basename(file_name).split('_').first
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_not_equal first_migration_number, second_migration_number
|
|
|
|
end
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_migration_with_class_name
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "ChangeTitleBodyFromPosts"
|
|
|
|
run_generator [migration]
|
2015-12-05 15:13:38 -05:00
|
|
|
assert_migration "db/migrate/change_title_body_from_posts.rb", /class #{migration} < ActiveRecord::Migration\[[0-9.]+\]/
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
2012-12-18 19:42:01 -05:00
|
|
|
|
2012-08-22 06:50:41 -04:00
|
|
|
def test_migration_with_invalid_file_name
|
|
|
|
migration = "add_something:datetime"
|
|
|
|
assert_raise ActiveRecord::IllegalMigrationNameError do
|
|
|
|
run_generator [migration]
|
|
|
|
end
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
|
|
|
|
def test_add_migration_with_attributes
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "add_title_body_to_posts"
|
|
|
|
run_generator [migration, "title:string", "body:text"]
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :posts, :title, :string/, change)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, change)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-20 21:20:35 -04:00
|
|
|
def test_remove_migration_with_indexed_attribute
|
|
|
|
migration = "remove_title_body_from_posts"
|
|
|
|
run_generator [migration, "title:string:index", "body:text"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:43:33 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/remove_column :posts, :title, :string/, change)
|
|
|
|
assert_match(/remove_column :posts, :body, :text/, change)
|
|
|
|
assert_match(/remove_index :posts, :title/, change)
|
2012-03-20 21:20:35 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-06-28 07:16:14 -04:00
|
|
|
def test_remove_migration_with_attributes
|
2010-01-03 10:34:32 -05:00
|
|
|
migration = "remove_title_body_from_posts"
|
|
|
|
run_generator [migration, "title:string", "body:text"]
|
2009-06-28 07:16:14 -04:00
|
|
|
|
2010-01-03 10:34:32 -05:00
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:43:33 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/remove_column :posts, :title, :string/, change)
|
|
|
|
assert_match(/remove_column :posts, :body, :text/, change)
|
2009-06-28 13:46:34 -04:00
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|
|
|
|
end
|
2010-06-19 22:08:06 -04:00
|
|
|
|
2012-07-08 09:01:49 -04:00
|
|
|
def test_remove_migration_with_references_options
|
|
|
|
migration = "remove_references_from_books"
|
|
|
|
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:43:33 -05:00
|
|
|
assert_method :change, content do |change|
|
2016-01-24 06:16:12 -05:00
|
|
|
assert_match(/remove_reference :books, :author/, change)
|
|
|
|
assert_match(/remove_reference :books, :distributor, polymorphic: true/, change)
|
2012-07-08 09:01:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-24 21:25:15 -05:00
|
|
|
def test_remove_migration_with_references_removes_foreign_keys
|
|
|
|
migration = "remove_references_from_books"
|
|
|
|
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
2014-12-22 16:05:35 -05:00
|
|
|
assert_match(/remove_reference :books, :author,.*\sforeign_key: true/, change)
|
|
|
|
assert_match(/remove_reference :books, :distributor/, change) # sanity check
|
|
|
|
assert_no_match(/remove_reference :books, :distributor,.*\sforeign_key: true/, change)
|
2014-11-24 21:25:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-17 05:16:04 -04:00
|
|
|
def test_add_migration_with_attributes_and_indices
|
|
|
|
migration = "add_title_with_index_and_body_to_posts"
|
2011-12-24 04:38:19 -05:00
|
|
|
run_generator [migration, "title:string:index", "body:text", "user_id:integer:uniq"]
|
2011-08-17 05:16:04 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :posts, :title, :string/, change)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, change)
|
|
|
|
assert_match(/add_column :posts, :user_id, :integer/, change)
|
|
|
|
assert_match(/add_index :posts, :title/, change)
|
|
|
|
assert_match(/add_index :posts, :user_id, unique: true/, change)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_attributes_and_wrong_index_declaration
|
|
|
|
migration = "add_title_and_content_to_books"
|
|
|
|
run_generator [migration, "title:string:inex", "content:text", "user_id:integer:unik"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :books, :title, :string/, change)
|
|
|
|
assert_match(/add_column :books, :content, :text/, change)
|
|
|
|
assert_match(/add_column :books, :user_id, :integer/, change)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
2011-12-24 11:04:32 -05:00
|
|
|
assert_no_match(/add_index :books, :title/, content)
|
|
|
|
assert_no_match(/add_index :books, :user_id/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_attributes_without_type_and_index
|
|
|
|
migration = "add_title_with_index_and_body_to_posts"
|
|
|
|
run_generator [migration, "title:index", "body:text", "user_uuid:uniq"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :posts, :title, :string/, change)
|
|
|
|
assert_match(/add_column :posts, :body, :text/, change)
|
|
|
|
assert_match(/add_column :posts, :user_uuid, :string/, change)
|
|
|
|
assert_match(/add_index :posts, :title/, change)
|
|
|
|
assert_match(/add_index :posts, :user_uuid, unique: true/, change)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_attributes_index_declaration_and_attribute_options
|
|
|
|
migration = "add_title_and_content_to_books"
|
2012-01-22 09:45:18 -05:00
|
|
|
run_generator [migration, "title:string{40}:index", "content:string{255}", "price:decimal{1,2}:index", "discount:decimal{3.4}:uniq"]
|
2011-08-17 05:16:04 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :books, :title, :string, limit: 40/, change)
|
|
|
|
assert_match(/add_column :books, :content, :string, limit: 255/, change)
|
|
|
|
assert_match(/add_column :books, :price, :decimal, precision: 1, scale: 2/, change)
|
|
|
|
assert_match(/add_column :books, :discount, :decimal, precision: 3, scale: 4/, change)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
assert_match(/add_index :books, :title/, content)
|
|
|
|
assert_match(/add_index :books, :price/, content)
|
2011-12-24 16:06:25 -05:00
|
|
|
assert_match(/add_index :books, :discount, unique: true/, content)
|
2011-08-17 05:16:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-08 09:01:49 -04:00
|
|
|
def test_add_migration_with_references_options
|
|
|
|
migration = "add_references_to_books"
|
|
|
|
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
2016-01-24 06:16:12 -05:00
|
|
|
assert_match(/add_reference :books, :author/, change)
|
|
|
|
assert_match(/add_reference :books, :distributor, polymorphic: true/, change)
|
2012-07-08 09:01:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-07-04 17:16:00 -04:00
|
|
|
def test_add_migration_with_required_references
|
|
|
|
migration = "add_references_to_books"
|
|
|
|
run_generator [migration, "author:belongs_to{required}", "distributor:references{polymorphic,required}"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
2016-01-24 06:16:12 -05:00
|
|
|
assert_match(/add_reference :books, :author, null: false/, change)
|
|
|
|
assert_match(/add_reference :books, :distributor, polymorphic: true, null: false/, change)
|
2014-07-04 17:16:00 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-11-24 21:25:15 -05:00
|
|
|
def test_add_migration_with_references_adds_foreign_keys
|
|
|
|
migration = "add_references_to_books"
|
|
|
|
run_generator [migration, "author:belongs_to", "distributor:references{polymorphic}"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
Use the new `foreign_key` option on `references` in generators
Changes `rails g model Post user:references` from
def change
create_table :posts do |t|
t.references :user, index: true
end
add_foreign_key :posts, :users
end
to
def change
create_table :posts do |t|
t.references :user, index: true, foreign_key: true
end
end
Changes `rails g migration add_user_to_posts user:references` from
def change
add_reference :posts, :users, index: true
add_foreign_key :posts, :users
end
to
def change
add_reference :posts, :users, index: true, foreign_key: true
end
2014-12-22 15:36:20 -05:00
|
|
|
assert_match(/add_reference :books, :author,.*\sforeign_key: true/, change)
|
|
|
|
assert_match(/add_reference :books, :distributor/, change) # sanity check
|
|
|
|
assert_no_match(/add_reference :books, :distributor,.*\sforeign_key: true/, change)
|
2014-11-24 21:25:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-10 17:38:21 -04:00
|
|
|
def test_create_join_table_migration
|
|
|
|
migration = "add_media_join_table"
|
2012-07-18 16:24:21 -04:00
|
|
|
run_generator [migration, "artist_id", "musics:uniq"]
|
2012-07-10 17:38:21 -04:00
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:42:01 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_join_table :artists, :musics/, change)
|
|
|
|
assert_match(/# t.index \[:artist_id, :music_id\]/, change)
|
|
|
|
assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, change)
|
2012-07-10 17:38:21 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-26 14:02:28 -05:00
|
|
|
def test_create_table_migration
|
|
|
|
run_generator ["create_books", "title:string", "content:text"]
|
|
|
|
assert_migration "db/migrate/create_books.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_table :books/, change)
|
|
|
|
assert_match(/ t\.string :title/, change)
|
|
|
|
assert_match(/ t\.text :content/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-09-25 00:57:40 -04:00
|
|
|
def test_add_uuid_to_create_table_migration
|
2015-10-23 19:30:26 -04:00
|
|
|
run_generator ["create_books", "--primary_key_type=uuid"]
|
2015-09-25 00:57:40 -04:00
|
|
|
assert_migration "db/migrate/create_books.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_table :books, id: :uuid/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-26 14:02:28 -05:00
|
|
|
def test_should_create_empty_migrations_if_name_not_start_with_add_or_remove_or_create
|
|
|
|
migration = "delete_books"
|
2010-06-19 22:08:06 -04:00
|
|
|
run_generator [migration, "title:string", "content:text"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
2012-12-18 19:43:33 -05:00
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/^\s*$/, change)
|
2010-06-19 22:08:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2014-11-24 21:25:15 -05:00
|
|
|
|
2012-03-31 19:12:49 -04:00
|
|
|
def test_properly_identifies_usage_file
|
|
|
|
assert generator_class.send(:usage_path)
|
|
|
|
end
|
2014-03-25 10:57:37 -04:00
|
|
|
|
|
|
|
def test_migration_with_singular_table_name
|
|
|
|
with_singular_table_name do
|
|
|
|
migration = "add_title_body_to_post"
|
|
|
|
run_generator [migration, 'title:string']
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :post, :title, :string/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_join_table_migration_with_singular_table_name
|
|
|
|
with_singular_table_name do
|
|
|
|
migration = "add_media_join_table"
|
|
|
|
run_generator [migration, "artist_id", "music:uniq"]
|
|
|
|
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_join_table :artist, :music/, change)
|
|
|
|
assert_match(/# t.index \[:artist_id, :music_id\]/, change)
|
|
|
|
assert_match(/ t.index \[:music_id, :artist_id\], unique: true/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create_table_migration_with_singular_table_name
|
|
|
|
with_singular_table_name do
|
|
|
|
run_generator ["create_book", "title:string", "content:text"]
|
|
|
|
assert_migration "db/migrate/create_book.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_table :book/, change)
|
|
|
|
assert_match(/ t\.string :title/, change)
|
|
|
|
assert_match(/ t\.text :content/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-11 14:06:41 -05:00
|
|
|
def test_create_table_migration_with_token_option
|
|
|
|
run_generator ["create_users", "token:token", "auth_token:token"]
|
|
|
|
assert_migration "db/migrate/create_users.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/create_table :users/, change)
|
|
|
|
assert_match(/ t\.string :token/, change)
|
|
|
|
assert_match(/ t\.string :auth_token/, change)
|
|
|
|
assert_match(/add_index :users, :token, unique: true/, change)
|
|
|
|
assert_match(/add_index :users, :auth_token, unique: true/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_add_migration_with_token_option
|
|
|
|
migration = "add_token_to_users"
|
|
|
|
run_generator [migration, "auth_token:token"]
|
|
|
|
assert_migration "db/migrate/#{migration}.rb" do |content|
|
|
|
|
assert_method :change, content do |change|
|
|
|
|
assert_match(/add_column :users, :auth_token, :string/, change)
|
|
|
|
assert_match(/add_index :users, :auth_token, unique: true/, change)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-03-25 10:57:37 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def with_singular_table_name
|
|
|
|
old_state = ActiveRecord::Base.pluralize_table_names
|
|
|
|
ActiveRecord::Base.pluralize_table_names = false
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
ActiveRecord::Base.pluralize_table_names = old_state
|
|
|
|
end
|
2009-06-28 07:16:14 -04:00
|
|
|
end
|