activerecord 4.2.0 → 5.2.8.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of activerecord might be problematic. Click here for more details.
- checksums.yaml +5 -5
- data/CHANGELOG.md +640 -928
- data/MIT-LICENSE +2 -2
- data/README.rdoc +10 -11
- data/examples/performance.rb +32 -31
- data/examples/simple.rb +5 -4
- data/lib/active_record/aggregations.rb +264 -247
- data/lib/active_record/association_relation.rb +24 -6
- data/lib/active_record/associations/alias_tracker.rb +29 -35
- data/lib/active_record/associations/association.rb +87 -41
- data/lib/active_record/associations/association_scope.rb +106 -132
- data/lib/active_record/associations/belongs_to_association.rb +55 -36
- data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
- data/lib/active_record/associations/builder/association.rb +29 -38
- data/lib/active_record/associations/builder/belongs_to.rb +77 -30
- data/lib/active_record/associations/builder/collection_association.rb +14 -23
- data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +50 -39
- data/lib/active_record/associations/builder/has_many.rb +6 -4
- data/lib/active_record/associations/builder/has_one.rb +13 -6
- data/lib/active_record/associations/builder/singular_association.rb +15 -11
- data/lib/active_record/associations/collection_association.rb +145 -266
- data/lib/active_record/associations/collection_proxy.rb +242 -138
- data/lib/active_record/associations/foreign_association.rb +13 -0
- data/lib/active_record/associations/has_many_association.rb +35 -75
- data/lib/active_record/associations/has_many_through_association.rb +51 -69
- data/lib/active_record/associations/has_one_association.rb +39 -24
- data/lib/active_record/associations/has_one_through_association.rb +18 -9
- data/lib/active_record/associations/join_dependency/join_association.rb +40 -81
- data/lib/active_record/associations/join_dependency/join_base.rb +10 -9
- data/lib/active_record/associations/join_dependency/join_part.rb +12 -12
- data/lib/active_record/associations/join_dependency.rb +134 -154
- data/lib/active_record/associations/preloader/association.rb +85 -116
- data/lib/active_record/associations/preloader/through_association.rb +85 -74
- data/lib/active_record/associations/preloader.rb +83 -93
- data/lib/active_record/associations/singular_association.rb +27 -40
- data/lib/active_record/associations/through_association.rb +48 -23
- data/lib/active_record/associations.rb +1732 -1596
- data/lib/active_record/attribute_assignment.rb +58 -182
- data/lib/active_record/attribute_decorators.rb +39 -15
- data/lib/active_record/attribute_methods/before_type_cast.rb +12 -5
- data/lib/active_record/attribute_methods/dirty.rb +94 -125
- data/lib/active_record/attribute_methods/primary_key.rb +86 -71
- data/lib/active_record/attribute_methods/query.rb +4 -2
- data/lib/active_record/attribute_methods/read.rb +45 -63
- data/lib/active_record/attribute_methods/serialization.rb +40 -20
- data/lib/active_record/attribute_methods/time_zone_conversion.rb +62 -36
- data/lib/active_record/attribute_methods/write.rb +31 -46
- data/lib/active_record/attribute_methods.rb +170 -117
- data/lib/active_record/attributes.rb +201 -74
- data/lib/active_record/autosave_association.rb +118 -45
- data/lib/active_record/base.rb +60 -48
- data/lib/active_record/callbacks.rb +97 -57
- data/lib/active_record/coders/json.rb +3 -1
- data/lib/active_record/coders/yaml_column.rb +37 -13
- data/lib/active_record/collection_cache_key.rb +53 -0
- data/lib/active_record/connection_adapters/abstract/connection_pool.rb +712 -284
- data/lib/active_record/connection_adapters/abstract/database_limits.rb +10 -5
- data/lib/active_record/connection_adapters/abstract/database_statements.rb +254 -87
- data/lib/active_record/connection_adapters/abstract/query_cache.rb +72 -22
- data/lib/active_record/connection_adapters/abstract/quoting.rb +119 -52
- data/lib/active_record/connection_adapters/abstract/savepoints.rb +6 -4
- data/lib/active_record/connection_adapters/abstract/schema_creation.rb +67 -46
- data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +328 -217
- data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +81 -36
- data/lib/active_record/connection_adapters/abstract/schema_statements.rb +617 -212
- data/lib/active_record/connection_adapters/abstract/transaction.rb +139 -75
- data/lib/active_record/connection_adapters/abstract_adapter.rb +332 -191
- data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +567 -563
- data/lib/active_record/connection_adapters/column.rb +50 -41
- data/lib/active_record/connection_adapters/connection_specification.rb +147 -135
- data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +33 -0
- data/lib/active_record/connection_adapters/mysql/column.rb +27 -0
- data/lib/active_record/connection_adapters/mysql/database_statements.rb +140 -0
- data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +72 -0
- data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -0
- data/lib/active_record/connection_adapters/mysql/schema_creation.rb +73 -0
- data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +87 -0
- data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +80 -0
- data/lib/active_record/connection_adapters/mysql/schema_statements.rb +148 -0
- data/lib/active_record/connection_adapters/mysql/type_metadata.rb +35 -0
- data/lib/active_record/connection_adapters/mysql2_adapter.rb +42 -195
- data/lib/active_record/connection_adapters/postgresql/column.rb +35 -11
- data/lib/active_record/connection_adapters/postgresql/database_statements.rb +46 -115
- data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +44 -0
- data/lib/active_record/connection_adapters/postgresql/oid/array.rb +50 -57
- data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +10 -6
- data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +5 -2
- data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +5 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date.rb +13 -1
- data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +9 -13
- data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +7 -3
- data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +31 -19
- data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
- data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
- data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +45 -0
- data/lib/active_record/connection_adapters/postgresql/oid/money.rb +7 -9
- data/lib/active_record/connection_adapters/postgresql/oid/{integer.rb → oid.rb} +6 -2
- data/lib/active_record/connection_adapters/postgresql/oid/point.rb +33 -11
- data/lib/active_record/connection_adapters/postgresql/oid/range.rb +52 -34
- data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +4 -1
- data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +65 -51
- data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +5 -3
- data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +3 -1
- data/lib/active_record/connection_adapters/postgresql/oid.rb +23 -25
- data/lib/active_record/connection_adapters/postgresql/quoting.rb +107 -47
- data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +27 -14
- data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +65 -0
- data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +144 -90
- data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +50 -0
- data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +466 -280
- data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +39 -0
- data/lib/active_record/connection_adapters/postgresql/utils.rb +12 -8
- data/lib/active_record/connection_adapters/postgresql_adapter.rb +439 -330
- data/lib/active_record/connection_adapters/schema_cache.rb +48 -24
- data/lib/active_record/connection_adapters/sql_type_metadata.rb +34 -0
- data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +21 -0
- data/lib/active_record/connection_adapters/sqlite3/quoting.rb +67 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +17 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +19 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +18 -0
- data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +106 -0
- data/lib/active_record/connection_adapters/sqlite3_adapter.rb +269 -324
- data/lib/active_record/connection_adapters/statement_pool.rb +34 -13
- data/lib/active_record/connection_handling.rb +40 -27
- data/lib/active_record/core.rb +205 -202
- data/lib/active_record/counter_cache.rb +80 -37
- data/lib/active_record/define_callbacks.rb +22 -0
- data/lib/active_record/dynamic_matchers.rb +87 -105
- data/lib/active_record/enum.rb +136 -90
- data/lib/active_record/errors.rb +180 -52
- data/lib/active_record/explain.rb +23 -11
- data/lib/active_record/explain_registry.rb +4 -2
- data/lib/active_record/explain_subscriber.rb +11 -6
- data/lib/active_record/fixture_set/file.rb +35 -9
- data/lib/active_record/fixtures.rb +193 -135
- data/lib/active_record/gem_version.rb +5 -3
- data/lib/active_record/inheritance.rb +148 -112
- data/lib/active_record/integration.rb +70 -28
- data/lib/active_record/internal_metadata.rb +45 -0
- data/lib/active_record/legacy_yaml_adapter.rb +48 -0
- data/lib/active_record/locale/en.yml +3 -2
- data/lib/active_record/locking/optimistic.rb +92 -98
- data/lib/active_record/locking/pessimistic.rb +15 -3
- data/lib/active_record/log_subscriber.rb +95 -33
- data/lib/active_record/migration/command_recorder.rb +133 -90
- data/lib/active_record/migration/compatibility.rb +217 -0
- data/lib/active_record/migration/join_table.rb +8 -6
- data/lib/active_record/migration.rb +594 -267
- data/lib/active_record/model_schema.rb +292 -111
- data/lib/active_record/nested_attributes.rb +266 -214
- data/lib/active_record/no_touching.rb +8 -2
- data/lib/active_record/null_relation.rb +24 -37
- data/lib/active_record/persistence.rb +350 -119
- data/lib/active_record/query_cache.rb +13 -24
- data/lib/active_record/querying.rb +19 -17
- data/lib/active_record/railtie.rb +117 -35
- data/lib/active_record/railties/console_sandbox.rb +2 -0
- data/lib/active_record/railties/controller_runtime.rb +9 -3
- data/lib/active_record/railties/databases.rake +160 -174
- data/lib/active_record/readonly_attributes.rb +5 -4
- data/lib/active_record/reflection.rb +447 -288
- data/lib/active_record/relation/batches/batch_enumerator.rb +69 -0
- data/lib/active_record/relation/batches.rb +204 -55
- data/lib/active_record/relation/calculations.rb +259 -244
- data/lib/active_record/relation/delegation.rb +67 -60
- data/lib/active_record/relation/finder_methods.rb +290 -253
- data/lib/active_record/relation/from_clause.rb +26 -0
- data/lib/active_record/relation/merger.rb +91 -68
- data/lib/active_record/relation/predicate_builder/array_handler.rb +24 -23
- data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
- data/lib/active_record/relation/predicate_builder/base_handler.rb +19 -0
- data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +20 -0
- data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
- data/lib/active_record/relation/predicate_builder/range_handler.rb +42 -0
- data/lib/active_record/relation/predicate_builder/relation_handler.rb +7 -1
- data/lib/active_record/relation/predicate_builder.rb +118 -92
- data/lib/active_record/relation/query_attribute.rb +45 -0
- data/lib/active_record/relation/query_methods.rb +446 -389
- data/lib/active_record/relation/record_fetch_warning.rb +51 -0
- data/lib/active_record/relation/spawn_methods.rb +18 -16
- data/lib/active_record/relation/where_clause.rb +186 -0
- data/lib/active_record/relation/where_clause_factory.rb +34 -0
- data/lib/active_record/relation.rb +287 -339
- data/lib/active_record/result.rb +54 -36
- data/lib/active_record/runtime_registry.rb +6 -4
- data/lib/active_record/sanitization.rb +155 -124
- data/lib/active_record/schema.rb +30 -24
- data/lib/active_record/schema_dumper.rb +91 -87
- data/lib/active_record/schema_migration.rb +19 -19
- data/lib/active_record/scoping/default.rb +102 -84
- data/lib/active_record/scoping/named.rb +81 -32
- data/lib/active_record/scoping.rb +45 -26
- data/lib/active_record/secure_token.rb +40 -0
- data/lib/active_record/serialization.rb +5 -5
- data/lib/active_record/statement_cache.rb +45 -35
- data/lib/active_record/store.rb +42 -36
- data/lib/active_record/suppressor.rb +61 -0
- data/lib/active_record/table_metadata.rb +82 -0
- data/lib/active_record/tasks/database_tasks.rb +136 -95
- data/lib/active_record/tasks/mysql_database_tasks.rb +59 -89
- data/lib/active_record/tasks/postgresql_database_tasks.rb +84 -31
- data/lib/active_record/tasks/sqlite_database_tasks.rb +44 -16
- data/lib/active_record/timestamp.rb +70 -38
- data/lib/active_record/touch_later.rb +64 -0
- data/lib/active_record/transactions.rb +208 -123
- data/lib/active_record/translation.rb +2 -0
- data/lib/active_record/type/adapter_specific_registry.rb +136 -0
- data/lib/active_record/type/date.rb +4 -41
- data/lib/active_record/type/date_time.rb +4 -38
- data/lib/active_record/type/decimal_without_scale.rb +6 -2
- data/lib/active_record/type/hash_lookup_type_map.rb +13 -5
- data/lib/active_record/type/internal/timezone.rb +17 -0
- data/lib/active_record/type/json.rb +30 -0
- data/lib/active_record/type/serialized.rb +30 -15
- data/lib/active_record/type/text.rb +2 -2
- data/lib/active_record/type/time.rb +11 -16
- data/lib/active_record/type/type_map.rb +15 -17
- data/lib/active_record/type/unsigned_integer.rb +9 -7
- data/lib/active_record/type.rb +79 -23
- data/lib/active_record/type_caster/connection.rb +33 -0
- data/lib/active_record/type_caster/map.rb +23 -0
- data/lib/active_record/type_caster.rb +9 -0
- data/lib/active_record/validations/absence.rb +25 -0
- data/lib/active_record/validations/associated.rb +13 -4
- data/lib/active_record/validations/length.rb +26 -0
- data/lib/active_record/validations/presence.rb +14 -13
- data/lib/active_record/validations/uniqueness.rb +41 -32
- data/lib/active_record/validations.rb +38 -35
- data/lib/active_record/version.rb +3 -1
- data/lib/active_record.rb +36 -21
- data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
- data/lib/rails/generators/active_record/application_record/templates/application_record.rb.tt +5 -0
- data/lib/rails/generators/active_record/migration/migration_generator.rb +43 -35
- data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +8 -6
- data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +8 -7
- data/lib/rails/generators/active_record/migration.rb +18 -1
- data/lib/rails/generators/active_record/model/model_generator.rb +18 -22
- data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +3 -0
- data/lib/rails/generators/active_record.rb +7 -5
- metadata +77 -53
- data/lib/active_record/associations/preloader/belongs_to.rb +0 -17
- data/lib/active_record/associations/preloader/collection_association.rb +0 -24
- data/lib/active_record/associations/preloader/has_many.rb +0 -17
- data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
- data/lib/active_record/associations/preloader/has_one.rb +0 -23
- data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
- data/lib/active_record/associations/preloader/singular_association.rb +0 -21
- data/lib/active_record/attribute.rb +0 -149
- data/lib/active_record/attribute_set/builder.rb +0 -86
- data/lib/active_record/attribute_set.rb +0 -77
- data/lib/active_record/connection_adapters/mysql_adapter.rb +0 -491
- data/lib/active_record/connection_adapters/postgresql/array_parser.rb +0 -93
- data/lib/active_record/connection_adapters/postgresql/oid/float.rb +0 -21
- data/lib/active_record/connection_adapters/postgresql/oid/infinity.rb +0 -13
- data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -35
- data/lib/active_record/connection_adapters/postgresql/oid/time.rb +0 -11
- data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
- data/lib/active_record/serializers/xml_serializer.rb +0 -193
- data/lib/active_record/type/big_integer.rb +0 -13
- data/lib/active_record/type/binary.rb +0 -50
- data/lib/active_record/type/boolean.rb +0 -30
- data/lib/active_record/type/decimal.rb +0 -40
- data/lib/active_record/type/decorator.rb +0 -14
- data/lib/active_record/type/float.rb +0 -19
- data/lib/active_record/type/integer.rb +0 -55
- data/lib/active_record/type/mutable.rb +0 -16
- data/lib/active_record/type/numeric.rb +0 -36
- data/lib/active_record/type/string.rb +0 -36
- data/lib/active_record/type/time_value.rb +0 -38
- data/lib/active_record/type/value.rb +0 -101
- /data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
@@ -1,221 +1,235 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_record"
|
2
4
|
|
3
5
|
db_namespace = namespace :db do
|
4
|
-
|
6
|
+
desc "Set the environment value for the database"
|
7
|
+
task "environment:set" => :load_config do
|
8
|
+
ActiveRecord::InternalMetadata.create_table
|
9
|
+
ActiveRecord::InternalMetadata[:environment] = ActiveRecord::Base.connection.migration_context.current_environment
|
10
|
+
end
|
11
|
+
|
12
|
+
task check_protected_environments: :load_config do
|
13
|
+
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
14
|
+
end
|
15
|
+
|
16
|
+
task load_config: :environment do
|
5
17
|
ActiveRecord::Base.configurations = ActiveRecord::Tasks::DatabaseTasks.database_configuration || {}
|
6
18
|
ActiveRecord::Migrator.migrations_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
|
7
19
|
end
|
8
20
|
|
9
21
|
namespace :create do
|
10
|
-
task :
|
22
|
+
task all: :load_config do
|
11
23
|
ActiveRecord::Tasks::DatabaseTasks.create_all
|
12
24
|
end
|
13
25
|
end
|
14
26
|
|
15
|
-
desc
|
16
|
-
task :
|
27
|
+
desc "Creates the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:create:all to create all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to creating the development and test databases."
|
28
|
+
task create: [:load_config] do
|
17
29
|
ActiveRecord::Tasks::DatabaseTasks.create_current
|
18
30
|
end
|
19
31
|
|
20
32
|
namespace :drop do
|
21
|
-
task :
|
33
|
+
task all: [:load_config, :check_protected_environments] do
|
22
34
|
ActiveRecord::Tasks::DatabaseTasks.drop_all
|
23
35
|
end
|
24
36
|
end
|
25
37
|
|
26
|
-
desc
|
27
|
-
task :
|
38
|
+
desc "Drops the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV or when RAILS_ENV is development, it defaults to dropping the development and test databases."
|
39
|
+
task drop: [:load_config, :check_protected_environments] do
|
40
|
+
db_namespace["drop:_unsafe"].invoke
|
41
|
+
end
|
42
|
+
|
43
|
+
task "drop:_unsafe" => [:load_config] do
|
28
44
|
ActiveRecord::Tasks::DatabaseTasks.drop_current
|
29
45
|
end
|
30
46
|
|
31
47
|
namespace :purge do
|
32
|
-
task :
|
48
|
+
task all: [:load_config, :check_protected_environments] do
|
33
49
|
ActiveRecord::Tasks::DatabaseTasks.purge_all
|
34
50
|
end
|
35
51
|
end
|
36
52
|
|
37
|
-
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:
|
38
|
-
task :
|
53
|
+
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:purge:all to purge all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
|
54
|
+
task purge: [:load_config, :check_protected_environments] do
|
39
55
|
ActiveRecord::Tasks::DatabaseTasks.purge_current
|
40
56
|
end
|
41
57
|
|
42
58
|
desc "Migrate the database (options: VERSION=x, VERBOSE=false, SCOPE=blog)."
|
43
|
-
task
|
59
|
+
task migrate: :load_config do
|
44
60
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
45
|
-
db_namespace[
|
61
|
+
db_namespace["_dump"].invoke
|
46
62
|
end
|
47
63
|
|
64
|
+
# IMPORTANT: This task won't dump the schema if ActiveRecord::Base.dump_schema_after_migration is set to false
|
48
65
|
task :_dump do
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
66
|
+
if ActiveRecord::Base.dump_schema_after_migration
|
67
|
+
case ActiveRecord::Base.schema_format
|
68
|
+
when :ruby then db_namespace["schema:dump"].invoke
|
69
|
+
when :sql then db_namespace["structure:dump"].invoke
|
70
|
+
else
|
71
|
+
raise "unknown schema format #{ActiveRecord::Base.schema_format}"
|
72
|
+
end
|
54
73
|
end
|
55
74
|
# Allow this task to be called as many times as required. An example is the
|
56
75
|
# migrate:redo task, which calls other two internally that depend on this one.
|
57
|
-
db_namespace[
|
76
|
+
db_namespace["_dump"].reenable
|
58
77
|
end
|
59
78
|
|
60
79
|
namespace :migrate do
|
61
80
|
# desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
|
62
|
-
task
|
63
|
-
if ENV[
|
64
|
-
|
65
|
-
|
81
|
+
task redo: :load_config do
|
82
|
+
raise "Empty VERSION provided" if ENV["VERSION"] && ENV["VERSION"].empty?
|
83
|
+
|
84
|
+
if ENV["VERSION"]
|
85
|
+
db_namespace["migrate:down"].invoke
|
86
|
+
db_namespace["migrate:up"].invoke
|
66
87
|
else
|
67
|
-
db_namespace[
|
68
|
-
db_namespace[
|
88
|
+
db_namespace["rollback"].invoke
|
89
|
+
db_namespace["migrate"].invoke
|
69
90
|
end
|
70
91
|
end
|
71
92
|
|
72
93
|
# desc 'Resets your database using your migrations for the current environment'
|
73
|
-
task :
|
94
|
+
task reset: ["db:drop", "db:create", "db:migrate"]
|
74
95
|
|
75
96
|
# desc 'Runs the "up" for a given migration VERSION.'
|
76
|
-
task
|
77
|
-
|
78
|
-
|
79
|
-
ActiveRecord::
|
80
|
-
|
97
|
+
task up: :load_config do
|
98
|
+
raise "VERSION is required" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
99
|
+
|
100
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
101
|
+
|
102
|
+
ActiveRecord::Base.connection.migration_context.run(
|
103
|
+
:up,
|
104
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
105
|
+
)
|
106
|
+
db_namespace["_dump"].invoke
|
81
107
|
end
|
82
108
|
|
83
109
|
# desc 'Runs the "down" for a given migration VERSION.'
|
84
|
-
task
|
85
|
-
|
86
|
-
|
87
|
-
ActiveRecord::
|
88
|
-
|
110
|
+
task down: :load_config do
|
111
|
+
raise "VERSION is required - To go down one migration, use db:rollback" if !ENV["VERSION"] || ENV["VERSION"].empty?
|
112
|
+
|
113
|
+
ActiveRecord::Tasks::DatabaseTasks.check_target_version
|
114
|
+
|
115
|
+
ActiveRecord::Base.connection.migration_context.run(
|
116
|
+
:down,
|
117
|
+
ActiveRecord::Tasks::DatabaseTasks.target_version
|
118
|
+
)
|
119
|
+
db_namespace["_dump"].invoke
|
89
120
|
end
|
90
121
|
|
91
|
-
desc
|
92
|
-
task
|
122
|
+
desc "Display status of migrations"
|
123
|
+
task status: :load_config do
|
93
124
|
unless ActiveRecord::SchemaMigration.table_exists?
|
94
|
-
abort
|
125
|
+
abort "Schema migrations table does not exist yet."
|
95
126
|
end
|
96
|
-
db_list = ActiveRecord::SchemaMigration.normalized_versions
|
97
|
-
|
98
|
-
file_list =
|
99
|
-
ActiveRecord::Migrator.migrations_paths.flat_map do |path|
|
100
|
-
# match "20091231235959_some_name.rb" and "001_some_name.rb" pattern
|
101
|
-
Dir.foreach(path).grep(/^(\d{3,})_(.+)\.rb$/) do
|
102
|
-
version = ActiveRecord::SchemaMigration.normalize_migration_number($1)
|
103
|
-
status = db_list.delete(version) ? 'up' : 'down'
|
104
|
-
[status, version, $2.humanize]
|
105
|
-
end
|
106
|
-
end
|
107
127
|
|
108
|
-
db_list.map! do |version|
|
109
|
-
['up', version, '********** NO FILE **********']
|
110
|
-
end
|
111
128
|
# output
|
112
129
|
puts "\ndatabase: #{ActiveRecord::Base.connection_config[:database]}\n\n"
|
113
130
|
puts "#{'Status'.center(8)} #{'Migration ID'.ljust(14)} Migration Name"
|
114
131
|
puts "-" * 50
|
115
|
-
|
132
|
+
ActiveRecord::Base.connection.migration_context.migrations_status.each do |status, version, name|
|
116
133
|
puts "#{status.center(8)} #{version.ljust(14)} #{name}"
|
117
134
|
end
|
118
135
|
puts
|
119
136
|
end
|
120
137
|
end
|
121
138
|
|
122
|
-
desc
|
123
|
-
task
|
124
|
-
step = ENV[
|
125
|
-
ActiveRecord::
|
126
|
-
db_namespace[
|
139
|
+
desc "Rolls the schema back to the previous version (specify steps w/ STEP=n)."
|
140
|
+
task rollback: :load_config do
|
141
|
+
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
142
|
+
ActiveRecord::Base.connection.migration_context.rollback(step)
|
143
|
+
db_namespace["_dump"].invoke
|
127
144
|
end
|
128
145
|
|
129
146
|
# desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
|
130
|
-
task
|
131
|
-
step = ENV[
|
132
|
-
ActiveRecord::
|
133
|
-
db_namespace[
|
147
|
+
task forward: :load_config do
|
148
|
+
step = ENV["STEP"] ? ENV["STEP"].to_i : 1
|
149
|
+
ActiveRecord::Base.connection.migration_context.forward(step)
|
150
|
+
db_namespace["_dump"].invoke
|
134
151
|
end
|
135
152
|
|
136
153
|
# desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
|
137
|
-
task :
|
138
|
-
db_namespace["drop"].invoke
|
139
|
-
db_namespace["setup"].invoke
|
140
|
-
end
|
154
|
+
task reset: [ "db:drop", "db:setup" ]
|
141
155
|
|
142
156
|
# desc "Retrieves the charset for the current environment's database"
|
143
|
-
task
|
157
|
+
task charset: :load_config do
|
144
158
|
puts ActiveRecord::Tasks::DatabaseTasks.charset_current
|
145
159
|
end
|
146
160
|
|
147
161
|
# desc "Retrieves the collation for the current environment's database"
|
148
|
-
task
|
162
|
+
task collation: :load_config do
|
149
163
|
begin
|
150
164
|
puts ActiveRecord::Tasks::DatabaseTasks.collation_current
|
151
165
|
rescue NoMethodError
|
152
|
-
$stderr.puts
|
166
|
+
$stderr.puts "Sorry, your database adapter is not supported yet. Feel free to submit a patch."
|
153
167
|
end
|
154
168
|
end
|
155
169
|
|
156
|
-
desc
|
157
|
-
task
|
158
|
-
puts "Current version: #{ActiveRecord::
|
170
|
+
desc "Retrieves the current schema version number"
|
171
|
+
task version: :load_config do
|
172
|
+
puts "Current version: #{ActiveRecord::Base.connection.migration_context.current_version}"
|
159
173
|
end
|
160
174
|
|
161
175
|
# desc "Raises an error if there are pending migrations"
|
162
|
-
task :
|
163
|
-
pending_migrations = ActiveRecord::
|
176
|
+
task abort_if_pending_migrations: :load_config do
|
177
|
+
pending_migrations = ActiveRecord::Base.connection.migration_context.open.pending_migrations
|
164
178
|
|
165
179
|
if pending_migrations.any?
|
166
180
|
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
|
167
181
|
pending_migrations.each do |pending_migration|
|
168
|
-
puts
|
182
|
+
puts " %4d %s" % [pending_migration.version, pending_migration.name]
|
169
183
|
end
|
170
|
-
abort %{Run `
|
184
|
+
abort %{Run `rails db:migrate` to update your database then try again.}
|
171
185
|
end
|
172
186
|
end
|
173
187
|
|
174
|
-
desc
|
175
|
-
task :
|
188
|
+
desc "Creates the database, loads the schema, and initializes with the seed data (use db:reset to also drop the database first)"
|
189
|
+
task setup: ["db:schema:load_if_ruby", "db:structure:load_if_sql", :seed]
|
176
190
|
|
177
|
-
desc
|
191
|
+
desc "Loads the seed data from db/seeds.rb"
|
178
192
|
task :seed do
|
179
|
-
db_namespace[
|
193
|
+
db_namespace["abort_if_pending_migrations"].invoke
|
180
194
|
ActiveRecord::Tasks::DatabaseTasks.load_seed
|
181
195
|
end
|
182
196
|
|
183
197
|
namespace :fixtures do
|
184
|
-
desc "
|
185
|
-
task
|
186
|
-
require
|
198
|
+
desc "Loads fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y. Load from subdirectory in test/fixtures using FIXTURES_DIR=z. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
199
|
+
task load: :load_config do
|
200
|
+
require "active_record/fixtures"
|
187
201
|
|
188
202
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
189
203
|
|
190
|
-
fixtures_dir = if ENV[
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
204
|
+
fixtures_dir = if ENV["FIXTURES_DIR"]
|
205
|
+
File.join base_dir, ENV["FIXTURES_DIR"]
|
206
|
+
else
|
207
|
+
base_dir
|
208
|
+
end
|
195
209
|
|
196
|
-
fixture_files = if ENV[
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
210
|
+
fixture_files = if ENV["FIXTURES"]
|
211
|
+
ENV["FIXTURES"].split(",")
|
212
|
+
else
|
213
|
+
# The use of String#[] here is to support namespaced fixtures.
|
214
|
+
Dir["#{fixtures_dir}/**/*.yml"].map { |f| f[(fixtures_dir.size + 1)..-5] }
|
215
|
+
end
|
202
216
|
|
203
217
|
ActiveRecord::FixtureSet.create_fixtures(fixtures_dir, fixture_files)
|
204
218
|
end
|
205
219
|
|
206
220
|
# desc "Search for a fixture given a LABEL or ID. Specify an alternative path (eg. spec/fixtures) using FIXTURES_PATH=spec/fixtures."
|
207
|
-
task
|
208
|
-
require
|
221
|
+
task identify: :load_config do
|
222
|
+
require "active_record/fixtures"
|
209
223
|
|
210
|
-
label, id = ENV[
|
211
|
-
raise
|
224
|
+
label, id = ENV["LABEL"], ENV["ID"]
|
225
|
+
raise "LABEL or ID required" if label.blank? && id.blank?
|
212
226
|
|
213
227
|
puts %Q(The fixture ID for "#{label}" is #{ActiveRecord::FixtureSet.identify(label)}.) if label
|
214
228
|
|
215
229
|
base_dir = ActiveRecord::Tasks::DatabaseTasks.fixtures_path
|
216
230
|
|
217
231
|
Dir["#{base_dir}/**/*.yml"].each do |file|
|
218
|
-
if data = YAML
|
232
|
+
if data = YAML.load(ERB.new(IO.read(file)).result)
|
219
233
|
data.each_key do |key|
|
220
234
|
key_id = ActiveRecord::FixtureSet.identify(key)
|
221
235
|
|
@@ -229,97 +243,85 @@ db_namespace = namespace :db do
|
|
229
243
|
end
|
230
244
|
|
231
245
|
namespace :schema do
|
232
|
-
desc
|
233
|
-
task
|
234
|
-
require
|
235
|
-
filename = ENV[
|
246
|
+
desc "Creates a db/schema.rb file that is portable against any DB supported by Active Record"
|
247
|
+
task dump: :load_config do
|
248
|
+
require "active_record/schema_dumper"
|
249
|
+
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema.rb")
|
236
250
|
File.open(filename, "w:utf-8") do |file|
|
237
251
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
|
238
252
|
end
|
239
|
-
db_namespace[
|
253
|
+
db_namespace["schema:dump"].reenable
|
240
254
|
end
|
241
255
|
|
242
|
-
desc
|
243
|
-
task :
|
244
|
-
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV[
|
256
|
+
desc "Loads a schema.rb file into the database"
|
257
|
+
task load: [:load_config, :check_protected_environments] do
|
258
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV["SCHEMA"])
|
245
259
|
end
|
246
260
|
|
247
|
-
task :
|
261
|
+
task load_if_ruby: ["db:create", :environment] do
|
248
262
|
db_namespace["schema:load"].invoke if ActiveRecord::Base.schema_format == :ruby
|
249
263
|
end
|
250
264
|
|
251
265
|
namespace :cache do
|
252
|
-
desc
|
253
|
-
task
|
254
|
-
|
255
|
-
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.
|
256
|
-
|
257
|
-
con.schema_cache.clear!
|
258
|
-
con.tables.each { |table| con.schema_cache.add(table) }
|
259
|
-
open(filename, 'wb') { |f| f.write(Marshal.dump(con.schema_cache)) }
|
266
|
+
desc "Creates a db/schema_cache.yml file."
|
267
|
+
task dump: :load_config do
|
268
|
+
conn = ActiveRecord::Base.connection
|
269
|
+
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
|
270
|
+
ActiveRecord::Tasks::DatabaseTasks.dump_schema_cache(conn, filename)
|
260
271
|
end
|
261
272
|
|
262
|
-
desc
|
263
|
-
task
|
264
|
-
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.
|
265
|
-
|
273
|
+
desc "Clears a db/schema_cache.yml file."
|
274
|
+
task clear: :load_config do
|
275
|
+
filename = File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "schema_cache.yml")
|
276
|
+
rm_f filename, verbose: false
|
266
277
|
end
|
267
278
|
end
|
268
279
|
|
269
280
|
end
|
270
281
|
|
271
282
|
namespace :structure do
|
272
|
-
desc
|
273
|
-
task
|
274
|
-
filename = ENV[
|
283
|
+
desc "Dumps the database structure to db/structure.sql. Specify another file with SCHEMA=db/my_structure.sql"
|
284
|
+
task dump: :load_config do
|
285
|
+
filename = ENV["SCHEMA"] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, "structure.sql")
|
275
286
|
current_config = ActiveRecord::Tasks::DatabaseTasks.current_config
|
276
287
|
ActiveRecord::Tasks::DatabaseTasks.structure_dump(current_config, filename)
|
277
288
|
|
278
|
-
if ActiveRecord::
|
279
|
-
ActiveRecord::SchemaMigration.table_exists?
|
289
|
+
if ActiveRecord::SchemaMigration.table_exists?
|
280
290
|
File.open(filename, "a") do |f|
|
281
291
|
f.puts ActiveRecord::Base.connection.dump_schema_information
|
282
292
|
f.print "\n"
|
283
293
|
end
|
284
294
|
end
|
285
|
-
db_namespace[
|
295
|
+
db_namespace["structure:dump"].reenable
|
286
296
|
end
|
287
297
|
|
288
|
-
desc "
|
289
|
-
task :
|
290
|
-
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV[
|
298
|
+
desc "Recreates the databases from the structure.sql file"
|
299
|
+
task load: [:load_config, :check_protected_environments] do
|
300
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:sql, ENV["SCHEMA"])
|
291
301
|
end
|
292
302
|
|
293
|
-
task :
|
303
|
+
task load_if_sql: ["db:create", :environment] do
|
294
304
|
db_namespace["structure:load"].invoke if ActiveRecord::Base.schema_format == :sql
|
295
305
|
end
|
296
306
|
end
|
297
307
|
|
298
308
|
namespace :test do
|
299
|
-
|
300
|
-
task :deprecated do
|
301
|
-
Rake.application.top_level_tasks.grep(/^db:test:/).each do |task|
|
302
|
-
$stderr.puts "WARNING: #{task} is deprecated. The Rails test helper now maintains " \
|
303
|
-
"your test schema automatically, see the release notes for details."
|
304
|
-
end
|
305
|
-
end
|
306
|
-
|
307
309
|
# desc "Recreate the test database from the current schema"
|
308
|
-
task :
|
310
|
+
task load: %w(db:test:purge) do
|
309
311
|
case ActiveRecord::Base.schema_format
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
312
|
+
when :ruby
|
313
|
+
db_namespace["test:load_schema"].invoke
|
314
|
+
when :sql
|
315
|
+
db_namespace["test:load_structure"].invoke
|
314
316
|
end
|
315
317
|
end
|
316
318
|
|
317
319
|
# desc "Recreate the test database from an existent schema.rb file"
|
318
|
-
task :
|
320
|
+
task load_schema: %w(db:test:purge) do
|
319
321
|
begin
|
320
322
|
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
|
321
323
|
ActiveRecord::Schema.verbose = false
|
322
|
-
ActiveRecord::Tasks::DatabaseTasks.
|
324
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :ruby, ENV["SCHEMA"], "test"
|
323
325
|
ensure
|
324
326
|
if should_reconnect
|
325
327
|
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ActiveRecord::Tasks::DatabaseTasks.env])
|
@@ -328,35 +330,19 @@ db_namespace = namespace :db do
|
|
328
330
|
end
|
329
331
|
|
330
332
|
# desc "Recreate the test database from an existent structure.sql file"
|
331
|
-
task :
|
332
|
-
ActiveRecord::Tasks::DatabaseTasks.
|
333
|
-
end
|
334
|
-
|
335
|
-
# desc "Recreate the test database from a fresh schema"
|
336
|
-
task :clone => %w(db:test:deprecated environment) do
|
337
|
-
case ActiveRecord::Base.schema_format
|
338
|
-
when :ruby
|
339
|
-
db_namespace["test:clone_schema"].invoke
|
340
|
-
when :sql
|
341
|
-
db_namespace["test:clone_structure"].invoke
|
342
|
-
end
|
333
|
+
task load_structure: %w(db:test:purge) do
|
334
|
+
ActiveRecord::Tasks::DatabaseTasks.load_schema ActiveRecord::Base.configurations["test"], :sql, ENV["SCHEMA"], "test"
|
343
335
|
end
|
344
336
|
|
345
|
-
# desc "Recreate the test database from a fresh schema.rb file"
|
346
|
-
task :clone_schema => %w(db:test:deprecated db:schema:dump db:test:load_schema)
|
347
|
-
|
348
|
-
# desc "Recreate the test database from a fresh structure.sql file"
|
349
|
-
task :clone_structure => %w(db:test:deprecated db:structure:dump db:test:load_structure)
|
350
|
-
|
351
337
|
# desc "Empty the test database"
|
352
|
-
task :
|
353
|
-
ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations[
|
338
|
+
task purge: %w(load_config check_protected_environments) do
|
339
|
+
ActiveRecord::Tasks::DatabaseTasks.purge ActiveRecord::Base.configurations["test"]
|
354
340
|
end
|
355
341
|
|
356
|
-
# desc '
|
357
|
-
task :
|
342
|
+
# desc 'Load the test schema'
|
343
|
+
task prepare: :load_config do
|
358
344
|
unless ActiveRecord::Base.configurations.blank?
|
359
|
-
db_namespace[
|
345
|
+
db_namespace["test:load"].invoke
|
360
346
|
end
|
361
347
|
end
|
362
348
|
end
|
@@ -365,13 +351,13 @@ end
|
|
365
351
|
namespace :railties do
|
366
352
|
namespace :install do
|
367
353
|
# desc "Copies missing migrations from Railties (e.g. engines). You can specify Railties to use with FROM=railtie1,railtie2"
|
368
|
-
task :
|
369
|
-
to_load = ENV[
|
354
|
+
task migrations: :'db:load_config' do
|
355
|
+
to_load = ENV["FROM"].blank? ? :all : ENV["FROM"].split(",").map(&:strip)
|
370
356
|
railties = {}
|
371
357
|
Rails.application.migration_railties.each do |railtie|
|
372
358
|
next unless to_load == :all || to_load.include?(railtie.railtie_name)
|
373
359
|
|
374
|
-
if railtie.respond_to?(:paths) && (path = railtie.paths[
|
360
|
+
if railtie.respond_to?(:paths) && (path = railtie.paths["db/migrate"].first)
|
375
361
|
railties[railtie.railtie_name] = path
|
376
362
|
end
|
377
363
|
end
|
@@ -384,8 +370,8 @@ namespace :railties do
|
|
384
370
|
puts "Copied migration #{migration.basename} from #{name}"
|
385
371
|
end
|
386
372
|
|
387
|
-
ActiveRecord::Migration.copy(ActiveRecord::
|
388
|
-
:
|
373
|
+
ActiveRecord::Migration.copy(ActiveRecord::Tasks::DatabaseTasks.migrations_paths.first, railties,
|
374
|
+
on_skip: on_skip, on_copy: on_copy)
|
389
375
|
end
|
390
376
|
end
|
391
377
|
end
|
@@ -1,22 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module ActiveRecord
|
2
4
|
module ReadonlyAttributes
|
3
5
|
extend ActiveSupport::Concern
|
4
6
|
|
5
7
|
included do
|
6
|
-
class_attribute :_attr_readonly, instance_accessor: false
|
7
|
-
self._attr_readonly = []
|
8
|
+
class_attribute :_attr_readonly, instance_accessor: false, default: []
|
8
9
|
end
|
9
10
|
|
10
11
|
module ClassMethods
|
11
12
|
# Attributes listed as readonly will be used to create a new record but update operations will
|
12
13
|
# ignore these fields.
|
13
14
|
def attr_readonly(*attributes)
|
14
|
-
self._attr_readonly = Set.new(attributes.map
|
15
|
+
self._attr_readonly = Set.new(attributes.map(&:to_s)) + (_attr_readonly || [])
|
15
16
|
end
|
16
17
|
|
17
18
|
# Returns an array of all the attributes that have been specified as readonly.
|
18
19
|
def readonly_attributes
|
19
|
-
|
20
|
+
_attr_readonly
|
20
21
|
end
|
21
22
|
end
|
22
23
|
end
|