ibm_db 5.2.0-x86-mingw32 → 5.4.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (625) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES +9 -0
  3. data/LICENSE +55 -18
  4. data/README +1 -1
  5. data/debug.log +1 -0
  6. data/ext/Makefile +28 -24
  7. data/ext/ibm_db.c +66 -65
  8. data/ext/ibm_db.o +0 -0
  9. data/ext/ibm_db.so +0 -0
  10. data/ext/mkmf.log +26 -24
  11. data/ext/ruby_ibm_db_cli.c +1 -0
  12. data/ext/ruby_ibm_db_cli.o +0 -0
  13. data/lib/active_record/connection_adapters/ibm_db_adapter.rb +1520 -1282
  14. data/lib/ibm_db.so +1 -0
  15. data/lib/mswin32/ibm_db.rb +3 -1
  16. data/lib/mswin32/rb3x/i386/ruby30/ibm_db.so +0 -0
  17. data/lib/mswin32/rb3x/i386/ruby31/ibm_db.so +0 -0
  18. data/test/active_record/connection_adapters/fake_adapter.rb +5 -2
  19. data/test/activejob/destroy_association_async_test.rb +305 -0
  20. data/test/activejob/destroy_async_job_not_present_test.rb +31 -0
  21. data/test/activejob/helper.rb +15 -0
  22. data/test/assets/schema_dump_5_1.yml +345 -0
  23. data/test/cases/adapter_prevent_writes_test.rb +334 -0
  24. data/test/cases/adapter_test.rb +432 -218
  25. data/test/cases/adapters/mysql2/active_schema_test.rb +85 -75
  26. data/test/cases/adapters/mysql2/auto_increment_test.rb +34 -0
  27. data/test/cases/adapters/mysql2/bind_parameter_test.rb +5 -3
  28. data/test/cases/adapters/mysql2/boolean_test.rb +6 -4
  29. data/test/cases/adapters/mysql2/case_sensitivity_test.rb +26 -24
  30. data/test/cases/adapters/mysql2/charset_collation_test.rb +20 -17
  31. data/test/cases/adapters/mysql2/connection_test.rb +48 -50
  32. data/test/cases/adapters/mysql2/count_deleted_rows_with_lock_test.rb +28 -0
  33. data/test/cases/adapters/mysql2/datetime_precision_quoting_test.rb +23 -19
  34. data/test/cases/adapters/mysql2/enum_test.rb +32 -11
  35. data/test/cases/adapters/mysql2/explain_test.rb +13 -11
  36. data/test/cases/adapters/mysql2/json_test.rb +17 -188
  37. data/test/cases/adapters/mysql2/mysql2_adapter_prevent_writes_test.rb +208 -0
  38. data/test/cases/adapters/mysql2/mysql2_adapter_test.rb +183 -28
  39. data/test/cases/adapters/mysql2/nested_deadlock_test.rb +75 -0
  40. data/test/cases/adapters/mysql2/optimizer_hints_test.rb +69 -0
  41. data/test/cases/adapters/mysql2/schema_migrations_test.rb +26 -21
  42. data/test/cases/adapters/mysql2/schema_test.rb +24 -22
  43. data/test/cases/adapters/mysql2/set_test.rb +32 -0
  44. data/test/cases/adapters/mysql2/sp_test.rb +10 -8
  45. data/test/cases/adapters/mysql2/sql_types_test.rb +8 -6
  46. data/test/cases/adapters/mysql2/table_options_test.rb +93 -10
  47. data/test/cases/adapters/mysql2/transaction_test.rb +151 -0
  48. data/test/cases/adapters/mysql2/unsigned_type_test.rb +11 -9
  49. data/test/cases/adapters/mysql2/virtual_column_test.rb +66 -0
  50. data/test/cases/adapters/postgresql/active_schema_test.rb +40 -25
  51. data/test/cases/adapters/postgresql/array_test.rb +118 -63
  52. data/test/cases/adapters/postgresql/bit_string_test.rb +12 -10
  53. data/test/cases/adapters/postgresql/bytea_test.rb +26 -25
  54. data/test/cases/adapters/postgresql/case_insensitive_test.rb +10 -9
  55. data/test/cases/adapters/postgresql/change_schema_test.rb +7 -5
  56. data/test/cases/adapters/postgresql/cidr_test.rb +2 -0
  57. data/test/cases/adapters/postgresql/citext_test.rb +58 -58
  58. data/test/cases/adapters/postgresql/collation_test.rb +17 -15
  59. data/test/cases/adapters/postgresql/composite_test.rb +25 -23
  60. data/test/cases/adapters/postgresql/connection_test.rb +73 -85
  61. data/test/cases/adapters/postgresql/create_unlogged_tables_test.rb +74 -0
  62. data/test/cases/adapters/postgresql/datatype_test.rb +19 -22
  63. data/test/cases/adapters/postgresql/date_test.rb +42 -0
  64. data/test/cases/adapters/postgresql/domain_test.rb +9 -7
  65. data/test/cases/adapters/postgresql/enum_test.rb +12 -10
  66. data/test/cases/adapters/postgresql/explain_test.rb +10 -8
  67. data/test/cases/adapters/postgresql/extension_migration_test.rb +13 -12
  68. data/test/cases/adapters/postgresql/foreign_table_test.rb +109 -0
  69. data/test/cases/adapters/postgresql/full_text_test.rb +8 -6
  70. data/test/cases/adapters/postgresql/geometric_test.rb +57 -63
  71. data/test/cases/adapters/postgresql/hstore_test.rb +288 -280
  72. data/test/cases/adapters/postgresql/infinity_test.rb +54 -15
  73. data/test/cases/adapters/postgresql/integer_test.rb +2 -0
  74. data/test/cases/adapters/postgresql/interval_test.rb +99 -0
  75. data/test/cases/adapters/postgresql/json_test.rb +16 -201
  76. data/test/cases/adapters/postgresql/ltree_test.rb +14 -16
  77. data/test/cases/adapters/postgresql/money_test.rb +47 -16
  78. data/test/cases/adapters/postgresql/network_test.rb +36 -28
  79. data/test/cases/adapters/postgresql/numbers_test.rb +7 -5
  80. data/test/cases/adapters/postgresql/optimizer_hints_test.rb +71 -0
  81. data/test/cases/adapters/postgresql/partitions_test.rb +22 -0
  82. data/test/cases/adapters/postgresql/postgresql_adapter_prevent_writes_test.rb +205 -0
  83. data/test/cases/adapters/postgresql/postgresql_adapter_test.rb +178 -136
  84. data/test/cases/adapters/postgresql/prepared_statements_disabled_test.rb +27 -0
  85. data/test/cases/adapters/postgresql/quoting_test.rb +12 -6
  86. data/test/cases/adapters/postgresql/range_test.rb +406 -292
  87. data/test/cases/adapters/postgresql/referential_integrity_test.rb +16 -15
  88. data/test/cases/adapters/postgresql/rename_table_test.rb +9 -8
  89. data/test/cases/adapters/postgresql/schema_authorization_test.rb +14 -23
  90. data/test/cases/adapters/postgresql/schema_test.rb +207 -91
  91. data/test/cases/adapters/postgresql/serial_test.rb +9 -7
  92. data/test/cases/adapters/postgresql/statement_pool_test.rb +26 -6
  93. data/test/cases/adapters/postgresql/timestamp_test.rb +17 -15
  94. data/test/cases/adapters/postgresql/transaction_nested_test.rb +114 -0
  95. data/test/cases/adapters/postgresql/transaction_test.rb +189 -0
  96. data/test/cases/adapters/postgresql/type_lookup_test.rb +12 -10
  97. data/test/cases/adapters/postgresql/utils_test.rb +11 -9
  98. data/test/cases/adapters/postgresql/uuid_test.rb +226 -109
  99. data/test/cases/adapters/postgresql/xml_test.rb +10 -14
  100. data/test/cases/adapters/sqlite3/collation_test.rb +26 -15
  101. data/test/cases/adapters/sqlite3/copy_table_test.rb +31 -28
  102. data/test/cases/adapters/sqlite3/explain_test.rb +13 -11
  103. data/test/cases/adapters/sqlite3/json_test.rb +29 -0
  104. data/test/cases/adapters/sqlite3/quoting_test.rb +35 -57
  105. data/test/cases/adapters/sqlite3/sqlite3_adapter_prevent_writes_test.rb +186 -0
  106. data/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb +318 -131
  107. data/test/cases/adapters/sqlite3/sqlite3_create_folder_test.rb +11 -11
  108. data/test/cases/adapters/sqlite3/statement_pool_test.rb +7 -6
  109. data/test/cases/adapters/sqlite3/transaction_test.rb +123 -0
  110. data/test/cases/aggregations_test.rb +14 -12
  111. data/test/cases/annotate_test.rb +46 -0
  112. data/test/cases/ar_schema_test.rb +153 -86
  113. data/test/cases/arel/attributes/attribute_test.rb +1145 -0
  114. data/test/cases/arel/attributes/math_test.rb +83 -0
  115. data/test/cases/arel/attributes_test.rb +27 -0
  116. data/test/cases/arel/collectors/bind_test.rb +40 -0
  117. data/test/cases/arel/collectors/composite_test.rb +47 -0
  118. data/test/cases/arel/collectors/sql_string_test.rb +41 -0
  119. data/test/cases/arel/collectors/substitute_bind_collector_test.rb +48 -0
  120. data/test/cases/arel/crud_test.rb +65 -0
  121. data/test/cases/arel/delete_manager_test.rb +53 -0
  122. data/test/cases/arel/factory_methods_test.rb +46 -0
  123. data/test/cases/arel/helper.rb +45 -0
  124. data/test/cases/arel/insert_manager_test.rb +241 -0
  125. data/test/cases/arel/nodes/and_test.rb +30 -0
  126. data/test/cases/arel/nodes/as_test.rb +36 -0
  127. data/test/cases/arel/nodes/ascending_test.rb +46 -0
  128. data/test/cases/arel/nodes/bin_test.rb +35 -0
  129. data/test/cases/arel/nodes/binary_test.rb +29 -0
  130. data/test/cases/arel/nodes/bind_param_test.rb +22 -0
  131. data/test/cases/arel/nodes/case_test.rb +96 -0
  132. data/test/cases/arel/nodes/casted_test.rb +18 -0
  133. data/test/cases/arel/nodes/comment_test.rb +22 -0
  134. data/test/cases/arel/nodes/count_test.rb +35 -0
  135. data/test/cases/arel/nodes/delete_statement_test.rb +36 -0
  136. data/test/cases/arel/nodes/descending_test.rb +46 -0
  137. data/test/cases/arel/nodes/distinct_test.rb +21 -0
  138. data/test/cases/arel/nodes/equality_test.rb +62 -0
  139. data/test/cases/arel/nodes/extract_test.rb +43 -0
  140. data/test/cases/arel/nodes/false_test.rb +21 -0
  141. data/test/cases/arel/nodes/grouping_test.rb +26 -0
  142. data/test/cases/arel/nodes/infix_operation_test.rb +42 -0
  143. data/test/cases/arel/nodes/insert_statement_test.rb +44 -0
  144. data/test/cases/arel/nodes/named_function_test.rb +48 -0
  145. data/test/cases/arel/nodes/node_test.rb +22 -0
  146. data/test/cases/arel/nodes/not_test.rb +31 -0
  147. data/test/cases/arel/nodes/or_test.rb +36 -0
  148. data/test/cases/arel/nodes/over_test.rb +69 -0
  149. data/test/cases/arel/nodes/select_core_test.rb +79 -0
  150. data/test/cases/arel/nodes/select_statement_test.rb +51 -0
  151. data/test/cases/arel/nodes/sql_literal_test.rb +75 -0
  152. data/test/cases/arel/nodes/sum_test.rb +35 -0
  153. data/test/cases/arel/nodes/table_alias_test.rb +29 -0
  154. data/test/cases/arel/nodes/true_test.rb +21 -0
  155. data/test/cases/arel/nodes/unary_operation_test.rb +41 -0
  156. data/test/cases/arel/nodes/update_statement_test.rb +60 -0
  157. data/test/cases/arel/nodes/window_test.rb +81 -0
  158. data/test/cases/arel/nodes_test.rb +34 -0
  159. data/test/cases/arel/select_manager_test.rb +1238 -0
  160. data/test/cases/arel/support/fake_record.rb +135 -0
  161. data/test/cases/arel/table_test.rb +216 -0
  162. data/test/cases/arel/update_manager_test.rb +126 -0
  163. data/test/cases/arel/visitors/dispatch_contamination_test.rb +78 -0
  164. data/test/cases/arel/visitors/dot_test.rb +90 -0
  165. data/test/cases/arel/visitors/mysql_test.rb +157 -0
  166. data/test/cases/arel/visitors/postgres_test.rb +366 -0
  167. data/test/cases/arel/visitors/sqlite_test.rb +75 -0
  168. data/test/cases/arel/visitors/to_sql_test.rb +750 -0
  169. data/test/cases/associations/belongs_to_associations_test.rb +510 -158
  170. data/test/cases/associations/bidirectional_destroy_dependencies_test.rb +4 -2
  171. data/test/cases/associations/callbacks_test.rb +56 -38
  172. data/test/cases/associations/cascaded_eager_loading_test.rb +118 -61
  173. data/test/cases/associations/eager_load_includes_full_sti_class_test.rb +138 -18
  174. data/test/cases/associations/eager_load_nested_include_test.rb +38 -37
  175. data/test/cases/associations/eager_singularization_test.rb +21 -21
  176. data/test/cases/associations/eager_test.rb +559 -415
  177. data/test/cases/associations/extension_test.rb +18 -12
  178. data/test/cases/associations/has_and_belongs_to_many_associations_test.rb +234 -213
  179. data/test/cases/associations/has_many_associations_test.rb +1038 -465
  180. data/test/cases/associations/has_many_through_associations_test.rb +558 -249
  181. data/test/cases/associations/has_one_associations_test.rb +294 -129
  182. data/test/cases/associations/has_one_through_associations_test.rb +121 -75
  183. data/test/cases/associations/inner_join_association_test.rb +114 -38
  184. data/test/cases/associations/inverse_associations_test.rb +606 -398
  185. data/test/cases/associations/join_model_test.rb +158 -148
  186. data/test/cases/associations/left_outer_join_association_test.rb +59 -24
  187. data/test/cases/associations/nested_through_associations_test.rb +166 -109
  188. data/test/cases/associations/required_test.rb +35 -10
  189. data/test/cases/associations_test.rb +241 -110
  190. data/test/cases/attribute_methods/read_test.rb +11 -11
  191. data/test/cases/attribute_methods_test.rb +413 -298
  192. data/test/cases/attributes_test.rb +145 -27
  193. data/test/cases/autosave_association_test.rb +681 -436
  194. data/test/cases/base_prevent_writes_test.rb +229 -0
  195. data/test/cases/base_test.rb +599 -542
  196. data/test/cases/batches_test.rb +288 -82
  197. data/test/cases/binary_test.rb +26 -31
  198. data/test/cases/bind_parameter_test.rb +194 -21
  199. data/test/cases/boolean_test.rb +52 -0
  200. data/test/cases/cache_key_test.rb +110 -5
  201. data/test/cases/calculations_test.rb +740 -177
  202. data/test/cases/callbacks_test.rb +74 -207
  203. data/test/cases/clone_test.rb +15 -10
  204. data/test/cases/coders/json_test.rb +2 -0
  205. data/test/cases/coders/yaml_column_test.rb +16 -13
  206. data/test/cases/collection_cache_key_test.rb +177 -20
  207. data/test/cases/column_alias_test.rb +9 -7
  208. data/test/cases/column_definition_test.rb +10 -68
  209. data/test/cases/comment_test.rb +166 -107
  210. data/test/cases/connection_adapters/adapter_leasing_test.rb +14 -10
  211. data/test/cases/connection_adapters/connection_handler_test.rb +358 -51
  212. data/test/cases/connection_adapters/connection_handlers_multi_db_test.rb +400 -0
  213. data/test/cases/connection_adapters/connection_handlers_multi_pool_config_test.rb +103 -0
  214. data/test/cases/connection_adapters/connection_handlers_sharding_db_test.rb +499 -0
  215. data/test/cases/connection_adapters/connection_swapping_nested_test.rb +457 -0
  216. data/test/cases/connection_adapters/legacy_connection_handlers_multi_db_test.rb +486 -0
  217. data/test/cases/connection_adapters/legacy_connection_handlers_sharding_db_test.rb +586 -0
  218. data/test/cases/connection_adapters/merge_and_resolve_default_url_config_test.rb +319 -138
  219. data/test/cases/connection_adapters/mysql_type_lookup_test.rb +62 -50
  220. data/test/cases/connection_adapters/schema_cache_test.rb +259 -26
  221. data/test/cases/connection_adapters/type_lookup_test.rb +96 -95
  222. data/test/cases/connection_management_test.rb +13 -11
  223. data/test/cases/connection_pool_test.rb +316 -83
  224. data/test/cases/core_test.rb +82 -58
  225. data/test/cases/counter_cache_test.rb +204 -50
  226. data/test/cases/custom_locking_test.rb +5 -3
  227. data/test/cases/database_configurations/hash_config_test.rb +74 -0
  228. data/test/cases/database_configurations/resolver_test.rb +150 -0
  229. data/test/cases/database_configurations_test.rb +145 -0
  230. data/test/cases/database_selector_test.rb +296 -0
  231. data/test/cases/database_statements_test.rb +18 -16
  232. data/test/cases/date_test.rb +8 -16
  233. data/test/cases/date_time_precision_test.rb +100 -78
  234. data/test/cases/date_time_test.rb +23 -8
  235. data/test/cases/defaults_test.rb +106 -71
  236. data/test/cases/delegated_type_test.rb +57 -0
  237. data/test/cases/dirty_test.rb +419 -223
  238. data/test/cases/disconnected_test.rb +6 -6
  239. data/test/cases/dup_test.rb +54 -27
  240. data/test/cases/enum_test.rb +461 -82
  241. data/test/cases/errors_test.rb +7 -7
  242. data/test/cases/explain_subscriber_test.rb +17 -15
  243. data/test/cases/explain_test.rb +11 -19
  244. data/test/cases/filter_attributes_test.rb +153 -0
  245. data/test/cases/finder_respond_to_test.rb +14 -14
  246. data/test/cases/finder_test.rb +669 -287
  247. data/test/cases/fixture_set/file_test.rb +34 -38
  248. data/test/cases/fixtures_test.rb +833 -176
  249. data/test/cases/forbidden_attributes_protection_test.rb +32 -67
  250. data/test/cases/habtm_destroy_order_test.rb +25 -25
  251. data/test/cases/helper.rb +78 -49
  252. data/test/cases/hot_compatibility_test.rb +33 -32
  253. data/test/cases/i18n_test.rb +18 -17
  254. data/test/cases/inheritance_test.rb +180 -115
  255. data/test/cases/insert_all_test.rb +489 -0
  256. data/test/cases/instrumentation_test.rb +101 -0
  257. data/test/cases/integration_test.rb +119 -31
  258. data/test/cases/invalid_connection_test.rb +18 -16
  259. data/test/cases/invertible_migration_test.rb +183 -43
  260. data/test/cases/json_attribute_test.rb +35 -0
  261. data/test/cases/json_serialization_test.rb +57 -58
  262. data/test/cases/json_shared_test_cases.rb +290 -0
  263. data/test/cases/locking_test.rb +413 -119
  264. data/test/cases/log_subscriber_test.rb +68 -26
  265. data/test/cases/marshal_serialization_test.rb +39 -0
  266. data/test/cases/migration/change_schema_test.rb +118 -72
  267. data/test/cases/migration/change_table_test.rb +138 -30
  268. data/test/cases/migration/check_constraint_test.rb +162 -0
  269. data/test/cases/migration/column_attributes_test.rb +45 -35
  270. data/test/cases/migration/column_positioning_test.rb +18 -6
  271. data/test/cases/migration/columns_test.rb +93 -77
  272. data/test/cases/migration/command_recorder_test.rb +121 -34
  273. data/test/cases/migration/compatibility_test.rb +578 -23
  274. data/test/cases/migration/create_join_table_test.rb +35 -25
  275. data/test/cases/migration/foreign_key_test.rb +503 -284
  276. data/test/cases/migration/helper.rb +4 -3
  277. data/test/cases/migration/index_test.rb +119 -70
  278. data/test/cases/migration/logger_test.rb +9 -6
  279. data/test/cases/migration/pending_migrations_test.rb +88 -34
  280. data/test/cases/migration/references_foreign_key_test.rb +164 -150
  281. data/test/cases/migration/references_index_test.rb +38 -19
  282. data/test/cases/migration/references_statements_test.rb +15 -14
  283. data/test/cases/migration/rename_table_test.rb +53 -30
  284. data/test/cases/migration_test.rb +637 -269
  285. data/test/cases/migrator_test.rb +191 -135
  286. data/test/cases/mixin_test.rb +7 -11
  287. data/test/cases/modules_test.rb +36 -34
  288. data/test/cases/multi_db_migrator_test.rb +223 -0
  289. data/test/cases/multiparameter_attributes_test.rb +60 -33
  290. data/test/cases/multiple_db_test.rb +16 -22
  291. data/test/cases/nested_attributes_test.rb +341 -320
  292. data/test/cases/nested_attributes_with_callbacks_test.rb +26 -24
  293. data/test/cases/null_relation_test.rb +84 -0
  294. data/test/cases/numeric_data_test.rb +93 -0
  295. data/test/cases/persistence_test.rb +361 -269
  296. data/test/cases/pooled_connections_test.rb +18 -26
  297. data/test/cases/prepared_statement_status_test.rb +48 -0
  298. data/test/cases/primary_keys_test.rb +210 -104
  299. data/test/cases/query_cache_test.rb +610 -141
  300. data/test/cases/quoting_test.rb +132 -31
  301. data/test/cases/readonly_test.rb +49 -48
  302. data/test/cases/reaper_test.rb +146 -32
  303. data/test/cases/reflection_test.rb +167 -156
  304. data/test/cases/relation/delegation_test.rb +49 -36
  305. data/test/cases/relation/delete_all_test.rb +117 -0
  306. data/test/cases/relation/merging_test.rb +319 -42
  307. data/test/cases/relation/mutation_test.rb +55 -93
  308. data/test/cases/relation/or_test.rb +129 -29
  309. data/test/cases/relation/predicate_builder_test.rb +21 -6
  310. data/test/cases/relation/record_fetch_warning_test.rb +5 -3
  311. data/test/cases/relation/select_test.rb +67 -0
  312. data/test/cases/relation/update_all_test.rb +317 -0
  313. data/test/cases/relation/where_chain_test.rb +68 -32
  314. data/test/cases/relation/where_clause_test.rb +136 -61
  315. data/test/cases/relation/where_test.rb +155 -48
  316. data/test/cases/relation_test.rb +266 -112
  317. data/test/cases/relations_test.rb +969 -744
  318. data/test/cases/reload_models_test.rb +13 -9
  319. data/test/cases/reserved_word_test.rb +141 -0
  320. data/test/cases/result_test.rb +68 -17
  321. data/test/cases/sanitize_test.rb +87 -71
  322. data/test/cases/schema_dumper_test.rb +221 -128
  323. data/test/cases/schema_loading_test.rb +3 -2
  324. data/test/cases/scoping/default_scoping_test.rb +185 -144
  325. data/test/cases/scoping/named_scoping_test.rb +177 -89
  326. data/test/cases/scoping/relation_scoping_test.rb +197 -75
  327. data/test/cases/secure_token_test.rb +18 -3
  328. data/test/cases/serialization_test.rb +30 -28
  329. data/test/cases/serialized_attribute_test.rb +133 -42
  330. data/test/cases/signed_id_test.rb +168 -0
  331. data/test/cases/statement_cache_test.rb +41 -24
  332. data/test/cases/statement_invalid_test.rb +42 -0
  333. data/test/cases/store_test.rb +180 -55
  334. data/test/cases/strict_loading_test.rb +473 -0
  335. data/test/cases/suppressor_test.rb +26 -12
  336. data/test/cases/tasks/database_tasks_test.rb +1258 -194
  337. data/test/cases/tasks/mysql_rake_test.rb +370 -298
  338. data/test/cases/tasks/postgresql_rake_test.rb +481 -251
  339. data/test/cases/tasks/sqlite_rake_test.rb +225 -178
  340. data/test/cases/test_case.rb +51 -40
  341. data/test/cases/test_databases_test.rb +79 -0
  342. data/test/cases/test_fixtures_test.rb +79 -19
  343. data/test/cases/time_precision_test.rb +98 -76
  344. data/test/cases/timestamp_test.rb +102 -99
  345. data/test/cases/touch_later_test.rb +12 -10
  346. data/test/cases/transaction_callbacks_test.rb +344 -90
  347. data/test/cases/transaction_isolation_test.rb +12 -12
  348. data/test/cases/transactions_test.rb +612 -162
  349. data/test/cases/type/adapter_specific_registry_test.rb +14 -2
  350. data/test/cases/type/date_time_test.rb +4 -2
  351. data/test/cases/type/integer_test.rb +4 -2
  352. data/test/cases/type/string_test.rb +10 -8
  353. data/test/cases/type/time_test.rb +28 -0
  354. data/test/cases/type/type_map_test.rb +29 -28
  355. data/test/cases/type/unsigned_integer_test.rb +19 -0
  356. data/test/cases/type_test.rb +2 -0
  357. data/test/cases/types_test.rb +3 -1
  358. data/test/cases/unconnected_test.rb +14 -1
  359. data/test/cases/unsafe_raw_sql_test.rb +274 -0
  360. data/test/cases/validations/absence_validation_test.rb +19 -17
  361. data/test/cases/validations/association_validation_test.rb +30 -28
  362. data/test/cases/validations/i18n_generate_message_validation_test.rb +34 -16
  363. data/test/cases/validations/i18n_validation_test.rb +22 -21
  364. data/test/cases/validations/length_validation_test.rb +34 -33
  365. data/test/cases/validations/numericality_validation_test.rb +181 -0
  366. data/test/cases/validations/presence_validation_test.rb +21 -19
  367. data/test/cases/validations/uniqueness_validation_test.rb +156 -86
  368. data/test/cases/validations_repair_helper.rb +2 -0
  369. data/test/cases/validations_test.rb +61 -26
  370. data/test/cases/view_test.rb +122 -116
  371. data/test/cases/yaml_serialization_test.rb +79 -34
  372. data/test/config.example.yml +19 -19
  373. data/test/config.rb +3 -1
  374. data/test/config.yml +16 -6
  375. data/test/fixtures/all/namespaced/accounts.yml +2 -0
  376. data/test/fixtures/author_addresses.yml +1 -8
  377. data/test/fixtures/authors.yml +1 -7
  378. data/test/fixtures/binaries.yml +4 -0
  379. data/test/fixtures/books.yml +9 -2
  380. data/test/fixtures/categories_posts.yml +3 -0
  381. data/test/fixtures/citations.yml +5 -0
  382. data/test/fixtures/comments.yml +7 -0
  383. data/test/fixtures/companies.yml +5 -0
  384. data/test/fixtures/computers.yml +2 -0
  385. data/test/fixtures/customers.yml +10 -1
  386. data/test/fixtures/developers.yml +1 -1
  387. data/test/fixtures/essays.yml +10 -0
  388. data/test/fixtures/faces.yml +3 -3
  389. data/test/fixtures/humans.yml +5 -0
  390. data/test/fixtures/interests.yml +7 -7
  391. data/test/fixtures/memberships.yml +7 -0
  392. data/test/fixtures/minimalistics.yml +3 -0
  393. data/test/fixtures/mixed_case_monkeys.yml +2 -2
  394. data/test/fixtures/naked/yml/courses_with_invalid_key.yml +3 -0
  395. data/test/fixtures/naked/yml/parrots.yml +1 -0
  396. data/test/fixtures/other_books.yml +26 -0
  397. data/test/fixtures/other_posts.yml +1 -0
  398. data/test/fixtures/parrots.yml +7 -1
  399. data/test/fixtures/pirates.yml +3 -0
  400. data/test/fixtures/posts.yml +11 -3
  401. data/test/fixtures/readers.yml +6 -0
  402. data/test/fixtures/reserved_words/values.yml +2 -2
  403. data/test/fixtures/sponsors.yml +3 -0
  404. data/test/fixtures/strict_zines.yml +2 -0
  405. data/test/fixtures/subscribers.yml +1 -1
  406. data/test/fixtures/tasks.yml +1 -1
  407. data/test/fixtures/warehouse-things.yml +3 -0
  408. data/test/migrations/10_urban/9_add_expressions.rb +2 -0
  409. data/test/migrations/decimal/1_give_me_big_numbers.rb +6 -4
  410. data/test/migrations/magic/1_currencies_have_symbols.rb +3 -2
  411. data/test/migrations/missing/1000_people_have_middle_names.rb +2 -0
  412. data/test/migrations/missing/1_people_have_last_names.rb +2 -0
  413. data/test/migrations/missing/3_we_need_reminders.rb +2 -0
  414. data/test/migrations/missing/4_innocent_jointable.rb +3 -1
  415. data/test/migrations/rename/1_we_need_things.rb +2 -0
  416. data/test/migrations/rename/2_rename_things.rb +2 -0
  417. data/test/migrations/to_copy/1_people_have_hobbies.rb +3 -1
  418. data/test/migrations/to_copy/2_people_have_descriptions.rb +3 -1
  419. data/test/migrations/to_copy2/1_create_articles.rb +2 -0
  420. data/test/migrations/to_copy2/2_create_comments.rb +3 -1
  421. data/test/migrations/to_copy_with_name_collision/1_people_have_hobbies.rb +3 -1
  422. data/test/migrations/to_copy_with_timestamps/20090101010101_people_have_hobbies.rb +3 -1
  423. data/test/migrations/to_copy_with_timestamps/20090101010202_people_have_descriptions.rb +3 -1
  424. data/test/migrations/to_copy_with_timestamps2/20090101010101_create_articles.rb +2 -0
  425. data/test/migrations/to_copy_with_timestamps2/20090101010202_create_comments.rb +2 -0
  426. data/test/migrations/valid/1_valid_people_have_last_names.rb +2 -0
  427. data/test/migrations/valid/2_we_need_reminders.rb +2 -0
  428. data/test/migrations/valid/3_innocent_jointable.rb +3 -1
  429. data/test/migrations/valid_with_subdirectories/1_valid_people_have_last_names.rb +2 -0
  430. data/test/migrations/valid_with_subdirectories/sub/2_we_need_reminders.rb +2 -0
  431. data/test/migrations/valid_with_subdirectories/sub1/3_innocent_jointable.rb +3 -1
  432. data/test/migrations/valid_with_timestamps/20100101010101_valid_with_timestamps_people_have_last_names.rb +2 -0
  433. data/test/migrations/valid_with_timestamps/20100201010101_valid_with_timestamps_we_need_reminders.rb +2 -0
  434. data/test/migrations/valid_with_timestamps/20100301010101_valid_with_timestamps_innocent_jointable.rb +3 -1
  435. data/test/migrations/version_check/20131219224947_migration_version_check.rb +2 -0
  436. data/test/models/account.rb +46 -0
  437. data/test/models/admin/account.rb +3 -1
  438. data/test/models/admin/randomly_named_c1.rb +2 -0
  439. data/test/models/admin/user.rb +16 -8
  440. data/test/models/admin.rb +4 -2
  441. data/test/models/aircraft.rb +3 -1
  442. data/test/models/arunit2_model.rb +2 -0
  443. data/test/models/author.rb +153 -102
  444. data/test/models/auto_id.rb +2 -0
  445. data/test/models/autoloadable/extra_firm.rb +2 -0
  446. data/test/models/binary.rb +3 -1
  447. data/test/models/binary_field.rb +6 -0
  448. data/test/models/bird.rb +13 -1
  449. data/test/models/book.rb +14 -4
  450. data/test/models/book_destroy_async.rb +24 -0
  451. data/test/models/boolean.rb +5 -0
  452. data/test/models/bulb.rb +13 -4
  453. data/test/models/cake_designer.rb +2 -0
  454. data/test/models/car.rb +17 -10
  455. data/test/models/carrier.rb +2 -0
  456. data/test/models/cart.rb +5 -0
  457. data/test/models/cat.rb +2 -0
  458. data/test/models/categorization.rb +8 -6
  459. data/test/models/category.rb +28 -16
  460. data/test/models/chef.rb +2 -0
  461. data/test/models/citation.rb +5 -1
  462. data/test/models/club.rb +13 -10
  463. data/test/models/college.rb +4 -2
  464. data/test/models/column.rb +2 -0
  465. data/test/models/column_name.rb +2 -0
  466. data/test/models/comment.rb +32 -10
  467. data/test/models/company.rb +102 -106
  468. data/test/models/company_in_module.rb +27 -26
  469. data/test/models/computer.rb +3 -1
  470. data/test/models/contact.rb +15 -13
  471. data/test/models/content.rb +5 -3
  472. data/test/models/contract.rb +21 -3
  473. data/test/models/country.rb +2 -4
  474. data/test/models/course.rb +3 -1
  475. data/test/models/customer.rb +10 -8
  476. data/test/models/customer_carrier.rb +2 -0
  477. data/test/models/dashboard.rb +2 -0
  478. data/test/models/default.rb +2 -0
  479. data/test/models/department.rb +2 -0
  480. data/test/models/destroy_async_parent.rb +15 -0
  481. data/test/models/destroy_async_parent_soft_delete.rb +20 -0
  482. data/test/models/developer.rb +152 -85
  483. data/test/models/dl_keyed_belongs_to.rb +13 -0
  484. data/test/models/dl_keyed_belongs_to_soft_delete.rb +19 -0
  485. data/test/models/dl_keyed_has_many.rb +5 -0
  486. data/test/models/dl_keyed_has_many_through.rb +5 -0
  487. data/test/models/dl_keyed_has_one.rb +5 -0
  488. data/test/models/dl_keyed_join.rb +10 -0
  489. data/test/models/dog.rb +2 -0
  490. data/test/models/dog_lover.rb +2 -0
  491. data/test/models/doubloon.rb +3 -1
  492. data/test/models/drink_designer.rb +17 -0
  493. data/test/models/edge.rb +4 -2
  494. data/test/models/electron.rb +2 -0
  495. data/test/models/engine.rb +3 -2
  496. data/test/models/entrant.rb +2 -0
  497. data/test/models/entry.rb +5 -0
  498. data/test/models/essay.rb +6 -3
  499. data/test/models/essay_destroy_async.rb +12 -0
  500. data/test/models/event.rb +3 -1
  501. data/test/models/eye.rb +5 -3
  502. data/test/models/face.rb +14 -6
  503. data/test/models/family.rb +6 -0
  504. data/test/models/family_tree.rb +6 -0
  505. data/test/models/friendship.rb +5 -3
  506. data/test/models/frog.rb +8 -0
  507. data/test/models/guid.rb +3 -1
  508. data/test/models/guitar.rb +2 -0
  509. data/test/models/hotel.rb +5 -3
  510. data/test/models/human.rb +39 -0
  511. data/test/models/image.rb +3 -1
  512. data/test/models/interest.rb +14 -3
  513. data/test/models/invoice.rb +4 -2
  514. data/test/models/item.rb +3 -1
  515. data/test/models/job.rb +5 -3
  516. data/test/models/joke.rb +4 -2
  517. data/test/models/keyboard.rb +3 -1
  518. data/test/models/legacy_thing.rb +2 -0
  519. data/test/models/lesson.rb +2 -0
  520. data/test/models/line_item.rb +3 -1
  521. data/test/models/liquid.rb +2 -0
  522. data/test/models/matey.rb +3 -1
  523. data/test/models/measurement.rb +4 -0
  524. data/test/models/member.rb +23 -20
  525. data/test/models/member_detail.rb +3 -0
  526. data/test/models/member_type.rb +2 -0
  527. data/test/models/membership.rb +4 -1
  528. data/test/models/mentor.rb +3 -1
  529. data/test/models/message.rb +5 -0
  530. data/test/models/minimalistic.rb +2 -0
  531. data/test/models/minivan.rb +3 -2
  532. data/test/models/mixed_case_monkey.rb +3 -1
  533. data/test/models/molecule.rb +2 -0
  534. data/test/models/mouse.rb +6 -0
  535. data/test/models/movie.rb +2 -0
  536. data/test/models/node.rb +4 -2
  537. data/test/models/non_primary_key.rb +2 -0
  538. data/test/models/notification.rb +2 -0
  539. data/test/models/numeric_data.rb +12 -0
  540. data/test/models/order.rb +4 -2
  541. data/test/models/organization.rb +9 -7
  542. data/test/models/other_dog.rb +3 -1
  543. data/test/models/owner.rb +6 -4
  544. data/test/models/parrot.rb +12 -4
  545. data/test/models/person.rb +59 -54
  546. data/test/models/personal_legacy_thing.rb +3 -1
  547. data/test/models/pet.rb +4 -2
  548. data/test/models/pet_treasure.rb +2 -0
  549. data/test/models/pirate.rb +67 -43
  550. data/test/models/possession.rb +3 -1
  551. data/test/models/post.rb +184 -86
  552. data/test/models/price_estimate.rb +11 -1
  553. data/test/models/professor.rb +3 -1
  554. data/test/models/project.rb +14 -12
  555. data/test/models/publisher/article.rb +2 -0
  556. data/test/models/publisher/magazine.rb +2 -0
  557. data/test/models/publisher.rb +2 -0
  558. data/test/models/randomly_named_c1.rb +2 -0
  559. data/test/models/rating.rb +5 -1
  560. data/test/models/reader.rb +7 -5
  561. data/test/models/recipe.rb +2 -0
  562. data/test/models/record.rb +2 -0
  563. data/test/models/reference.rb +6 -3
  564. data/test/models/reply.rb +39 -21
  565. data/test/models/room.rb +6 -0
  566. data/test/models/section.rb +6 -0
  567. data/test/models/seminar.rb +6 -0
  568. data/test/models/session.rb +6 -0
  569. data/test/models/ship.rb +12 -9
  570. data/test/models/ship_part.rb +5 -3
  571. data/test/models/shop.rb +4 -2
  572. data/test/models/shop_account.rb +2 -0
  573. data/test/models/speedometer.rb +2 -0
  574. data/test/models/sponsor.rb +8 -5
  575. data/test/models/squeak.rb +6 -0
  576. data/test/models/strict_zine.rb +7 -0
  577. data/test/models/string_key_object.rb +2 -0
  578. data/test/models/student.rb +2 -0
  579. data/test/models/subscriber.rb +4 -2
  580. data/test/models/subscription.rb +5 -1
  581. data/test/models/tag.rb +6 -3
  582. data/test/models/tagging.rb +13 -6
  583. data/test/models/task.rb +2 -0
  584. data/test/models/topic.rb +54 -19
  585. data/test/models/toy.rb +4 -0
  586. data/test/models/traffic_light.rb +2 -0
  587. data/test/models/treasure.rb +5 -3
  588. data/test/models/treaty.rb +2 -4
  589. data/test/models/tree.rb +2 -0
  590. data/test/models/tuning_peg.rb +2 -0
  591. data/test/models/tyre.rb +2 -0
  592. data/test/models/user.rb +12 -4
  593. data/test/models/uuid_child.rb +2 -0
  594. data/test/models/uuid_item.rb +2 -0
  595. data/test/models/uuid_parent.rb +2 -0
  596. data/test/models/vegetables.rb +12 -3
  597. data/test/models/vertex.rb +6 -4
  598. data/test/models/warehouse_thing.rb +2 -0
  599. data/test/models/wheel.rb +3 -1
  600. data/test/models/without_table.rb +3 -1
  601. data/test/models/zine.rb +3 -1
  602. data/test/schema/mysql2_specific_schema.rb +49 -35
  603. data/test/schema/oracle_specific_schema.rb +13 -15
  604. data/test/schema/postgresql_specific_schema.rb +51 -40
  605. data/test/schema/schema.rb +334 -154
  606. data/test/schema/sqlite_specific_schema.rb +9 -16
  607. data/test/support/config.rb +26 -26
  608. data/test/support/connection.rb +14 -8
  609. data/test/support/connection_helper.rb +3 -1
  610. data/test/support/ddl_helper.rb +2 -0
  611. data/test/support/marshal_compatibility_fixtures/IBM_DB/rails_6_0_topic.dump +0 -0
  612. data/test/support/marshal_compatibility_fixtures/IBM_DB/rails_6_0_topic_associations.dump +0 -0
  613. data/test/support/marshal_compatibility_fixtures/Mysql2/rails_6_0_topic.dump +0 -0
  614. data/test/support/marshal_compatibility_fixtures/Mysql2/rails_6_0_topic_associations.dump +0 -0
  615. data/test/support/marshal_compatibility_fixtures/PostgreSQL/rails_6_0_topic.dump +0 -0
  616. data/test/support/marshal_compatibility_fixtures/PostgreSQL/rails_6_0_topic_associations.dump +0 -0
  617. data/test/support/marshal_compatibility_fixtures/SQLite/rails_6_0_topic.dump +0 -0
  618. data/test/support/marshal_compatibility_fixtures/SQLite/rails_6_0_topic_associations.dump +0 -0
  619. data/test/support/marshal_compatibility_fixtures/legacy_6_0_record_mysql.dump +0 -0
  620. data/test/support/marshal_compatibility_fixtures/legacy_relation.dump +0 -0
  621. data/test/support/schema_dumping_helper.rb +2 -0
  622. data/test/support/stubs/strong_parameters.rb +40 -0
  623. data/test/support/yaml_compatibility_fixtures/rails_v1_mysql.yml +206 -0
  624. data/test/support/yaml_compatibility_fixtures/rails_v2.yml +55 -0
  625. metadata +192 -14
