Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4775d3d

Browse files
committed
Refactor migrations_path command option to database
1 parent7d89337 commit4775d3d

File tree

11 files changed

+83
-19
lines changed

11 files changed

+83
-19
lines changed

‎activerecord/lib/active_record/database_configurations/database_config.rb‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def replica?
1717
raiseNotImplementedError
1818
end
1919

20+
defmigrations_paths
21+
raiseNotImplementedError
22+
end
23+
2024
defurl_config?
2125
false
2226
end

‎activerecord/lib/active_record/database_configurations/hash_config.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ def initialize(env_name, spec_name, config)
3838
defreplica?
3939
config["replica"]
4040
end
41+
42+
# The migrations paths for a database configuration. If the
43+
# `migrations_paths` key is present in the config, `migrations_paths`
44+
# will return its value.
45+
defmigrations_paths
46+
config["migrations_paths"]
47+
end
4148
end
4249
end
4350
end

‎activerecord/lib/active_record/database_configurations/url_config.rb‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ def replica?
4848
config["replica"]
4949
end
5050

51+
# The migrations paths for a database configuration. If the
52+
# `migrations_paths` key is present in the config, `migrations_paths`
53+
# will return its value.
54+
defmigrations_paths
55+
config["migrations_paths"]
56+
end
57+
5158
private
5259
defbuild_config(original_config,url)
5360
if/^jdbc:/.match?(url)

‎activerecord/lib/rails/generators/active_record/migration.rb‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,25 @@ def primary_key_type
2424
end
2525

2626
defdb_migrate_path
27-
ifmigrations_paths=options[:migrations_paths]
28-
migrations_paths
29-
elsifdefined?(Rails.application) &&Rails.application
30-
Rails.application.config.paths["db/migrate"].to_ary.first
27+
ifdefined?(Rails.application) &&Rails.application
28+
configured_migrate_path ||default_migrate_path
3129
else
3230
"db/migrate"
3331
end
3432
end
33+
34+
defdefault_migrate_path
35+
Rails.application.config.paths["db/migrate"].to_ary.first
36+
end
37+
38+
defconfigured_migrate_path
39+
returnunlessdatabase=options[:database]
40+
config=ActiveRecord::Base.configurations.configs_for(
41+
env_name:Rails.env,
42+
spec_name:database,
43+
)
44+
config&.migrations_paths
45+
end
3546
end
3647
end
3748
end

‎activerecord/lib/rails/generators/active_record/migration/migration_generator.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class MigrationGenerator < Base # :nodoc:
88
argument:attributes,type::array,default:[],banner:"field[:type][:index] field[:type][:index]"
99

1010
class_option:primary_key_type,type::string,desc:"The type for primary key"
11-
class_option:migrations_paths,type::string,desc:"Themigration pathfor yourgenerated migrations. If this is not set it will default to db/migrate"
11+
class_option:database,type::string,aliases:%i(db),desc:"Thedatabasefor yourmigration. By default, the current environment's primary database is used."
1212

1313
defcreate_migration_file
1414
set_local_assigns!

‎activerecord/lib/rails/generators/active_record/model/model_generator.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ModelGenerator < Base # :nodoc:
1414
class_option:parent,type::string,desc:"The parent class for the generated model"
1515
class_option:indexes,type::boolean,default:true,desc:"Add indexes for references and belongs_to columns"
1616
class_option:primary_key_type,type::string,desc:"The type for primary key"
17-
class_option:migrations_paths,type::string,desc:"Themigration pathfor yourgenerated migrations. If this is not set it will default to db/migrate"
17+
class_option:database,type::string,aliases:%i(db),desc:"Thedatabasefor yourmodel's migration. By default, the current environment's primary database is used."
1818

1919
# creates the migration file for the model.
2020
defcreate_migration_file

‎railties/CHANGELOG.md‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
* Refactors`migrations_paths` command option in generators
2+
to`database` (aliased as`db`). Now, the migrations paths
3+
will be read from the specified database configuration in the
4+
current environment.
5+
6+
```
7+
bin/rails g model Chair brand:string --database=kingston
8+
invoke active_record
9+
create db/kingston_migrate/20180830151055_create_chairs.rb
10+
```
11+
12+
`--database` can be used with the migration, model, and scaffold generators.
13+
14+
*Gannon McGibbon*
15+
116
* Adds an option to the model generator to allow setting the
217
migrations paths for that migration. This is useful for
318
applications that use multiple databases and put migrations

‎railties/test/generators/generators_test_helper.rb‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ def self.included(base)
4242
end
4343
end
4444

45+
defwith_secondary_database_configuration
46+
ActiveRecord::Base.configurations={
47+
test:{
48+
secondary:{
49+
database:"db/secondary.sqlite3",
50+
migrations_paths:"db/secondary_migrate",
51+
},
52+
},
53+
}
54+
yield
55+
ensure
56+
ActiveRecord::Base.configurations={}
57+
end
58+
4559
defcopy_routes
4660
routes=File.expand_path("../../lib/rails/generators/rails/app/templates/config/routes.rb.tt",__dir__)
4761
destination=File.join(destination_root,"config")

‎railties/test/generators/migration_generator_test.rb‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,13 @@ def test_add_uuid_to_create_table_migration
254254
end
255255
end
256256

257-
deftest_migrations_paths_puts_migrations_in_that_folder
258-
run_generator["create_books","--migrations_paths=db/test_migrate"]
259-
assert_migration"db/test_migrate/create_books.rb"do |content|
260-
assert_method:change,contentdo |change|
261-
assert_match(/create_table :books/,change)
257+
deftest_database_puts_migrations_in_configured_folder
258+
with_secondary_database_configurationdo
259+
run_generator["create_books","--database=secondary"]
260+
assert_migration"db/secondary_migrate/create_books.rb"do |content|
261+
assert_method:change,contentdo |change|
262+
assert_match(/create_table :books/,change)
263+
end
262264
end
263265
end
264266
end

‎railties/test/generators/model_generator_test.rb‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -392,11 +392,13 @@ def test_add_uuid_to_create_table_migration
392392
end
393393
end
394394

395-
deftest_migrations_paths_puts_migrations_in_that_folder
396-
run_generator["account","--migrations_paths=db/test_migrate"]
397-
assert_migration"db/test_migrate/create_accounts.rb"do |content|
398-
assert_method:change,contentdo |change|
399-
assert_match(/create_table :accounts/,change)
395+
deftest_database_puts_migrations_in_configured_folder
396+
with_secondary_database_configurationdo
397+
run_generator["account","--database=secondary"]
398+
assert_migration"db/secondary_migrate/create_accounts.rb"do |content|
399+
assert_method:change,contentdo |change|
400+
assert_match(/create_table :accounts/,change)
401+
end
400402
end
401403
end
402404
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp