ibm_db 3.0.4 → 5.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/CHANGES +8 -1
- data/LICENSE +1 -1
- data/ParameterizedQueries README +6 -6
- data/README +38 -55
- data/ext/Makefile +266 -0
- data/ext/extconf.rb +34 -3
- data/ext/gil_release_version +3 -0
- data/ext/ibm_db.c +106 -111
- data/ext/ibm_db.o +0 -0
- data/ext/ibm_db.so +0 -0
- data/ext/mkmf.log +103 -0
- data/ext/ruby_ibm_db_cli.o +0 -0
- data/ext/unicode_support_version +3 -0
- data/lib/active_record/connection_adapters/ibm_db_adapter.rb +911 -527
- data/lib/active_record/connection_adapters/ibmdb_adapter.rb +4 -1
- data/test/active_record/connection_adapters/fake_adapter.rb +8 -5
- data/test/cases/adapter_test.rb +148 -58
- data/test/cases/adapters/mysql2/active_schema_test.rb +193 -0
- data/test/cases/adapters/mysql2/bind_parameter_test.rb +50 -0
- data/test/cases/adapters/mysql2/boolean_test.rb +100 -0
- data/test/cases/adapters/mysql2/case_sensitivity_test.rb +63 -0
- data/test/cases/adapters/mysql2/charset_collation_test.rb +54 -0
- data/test/cases/adapters/mysql2/connection_test.rb +210 -0
- data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +45 -0
- data/test/cases/adapters/mysql2/enum_test.rb +26 -0
- data/test/cases/adapters/mysql2/explain_test.rb +21 -0
- data/test/cases/adapters/mysql2/json_test.rb +195 -0
- data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +83 -0
- data/test/cases/adapters/mysql2/reserved_word_test.rb +152 -0
- data/test/cases/adapters/mysql2/schema_migrations_test.rb +59 -0
- data/test/cases/adapters/mysql2/schema_test.rb +126 -0
- data/test/cases/adapters/mysql2/sp_test.rb +36 -0
- data/test/cases/adapters/mysql2/sql_types_test.rb +14 -0
- data/test/cases/adapters/mysql2/table_options_test.rb +42 -0
- data/test/cases/adapters/mysql2/unsigned_type_test.rb +66 -0
- data/test/cases/adapters/postgresql/active_schema_test.rb +98 -0
- data/test/cases/adapters/postgresql/array_test.rb +339 -0
- data/test/cases/adapters/postgresql/bit_string_test.rb +82 -0
- data/test/cases/adapters/postgresql/bytea_test.rb +134 -0
- data/test/cases/adapters/postgresql/case_insensitive_test.rb +26 -0
- data/test/cases/adapters/postgresql/change_schema_test.rb +38 -0
- data/test/cases/adapters/postgresql/cidr_test.rb +25 -0
- data/test/cases/adapters/postgresql/citext_test.rb +78 -0
- data/test/cases/adapters/postgresql/collation_test.rb +53 -0
- data/test/cases/adapters/postgresql/composite_test.rb +132 -0
- data/test/cases/adapters/postgresql/connection_test.rb +257 -0
- data/test/cases/adapters/postgresql/datatype_test.rb +92 -0
- data/test/cases/adapters/postgresql/domain_test.rb +47 -0
- data/test/cases/adapters/postgresql/enum_test.rb +91 -0
- data/test/cases/adapters/postgresql/explain_test.rb +20 -0
- data/test/cases/adapters/postgresql/extension_migration_test.rb +63 -0
- data/test/cases/adapters/postgresql/full_text_test.rb +44 -0
- data/test/cases/adapters/postgresql/geometric_test.rb +378 -0
- data/test/cases/adapters/postgresql/hstore_test.rb +382 -0
- data/test/cases/adapters/postgresql/infinity_test.rb +69 -0
- data/test/cases/adapters/postgresql/integer_test.rb +25 -0
- data/test/cases/adapters/postgresql/json_test.rb +237 -0
- data/test/cases/adapters/postgresql/ltree_test.rb +53 -0
- data/test/cases/adapters/postgresql/money_test.rb +96 -0
- data/test/cases/adapters/postgresql/network_test.rb +94 -0
- data/test/cases/adapters/postgresql/numbers_test.rb +49 -0
- data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +405 -0
- data/test/cases/adapters/postgresql/prepared_statements_test.rb +22 -0
- data/test/cases/adapters/postgresql/quoting_test.rb +44 -0
- data/test/cases/adapters/postgresql/range_test.rb +343 -0
- data/test/cases/adapters/postgresql/referential_integrity_test.rb +111 -0
- data/test/cases/adapters/postgresql/rename_table_test.rb +34 -0
- data/test/cases/adapters/postgresql/schema_authorization_test.rb +119 -0
- data/test/cases/adapters/postgresql/schema_test.rb +597 -0
- data/test/cases/adapters/postgresql/serial_test.rb +154 -0
- data/test/cases/adapters/postgresql/statement_pool_test.rb +41 -0
- data/test/cases/adapters/postgresql/timestamp_test.rb +90 -0
- data/test/cases/adapters/postgresql/type_lookup_test.rb +33 -0
- data/test/cases/adapters/postgresql/utils_test.rb +62 -0
- data/test/cases/adapters/postgresql/uuid_test.rb +294 -0
- data/test/cases/adapters/postgresql/xml_test.rb +54 -0
- data/test/cases/adapters/sqlite3/collation_test.rb +53 -0
- data/test/cases/adapters/sqlite3/copy_table_test.rb +98 -0
- data/test/cases/adapters/sqlite3/explain_test.rb +21 -0
- data/test/cases/adapters/sqlite3/quoting_test.rb +101 -0
- data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +441 -0
- data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +24 -0
- data/test/cases/adapters/sqlite3/statement_pool_test.rb +20 -0
- data/test/cases/aggregations_test.rb +11 -1
- data/test/cases/ar_schema_test.rb +35 -50
- data/test/cases/associations/association_scope_test.rb +1 -6
- data/test/cases/associations/belongs_to_associations_test.rb +122 -10
- data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +41 -0
- data/test/cases/associations/callbacks_test.rb +5 -7
- data/test/cases/associations/cascaded_eager_loading_test.rb +1 -1
- data/test/cases/associations/eager_load_nested_include_test.rb +1 -3
- data/test/cases/associations/eager_test.rb +176 -73
- data/test/cases/associations/extension_test.rb +7 -2
- data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +104 -32
- data/test/cases/associations/has_many_associations_test.rb +382 -43
- data/test/cases/associations/has_many_through_associations_test.rb +108 -41
- data/test/cases/associations/has_one_associations_test.rb +105 -8
- data/test/cases/associations/has_one_through_associations_test.rb +6 -3
- data/test/cases/associations/inner_join_association_test.rb +3 -3
- data/test/cases/associations/inverse_associations_test.rb +51 -11
- data/test/cases/associations/join_model_test.rb +59 -36
- data/test/cases/associations/left_outer_join_association_test.rb +88 -0
- data/test/cases/associations/nested_through_associations_test.rb +2 -2
- data/test/cases/associations/required_test.rb +25 -5
- data/test/cases/associations_test.rb +39 -34
- data/test/cases/attribute_decorators_test.rb +9 -8
- data/test/cases/attribute_methods/read_test.rb +5 -5
- data/test/cases/attribute_methods_test.rb +97 -40
- data/test/cases/attribute_set_test.rb +74 -4
- data/test/cases/attribute_test.rb +84 -18
- data/test/cases/attributes_test.rb +151 -34
- data/test/cases/autosave_association_test.rb +149 -36
- data/test/cases/base_test.rb +311 -236
- data/test/cases/batches_test.rb +299 -22
- data/test/cases/binary_test.rb +2 -10
- data/test/cases/bind_parameter_test.rb +76 -66
- data/test/cases/cache_key_test.rb +26 -0
- data/test/cases/calculations_test.rb +167 -15
- data/test/cases/callbacks_test.rb +161 -68
- data/test/cases/coders/json_test.rb +15 -0
- data/test/cases/collection_cache_key_test.rb +115 -0
- data/test/cases/column_definition_test.rb +26 -57
- data/test/cases/comment_test.rb +145 -0
- data/test/cases/connection_adapters/adapter_leasing_test.rb +5 -3
- data/test/cases/connection_adapters/connection_handler_test.rb +128 -21
- data/test/cases/connection_adapters/connection_specification_test.rb +1 -1
- data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +0 -38
- data/test/cases/connection_adapters/mysql_type_lookup_test.rb +5 -1
- data/test/cases/connection_adapters/schema_cache_test.rb +8 -3
- data/test/cases/connection_adapters/type_lookup_test.rb +15 -7
- data/test/cases/connection_management_test.rb +46 -56
- data/test/cases/connection_pool_test.rb +195 -20
- data/test/cases/connection_specification/resolver_test.rb +15 -0
- data/test/cases/counter_cache_test.rb +10 -5
- data/test/cases/custom_locking_test.rb +1 -1
- data/test/cases/database_statements_test.rb +18 -3
- data/test/cases/{invalid_date_test.rb → date_test.rb} +13 -1
- data/test/cases/date_time_precision_test.rb +107 -0
- data/test/cases/defaults_test.rb +85 -89
- data/test/cases/dirty_test.rb +32 -44
- data/test/cases/disconnected_test.rb +4 -2
- data/test/cases/enum_test.rb +178 -24
- data/test/cases/errors_test.rb +16 -0
- data/test/cases/explain_test.rb +32 -21
- data/test/cases/finder_test.rb +279 -151
- data/test/cases/fixture_set/file_test.rb +18 -0
- data/test/cases/fixtures_test.rb +123 -32
- data/test/cases/forbidden_attributes_protection_test.rb +69 -3
- data/test/cases/helper.rb +10 -16
- data/test/cases/hot_compatibility_test.rb +89 -1
- data/test/cases/inheritance_test.rb +284 -53
- data/test/cases/integration_test.rb +23 -7
- data/test/cases/invalid_connection_test.rb +4 -2
- data/test/cases/invertible_migration_test.rb +124 -32
- data/test/cases/json_serialization_test.rb +11 -2
- data/test/cases/locking_test.rb +22 -6
- data/test/cases/log_subscriber_test.rb +106 -17
- data/test/cases/migration/change_schema_test.rb +118 -132
- data/test/cases/migration/change_table_test.rb +34 -2
- data/test/cases/migration/column_attributes_test.rb +7 -23
- data/test/cases/migration/column_positioning_test.rb +8 -8
- data/test/cases/migration/columns_test.rb +17 -11
- data/test/cases/migration/command_recorder_test.rb +47 -2
- data/test/cases/migration/compatibility_test.rb +118 -0
- data/test/cases/migration/create_join_table_test.rb +21 -12
- data/test/cases/migration/foreign_key_test.rb +68 -66
- data/test/cases/migration/index_test.rb +14 -12
- data/test/cases/migration/logger_test.rb +1 -1
- data/test/cases/migration/pending_migrations_test.rb +0 -1
- data/test/cases/migration/references_foreign_key_test.rb +114 -107
- data/test/cases/migration/references_index_test.rb +4 -4
- data/test/cases/migration/references_statements_test.rb +26 -6
- data/test/cases/migration/rename_table_test.rb +25 -25
- data/test/cases/migration_test.rb +279 -81
- data/test/cases/migrator_test.rb +91 -8
- data/test/cases/mixin_test.rb +0 -2
- data/test/cases/modules_test.rb +3 -4
- data/test/cases/multiparameter_attributes_test.rb +24 -2
- data/test/cases/multiple_db_test.rb +18 -11
- data/test/cases/nested_attributes_test.rb +74 -33
- data/test/cases/persistence_test.rb +102 -10
- data/test/cases/pooled_connections_test.rb +3 -3
- data/test/cases/primary_keys_test.rb +170 -31
- data/test/cases/query_cache_test.rb +216 -96
- data/test/cases/quoting_test.rb +65 -19
- data/test/cases/readonly_test.rb +2 -1
- data/test/cases/reflection_test.rb +77 -22
- data/test/cases/relation/delegation_test.rb +3 -8
- data/test/cases/relation/merging_test.rb +10 -14
- data/test/cases/relation/mutation_test.rb +42 -24
- data/test/cases/relation/or_test.rb +92 -0
- data/test/cases/relation/predicate_builder_test.rb +4 -2
- data/test/cases/relation/record_fetch_warning_test.rb +40 -0
- data/test/cases/relation/where_chain_test.rb +23 -99
- data/test/cases/relation/where_clause_test.rb +182 -0
- data/test/cases/relation/where_test.rb +45 -23
- data/test/cases/relation_test.rb +89 -58
- data/test/cases/relations_test.rb +249 -38
- data/test/cases/result_test.rb +10 -0
- data/test/cases/sanitize_test.rb +108 -15
- data/test/cases/schema_dumper_test.rb +119 -125
- data/test/cases/schema_loading_test.rb +52 -0
- data/test/cases/scoping/default_scoping_test.rb +113 -39
- data/test/cases/scoping/named_scoping_test.rb +46 -9
- data/test/cases/scoping/relation_scoping_test.rb +47 -4
- data/test/cases/secure_token_test.rb +32 -0
- data/test/cases/serialization_test.rb +1 -1
- data/test/cases/serialized_attribute_test.rb +93 -6
- data/test/cases/statement_cache_test.rb +38 -0
- data/test/cases/store_test.rb +2 -1
- data/test/cases/suppressor_test.rb +63 -0
- data/test/cases/tasks/database_tasks_test.rb +74 -8
- data/test/cases/tasks/mysql_rake_test.rb +143 -109
- data/test/cases/tasks/postgresql_rake_test.rb +71 -12
- data/test/cases/tasks/sqlite_rake_test.rb +30 -3
- data/test/cases/test_case.rb +28 -20
- data/test/cases/test_fixtures_test.rb +36 -0
- data/test/cases/time_precision_test.rb +103 -0
- data/test/cases/timestamp_test.rb +47 -14
- data/test/cases/touch_later_test.rb +121 -0
- data/test/cases/transaction_callbacks_test.rb +128 -62
- data/test/cases/transaction_isolation_test.rb +2 -2
- data/test/cases/transactions_test.rb +61 -43
- data/test/cases/type/adapter_specific_registry_test.rb +133 -0
- data/test/cases/type/date_time_test.rb +14 -0
- data/test/cases/type/integer_test.rb +2 -96
- data/test/cases/type/string_test.rb +0 -14
- data/test/cases/type_test.rb +39 -0
- data/test/cases/types_test.rb +1 -118
- data/test/cases/unconnected_test.rb +1 -1
- data/test/cases/validations/absence_validation_test.rb +73 -0
- data/test/cases/validations/association_validation_test.rb +13 -2
- data/test/cases/validations/i18n_validation_test.rb +6 -10
- data/test/cases/validations/length_validation_test.rb +62 -30
- data/test/cases/validations/presence_validation_test.rb +36 -1
- data/test/cases/validations/uniqueness_validation_test.rb +150 -36
- data/test/cases/validations_repair_helper.rb +2 -6
- data/test/cases/validations_test.rb +36 -7
- data/test/cases/view_test.rb +108 -5
- data/test/cases/yaml_serialization_test.rb +36 -1
- data/test/config.example.yml +97 -0
- data/test/fixtures/bad_posts.yml +9 -0
- data/test/fixtures/books.yml +20 -0
- data/test/fixtures/content.yml +3 -0
- data/test/fixtures/content_positions.yml +3 -0
- data/test/fixtures/dead_parrots.yml +5 -0
- data/test/fixtures/live_parrots.yml +4 -0
- data/test/fixtures/naked/yml/parrots.yml +2 -0
- data/test/fixtures/naked/yml/trees.yml +3 -0
- data/test/fixtures/nodes.yml +29 -0
- data/test/fixtures/other_comments.yml +6 -0
- data/test/fixtures/other_dogs.yml +2 -0
- data/test/fixtures/other_posts.yml +7 -0
- data/test/fixtures/price_estimates.yml +10 -1
- data/test/fixtures/trees.yml +3 -0
- data/test/migrations/10_urban/9_add_expressions.rb +1 -1
- data/test/migrations/decimal/1_give_me_big_numbers.rb +1 -1
- data/test/migrations/magic/1_currencies_have_symbols.rb +1 -1
- data/test/migrations/missing/1000_people_have_middle_names.rb +2 -2
- data/test/migrations/missing/1_people_have_last_names.rb +2 -2
- data/test/migrations/missing/3_we_need_reminders.rb +2 -2
- data/test/migrations/missing/4_innocent_jointable.rb +2 -2
- data/test/migrations/rename/1_we_need_things.rb +2 -2
- data/test/migrations/rename/2_rename_things.rb +2 -2
- data/test/migrations/to_copy/1_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy/2_people_have_descriptions.rb +1 -1
- data/test/migrations/to_copy2/1_create_articles.rb +1 -1
- data/test/migrations/to_copy2/2_create_comments.rb +1 -1
- data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +1 -1
- data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +1 -1
- data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +1 -1
- data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +1 -1
- data/test/migrations/valid/1_valid_people_have_last_names.rb +1 -1
- data/test/migrations/valid/2_we_need_reminders.rb +2 -2
- data/test/migrations/valid/3_innocent_jointable.rb +2 -2
- data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +1 -1
- data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +2 -2
- data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +2 -2
- data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +1 -1
- data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +1 -1
- data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +1 -1
- data/test/migrations/version_check/20131219224947_migration_version_check.rb +1 -1
- data/test/models/admin/randomly_named_c1.rb +6 -2
- data/test/models/aircraft.rb +1 -0
- data/test/models/author.rb +4 -7
- data/test/models/bird.rb +1 -1
- data/test/models/book.rb +5 -0
- data/test/models/bulb.rb +2 -1
- data/test/models/car.rb +3 -0
- data/test/models/cat.rb +10 -0
- data/test/models/chef.rb +5 -0
- data/test/models/club.rb +2 -0
- data/test/models/comment.rb +17 -5
- data/test/models/company.rb +7 -2
- data/test/models/company_in_module.rb +1 -1
- data/test/models/contact.rb +1 -1
- data/test/models/content.rb +40 -0
- data/test/models/customer.rb +8 -2
- data/test/models/developer.rb +22 -0
- data/test/models/face.rb +1 -1
- data/test/models/guitar.rb +4 -0
- data/test/models/hotel.rb +5 -0
- data/test/models/member.rb +1 -0
- data/test/models/member_detail.rb +4 -3
- data/test/models/mentor.rb +3 -0
- data/test/models/mocktail_designer.rb +2 -0
- data/test/models/node.rb +5 -0
- data/test/models/non_primary_key.rb +2 -0
- data/test/models/notification.rb +3 -0
- data/test/models/other_dog.rb +5 -0
- data/test/models/owner.rb +4 -1
- data/test/models/parrot.rb +6 -7
- data/test/models/person.rb +0 -1
- data/test/models/pet.rb +3 -0
- data/test/models/pet_treasure.rb +6 -0
- data/test/models/pirate.rb +3 -3
- data/test/models/post.rb +18 -9
- data/test/models/project.rb +11 -0
- data/test/models/randomly_named_c1.rb +1 -1
- data/test/models/recipe.rb +3 -0
- data/test/models/ship.rb +8 -2
- data/test/models/tag.rb +6 -0
- data/test/models/topic.rb +2 -8
- data/test/models/tree.rb +3 -0
- data/test/models/tuning_peg.rb +4 -0
- data/test/models/user.rb +14 -0
- data/test/models/uuid_item.rb +6 -0
- data/test/schema/mysql2_specific_schema.rb +33 -23
- data/test/schema/oracle_specific_schema.rb +1 -4
- data/test/schema/postgresql_specific_schema.rb +36 -124
- data/test/schema/schema.rb +183 -64
- data/test/schema/schema.rb.original +1057 -0
- data/test/schema/sqlite_specific_schema.rb +1 -5
- data/test/support/connection.rb +1 -0
- data/test/support/schema_dumping_helper.rb +1 -1
- data/test/support/yaml_compatibility_fixtures/rails_4_1.yml +22 -0
- data/test/support/yaml_compatibility_fixtures/rails_4_2_0.yml +182 -0
- metadata +145 -26
- data/test/cases/associations/deprecated_counter_cache_on_has_many_through_test.rb +0 -26
- data/test/cases/attribute_methods/serialization_test.rb +0 -29
- data/test/cases/migration/change_schema_test - Copy.rb +0 -448
- data/test/cases/migration/foreign_key_test - Changed.rb +0 -325
- data/test/cases/migration/table_and_index_test.rb +0 -24
- data/test/cases/relation/where_test2.rb +0 -36
- data/test/cases/type/decimal_test.rb +0 -51
- data/test/cases/type/unsigned_integer_test.rb +0 -18
- data/test/cases/xml_serialization_test.rb +0 -457
- data/test/fixtures/naked/csv/accounts.csv +0 -1
- data/test/schema/mysql_specific_schema.rb +0 -70
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'cases/helper'
|
2
|
+
require 'models/user'
|
3
|
+
|
4
|
+
class SecureTokenTest < ActiveRecord::TestCase
|
5
|
+
setup do
|
6
|
+
@user = User.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_token_values_are_generated_for_specified_attributes_and_persisted_on_save
|
10
|
+
@user.save
|
11
|
+
assert_not_nil @user.token
|
12
|
+
assert_not_nil @user.auth_token
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_regenerating_the_secure_token
|
16
|
+
@user.save
|
17
|
+
old_token = @user.token
|
18
|
+
old_auth_token = @user.auth_token
|
19
|
+
@user.regenerate_token
|
20
|
+
@user.regenerate_auth_token
|
21
|
+
|
22
|
+
assert_not_equal @user.token, old_token
|
23
|
+
assert_not_equal @user.auth_token, old_auth_token
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_token_value_not_overwritten_when_present
|
27
|
+
@user.token = "custom-secure-token"
|
28
|
+
@user.save
|
29
|
+
|
30
|
+
assert_equal @user.token, "custom-secure-token"
|
31
|
+
end
|
32
|
+
end
|
@@ -22,12 +22,6 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
|
-
def test_list_of_serialized_attributes
|
26
|
-
assert_deprecated do
|
27
|
-
assert_equal %w(content), Topic.serialized_attributes.keys
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
25
|
def test_serialized_attribute
|
32
26
|
Topic.serialize("content", MyObject)
|
33
27
|
|
@@ -265,6 +259,12 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
|
265
259
|
assert_not topic.content_changed?
|
266
260
|
end
|
267
261
|
|
262
|
+
def test_classes_without_no_arg_constructors_are_not_supported
|
263
|
+
assert_raises(ArgumentError) do
|
264
|
+
Topic.serialize(:content, Regexp)
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
268
|
def test_newly_emptied_serialized_hash_is_changed
|
269
269
|
Topic.serialize(:content, Hash)
|
270
270
|
topic = Topic.create(content: { "things" => "stuff" })
|
@@ -274,4 +274,91 @@ class SerializedAttributeTest < ActiveRecord::TestCase
|
|
274
274
|
|
275
275
|
assert_equal({}, topic.content)
|
276
276
|
end
|
277
|
+
|
278
|
+
def test_values_cast_from_nil_are_persisted_as_nil
|
279
|
+
# This is required to fulfil the following contract, which must be universally
|
280
|
+
# true in Active Record:
|
281
|
+
#
|
282
|
+
# model.attribute = value
|
283
|
+
# assert_equal model.attribute, model.tap(&:save).reload.attribute
|
284
|
+
Topic.serialize(:content, Hash)
|
285
|
+
topic = Topic.create!(content: {})
|
286
|
+
topic2 = Topic.create!(content: nil)
|
287
|
+
|
288
|
+
assert_equal [topic, topic2], Topic.where(content: nil)
|
289
|
+
end
|
290
|
+
|
291
|
+
def test_nil_is_always_persisted_as_null
|
292
|
+
Topic.serialize(:content, Hash)
|
293
|
+
|
294
|
+
topic = Topic.create!(content: { foo: "bar" })
|
295
|
+
topic.update_attribute :content, nil
|
296
|
+
assert_equal [topic], Topic.where(content: nil)
|
297
|
+
end
|
298
|
+
|
299
|
+
def test_mutation_detection_does_not_double_serialize
|
300
|
+
coder = Object.new
|
301
|
+
def coder.dump(value)
|
302
|
+
return if value.nil?
|
303
|
+
value + " encoded"
|
304
|
+
end
|
305
|
+
def coder.load(value)
|
306
|
+
return if value.nil?
|
307
|
+
value.gsub(" encoded", "")
|
308
|
+
end
|
309
|
+
type = Class.new(ActiveModel::Type::Value) do
|
310
|
+
include ActiveModel::Type::Helpers::Mutable
|
311
|
+
|
312
|
+
def serialize(value)
|
313
|
+
return if value.nil?
|
314
|
+
value + " serialized"
|
315
|
+
end
|
316
|
+
|
317
|
+
def deserialize(value)
|
318
|
+
return if value.nil?
|
319
|
+
value.gsub(" serialized", "")
|
320
|
+
end
|
321
|
+
end.new
|
322
|
+
model = Class.new(Topic) do
|
323
|
+
attribute :foo, type
|
324
|
+
serialize :foo, coder
|
325
|
+
end
|
326
|
+
|
327
|
+
topic = model.create!(foo: "bar")
|
328
|
+
topic.foo
|
329
|
+
refute topic.changed?
|
330
|
+
end
|
331
|
+
end
|
332
|
+
|
333
|
+
class ThreadedSerializedAttributeTest < ActiveRecord::TestCase
|
334
|
+
self.use_transactional_tests = false
|
335
|
+
fixtures :topics
|
336
|
+
|
337
|
+
def test_serialized_attribute_works_under_concurrent_initial_access
|
338
|
+
model = Topic.dup
|
339
|
+
|
340
|
+
topic = model.last
|
341
|
+
topic.update group: "1"
|
342
|
+
|
343
|
+
model.serialize :group, JSON
|
344
|
+
model.reset_column_information
|
345
|
+
|
346
|
+
# This isn't strictly necessary for the test, but a little bit of
|
347
|
+
# knowledge of internals allows us to make failures far more likely.
|
348
|
+
model.define_singleton_method(:define_attribute) do |*args|
|
349
|
+
Thread.pass
|
350
|
+
super(*args)
|
351
|
+
end
|
352
|
+
|
353
|
+
threads = 4.times.map do
|
354
|
+
Thread.new do
|
355
|
+
topic.reload.group
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
# All the threads should retrieve the value knowing it is JSON, and
|
360
|
+
# thus decode it. If this fails, some threads will instead see the
|
361
|
+
# raw string ("1"), or raise an exception.
|
362
|
+
assert_equal [1] * threads.size, threads.map(&:value)
|
363
|
+
end
|
277
364
|
end
|
@@ -94,5 +94,43 @@ module ActiveRecord
|
|
94
94
|
additional_books = cache.execute([], Book, Book.connection)
|
95
95
|
assert first_books != additional_books
|
96
96
|
end
|
97
|
+
|
98
|
+
def test_unprepared_statements_dont_share_a_cache_with_prepared_statements
|
99
|
+
Book.create(name: "my book")
|
100
|
+
Book.create(name: "my other book")
|
101
|
+
|
102
|
+
book = Book.find_by(name: "my book")
|
103
|
+
other_book = Book.connection.unprepared_statement do
|
104
|
+
Book.find_by(name: "my other book")
|
105
|
+
end
|
106
|
+
|
107
|
+
refute_equal book, other_book
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_find_by_does_not_use_statement_cache_if_table_name_is_changed
|
111
|
+
book = Book.create(name: "my book")
|
112
|
+
|
113
|
+
Book.find_by(name: book.name) # warming the statement cache.
|
114
|
+
|
115
|
+
# changing the table name should change the query that is not cached.
|
116
|
+
Book.table_name = :birds
|
117
|
+
assert_nil Book.find_by(name: "my book")
|
118
|
+
ensure
|
119
|
+
Book.table_name = :books
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_find_does_not_use_statement_cache_if_table_name_is_changed
|
123
|
+
book = Book.create(name: "my book")
|
124
|
+
|
125
|
+
Book.find(book.id) # warming the statement cache.
|
126
|
+
|
127
|
+
# changing the table name should change the query that is not cached.
|
128
|
+
Book.table_name = :birds
|
129
|
+
assert_raise ActiveRecord::RecordNotFound do
|
130
|
+
Book.find(book.id)
|
131
|
+
end
|
132
|
+
ensure
|
133
|
+
Book.table_name = :books
|
134
|
+
end
|
97
135
|
end
|
98
136
|
end
|
data/test/cases/store_test.rb
CHANGED
@@ -104,7 +104,7 @@ class StoreTest < ActiveRecord::TestCase
|
|
104
104
|
assert_equal true, user.settings.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
105
105
|
end
|
106
106
|
|
107
|
-
test "convert store attributes from any format other than Hash or
|
107
|
+
test "convert store attributes from any format other than Hash or HashWithIndifferentAccess losing the data" do
|
108
108
|
@john.json_data = "somedata"
|
109
109
|
@john.height = 'low'
|
110
110
|
assert_equal true, @john.json_data.instance_of?(ActiveSupport::HashWithIndifferentAccess)
|
@@ -177,6 +177,7 @@ class StoreTest < ActiveRecord::TestCase
|
|
177
177
|
assert_equal [:color], first_model.stored_attributes[:data]
|
178
178
|
assert_equal [:color, :width, :height], second_model.stored_attributes[:data]
|
179
179
|
assert_equal [:color, :area, :volume], third_model.stored_attributes[:data]
|
180
|
+
assert_equal [:color], first_model.stored_attributes[:data]
|
180
181
|
end
|
181
182
|
|
182
183
|
test "YAML coder initializes the store when a Nil value is given" do
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'cases/helper'
|
2
|
+
require 'models/notification'
|
3
|
+
require 'models/user'
|
4
|
+
|
5
|
+
class SuppressorTest < ActiveRecord::TestCase
|
6
|
+
def test_suppresses_create
|
7
|
+
assert_no_difference -> { Notification.count } do
|
8
|
+
Notification.suppress do
|
9
|
+
Notification.create
|
10
|
+
Notification.create!
|
11
|
+
Notification.new.save
|
12
|
+
Notification.new.save!
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_suppresses_update
|
18
|
+
user = User.create! token: 'asdf'
|
19
|
+
|
20
|
+
User.suppress do
|
21
|
+
user.update token: 'ghjkl'
|
22
|
+
assert_equal 'asdf', user.reload.token
|
23
|
+
|
24
|
+
user.update! token: 'zxcvbnm'
|
25
|
+
assert_equal 'asdf', user.reload.token
|
26
|
+
|
27
|
+
user.token = 'qwerty'
|
28
|
+
user.save
|
29
|
+
assert_equal 'asdf', user.reload.token
|
30
|
+
|
31
|
+
user.token = 'uiop'
|
32
|
+
user.save!
|
33
|
+
assert_equal 'asdf', user.reload.token
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_suppresses_create_in_callback
|
38
|
+
assert_difference -> { User.count } do
|
39
|
+
assert_no_difference -> { Notification.count } do
|
40
|
+
Notification.suppress { UserWithNotification.create! }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_resumes_saving_after_suppression_complete
|
46
|
+
Notification.suppress { UserWithNotification.create! }
|
47
|
+
|
48
|
+
assert_difference -> { Notification.count } do
|
49
|
+
Notification.create!(message: "New Comment")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_suppresses_validations_on_create
|
54
|
+
assert_no_difference -> { Notification.count } do
|
55
|
+
Notification.suppress do
|
56
|
+
User.create
|
57
|
+
User.create!
|
58
|
+
User.new.save
|
59
|
+
User.new.save!
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -8,16 +8,50 @@ module ActiveRecord
|
|
8
8
|
ActiveRecord::Tasks::MySQLDatabaseTasks.stubs(:new).returns @mysql_tasks
|
9
9
|
ActiveRecord::Tasks::PostgreSQLDatabaseTasks.stubs(:new).returns @postgresql_tasks
|
10
10
|
ActiveRecord::Tasks::SQLiteDatabaseTasks.stubs(:new).returns @sqlite_tasks
|
11
|
+
|
12
|
+
$stdout, @original_stdout = StringIO.new, $stdout
|
13
|
+
$stderr, @original_stderr = StringIO.new, $stderr
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
$stdout, $stderr = @original_stdout, @original_stderr
|
11
18
|
end
|
12
19
|
end
|
13
20
|
|
14
21
|
ADAPTERS_TASKS = {
|
15
|
-
mysql: :mysql_tasks,
|
16
22
|
mysql2: :mysql_tasks,
|
17
23
|
postgresql: :postgresql_tasks,
|
18
24
|
sqlite3: :sqlite_tasks
|
19
25
|
}
|
20
26
|
|
27
|
+
class DatabaseTasksUtilsTask< ActiveRecord::TestCase
|
28
|
+
def test_raises_an_error_when_called_with_protected_environment
|
29
|
+
ActiveRecord::Migrator.stubs(:current_version).returns(1)
|
30
|
+
|
31
|
+
protected_environments = ActiveRecord::Base.protected_environments.dup
|
32
|
+
current_env = ActiveRecord::Migrator.current_environment
|
33
|
+
assert !protected_environments.include?(current_env)
|
34
|
+
# Assert no error
|
35
|
+
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
36
|
+
|
37
|
+
ActiveRecord::Base.protected_environments << current_env
|
38
|
+
assert_raise(ActiveRecord::ProtectedEnvironmentError) do
|
39
|
+
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
40
|
+
end
|
41
|
+
ensure
|
42
|
+
ActiveRecord::Base.protected_environments = protected_environments
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_raises_an_error_if_no_migrations_have_been_made
|
46
|
+
ActiveRecord::InternalMetadata.stubs(:table_exists?).returns(false)
|
47
|
+
ActiveRecord::Migrator.stubs(:current_version).returns(1)
|
48
|
+
|
49
|
+
assert_raise(ActiveRecord::NoEnvironmentInSchemaError) do
|
50
|
+
ActiveRecord::Tasks::DatabaseTasks.check_protected_environments!
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
21
55
|
class DatabaseTasksRegisterTask < ActiveRecord::TestCase
|
22
56
|
def test_register_task
|
23
57
|
klazz = Class.new do
|
@@ -134,21 +168,25 @@ module ActiveRecord
|
|
134
168
|
with('database' => 'dev-db')
|
135
169
|
ActiveRecord::Tasks::DatabaseTasks.expects(:create).
|
136
170
|
with('database' => 'test-db')
|
137
|
-
ENV.expects(:[]).with('RAILS_ENV').returns(nil)
|
138
171
|
|
139
172
|
ActiveRecord::Tasks::DatabaseTasks.create_current(
|
140
173
|
ActiveSupport::StringInquirer.new('development')
|
141
174
|
)
|
142
175
|
end
|
143
176
|
|
144
|
-
def
|
177
|
+
def test_creates_test_and_development_databases_when_rails_env_is_development
|
178
|
+
old_env = ENV['RAILS_ENV']
|
179
|
+
ENV['RAILS_ENV'] = 'development'
|
145
180
|
ActiveRecord::Tasks::DatabaseTasks.expects(:create).
|
146
181
|
with('database' => 'dev-db')
|
147
|
-
|
182
|
+
ActiveRecord::Tasks::DatabaseTasks.expects(:create).
|
183
|
+
with('database' => 'test-db')
|
148
184
|
|
149
185
|
ActiveRecord::Tasks::DatabaseTasks.create_current(
|
150
186
|
ActiveSupport::StringInquirer.new('development')
|
151
187
|
)
|
188
|
+
ensure
|
189
|
+
ENV['RAILS_ENV'] = old_env
|
152
190
|
end
|
153
191
|
|
154
192
|
def test_establishes_connection_for_the_given_environment
|
@@ -255,36 +293,64 @@ module ActiveRecord
|
|
255
293
|
with('database' => 'dev-db')
|
256
294
|
ActiveRecord::Tasks::DatabaseTasks.expects(:drop).
|
257
295
|
with('database' => 'test-db')
|
258
|
-
ENV.expects(:[]).with('RAILS_ENV').returns(nil)
|
259
296
|
|
260
297
|
ActiveRecord::Tasks::DatabaseTasks.drop_current(
|
261
298
|
ActiveSupport::StringInquirer.new('development')
|
262
299
|
)
|
263
300
|
end
|
264
301
|
|
265
|
-
def
|
302
|
+
def test_drops_testand_development_databases_when_rails_env_is_development
|
303
|
+
old_env = ENV['RAILS_ENV']
|
304
|
+
ENV['RAILS_ENV'] = 'development'
|
266
305
|
ActiveRecord::Tasks::DatabaseTasks.expects(:drop).
|
267
306
|
with('database' => 'dev-db')
|
268
|
-
|
307
|
+
ActiveRecord::Tasks::DatabaseTasks.expects(:drop).
|
308
|
+
with('database' => 'test-db')
|
269
309
|
|
270
310
|
ActiveRecord::Tasks::DatabaseTasks.drop_current(
|
271
311
|
ActiveSupport::StringInquirer.new('development')
|
272
312
|
)
|
313
|
+
ensure
|
314
|
+
ENV['RAILS_ENV'] = old_env
|
273
315
|
end
|
274
316
|
end
|
275
317
|
|
276
318
|
class DatabaseTasksMigrateTest < ActiveRecord::TestCase
|
319
|
+
self.use_transactional_tests = false
|
320
|
+
|
321
|
+
def setup
|
322
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = 'custom/path'
|
323
|
+
end
|
324
|
+
|
325
|
+
def teardown
|
326
|
+
ActiveRecord::Tasks::DatabaseTasks.migrations_paths = nil
|
327
|
+
end
|
328
|
+
|
277
329
|
def test_migrate_receives_correct_env_vars
|
278
330
|
verbose, version = ENV['VERBOSE'], ENV['VERSION']
|
279
331
|
|
280
332
|
ENV['VERBOSE'] = 'false'
|
281
333
|
ENV['VERSION'] = '4'
|
282
334
|
|
283
|
-
ActiveRecord::Migrator.expects(:migrate).with(
|
335
|
+
ActiveRecord::Migrator.expects(:migrate).with('custom/path', 4)
|
284
336
|
ActiveRecord::Tasks::DatabaseTasks.migrate
|
285
337
|
ensure
|
286
338
|
ENV['VERBOSE'], ENV['VERSION'] = verbose, version
|
287
339
|
end
|
340
|
+
|
341
|
+
def test_migrate_raise_error_on_empty_version
|
342
|
+
version = ENV["VERSION"]
|
343
|
+
ENV["VERSION"] = ""
|
344
|
+
e = assert_raise(RuntimeError) { ActiveRecord::Tasks::DatabaseTasks.migrate }
|
345
|
+
assert_equal "Empty VERSION provided", e.message
|
346
|
+
ensure
|
347
|
+
ENV["VERSION"] = version
|
348
|
+
end
|
349
|
+
|
350
|
+
def test_migrate_clears_schema_cache_afterward
|
351
|
+
ActiveRecord::Base.expects(:clear_cache!)
|
352
|
+
ActiveRecord::Tasks::DatabaseTasks.migrate
|
353
|
+
end
|
288
354
|
end
|
289
355
|
|
290
356
|
class DatabaseTasksPurgeTest < ActiveRecord::TestCase
|
@@ -1,48 +1,49 @@
|
|
1
1
|
require 'cases/helper'
|
2
|
+
require 'active_record/tasks/database_tasks'
|
2
3
|
|
3
|
-
if current_adapter?(:
|
4
|
+
if current_adapter?(:Mysql2Adapter)
|
4
5
|
module ActiveRecord
|
5
6
|
class MysqlDBCreateTest < ActiveRecord::TestCase
|
6
7
|
def setup
|
7
8
|
@connection = stub(:create_database => true)
|
8
9
|
@configuration = {
|
9
|
-
'adapter' => '
|
10
|
+
'adapter' => 'mysql2',
|
10
11
|
'database' => 'my-app-db'
|
11
12
|
}
|
12
13
|
|
13
14
|
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
14
15
|
ActiveRecord::Base.stubs(:establish_connection).returns(true)
|
16
|
+
|
17
|
+
$stdout, @original_stdout = StringIO.new, $stdout
|
18
|
+
$stderr, @original_stderr = StringIO.new, $stderr
|
19
|
+
end
|
20
|
+
|
21
|
+
def teardown
|
22
|
+
$stdout, $stderr = @original_stdout, @original_stderr
|
15
23
|
end
|
16
24
|
|
17
25
|
def test_establishes_connection_without_database
|
18
26
|
ActiveRecord::Base.expects(:establish_connection).
|
19
|
-
with('adapter' => '
|
27
|
+
with('adapter' => 'mysql2', 'database' => nil)
|
20
28
|
|
21
29
|
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
22
30
|
end
|
23
31
|
|
24
|
-
def
|
32
|
+
def test_creates_database_with_no_default_options
|
25
33
|
@connection.expects(:create_database).
|
26
|
-
with('my-app-db',
|
34
|
+
with('my-app-db', {})
|
27
35
|
|
28
36
|
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
29
37
|
end
|
30
38
|
|
31
|
-
def
|
32
|
-
@connection.expects(:create_database).
|
33
|
-
with('my-app-db', charset: 'utf8', collation: 'utf8_unicode_ci')
|
34
|
-
|
35
|
-
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'utf8')
|
36
|
-
end
|
37
|
-
|
38
|
-
def test_creates_database_with_given_encoding_and_no_collation
|
39
|
+
def test_creates_database_with_given_encoding
|
39
40
|
@connection.expects(:create_database).
|
40
41
|
with('my-app-db', charset: 'latin1')
|
41
42
|
|
42
43
|
ActiveRecord::Tasks::DatabaseTasks.create @configuration.merge('encoding' => 'latin1')
|
43
44
|
end
|
44
45
|
|
45
|
-
def
|
46
|
+
def test_creates_database_with_given_collation
|
46
47
|
@connection.expects(:create_database).
|
47
48
|
with('my-app-db', collation: 'latin1_swedish_ci')
|
48
49
|
|
@@ -55,108 +56,118 @@ module ActiveRecord
|
|
55
56
|
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
56
57
|
end
|
57
58
|
|
58
|
-
def
|
59
|
-
|
59
|
+
def test_when_database_created_successfully_outputs_info_to_stdout
|
60
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
60
61
|
|
62
|
+
assert_equal $stdout.string, "Created database 'my-app-db'\n"
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_create_when_database_exists_outputs_info_to_stderr
|
61
66
|
ActiveRecord::Base.connection.stubs(:create_database).raises(
|
62
|
-
ActiveRecord::
|
67
|
+
ActiveRecord::Tasks::DatabaseAlreadyExists
|
63
68
|
)
|
64
69
|
|
65
70
|
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
71
|
+
|
72
|
+
assert_equal $stderr.string, "Database 'my-app-db' already exists\n"
|
66
73
|
end
|
67
74
|
end
|
68
75
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
}
|
80
|
-
|
81
|
-
$stdin.stubs(:gets).returns("secret\n")
|
82
|
-
$stdout.stubs(:print).returns(nil)
|
83
|
-
@error.stubs(:errno).returns(1045)
|
84
|
-
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
85
|
-
ActiveRecord::Base.stubs(:establish_connection).
|
86
|
-
raises(@error).
|
87
|
-
then.returns(true)
|
88
|
-
end
|
76
|
+
class MysqlDBCreateAsRootTest < ActiveRecord::TestCase
|
77
|
+
def setup
|
78
|
+
@connection = stub("Connection", create_database: true)
|
79
|
+
@error = Mysql2::Error.new("Invalid permissions")
|
80
|
+
@configuration = {
|
81
|
+
'adapter' => 'mysql2',
|
82
|
+
'database' => 'my-app-db',
|
83
|
+
'username' => 'pat',
|
84
|
+
'password' => 'wossname'
|
85
|
+
}
|
89
86
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
87
|
+
$stdin.stubs(:gets).returns("secret\n")
|
88
|
+
$stdout.stubs(:print).returns(nil)
|
89
|
+
@error.stubs(:errno).returns(1045)
|
90
|
+
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
91
|
+
ActiveRecord::Base.stubs(:establish_connection).
|
92
|
+
raises(@error).
|
93
|
+
then.returns(true)
|
94
94
|
|
95
|
-
|
96
|
-
|
97
|
-
|
95
|
+
$stdout, @original_stdout = StringIO.new, $stdout
|
96
|
+
$stderr, @original_stderr = StringIO.new, $stderr
|
97
|
+
end
|
98
98
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
'adapter' => 'mysql',
|
103
|
-
'database' => nil,
|
104
|
-
'username' => 'root',
|
105
|
-
'password' => 'secret'
|
106
|
-
)
|
99
|
+
def teardown
|
100
|
+
$stdout, $stderr = @original_stdout, @original_stderr
|
101
|
+
end
|
107
102
|
|
108
|
-
|
109
|
-
|
103
|
+
def test_root_password_is_requested
|
104
|
+
assert_permissions_granted_for("pat")
|
105
|
+
$stdin.expects(:gets).returns("secret\n")
|
110
106
|
|
111
|
-
|
112
|
-
|
113
|
-
@connection.expects(:create_database).
|
114
|
-
with('my-app-db', :charset => 'utf8', :collation => 'utf8_unicode_ci')
|
107
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
108
|
+
end
|
115
109
|
|
116
|
-
|
117
|
-
|
110
|
+
def test_connection_established_as_root
|
111
|
+
assert_permissions_granted_for("pat")
|
112
|
+
ActiveRecord::Base.expects(:establish_connection).with(
|
113
|
+
'adapter' => 'mysql2',
|
114
|
+
'database' => nil,
|
115
|
+
'username' => 'root',
|
116
|
+
'password' => 'secret'
|
117
|
+
)
|
118
118
|
|
119
|
-
|
120
|
-
|
121
|
-
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
122
|
-
end
|
119
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
120
|
+
end
|
123
121
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
end
|
122
|
+
def test_database_created_by_root
|
123
|
+
assert_permissions_granted_for("pat")
|
124
|
+
@connection.expects(:create_database).
|
125
|
+
with('my-app-db', {})
|
129
126
|
|
130
|
-
|
131
|
-
|
132
|
-
ActiveRecord::Base.expects(:establish_connection).returns do
|
133
|
-
ActiveRecord::Base.expects(:establish_connection).with(
|
134
|
-
'adapter' => 'mysql',
|
135
|
-
'database' => 'my-app-db',
|
136
|
-
'username' => 'pat',
|
137
|
-
'password' => 'secret'
|
138
|
-
)
|
127
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
128
|
+
end
|
139
129
|
|
140
|
-
|
141
|
-
|
130
|
+
def test_grant_privileges_for_normal_user
|
131
|
+
assert_permissions_granted_for("pat")
|
132
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
133
|
+
end
|
134
|
+
|
135
|
+
def test_do_not_grant_privileges_for_root_user
|
136
|
+
@configuration['username'] = 'root'
|
137
|
+
@configuration['password'] = ''
|
138
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_connection_established_as_normal_user
|
142
|
+
assert_permissions_granted_for("pat")
|
143
|
+
ActiveRecord::Base.expects(:establish_connection).returns do
|
144
|
+
ActiveRecord::Base.expects(:establish_connection).with(
|
145
|
+
'adapter' => 'mysql2',
|
146
|
+
'database' => 'my-app-db',
|
147
|
+
'username' => 'pat',
|
148
|
+
'password' => 'secret'
|
149
|
+
)
|
142
150
|
|
143
|
-
|
151
|
+
raise @error
|
144
152
|
end
|
145
153
|
|
146
|
-
|
147
|
-
|
154
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
155
|
+
end
|
148
156
|
|
149
|
-
|
157
|
+
def test_sends_output_to_stderr_when_other_errors
|
158
|
+
@error.stubs(:errno).returns(42)
|
150
159
|
|
151
|
-
|
152
|
-
|
160
|
+
$stderr.expects(:puts).at_least_once.returns(nil)
|
161
|
+
|
162
|
+
ActiveRecord::Tasks::DatabaseTasks.create @configuration
|
163
|
+
end
|
153
164
|
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
165
|
+
private
|
166
|
+
|
167
|
+
def assert_permissions_granted_for(db_user)
|
168
|
+
db_name = @configuration['database']
|
169
|
+
db_password = @configuration['password']
|
170
|
+
@connection.expects(:execute).with("GRANT ALL PRIVILEGES ON #{db_name}.* TO '#{db_user}'@'localhost' IDENTIFIED BY '#{db_password}' WITH GRANT OPTION;")
|
160
171
|
end
|
161
172
|
end
|
162
173
|
|
@@ -164,12 +175,19 @@ module ActiveRecord
|
|
164
175
|
def setup
|
165
176
|
@connection = stub(:drop_database => true)
|
166
177
|
@configuration = {
|
167
|
-
'adapter' => '
|
178
|
+
'adapter' => 'mysql2',
|
168
179
|
'database' => 'my-app-db'
|
169
180
|
}
|
170
181
|
|
171
182
|
ActiveRecord::Base.stubs(:connection).returns(@connection)
|
172
183
|
ActiveRecord::Base.stubs(:establish_connection).returns(true)
|
184
|
+
|
185
|
+
$stdout, @original_stdout = StringIO.new, $stdout
|
186
|
+
$stderr, @original_stderr = StringIO.new, $stderr
|
187
|
+
end
|
188
|
+
|
189
|
+
def teardown
|
190
|
+
$stdout, $stderr = @original_stdout, @original_stderr
|
173
191
|
end
|
174
192
|
|
175
193
|
def test_establishes_connection_to_mysql_database
|
@@ -183,13 +201,19 @@ module ActiveRecord
|
|
183
201
|
|
184
202
|
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
|
185
203
|
end
|
204
|
+
|
205
|
+
def test_when_database_dropped_successfully_outputs_info_to_stdout
|
206
|
+
ActiveRecord::Tasks::DatabaseTasks.drop @configuration
|
207
|
+
|
208
|
+
assert_equal $stdout.string, "Dropped database 'my-app-db'\n"
|
209
|
+
end
|
186
210
|
end
|
187
211
|
|
188
212
|
class MySQLPurgeTest < ActiveRecord::TestCase
|
189
213
|
def setup
|
190
214
|
@connection = stub(:recreate_database => true)
|
191
215
|
@configuration = {
|
192
|
-
'adapter' => '
|
216
|
+
'adapter' => 'mysql2',
|
193
217
|
'database' => 'test-db'
|
194
218
|
}
|
195
219
|
|
@@ -203,9 +227,9 @@ module ActiveRecord
|
|
203
227
|
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
204
228
|
end
|
205
229
|
|
206
|
-
def
|
230
|
+
def test_recreates_database_with_no_default_options
|
207
231
|
@connection.expects(:recreate_database).
|
208
|
-
with('test-db',
|
232
|
+
with('test-db', {})
|
209
233
|
|
210
234
|
ActiveRecord::Tasks::DatabaseTasks.purge @configuration
|
211
235
|
end
|
@@ -223,7 +247,7 @@ module ActiveRecord
|
|
223
247
|
def setup
|
224
248
|
@connection = stub(:create_database => true)
|
225
249
|
@configuration = {
|
226
|
-
'adapter' => '
|
250
|
+
'adapter' => 'mysql2',
|
227
251
|
'database' => 'my-app-db'
|
228
252
|
}
|
229
253
|
|
@@ -241,7 +265,7 @@ module ActiveRecord
|
|
241
265
|
def setup
|
242
266
|
@connection = stub(:create_database => true)
|
243
267
|
@configuration = {
|
244
|
-
'adapter' => '
|
268
|
+
'adapter' => 'mysql2',
|
245
269
|
'database' => 'my-app-db'
|
246
270
|
}
|
247
271
|
|
@@ -258,43 +282,53 @@ module ActiveRecord
|
|
258
282
|
class MySQLStructureDumpTest < ActiveRecord::TestCase
|
259
283
|
def setup
|
260
284
|
@configuration = {
|
261
|
-
'adapter' => '
|
285
|
+
'adapter' => 'mysql2',
|
262
286
|
'database' => 'test-db'
|
263
287
|
}
|
264
288
|
end
|
265
289
|
|
266
290
|
def test_structure_dump
|
267
291
|
filename = "awesome-file.sql"
|
268
|
-
Kernel.expects(:system).with("mysqldump", "--result-file", filename, "--no-data", "test-db").returns(true)
|
292
|
+
Kernel.expects(:system).with("mysqldump", "--result-file", filename, "--no-data", "--routines", "test-db").returns(true)
|
269
293
|
|
270
294
|
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
|
271
295
|
end
|
272
296
|
|
273
|
-
def
|
297
|
+
def test_warn_when_external_structure_dump_command_execution_fails
|
274
298
|
filename = "awesome-file.sql"
|
275
|
-
Kernel.expects(:system)
|
299
|
+
Kernel.expects(:system)
|
300
|
+
.with("mysqldump", "--result-file", filename, "--no-data", "--routines", "test-db")
|
301
|
+
.returns(false)
|
276
302
|
|
277
|
-
|
303
|
+
e = assert_raise(RuntimeError) {
|
278
304
|
ActiveRecord::Tasks::DatabaseTasks.structure_dump(@configuration, filename)
|
279
|
-
|
280
|
-
|
281
|
-
assert_match(/Could not dump the database structure/, warnings)
|
305
|
+
}
|
306
|
+
assert_match(/^failed to execute: `mysqldump`$/, e.message)
|
282
307
|
end
|
283
308
|
|
284
309
|
def test_structure_dump_with_port_number
|
285
310
|
filename = "awesome-file.sql"
|
286
|
-
Kernel.expects(:system).with("mysqldump", "--port
|
311
|
+
Kernel.expects(:system).with("mysqldump", "--port=10000", "--result-file", filename, "--no-data", "--routines", "test-db").returns(true)
|
287
312
|
|
288
313
|
ActiveRecord::Tasks::DatabaseTasks.structure_dump(
|
289
314
|
@configuration.merge('port' => 10000),
|
290
315
|
filename)
|
291
316
|
end
|
317
|
+
|
318
|
+
def test_structure_dump_with_ssl
|
319
|
+
filename = "awesome-file.sql"
|
320
|
+
Kernel.expects(:system).with("mysqldump", "--ssl-ca=ca.crt", "--result-file", filename, "--no-data", "--routines", "test-db").returns(true)
|
321
|
+
|
322
|
+
ActiveRecord::Tasks::DatabaseTasks.structure_dump(
|
323
|
+
@configuration.merge("sslca" => "ca.crt"),
|
324
|
+
filename)
|
325
|
+
end
|
292
326
|
end
|
293
327
|
|
294
328
|
class MySQLStructureLoadTest < ActiveRecord::TestCase
|
295
329
|
def setup
|
296
330
|
@configuration = {
|
297
|
-
'adapter' => '
|
331
|
+
'adapter' => 'mysql2',
|
298
332
|
'database' => 'test-db'
|
299
333
|
}
|
300
334
|
end
|
@@ -302,10 +336,10 @@ module ActiveRecord
|
|
302
336
|
def test_structure_load
|
303
337
|
filename = "awesome-file.sql"
|
304
338
|
Kernel.expects(:system).with('mysql', '--execute', %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}, "--database", "test-db")
|
339
|
+
.returns(true)
|
305
340
|
|
306
341
|
ActiveRecord::Tasks::DatabaseTasks.structure_load(@configuration, filename)
|
307
342
|
end
|
308
343
|
end
|
309
|
-
|
310
344
|
end
|
311
345
|
end
|