@@ -1,12 +1,14 @@
1
- require 'cases/helper'
2
- require 'cases/migration/helper'
3
- require 'bigdecimal/util'
4
- require 'concurrent/atomic/count_down_latch'
1
+ # frozen_string_literal: true
5
2
 
6
- require 'models/person'
7
- require 'models/topic'
8
- require 'models/developer'
9
- require 'models/computer'
3
+ require "cases/helper"
4
+ require "cases/migration/helper"
5
+ require "bigdecimal/util"
6
+ require "concurrent/atomic/count_down_latch"
7
+
8
+ require "models/person"
9
+ require "models/topic"
10
+ require "models/developer"
11
+ require "models/computer"
10
12
 
11
13
  require MIGRATIONS_ROOT + "/valid/2_we_need_reminders"
12
14
  require MIGRATIONS_ROOT + "/rename/1_we_need_things"
@@ -36,6 +38,7 @@ class MigrationTest < ActiveRecord::TestCase
36
38
  end
37
39
  Reminder.reset_column_information
38
40
  @verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, false
41
+ @schema_migration = ActiveRecord::Base.connection.schema_migration
39
42
  ActiveRecord::Base.connection.schema_cache.clear!
40
43
  end
41
44
 
@@ -43,10 +46,10 @@ class MigrationTest < ActiveRecord::TestCase
43
46
  ActiveRecord::Base.table_name_prefix = ""
