Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate migrations at path set by config.paths["db/migrate"] #27674

Merged
merged 1 commit into from
Jan 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
* Place generated migrations into the path set by `config.paths["db/migrate"]`

*Kevin Glowacz*

* Raise `ActiveRecord::InvalidForeignKey` when a foreign key constraint fails on Sqlite3.

*Ryuta Kamizono*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class MigrationGenerator < Base # :nodoc:
def create_migration_file
set_local_assigns!
validate_file_name!
migration_template @migration_template, "db/migrate/#{file_name}.rb"
migration_template @migration_template, File.join(db_migrate_path, "#{file_name}.rb")
end

# TODO Change this to private once we've dropped Ruby 2.2 support.
Expand Down Expand Up @@ -71,6 +71,14 @@ def validate_file_name!
def normalize_table_name(_table_name)
pluralize_table_names? ? _table_name.pluralize : _table_name.singularize
end

def db_migrate_path
if defined?(Rails) && Rails.application
Rails.application.config.paths["db/migrate"].to_ary.first
else
"db/migrate"
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class ModelGenerator < Base # :nodoc:
def create_migration_file
return unless options[:migration] && options[:parent].nil?
attributes.each { |a| a.attr_options.delete(:index) if a.reference? && !a.has_index? } if options[:indexes] == false
migration_template "../../migration/templates/create_table_migration.rb", "db/migrate/create_#{table_name}.rb"
migration_template "../../migration/templates/create_table_migration.rb", File.join(db_migrate_path, "create_#{table_name}.rb")
end

def create_model_file
Expand Down Expand Up @@ -64,6 +64,14 @@ def application_record_file_name
"app/models/application_record.rb"
end
end

def db_migrate_path
if defined?(Rails) && Rails.application
Rails.application.config.paths["db/migrate"].to_ary.first
else
"db/migrate"
end
end
end
end
end
10 changes: 10 additions & 0 deletions railties/test/generators/migration_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,16 @@ def test_add_migration_with_token_option
end
end

def test_add_migration_to_configured_path
old_paths = Rails.application.config.paths["db/migrate"]
Rails.application.config.paths.add "db/migrate", with: "db2/migrate"

migration = "migration_in_custom_path"
run_generator [migration]
Rails.application.config.paths["db/migrate"] = old_paths
Copy link
Contributor

@simi simi Jan 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will be nice to wrap this into ensure block to cleanup changed setting also when something fails during this test.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simi thanks - fixed in a0e0505

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same problem is in model_generator_test. I just mentioned that only once here. Sorry :(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@simi np - fixed in 33eef3f

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

assert_migration "db2/migrate/#{migration}.rb", /.*/
end

private

def with_singular_table_name
Expand Down
10 changes: 10 additions & 0 deletions railties/test/generators/model_generator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,16 @@ def test_migration_without_timestamps
ActiveRecord::Base.timestamped_migrations = true
end

def test_migration_with_configured_path
old_paths = Rails.application.config.paths["db/migrate"]
Rails.application.config.paths.add "db/migrate", with: "db2/migrate"

run_generator

Rails.application.config.paths["db/migrate"] = old_paths
assert_migration "db2/migrate/create_accounts.rb", /class CreateAccounts < ActiveRecord::Migration\[[0-9.]+\]/
end

def test_model_with_references_attribute_generates_belongs_to_associations
run_generator ["product", "name:string", "supplier:references"]
assert_file "app/models/product.rb", /belongs_to :supplier/
Expand Down