44
47
  ActiveRecord::Base.table_name_suffix = ""
45
48
 
46
- ActiveRecord::Base.connection.initialize_schema_migrations_table
47
- ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::Migrator.schema_migrations_table_name}"
49
+ ActiveRecord::SchemaMigration.create_table
50
+ ActiveRecord::SchemaMigration.delete_all
48
51
 
49
- %w(things awesome_things prefix_things_suffix p_awesome_things_s ).each do |table|
52
+ %w(things awesome_things prefix_things_suffix p_awesome_things_s).each do |table|
50
53
  Thing.connection.drop_table(table) rescue nil
51
54
  end
52
55
  Thing.reset_column_information
@@ -59,7 +62,7 @@ class MigrationTest < ActiveRecord::TestCase
59
62
 
60
63
  %w(last_name key bio age height wealth birthday favorite_day
61
64
  moment_of_truth male administrator funny).each do |column|
62
- Person.connection.remove_column('people', column) rescue nil
65
+ Person.connection.remove_column("people", column) rescue nil
63
66
  end
64
67
  Person.connection.remove_column("people", "first_name") rescue nil
65
68
  Person.connection.remove_column("people", "middle_name") rescue nil
@@ -75,51 +78,91 @@ class MigrationTest < ActiveRecord::TestCase
75
78
 
76
79
  def test_migrator_versions
77
80
  migrations_path = MIGRATIONS_ROOT + "/valid"
78
- old_path = ActiveRecord::Migrator.migrations_paths
79
- ActiveRecord::Migrator.migrations_paths = migrations_path
81
+ migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration)
80
82
 
81
- ActiveRecord::Migrator.up(migrations_path)
82
- assert_equal 3, ActiveRecord::Migrator.current_version
83
- assert_equal false, ActiveRecord::Migrator.needs_migration?
83
+ migrator.up
84
+ assert_equal 3, migrator.current_version
85
+ assert_equal false, migrator.needs_migration?
84
86
 
85
- ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid")
86
- assert_equal 0, ActiveRecord::Migrator.current_version
87
- assert_equal true, ActiveRecord::Migrator.needs_migration?
87
+ migrator.down
88
+ assert_equal 0, migrator.current_version
89
+ assert_equal true, migrator.needs_migration?
88
90
 
89
91
  ActiveRecord::SchemaMigration.create!(version: 3)
90
- assert_equal true, ActiveRecord::Migrator.needs_migration?
91
- ensure
92
- ActiveRecord::Migrator.migrations_paths = old_path
92
+ assert_equal true, migrator.needs_migration?
93
93
  end
94
94
 
95
95
  def test_migration_detection_without_schema_migration_table
96
- ActiveRecord::Base.connection.drop_table 'schema_migrations'
97
- #, if_exists: true
96
+ ActiveRecord::Base.connection.drop_table "schema_migrations", if_exists: true
98
97
 
99
98
  migrations_path = MIGRATIONS_ROOT + "/valid"
100
- old_path = ActiveRecord::Migrator.migrations_paths
101
- ActiveRecord::Migrator.migrations_paths = migrations_path
99
+ migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration)
102
100
 
103
- assert_equal true, ActiveRecord::Migrator.needs_migration?
104
- ensure
105
- ActiveRecord::Migrator.migrations_paths = old_path
101
+ assert_equal true, migrator.needs_migration?
106
102
  end
107
103
 
108
104
  def test_any_migrations
109
- old_path = ActiveRecord::Migrator.migrations_paths
110
- ActiveRecord::Migrator.migrations_paths = MIGRATIONS_ROOT + "/valid"
105
+ migrator = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid", @schema_migration)
111
106
 
112
- assert ActiveRecord::Migrator.any_migrations?
107
+ assert_predicate migrator, :any_migrations?
113
108
 
114
- ActiveRecord::Migrator.migrations_paths = MIGRATIONS_ROOT + "/empty"
109
+ migrator_empty = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/empty", @schema_migration)
115
110
 
116
- assert_not ActiveRecord::Migrator.any_migrations?
117
- ensure
118
- ActiveRecord::Migrator.migrations_paths = old_path
111
+ assert_not_predicate migrator_empty, :any_migrations?
119
112
  end
120
113
 
121
114
  def test_migration_version
122
- assert_nothing_raised { ActiveRecord::Migrator.run(:up, MIGRATIONS_ROOT + "/version_check", 20131219224947) }
115
+ migrator = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/version_check", @schema_migration)
116
+ assert_equal 0, migrator.current_version
117
+ migrator.up(20131219224947)
118
+ assert_equal 20131219224947, migrator.current_version
119
+ end
120
+
121
+ def test_create_table_raises_if_already_exists
122
+ connection = Person.connection
123
+ connection.create_table :testings, force: true do |t|
124
+ t.string :foo
125
+ end
126
+
127
+ assert_raise(ActiveRecord::StatementInvalid) do
128
+ connection.create_table :testings do |t|
129
+ t.string :foo
130
+ end
131
+ end
132
+ ensure
133
+ connection.drop_table :testings, if_exists: true
134
+ end
135
+
136
+ def test_create_table_with_if_not_exists_true
137
+ connection = Person.connection
138
+ connection.create_table :testings, force: true do |t|
139
+ t.string :foo
140
+ end
141
+
142
+ assert_nothing_raised do
143
+ connection.create_table :testings, if_not_exists: true do |t|
144
+ t.string :foo
145
+ end
146
+ end
147
+ ensure
148
+ connection.drop_table :testings, if_exists: true
149
+ end
150
+
151
+ def test_create_table_with_indexes_and_if_not_exists_true
152
+ connection = Person.connection
153
+ connection.create_table :testings, force: true do |t|
154
+ t.references :people
155
+ t.string :foo
156
+ end
157
+
158
+ assert_nothing_raised do
159
+ connection.create_table :testings, if_not_exists: true do |t|
160
+ t.references :people
161
+ t.string :foo
162
+ end
163
+ end
164
+ ensure
165
+ connection.drop_table :testings, if_exists: true
123
166
  end
124
167
 
125
168
  def test_create_table_with_force_true_does_not_drop_nonexisting_table
@@ -129,12 +172,185 @@ class MigrationTest < ActiveRecord::TestCase
129
172
 
130
173
  assert_not_equal temp_conn, Person.connection
131
174
 
132
- temp_conn.create_table :testings2, :force => true do |t|
175
+ temp_conn.create_table :testings2, force: true do |t|
133
176
  t.column :foo, :string
134
177
  end
135
178
  ensure
136
- Person.connection.drop_table :testings2
137
- #, if_exists: true
179
+ Person.connection.drop_table :testings2, if_exists: true
180
+ end
181
+
182
+ def test_remove_column_with_if_not_exists_not_set
183
+ migration_a = Class.new(ActiveRecord::Migration::Current) {
184
+ def version; 100 end
185
+ def migrate(x)
186
+ add_column "people", "last_name", :string
187
+ end
188
+ }.new
189
+
190
+ migration_b = Class.new(ActiveRecord::Migration::Current) {
191
+ def version; 101 end
192
+ def migrate(x)
193
+ remove_column "people", "last_name"
194
+ end
195
+ }.new
196
+
197
+ migration_c = Class.new(ActiveRecord::Migration::Current) {
198
+ def version; 102 end
199
+ def migrate(x)
200
+ remove_column "people", "last_name"
201
+ end
202
+ }.new
203
+
204
+ ActiveRecord::Migrator.new(:up, [migration_a], @schema_migration, 100).migrate
205
+ assert_column Person, :last_name, "migration_a should have added the last_name column on people"
206
+
207
+ ActiveRecord::Migrator.new(:up, [migration_b], @schema_migration, 101).migrate
208
+ assert_no_column Person, :last_name, "migration_b should have dropped the last_name column on people"
209
+
210
+ migrator = ActiveRecord::Migrator.new(:up, [migration_c], @schema_migration, 102)
211
+
212
+ if current_adapter?(:SQLite3Adapter)
213
+ assert_nothing_raised do
214
+ migrator.migrate
215
+ end
216
+ else
217
+ error = assert_raises do
218
+ migrator.migrate
219
+ end
220
+
221
+ if current_adapter?(:Mysql2Adapter)
222
+ if ActiveRecord::Base.connection.mariadb?
223
+ assert_match(/Can't DROP COLUMN `last_name`; check that it exists/, error.message)
224
+ else
225
+ assert_match(/check that column\/key exists/, error.message)
226
+ end
227
+ elsif current_adapter?(:PostgreSQLAdapter)
228
+ assert_match(/column \"last_name\" of relation \"people\" does not exist/, error.message)
229
+ end
230
+ end
231
+ ensure
232
+ Person.reset_column_information
233
+ end
234
+
235
+ def test_remove_column_with_if_exists_set
236
+ migration_a = Class.new(ActiveRecord::Migration::Current) {
237
+ def version; 100 end
238
+ def migrate(x)
239
+ add_column "people", "last_name", :string
240
+ end
241
+ }.new
242
+
243
+ migration_b = Class.new(ActiveRecord::Migration::Current) {
244
+ def version; 101 end
245
+ def migrate(x)
246
+ remove_column "people", "last_name"
247
+ end
248
+ }.new
249
+
250
+ migration_c = Class.new(ActiveRecord::Migration::Current) {
251
+ def version; 102 end
252
+ def migrate(x)
253
+ remove_column "people", "last_name", if_exists: true
254
+ end
255
+ }.new
256
+
257
+ ActiveRecord::Migrator.new(:up, [migration_a], @schema_migration, 100).migrate
258
+ assert_column Person, :last_name, "migration_a should have added the last_name column on people"
259
+
260
+ ActiveRecord::Migrator.new(:up, [migration_b], @schema_migration, 101).migrate
261
+ assert_no_column Person, :last_name, "migration_b should have dropped the last_name column on people"
262
+
263
+ migrator = ActiveRecord::Migrator.new(:up, [migration_c], @schema_migration, 102)
264
+
265
+ assert_nothing_raised do
266
+ migrator.migrate
267
+ end
268
+ ensure
269
+ Person.reset_column_information
270
+ end
271
+
272
+ def test_add_column_with_if_not_exists_not_set
273
+ migration_a = Class.new(ActiveRecord::Migration::Current) {
274
+ def version; 100 end
275
+ def migrate(x)
276
+ add_column "people", "last_name", :string
277
+ end
278
+ }.new
279
+
280
+ migration_b = Class.new(ActiveRecord::Migration::Current) {
281
+ def version; 101 end
282
+ def migrate(x)
283
+ add_column "people", "last_name", :string
284
+ end
285
+ }.new
286
+
287
+ ActiveRecord::Migrator.new(:up, [migration_a], @schema_migration, 100).migrate
288
+ assert_column Person, :last_name, "migration_a should have created the last_name column on people"
289
+
290
+ assert_raises do
291
+ ActiveRecord::Migrator.new(:up, [migration_b], @schema_migration, 101).migrate
292
+ end
293
+ ensure
294
+ Person.reset_column_information
295
+ if Person.column_names.include?("last_name")
296
+ Person.connection.remove_column("people", "last_name")
297
+ end
298
+ end
299
+
300
+ def test_add_column_with_if_not_exists_set_to_true
301
+ migration_a = Class.new(ActiveRecord::Migration::Current) {
302
+ def version; 100 end
303
+ def migrate(x)
304
+ add_column "people", "last_name", :string
305
+ end
306
+ }.new
307
+
308
+ migration_b = Class.new(ActiveRecord::Migration::Current) {
309
+ def version; 101 end
310
+ def migrate(x)
311
+ add_column "people", "last_name", :string, if_not_exists: true
312
+ end
313
+ }.new
314
+
315
+ ActiveRecord::Migrator.new(:up, [migration_a], @schema_migration, 100).migrate
316
+ assert_column Person, :last_name, "migration_a should have created the last_name column on people"
317
+
318
+ assert_nothing_raised do
319
+ ActiveRecord::Migrator.new(:up, [migration_b], @schema_migration, 101).migrate
320
+ end
321
+ ensure
322
+ Person.reset_column_information
323
+ if Person.column_names.include?("last_name")
324
+ Person.connection.remove_column("people", "last_name")
325
+ end
326
+ end
327
+
328
+ def test_add_column_with_if_not_exists_set_to_true_still_raises_if_type_is_different
329
+ migration_a = Class.new(ActiveRecord::Migration::Current) {
330
+ def version; 100 end
331
+ def migrate(x)
332
+ add_column "people", "last_name", :string
333
+ end
334
+ }.new
335
+
336
+ migration_b = Class.new(ActiveRecord::Migration::Current) {
337
+ def version; 101 end
338
+ def migrate(x)
339
+ add_column "people", "last_name", :boolean, if_not_exists: true
340
+ end
341
+ }.new
342
+
343
+ ActiveRecord::Migrator.new(:up, [migration_a], @schema_migration, 100).migrate
344
+ assert_column Person, :last_name, "migration_a should have created the last_name column on people"
345
+
346
+ assert_raises do
347
+ ActiveRecord::Migrator.new(:up, [migration_b], @schema_migration, 101).migrate
348
+ end
349
+ ensure
350
+ Person.reset_column_information
351
+ if Person.column_names.include?("last_name")
352
+ Person.connection.remove_column("people", "last_name")
353
+ end
138
354
  end
139
355
 
140
356
  def test_migration_instance_has_connection
@@ -157,16 +373,17 @@ class MigrationTest < ActiveRecord::TestCase
157
373
  def test_add_table_with_decimals
158
374
  Person.connection.drop_table :big_numbers rescue nil
159
375
 
160
- assert !BigNumber.table_exists?
376
+ assert_not_predicate BigNumber, :table_exists?
161
377
  GiveMeBigNumbers.up
378
+ assert_predicate BigNumber, :table_exists?
162
379
  BigNumber.reset_column_information
163
380
 
164
381
  assert BigNumber.create(
165
- :bank_balance => 1586.43,
166
- :big_bank_balance => BigDecimal("1000234000567.95"),
167
- :world_population => 6000000000,
168
- :my_house_population => 3,
169
- :value_of_e => BigDecimal("2.7182818284590452353602875")
382
+ bank_balance: 1586.43,
383
+ big_bank_balance: BigDecimal("1000234000567.95"),
384
+ world_population: 2**62,
385
+ my_house_population: 3,
386
+ value_of_e: BigDecimal("2.7182818284590452353602875")
170
387
  )
171
388
 
172
389
  b = BigNumber.first
@@ -178,10 +395,8 @@ class MigrationTest < ActiveRecord::TestCase
178
395
  assert_not_nil b.my_house_population
179
396
  assert_not_nil b.value_of_e
180
397
 
181
- # TODO: set world_population >= 2**62 to cover 64-bit platforms and test
182
- # is_a?(Bignum)
183
398
  assert_kind_of Integer, b.world_population
184
- assert_equal 6000000000, b.world_population
399
+ assert_equal 2**62, b.world_population
185
400
  assert_kind_of Integer, b.my_house_population
186
401
  assert_equal 3, b.my_house_population
187
402
  assert_kind_of BigDecimal, b.bank_balance
@@ -216,15 +431,16 @@ class MigrationTest < ActiveRecord::TestCase
216
431
 
217
432
  def test_filtering_migrations
218
433
  assert_no_column Person, :last_name
219
- assert !Reminder.table_exists?
434
+ assert_not_predicate Reminder, :table_exists?
220
435
 
221
436
  name_filter = lambda { |migration| migration.name == "ValidPeopleHaveLastNames" }
222
- ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", &name_filter)
437
+ migrator = ActiveRecord::MigrationContext.new(MIGRATIONS_ROOT + "/valid", @schema_migration)
438
+ migrator.up(&name_filter)
223
439
 
224
440
  assert_column Person, :last_name
225
441
  assert_raise(ActiveRecord::StatementInvalid) { Reminder.first }
226
442
 
227
- ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/valid", &name_filter)
443
+ migrator.down(&name_filter)
228
444
 
229
445
  assert_no_column Person, :last_name
230
446
  assert_raise(ActiveRecord::StatementInvalid) { Reminder.first }
@@ -250,22 +466,22 @@ class MigrationTest < ActiveRecord::TestCase
250
466
 
251
467
  def test_instance_based_migration_up
252
468
  migration = MockMigration.new
253
- assert !migration.went_up, 'have not gone up'
254
- assert !migration.went_down, 'have not gone down'
469
+ assert_not migration.went_up, "have not gone up"
470
+ assert_not migration.went_down, "have not gone down"
255
471
 
256
472
  migration.migrate :up
257
- assert migration.went_up, 'have gone up'
258
- assert !migration.went_down, 'have not gone down'
473
+ assert migration.went_up, "have gone up"
474
+ assert_not migration.went_down, "have not gone down"
259
475
  end
260
476
 
261
477
  def test_instance_based_migration_down
262
478
  migration = MockMigration.new
263
- assert !migration.went_up, 'have not gone up'
264
- assert !migration.went_down, 'have not gone down'
479
+ assert_not migration.went_up, "have not gone up"
480
+ assert_not migration.went_down, "have not gone down"
265
481
 
266
482
  migration.migrate :down
267
- assert !migration.went_up, 'have gone up'
268
- assert migration.went_down, 'have not gone down'
483
+ assert_not migration.went_up, "have gone up"
484
+ assert migration.went_down, "have not gone down"
269
485
  end
270
486
 
271
487
  if ActiveRecord::Base.connection.supports_ddl_transactions?
@@ -276,11 +492,11 @@ class MigrationTest < ActiveRecord::TestCase
276
492
  def version; 100 end
277
493
  def migrate(x)
278
494
  add_column "people", "last_name", :string
279
- raise 'Something broke'
495
+ raise "Something broke"
280
496
  end
281
497
  }.new
282
498
 
283
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
499
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
284
500
 
285
501
  e = assert_raise(StandardError) { migrator.migrate }
286
502
 
@@ -297,11 +513,11 @@ class MigrationTest < ActiveRecord::TestCase
297
513
  def version; 100 end
298
514
  def migrate(x)
299
515
  add_column "people", "last_name", :string
300
- raise 'Something broke'
516
+ raise "Something broke"
301
517
  end
302
518
  }.new
303
519
 
304
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
520
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
305
521
 
306
522
  e = assert_raise(StandardError) { migrator.run }
307
523
 
@@ -315,16 +531,16 @@ class MigrationTest < ActiveRecord::TestCase
315
531
  assert_no_column Person, :last_name
316
532
 
317
533
  migration = Class.new(ActiveRecord::Migration::Current) {
318
- self.disable_ddl_transaction!
534
+ disable_ddl_transaction!
319
535
 
320
536
  def version; 101 end
321
537
  def migrate(x)
322
538
  add_column "people", "last_name", :string
323
- raise 'Something broke'
539
+ raise "Something broke"
324
540
  end
325
541
  }.new
326
542
 
327
- migrator = ActiveRecord::Migrator.new(:up, [migration], 101)
543
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 101)
328
544
  e = assert_raise(StandardError) { migrator.migrate }
329
545
  assert_equal "An error has occurred, all later migrations canceled:\n\nSomething broke", e.message
330
546
 
@@ -332,29 +548,30 @@ class MigrationTest < ActiveRecord::TestCase
332
548
  "without ddl transactions, the Migrator should not rollback on error but it did."
333
549
  ensure
334
550
  Person.reset_column_information
335
- if Person.column_names.include?('last_name')
336
- Person.connection.remove_column('people', 'last_name')
551
+ if Person.column_names.include?("last_name")
552
+ Person.connection.remove_column("people", "last_name")
337
553
  end
338
554
  end
339
555
  end
340
556
 
341
557
  def test_schema_migrations_table_name
342
- original_schema_migrations_table_name = ActiveRecord::Migrator.schema_migrations_table_name
558
+ original_schema_migrations_table_name = ActiveRecord::Base.schema_migrations_table_name
343
559
 
344
- assert_equal "schema_migrations", ActiveRecord::Migrator.schema_migrations_table_name
560
+ assert_equal "schema_migrations", ActiveRecord::SchemaMigration.table_name
345
561
  ActiveRecord::Base.table_name_prefix = "prefix_"
346
562
  ActiveRecord::Base.table_name_suffix = "_suffix"
347
563
  Reminder.reset_table_name
348
- assert_equal "prefix_schema_migrations_suffix", ActiveRecord::Migrator.schema_migrations_table_name
564
+ assert_equal "prefix_schema_migrations_suffix", ActiveRecord::SchemaMigration.table_name
349
565
  ActiveRecord::Base.schema_migrations_table_name = "changed"
350
566
  Reminder.reset_table_name
351
- assert_equal "prefix_changed_suffix", ActiveRecord::Migrator.schema_migrations_table_name
567
+ assert_equal "prefix_changed_suffix", ActiveRecord::SchemaMigration.table_name
352
568
  ActiveRecord::Base.table_name_prefix = ""
353
569
  ActiveRecord::Base.table_name_suffix = ""
354
570
  Reminder.reset_table_name
355
- assert_equal "changed", ActiveRecord::Migrator.schema_migrations_table_name
571
+ assert_equal "changed", ActiveRecord::SchemaMigration.table_name
356
572
  ensure
357
573
  ActiveRecord::Base.schema_migrations_table_name = original_schema_migrations_table_name
574
+ ActiveRecord::SchemaMigration.reset_table_name
358
575
  Reminder.reset_table_name
359
576
  end
360
577
 
@@ -375,97 +592,124 @@ class MigrationTest < ActiveRecord::TestCase
375
592
  assert_equal "changed", ActiveRecord::InternalMetadata.table_name
376
593
  ensure
377
594
  ActiveRecord::Base.internal_metadata_table_name = original_internal_metadata_table_name
595
+ ActiveRecord::InternalMetadata.reset_table_name
378
596
  Reminder.reset_table_name
379
597
  end
380
598
 
381
599
  def test_internal_metadata_stores_environment
382
600
  current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
383
601
  migrations_path = MIGRATIONS_ROOT + "/valid"
384
- old_path = ActiveRecord::Migrator.migrations_paths
385
- ActiveRecord::Migrator.migrations_paths = migrations_path
602
+ migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration)
386
603
 
387
- ActiveRecord::Migrator.up(migrations_path)
604
+ migrator.up
388
605
  assert_equal current_env, ActiveRecord::InternalMetadata[:environment]
389
606
 
390
607
  original_rails_env = ENV["RAILS_ENV"]
391
608
  original_rack_env = ENV["RACK_ENV"]
392
609
  ENV["RAILS_ENV"] = ENV["RACK_ENV"] = "foofoo"
393
- new_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
610
+ new_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
394
611
 
395
- refute_equal current_env, new_env
612
+ assert_not_equal current_env, new_env
396
613
 
397
614
  sleep 1 # mysql by default does not store fractional seconds in the database
398
- ActiveRecord::Migrator.up(migrations_path)
615
+ migrator.up
399
616
  assert_equal new_env, ActiveRecord::InternalMetadata[:environment]
400
617
  ensure
401
- ActiveRecord::Migrator.migrations_paths = old_path
402
618
  ENV["RAILS_ENV"] = original_rails_env
403
619
  ENV["RACK_ENV"] = original_rack_env
620
+ migrator.up
404
621
  end
405
622
 
623
+ def test_internal_metadata_stores_environment_when_other_data_exists
624
+ ActiveRecord::InternalMetadata.delete_all
625
+ ActiveRecord::InternalMetadata[:foo] = "bar"
406
626
 
407
- def test_migration_sets_internal_metadata_even_when_fully_migrated
408
627
  current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
409
628
  migrations_path = MIGRATIONS_ROOT + "/valid"
410
- old_path = ActiveRecord::Migrator.migrations_paths
411
- ActiveRecord::Migrator.migrations_paths = migrations_path
412
629
 
413
- ActiveRecord::Migrator.up(migrations_path)
630
+ migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration)
631
+ migrator.up
414
632
  assert_equal current_env, ActiveRecord::InternalMetadata[:environment]
633
+ assert_equal "bar", ActiveRecord::InternalMetadata[:foo]
634
+ end
415
635
 
416
- original_rails_env = ENV["RAILS_ENV"]
417
- original_rack_env = ENV["RACK_ENV"]
418
- ENV["RAILS_ENV"] = ENV["RACK_ENV"] = "foofoo"
419
- new_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
636
+ def test_internal_metadata_not_used_when_not_enabled
637
+ ActiveRecord::InternalMetadata.drop_table
638
+ original_config = ActiveRecord::Base.connection.instance_variable_get("@config")
420
639
 
421
- refute_equal current_env, new_env
640
+ modified_config = original_config.dup.merge(use_metadata_table: false)
422
641
 
423
- sleep 1 # mysql by default does not store fractional seconds in the database
642
+ ActiveRecord::Base.connection
643
+ .instance_variable_set("@config", modified_config)
424
644
 
425
- ActiveRecord::Migrator.up(migrations_path)
426
- assert_equal new_env, ActiveRecord::InternalMetadata[:environment]
645
+ assert_not ActiveRecord::InternalMetadata.enabled?
646
+ assert_not ActiveRecord::InternalMetadata.table_exists?
647
+
648
+ migrations_path = MIGRATIONS_ROOT + "/valid"
649
+ migrator = ActiveRecord::MigrationContext.new(migrations_path, @schema_migration)
650
+ migrator.up
651
+
652
+ assert_not ActiveRecord::InternalMetadata[:environment]
653
+ assert_not ActiveRecord::InternalMetadata.table_exists?
427
654
  ensure
428
- ActiveRecord::Migrator.migrations_paths = old_path
429
- ENV["RAILS_ENV"] = original_rails_env
430
- ENV["RACK_ENV"] = original_rack_env
655
+ ActiveRecord::Base.connection.instance_variable_set("@config", original_config)
656
+ ActiveRecord::InternalMetadata.create_table
431
657
  end
432
658
 
433
- def test_internal_metadata_stores_environment_when_other_data_exists
434
- ActiveRecord::InternalMetadata.delete_all
435
- ActiveRecord::InternalMetadata[:foo] = 'bar'
659
+ def test_internal_metadata_create_table_wont_be_affected_by_schema_cache
660
+ ActiveRecord::InternalMetadata.drop_table
661
+ assert_not_predicate ActiveRecord::InternalMetadata, :table_exists?
436
662
 
437
- current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
438
- migrations_path = MIGRATIONS_ROOT + "/valid"
439
- old_path = ActiveRecord::Migrator.migrations_paths
440
- ActiveRecord::Migrator.migrations_paths = migrations_path
663
+ ActiveRecord::InternalMetadata.connection.transaction do
664
+ ActiveRecord::InternalMetadata.create_table
665
+ assert_predicate ActiveRecord::InternalMetadata, :table_exists?
441
666
 
442
- current_env = ActiveRecord::ConnectionHandling::DEFAULT_ENV.call
443
- ActiveRecord::Migrator.up(migrations_path)
444
- assert_equal current_env, ActiveRecord::InternalMetadata[:environment]
445
- assert_equal 'bar', ActiveRecord::InternalMetadata[:foo]
667
+ ActiveRecord::InternalMetadata[:version] = "foo"
668
+ assert_equal "foo", ActiveRecord::InternalMetadata[:version]
669
+ raise ActiveRecord::Rollback
670
+ end
671
+
672
+ ActiveRecord::InternalMetadata.connection.transaction do
673
+ ActiveRecord::InternalMetadata.create_table
674
+ assert_predicate ActiveRecord::InternalMetadata, :table_exists?
675
+
676
+ ActiveRecord::InternalMetadata[:version] = "bar"
677
+ assert_equal "bar", ActiveRecord::InternalMetadata[:version]
678
+ raise ActiveRecord::Rollback
679
+ end
446
680
  ensure
447
- ActiveRecord::Migrator.migrations_paths = old_path
681
+ ActiveRecord::InternalMetadata.create_table
448
682
  end
449
683
 
450
- def test_rename_internal_metadata_table
451
- original_internal_metadata_table_name = ActiveRecord::Base.internal_metadata_table_name
684
+ def test_schema_migration_create_table_wont_be_affected_by_schema_cache
685
+ ActiveRecord::SchemaMigration.drop_table
686
+ assert_not_predicate ActiveRecord::SchemaMigration, :table_exists?
452
687
 
453
- ActiveRecord::Base.internal_metadata_table_name = "active_record_internal_metadatas"
454
- Reminder.reset_table_name
688
+ ActiveRecord::SchemaMigration.connection.transaction do
689
+ ActiveRecord::SchemaMigration.create_table
690
+ assert_predicate ActiveRecord::SchemaMigration, :table_exists?
455
691
 
456
- ActiveRecord::Base.internal_metadata_table_name = original_internal_metadata_table_name
457
- Reminder.reset_table_name
692
+ schema_migration = ActiveRecord::SchemaMigration.create!(version: "foo")
693
+ assert_equal "foo", schema_migration[:version]
694
+ raise ActiveRecord::Rollback
695
+ end
458
696
 
459
- assert_equal "ar_internal_metadata", ActiveRecord::InternalMetadata.table_name
697
+ ActiveRecord::SchemaMigration.connection.transaction do
698
+ ActiveRecord::SchemaMigration.create_table
699
+ assert_predicate ActiveRecord::SchemaMigration, :table_exists?
700
+
701
+ schema_migration = ActiveRecord::SchemaMigration.create!(version: "bar")
702
+ assert_equal "bar", schema_migration[:version]
703
+ raise ActiveRecord::Rollback
704
+ end
460
705
  ensure
461
- ActiveRecord::Base.internal_metadata_table_name = original_internal_metadata_table_name
462
- Reminder.reset_table_name
706
+ ActiveRecord::SchemaMigration.create_table
463
707
  end
464
708
 
465
709
  def test_proper_table_name_on_migration
466
710
  reminder_class = new_isolated_reminder_class
467
711
  migration = ActiveRecord::Migration.new
468
- assert_equal "table", migration.proper_table_name('table')
712
+ assert_equal "table", migration.proper_table_name("table")
469
713
  assert_equal "table", migration.proper_table_name(:table)
470
714
  assert_equal "reminders", migration.proper_table_name(reminder_class)
471
715
  reminder_class.reset_table_name
@@ -474,29 +718,30 @@ class MigrationTest < ActiveRecord::TestCase
474
718
  # Use the model's own prefix/suffix if a model is given
475
719
  ActiveRecord::Base.table_name_prefix = "ARprefix_"
476
720
  ActiveRecord::Base.table_name_suffix = "_ARsuffix"
477
- reminder_class.table_name_prefix = 'prefix_'
478
- reminder_class.table_name_suffix = '_suffix'
721
+ reminder_class.table_name_prefix = "prefix_"
722
+ reminder_class.table_name_suffix = "_suffix"
479
723
  reminder_class.reset_table_name
480
724
  assert_equal "prefix_reminders_suffix", migration.proper_table_name(reminder_class)
481
- reminder_class.table_name_prefix = ''
482
- reminder_class.table_name_suffix = ''
725
+ reminder_class.table_name_prefix = ""
726
+ reminder_class.table_name_suffix = ""
483
727
  reminder_class.reset_table_name
484
728
 
485
729
  # Use AR::Base's prefix/suffix if string or symbol is given
486
730
  ActiveRecord::Base.table_name_prefix = "prefix_"
487
731
  ActiveRecord::Base.table_name_suffix = "_suffix"
488
732
  reminder_class.reset_table_name
489
- assert_equal "prefix_table_suffix", migration.proper_table_name('table', migration.table_name_options)
733
+ assert_equal "prefix_table_suffix", migration.proper_table_name("table", migration.table_name_options)
490
734
  assert_equal "prefix_table_suffix", migration.proper_table_name(:table, migration.table_name_options)
491
735
  end
492
736
 
493
737
  def test_rename_table_with_prefix_and_suffix
494
- assert !Thing.table_exists?
495
- ActiveRecord::Base.table_name_prefix = 'p_'
496
- ActiveRecord::Base.table_name_suffix = '_s'
738
+ assert_not_predicate Thing, :table_exists?
739
+ ActiveRecord::Base.table_name_prefix = "p_"
740
+ ActiveRecord::Base.table_name_suffix = "_s"
497
741
  Thing.reset_table_name
498
742
  Thing.reset_sequence_name
499
743
  WeNeedThings.up
744
+ assert_predicate Thing, :table_exists?
500
745
  Thing.reset_column_information
501
746
 
502
747
  assert Thing.create("content" => "hello world")
@@ -512,13 +757,14 @@ class MigrationTest < ActiveRecord::TestCase
512
757
  end
513
758
 
514
759
  def test_add_drop_table_with_prefix_and_suffix
515
- assert !Reminder.table_exists?
516
- ActiveRecord::Base.table_name_prefix = 'prefix_'
517
- ActiveRecord::Base.table_name_suffix = '_suffix'
760
+ assert_not_predicate Reminder, :table_exists?
761
+ ActiveRecord::Base.table_name_prefix = "prefix_"
762
+ ActiveRecord::Base.table_name_suffix = "_suffix"
518
763
  Reminder.reset_table_name
519
764
  Reminder.reset_sequence_name
520
- Reminder.reset_column_information
521
765
  WeNeedReminders.up
766
+ assert_predicate Reminder, :table_exists?
767
+ Reminder.reset_column_information
522
768
  assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
523
769
  assert_equal "hello world", Reminder.first.content
524
770
 
@@ -531,7 +777,7 @@ class MigrationTest < ActiveRecord::TestCase
531
777
  def test_create_table_with_binary_column
532
778
  assert_nothing_raised {
533
779
  Person.connection.create_table :binary_testings do |t|
534
- t.column "data", :binary, :null => false
780
+ t.column "data", :binary, null: false
535
781
  end
536
782
  }
537
783
 
@@ -540,17 +786,15 @@ class MigrationTest < ActiveRecord::TestCase
540
786
 
541
787
  assert_nil data_column.default
542
788
  ensure
543
- Person.connection.drop_table :binary_testings
544
- #, if_exists: true
789
+ Person.connection.drop_table :binary_testings, if_exists: true
545
790
  end
546
791
 
547
792
  unless mysql_enforcing_gtid_consistency?
548
793
  def test_create_table_with_query
549
- Person.connection.create_table(:person, force: true)
550
-
551
- Person.connection.create_table :table_from_query_testings, as: "SELECT id FROM person"
794
+ Person.connection.create_table :table_from_query_testings, as: "SELECT id FROM people WHERE id = 1"
552
795
 
553
796
  columns = Person.connection.columns(:table_from_query_testings)
797
+ assert_equal [1], Person.connection.select_values("SELECT * FROM table_from_query_testings")
554
798
  assert_equal 1, columns.length
555
799
  assert_equal "id", columns.first.name
556
800
  ensure
@@ -558,11 +802,10 @@ class MigrationTest < ActiveRecord::TestCase
558
802
  end
559
803
 
560
804
  def test_create_table_with_query_from_relation
561
- Person.connection.create_table(:person, force: true)
562
-
563
- Person.connection.create_table :table_from_query_testings, as: Person.select(:id)
805
+ Person.connection.create_table :table_from_query_testings, as: Person.select(:id).where(id: 1)
564
806
 
565
807
  columns = Person.connection.columns(:table_from_query_testings)
808
+ assert_equal [1], Person.connection.select_values("SELECT * FROM table_from_query_testings")
566
809
  assert_equal 1, columns.length
567
810
  assert_equal "id", columns.first.name
568
811
  ensure
@@ -579,86 +822,82 @@ class MigrationTest < ActiveRecord::TestCase
579
822
  end
580
823
  assert Person.connection.column_exists?(:something, :foo)
581
824
  assert_nothing_raised { Person.connection.remove_column :something, :foo, :bar }
582
- assert !Person.connection.column_exists?(:something, :foo)
825
+ assert_not Person.connection.column_exists?(:something, :foo)
583
826
  assert Person.connection.column_exists?(:something, :name)
584
827
  assert Person.connection.column_exists?(:something, :number)
585
828
  ensure
586
- Person.connection.drop_table :something
587
- #, if_exists: true
829
+ Person.connection.drop_table :something, if_exists: true
588
830
  end
589
831
  end
590
832
 
591
- if current_adapter? :OracleAdapter
592
- def test_create_table_with_custom_sequence_name
593
- # table name is 29 chars, the standard sequence name will
594
- # be 33 chars and should be shortened
595
- assert_nothing_raised do
596
- begin
597
- Person.connection.create_table :table_with_name_thats_just_ok do |t|
598
- t.column :foo, :string, :null => false
599
- end
600
- ensure
601
- Person.connection.drop_table :table_with_name_thats_just_ok rescue nil
602
- end
833
+ def test_decimal_scale_without_precision_should_raise
834
+ e = assert_raise(ArgumentError) do
835
+ Person.connection.create_table :test_decimal_scales, force: true do |t|
836
+ t.decimal :scaleonly, scale: 10
603
837
  end
838
+ end
604
839
 
605
- # should be all good w/ a custom sequence name
606
- assert_nothing_raised do
607
- begin
608
- Person.connection.create_table :table_with_name_thats_just_ok,
609
- :sequence_name => 'suitably_short_seq' do |t|
610
- t.column :foo, :string, :null => false
611
- end
612
-
613
- Person.connection.execute("select suitably_short_seq.nextval from dual")
840
+ assert_equal "Error adding decimal column: precision cannot be empty if scale is specified", e.message
841
+ ensure
842
+ Person.connection.drop_table :test_decimal_scales, if_exists: true
843
+ end
614
844
 
615
- ensure
616
- Person.connection.drop_table :table_with_name_thats_just_ok,
617
- :sequence_name => 'suitably_short_seq' rescue nil
845
+ if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
846
+ def test_out_of_range_integer_limit_should_raise
847
+ e = assert_raise(ArgumentError) do
848
+ Person.connection.create_table :test_integer_limits, force: true do |t|
849
+ t.column :bigone, :integer, limit: 10
618
850
  end
619
851
  end
620
852
 
621
- # confirm the custom sequence got dropped
622
- assert_raise(ActiveRecord::StatementInvalid) do
623
- Person.connection.execute("select suitably_short_seq.nextval from dual")
853
+ assert_includes e.message, "No integer type has byte size 10"
854
+ ensure
855
+ Person.connection.drop_table :test_integer_limits, if_exists: true
856
+ end
857
+
858
+ def test_out_of_range_text_limit_should_raise
859
+ e = assert_raise(ArgumentError) do
860
+ Person.connection.create_table :test_text_limits, force: true do |t|
861
+ t.text :bigtext, limit: 0xfffffffff
862
+ end
624
863
  end
864
+
865
+ assert_includes e.message, "No text type has byte size #{0xfffffffff}"
866
+ ensure
867
+ Person.connection.drop_table :test_text_limits, if_exists: true
625
868
  end
626
- end
627
869
 
628
- if current_adapter?(:Mysql2Adapter, :PostgreSQLAdapter)
629
- def test_out_of_range_integer_limit_should_raise
630
- e = assert_raise(ActiveRecord::ActiveRecordError, "integer limit didn't raise") do
631
- Person.connection.create_table :test_integer_limits, :force => true do |t|
632
- t.column :bigone, :integer, :limit => 10
870
+ def test_out_of_range_binary_limit_should_raise
871
+ e = assert_raise(ArgumentError) do
872
+ Person.connection.create_table :test_binary_limits, force: true do |t|
873
+ t.binary :bigbinary, limit: 0xfffffffff
633
874
  end
634
875
  end
635
876
 
636
- assert_match(/No integer type has byte size 10/, e.message)
877
+ assert_includes e.message, "No binary type has byte size #{0xfffffffff}"
637
878
  ensure
638
- Person.connection.drop_table :test_integer_limits
639
- #, if_exists: true
879
+ Person.connection.drop_table :test_binary_limits, if_exists: true
640
880
  end
641
881
  end
642
882
 
643
883
  if current_adapter?(:Mysql2Adapter)
644
- def test_out_of_range_text_limit_should_raise
645
- e = assert_raise(ActiveRecord::ActiveRecordError, "text limit didn't raise") do
646
- Person.connection.create_table :test_text_limits, force: true do |t|
647
- t.text :bigtext, limit: 0xfffffffff
884
+ def test_invalid_text_size_should_raise
885
+ e = assert_raise(ArgumentError) do
886
+ Person.connection.create_table :test_text_sizes, force: true do |t|
887
+ t.text :bigtext, size: 0xfffffffff
648
888
  end
649
889
  end
650
890
 
651
- assert_match(/No text type has byte length #{0xfffffffff}/, e.message)
891
+ assert_equal "#{0xfffffffff} is invalid :size value. Only :tiny, :medium, and :long are allowed.", e.message
652
892
  ensure
653
- Person.connection.drop_table :test_text_limits
654
- #, if_exists: true
893
+ Person.connection.drop_table :test_text_sizes, if_exists: true
655
894
  end
656
895
  end
657
896
 
658
897
  if ActiveRecord::Base.connection.supports_advisory_locks?
659
898
  def test_migrator_generates_valid_lock_id
660
899
  migration = Class.new(ActiveRecord::Migration::Current).new
661
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
900
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
662
901
 
663
902
  lock_id = migrator.send(:generate_migrator_advisory_lock_id)
664
903
 
@@ -672,7 +911,7 @@ class MigrationTest < ActiveRecord::TestCase
672
911
  # It is important we are consistent with how we generate this so that
673
912
  # exclusive locking works across migrator versions
674
913
  migration = Class.new(ActiveRecord::Migration::Current).new
675
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
914
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
676
915
 
677
916
  lock_id = migrator.send(:generate_migrator_advisory_lock_id)
678
917
 
@@ -694,7 +933,7 @@ class MigrationTest < ActiveRecord::TestCase
694
933
  end
695
934
  }.new
696
935
 
697
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
936
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
698
937
  lock_id = migrator.send(:generate_migrator_advisory_lock_id)
699
938
 
700
939
  with_another_process_holding_lock(lock_id) do
@@ -715,7 +954,7 @@ class MigrationTest < ActiveRecord::TestCase
715
954
  end
716
955
  }.new
717
956
 
718
- migrator = ActiveRecord::Migrator.new(:up, [migration], 100)
957
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
719
958
  lock_id = migrator.send(:generate_migrator_advisory_lock_id)
720
959
 
721
960
  with_another_process_holding_lock(lock_id) do
@@ -725,9 +964,66 @@ class MigrationTest < ActiveRecord::TestCase
725
964
  assert_no_column Person, :last_name,
726
965
  "without an advisory lock, the Migrator should not make any changes, but it did."
727
966
  end
967
+
968
+ def test_with_advisory_lock_doesnt_release_closed_connections
969
+ migration = Class.new(ActiveRecord::Migration::Current).new
970
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
971
+
972
+ silence_stream($stderr) do
973
+ migrator.send(:with_advisory_lock) do
974
+ ActiveRecord::Base.establish_connection :arunit
975
+ end
976
+ end
977
+ end
978
+
979
+ if current_adapter?(:PostgreSQLAdapter)
980
+ def test_with_advisory_lock_closes_connection
981
+ migration = Class.new(ActiveRecord::Migration::Current) {
982
+ def version; 100 end
983
+ def migrate(x)
984
+ end
985
+ }.new
986
+
987
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
988
+ lock_id = migrator.send(:generate_migrator_advisory_lock_id)
989
+
990
+ query = <<~SQL
991
+ SELECT query
992
+ FROM pg_stat_activity
993
+ WHERE datname = '#{ActiveRecord::Base.connection_db_config.database}'
994
+ AND state = 'idle'
995
+ AND query LIKE '%#{lock_id}%'
996
+ SQL
997
+
998
+ assert_no_changes -> { ActiveRecord::Base.connection.exec_query(query).rows.flatten } do
999
+ migrator.migrate
1000
+ end
1001
+ end
1002
+ end
1003
+
1004
+ def test_with_advisory_lock_raises_the_right_error_when_it_fails_to_release_lock
1005
+ migration = Class.new(ActiveRecord::Migration::Current).new
1006
+ migrator = ActiveRecord::Migrator.new(:up, [migration], @schema_migration, 100)
1007
+ lock_id = migrator.send(:generate_migrator_advisory_lock_id)
1008
+
1009
+ e = assert_raises(ActiveRecord::ConcurrentMigrationError) do
1010
+ silence_stream($stderr) do
1011
+ migrator.stub(:with_advisory_lock_connection, ->(&block) { block.call(ActiveRecord::Base.connection) }) do
1012
+ migrator.send(:with_advisory_lock) do
1013
+ ActiveRecord::Base.connection.release_advisory_lock(lock_id)
1014
+ end
1015
+ end
1016
+ end
1017
+ end
1018
+
1019
+ assert_match(
1020
+ /#{ActiveRecord::ConcurrentMigrationError::RELEASE_LOCK_FAILED_MESSAGE}/,
1021
+ e.message
1022
+ )
1023
+ end
728
1024
  end
729
1025
 
730
- protected
1026
+ private
731
1027
  # This is needed to isolate class_attribute assignments like `table_name_prefix`
732
1028
  # for each test case.
733
1029
  def new_isolated_reminder_class
@@ -742,15 +1038,13 @@ class MigrationTest < ActiveRecord::TestCase
742
1038
  test_terminated = Concurrent::CountDownLatch.new
743
1039
 
744
1040
  other_process = Thread.new do
745
- begin
746
- conn = ActiveRecord::Base.connection_pool.checkout
747
- conn.get_advisory_lock(lock_id)
748
- thread_lock.count_down
749
- test_terminated.wait # hold the lock open until we tested everything
750
- ensure
751
- conn.release_advisory_lock(lock_id)
752
- ActiveRecord::Base.connection_pool.checkin(conn)
753
- end
1041
+ conn = ActiveRecord::Base.connection_pool.checkout
1042
+ conn.get_advisory_lock(lock_id)
1043
+ thread_lock.count_down
1044
+ test_terminated.wait # hold the lock open until we tested everything
1045
+ ensure
1046
+ conn.release_advisory_lock(lock_id)
1047
+ ActiveRecord::Base.connection_pool.checkin(conn)
754
1048
  end
755
1049
 
756
1050
  thread_lock.wait # wait until the 'other process' has the lock
@@ -765,13 +1059,13 @@ end
765
1059
  class ReservedWordsMigrationTest < ActiveRecord::TestCase
766
1060
  def test_drop_index_from_table_named_values
767
1061
  connection = Person.connection
768
- connection.create_table :values, :force => true do |t|
1062
+ connection.create_table :values, force: true do |t|
769
1063
  t.integer :value
770
1064
  end
771
1065
 
772
1066
  assert_nothing_raised do
773
1067
  connection.add_index :values, :value
774
- connection.remove_index :values, :column => :value
1068
+ connection.remove_index :values, :value
775
1069
  end
776
1070
  ensure
777
1071
  connection.drop_table :values rescue nil
@@ -786,8 +1080,8 @@ class ExplicitlyNamedIndexMigrationTest < ActiveRecord::TestCase
786
1080
  end
787
1081
 
788
1082
  assert_nothing_raised do
789
- connection.add_index :values, :value, name: 'a_different_name'
790
- connection.remove_index :values, column: :value, name: 'a_different_name'
1083
+ connection.add_index :values, :value, name: "a_different_name"
1084
+ connection.remove_index :values, :value, name: "a_different_name"
791
1085
  end
792
1086
  ensure
793
1087
  connection.drop_table :values rescue nil
@@ -798,7 +1092,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
798
1092
  class BulkAlterTableMigrationsTest < ActiveRecord::TestCase
799
1093
  def setup
800
1094
  @connection = Person.connection
801
- @connection.create_table(:delete_me, :force => true) {|t| }
1095
+ @connection.create_table(:delete_me, force: true) { |t| }
802
1096
  Person.reset_column_information
803
1097
  Person.reset_sequence_name
804
1098
  end
@@ -808,19 +1102,45 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
808
1102
  end
809
1103
 
810
1104
  def test_adding_multiple_columns
811
- assert_queries(1) do
1105
+ classname = ActiveRecord::Base.connection.class.name[/[^:]*$/]
1106
+ expected_query_count = {
1107
+ "Mysql2Adapter" => 1,
1108
+ "PostgreSQLAdapter" => 2, # one for bulk change, one for comment
1109
+ }.fetch(classname) {
1110
+ raise "need an expected query count for #{classname}"
1111
+ }
1112
+
1113
+ assert_queries(expected_query_count) do
812
1114
  with_bulk_change_table do |t|
813
1115
  t.column :name, :string
814
1116
  t.string :qualification, :experience
815
- t.integer :age, :default => 0
816
- t.date :birthdate
1117
+ t.integer :age, default: 0
1118
+ t.date :birthdate, comment: "This is a comment"
817
1119
  t.timestamps null: true
818
1120
  end
819
1121
  end
820
1122
 
821
1123
  assert_equal 8, columns.size
822
- [:name, :qualification, :experience].each {|s| assert_equal :string, column(s).type }
823
- assert_equal '0', column(:age).default
1124
+ [:name, :qualification, :experience].each { |s| assert_equal :string, column(s).type }
1125
+ assert_equal "0", column(:age).default
1126
+ assert_equal "This is a comment", column(:birthdate).comment
1127
+ end
1128
+
1129
+ def test_rename_columns
1130
+ with_bulk_change_table do |t|
1131
+ t.string :qualification
1132
+ end
1133
+
1134
+ assert column(:qualification)
1135
+
1136
+ with_bulk_change_table do |t|
1137
+ t.rename :qualification, :experience
1138
+ t.string :qualification_experience
1139
+ end
1140
+
1141
+ assert_not column(:qualification)
1142
+ assert column(:experience)
1143
+ assert column(:qualification_experience)
824
1144
  end
825
1145
 
826
1146
  def test_removing_columns
@@ -828,7 +1148,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
828
1148
  t.string :qualification, :experience
829
1149
  end
830
1150
 
831
- [:qualification, :experience].each {|c| assert column(c) }
1151
+ [:qualification, :experience].each { |c| assert column(c) }
832
1152
 
833
1153
  assert_queries(1) do
834
1154
  with_bulk_change_table do |t|
@@ -837,7 +1157,7 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
837
1157
  end
838
1158
  end
839
1159
 
840
- [:qualification, :experience].each {|c| assert ! column(c) }
1160
+ [:qualification, :experience].each { |c| assert_not column(c) }
841
1161
  assert column(:qualification_experience)
842
1162
  end
843
1163
 
@@ -848,19 +1168,27 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
848
1168
  t.integer :age
849
1169
  end
850
1170
 
851
- # Adding an index fires a query every time to check if an index already exists or not
852
- assert_queries(3) do
1171
+ classname = ActiveRecord::Base.connection.class.name[/[^:]*$/]
1172
+ expected_query_count = {
1173
+ "Mysql2Adapter" => 1, # mysql2 supports creating two indexes using one statement
1174
+ "PostgreSQLAdapter" => 3,
1175
+ }.fetch(classname) {
1176
+ raise "need an expected query count for #{classname}"
1177
+ }
1178
+
1179
+ assert_queries(expected_query_count) do
853
1180
  with_bulk_change_table do |t|
854
- t.index :username, :unique => true, :name => :awesome_username_index
855
- t.index [:name, :age]
1181
+ t.index :username, unique: true, name: :awesome_username_index
1182
+ t.index [:name, :age], comment: "This is a comment"
856
1183
  end
857
1184
  end
858
1185
 
859
1186
  assert_equal 2, indexes.size
860
1187
 
861
1188
  name_age_index = index(:index_delete_me_on_name_and_age)
862
- assert_equal ['name', 'age'].sort, name_age_index.columns.sort
863
- assert ! name_age_index.unique
1189
+ assert_equal ["name", "age"].sort, name_age_index.columns.sort
1190
+ assert_equal "This is a comment", name_age_index.comment
1191
+ assert_not name_age_index.unique
864
1192
 
865
1193
  assert index(:awesome_username_index).unique
866
1194
  end
@@ -873,14 +1201,22 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
873
1201
 
874
1202
  assert index(:index_delete_me_on_name)
875
1203
 
876
- assert_queries(3) do
1204
+ classname = ActiveRecord::Base.connection.class.name[/[^:]*$/]
1205
+ expected_query_count = {
1206
+ "Mysql2Adapter" => 1, # mysql2 supports dropping and creating two indexes using one statement
1207
+ "PostgreSQLAdapter" => 2,
1208
+ }.fetch(classname) {
1209
+ raise "need an expected query count for #{classname}"
1210
+ }
1211
+
1212
+ assert_queries(expected_query_count) do
877
1213
  with_bulk_change_table do |t|
878
1214
  t.remove_index :name
879
- t.index :name, :name => :new_name_index, :unique => true
1215
+ t.index :name, name: :new_name_index, unique: true
880
1216
  end
881
1217
  end
882
1218
 
883
- assert ! index(:index_delete_me_on_name)
1219
+ assert_not index(:index_delete_me_on_name)
884
1220
 
885
1221
  new_name_index = index(:new_name_index)
886
1222
  assert new_name_index.unique
@@ -892,51 +1228,83 @@ if ActiveRecord::Base.connection.supports_bulk_alter?
892
1228
  t.date :birthdate
893
1229
  end
894
1230
 
895
- assert ! column(:name).default
1231
+ assert_not column(:name).default
896
1232
  assert_equal :date, column(:birthdate).type
897
1233
 
898
- # One query for columns (delete_me table)
899
- # One query for primary key (delete_me table)
900
- # One query to do the bulk change
901
- assert_queries(3, :ignore_none => true) do
1234
+ classname = ActiveRecord::Base.connection.class.name[/[^:]*$/]
1235
+ expected_query_count = {
1236
+ "Mysql2Adapter" => 3, # one query for columns, one query for primary key, one query to do the bulk change
1237
+ "PostgreSQLAdapter" => 3, # one query for columns, one for bulk change, one for comment
1238
+ }.fetch(classname) {
1239
+ raise "need an expected query count for #{classname}"
1240
+ }
1241
+
1242
+ assert_queries(expected_query_count, ignore_none: true) do
902
1243
  with_bulk_change_table do |t|
903
- t.change :name, :string, :default => 'NONAME'
904
- t.change :birthdate, :datetime
1244
+ t.change :name, :string, default: "NONAME"
1245
+ t.change :birthdate, :datetime, comment: "This is a comment"
905
1246
  end
906
1247
  end
907
1248
 
908
- assert_equal 'NONAME', column(:name).default
1249
+ assert_equal "NONAME", column(:name).default
909
1250
  assert_equal :datetime, column(:birthdate).type
1251
+ assert_equal "This is a comment", column(:birthdate).comment
910
1252
  end
911
1253
 
912
- protected
1254
+ def test_changing_index
1255
+ with_bulk_change_table do |t|
1256
+ t.string :username
1257
+ t.index :username, name: :username_index
1258
+ end
913
1259
 
914
- def with_bulk_change_table
915
- # Reset columns/indexes cache as we're changing the table
916
- @columns = @indexes = nil
1260
+ assert index(:username_index)
1261
+ assert_not index(:username_index).unique
917
1262
 
918
- Person.connection.change_table(:delete_me, :bulk => true) do |t|
919
- yield t
1263
+ classname = ActiveRecord::Base.connection.class.name[/[^:]*$/]
1264
+ expected_query_count = {
1265
+ "Mysql2Adapter" => 1, # mysql2 supports dropping and creating two indexes using one statement
1266
+ "PostgreSQLAdapter" => 2,
1267
+ }.fetch(classname) {
1268
+ raise "need an expected query count for #{classname}"
1269
+ }
1270
+
1271
+ assert_queries(expected_query_count) do
1272
+ with_bulk_change_table do |t|
1273
+ t.remove_index name: :username_index
1274
+ t.index :username, name: :username_index, unique: true
1275
+ end
920
1276
  end
921
- end
922
1277
 
923
- def column(name)
924
- columns.detect {|c| c.name == name.to_s }
1278
+ assert index(:username_index)
1279
+ assert index(:username_index).unique
925
1280
  end
926
1281
 
927
- def columns
928
- @columns ||= Person.connection.columns('delete_me')
929
- end
1282
+ private
1283
+ def with_bulk_change_table
1284
+ # Reset columns/indexes cache as we're changing the table
1285
+ @columns = @indexes = nil
930
1286
 
931
- def index(name)
932
- indexes.detect {|i| i.name == name.to_s }
933
- end
1287
+ Person.connection.change_table(:delete_me, bulk: true) do |t|
1288
+ yield t
1289
+ end
1290
+ end
934
1291
 
935
- def indexes
936
- @indexes ||= Person.connection.indexes('delete_me')
937
- end
938
- end # AlterTableMigrationsTest
1292
+ def column(name)
1293
+ columns.detect { |c| c.name == name.to_s }
1294
+ end
1295
+
1296
+ def columns
1297
+ @columns ||= Person.connection.columns("delete_me")
1298
+ end
939
1299
 
1300
+ def index(name)
1301
+ indexes.detect { |i| i.name == name.to_s }
1302
+ end
1303
+
1304
+ def indexes
1305
+ @indexes ||= Person.connection.indexes("delete_me")
1306
+ end
1307
+ end # AlterTableMigrationsTest
940
1308
  end
941
1309
 
942
1310
  class CopyMigrationsTest < ActiveRecord::TestCase
@@ -956,18 +1324,18 @@ class CopyMigrationsTest < ActiveRecord::TestCase
956
1324
  @migrations_path = MIGRATIONS_ROOT + "/valid"
957
1325
  @existing_migrations = Dir[@migrations_path + "/*.rb"]
958
1326
 
959
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy"})
1327
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy")
960
1328
  assert File.exist?(@migrations_path + "/4_people_have_hobbies.bukkits.rb")
961
1329
  assert File.exist?(@migrations_path + "/5_people_have_descriptions.bukkits.rb")
962
1330
  assert_equal [@migrations_path + "/4_people_have_hobbies.bukkits.rb", @migrations_path + "/5_people_have_descriptions.bukkits.rb"], copied.map(&:filename)
963
1331
 
964
1332
  expected = "# This migration comes from bukkits (originally 1)"
965
- assert_equal expected, IO.readlines(@migrations_path + "/4_people_have_hobbies.bukkits.rb")[0].chomp
1333
+ assert_equal expected, IO.readlines(@migrations_path + "/4_people_have_hobbies.bukkits.rb")[1].chomp
966
1334
 
967
1335
  files_count = Dir[@migrations_path + "/*.rb"].length
968
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy"})
1336
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy")
969
1337
  assert_equal files_count, Dir[@migrations_path + "/*.rb"].length
970
- assert copied.empty?
1338
+ assert_empty copied
971
1339
  ensure
972
1340
  clear
973
1341
  end
@@ -998,7 +1366,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase
998
1366
  @existing_migrations = Dir[@migrations_path + "/*.rb"]
999
1367
 
1000
1368
  travel_to(Time.utc(2010, 7, 26, 10, 10, 10)) do
1001
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1369
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1002
1370
  assert File.exist?(@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb")
1003
1371
  assert File.exist?(@migrations_path + "/20100726101011_people_have_descriptions.bukkits.rb")
1004
1372
  expected = [@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb",
@@ -1006,9 +1374,9 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1006
1374
  assert_equal expected, copied.map(&:filename)
1007
1375
 
1008
1376
  files_count = Dir[@migrations_path + "/*.rb"].length
1009
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1377
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1010
1378
  assert_equal files_count, Dir[@migrations_path + "/*.rb"].length
1011
- assert copied.empty?
1379
+ assert_empty copied
1012
1380
  end
1013
1381
  ensure
1014
1382
  clear
@@ -1043,14 +1411,14 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1043
1411
  @existing_migrations = Dir[@migrations_path + "/*.rb"]
1044
1412
 
1045
1413
  travel_to(Time.utc(2010, 2, 20, 10, 10, 10)) do
1046
- ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1414
+ ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1047
1415
  assert File.exist?(@migrations_path + "/20100301010102_people_have_hobbies.bukkits.rb")
1048
1416
  assert File.exist?(@migrations_path + "/20100301010103_people_have_descriptions.bukkits.rb")
1049
1417
 
1050
1418
  files_count = Dir[@migrations_path + "/*.rb"].length
1051
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1419
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1052
1420
  assert_equal files_count, Dir[@migrations_path + "/*.rb"].length
1053
- assert copied.empty?
1421
+ assert_empty copied
1054
1422
  end
1055
1423
  ensure
1056
1424
  clear
@@ -1061,17 +1429,17 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1061
1429
  @migrations_path = MIGRATIONS_ROOT + "/valid"
1062
1430
  @existing_migrations = Dir[@migrations_path + "/*.rb"]
1063
1431
 
1064
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/magic"})
1432
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/magic")
1065
1433
  assert File.exist?(@migrations_path + "/4_currencies_have_symbols.bukkits.rb")
1066
1434
  assert_equal [@migrations_path + "/4_currencies_have_symbols.bukkits.rb"], copied.map(&:filename)
1067
1435
 
1068
- expected = "# coding: ISO-8859-15\n# This migration comes from bukkits (originally 1)"
1069
- assert_equal expected, IO.readlines(@migrations_path + "/4_currencies_have_symbols.bukkits.rb")[0..1].join.chomp
1436
+ expected = "# frozen_string_literal: true\n# coding: ISO-8859-15\n# This migration comes from bukkits (originally 1)"
1437
+ assert_equal expected, IO.readlines(@migrations_path + "/4_currencies_have_symbols.bukkits.rb")[0..2].join.chomp
1070
1438
 
1071
1439
  files_count = Dir[@migrations_path + "/*.rb"].length
1072
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/magic"})
1440
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/magic")
1073
1441
  assert_equal files_count, Dir[@migrations_path + "/*.rb"].length
1074
- assert copied.empty?
1442
+ assert_empty copied
1075
1443
  ensure
1076
1444
  clear
1077
1445
  end
@@ -1086,7 +1454,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1086
1454
 
1087
1455
  skipped = []
1088
1456
  on_skip = Proc.new { |name, migration| skipped << "#{name} #{migration.name}" }
1089
- copied = ActiveRecord::Migration.copy(@migrations_path, sources, :on_skip => on_skip)
1457
+ copied = ActiveRecord::Migration.copy(@migrations_path, sources, on_skip: on_skip)
1090
1458
  assert_equal 2, copied.length
1091
1459
 
1092
1460
  assert_equal 1, skipped.length
@@ -1104,8 +1472,8 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1104
1472
 
1105
1473
  skipped = []
1106
1474
  on_skip = Proc.new { |name, migration| skipped << "#{name} #{migration.name}" }
1107
- copied = ActiveRecord::Migration.copy(@migrations_path, sources, :on_skip => on_skip)
1108
- ActiveRecord::Migration.copy(@migrations_path, sources, :on_skip => on_skip)
1475
+ copied = ActiveRecord::Migration.copy(@migrations_path, sources, on_skip: on_skip)
1476
+ ActiveRecord::Migration.copy(@migrations_path, sources, on_skip: on_skip)
1109
1477
 
1110
1478
  assert_equal 2, copied.length
1111
1479
  assert_equal 0, skipped.length
@@ -1118,7 +1486,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1118
1486
  @existing_migrations = []
1119
1487
 
1120
1488
  travel_to(Time.utc(2010, 7, 26, 10, 10, 10)) do
1121
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1489
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1122
1490
  assert File.exist?(@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb")
1123
1491
  assert File.exist?(@migrations_path + "/20100726101011_people_have_descriptions.bukkits.rb")
1124
1492
  assert_equal 2, copied.length
@@ -1133,7 +1501,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1133
1501
  @existing_migrations = []
1134
1502
 
1135
1503
  travel_to(Time.utc(2010, 7, 26, 10, 10, 10)) do
1136
- copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"})
1504
+ copied = ActiveRecord::Migration.copy(@migrations_path, bukkits: MIGRATIONS_ROOT + "/to_copy_with_timestamps")
1137
1505
  assert File.exist?(@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb")
1138
1506
  assert File.exist?(@migrations_path + "/20100726101011_people_have_descriptions.bukkits.rb")
1139
1507
  assert_equal 2, copied.length
@@ -1145,7 +1513,7 @@ class CopyMigrationsTest < ActiveRecord::TestCase
1145
1513
  def test_check_pending_with_stdlib_logger
1146
1514
  old, ActiveRecord::Base.logger = ActiveRecord::Base.logger, ::Logger.new($stdout)
1147
1515
  quietly do
1148
- assert_nothing_raised { ActiveRecord::Migration::CheckPending.new(Proc.new {}).call({}) }
1516
+ assert_nothing_raised { ActiveRecord::Migration::CheckPending.new(Proc.new { }).call({}) }
1149
1517
  end
1150
1518
  ensure
1151
1519
  ActiveRecord::Base.logger = old