sequel 5.8.0 → 5.38.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (510) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +409 -1795
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -4
  5. data/bin/sequel +4 -0
  6. data/doc/advanced_associations.rdoc +136 -18
  7. data/doc/association_basics.rdoc +10 -5
  8. data/doc/cheat_sheet.rdoc +1 -0
  9. data/doc/code_order.rdoc +12 -2
  10. data/doc/dataset_filtering.rdoc +17 -2
  11. data/doc/mass_assignment.rdoc +3 -3
  12. data/doc/model_dataset_method_design.rdoc +1 -1
  13. data/doc/model_plugins.rdoc +1 -1
  14. data/doc/opening_databases.rdoc +30 -8
  15. data/doc/postgresql.rdoc +107 -2
  16. data/doc/release_notes/5.10.0.txt +84 -0
  17. data/doc/release_notes/5.11.0.txt +83 -0
  18. data/doc/release_notes/5.12.0.txt +141 -0
  19. data/doc/release_notes/5.13.0.txt +27 -0
  20. data/doc/release_notes/5.14.0.txt +63 -0
  21. data/doc/release_notes/5.15.0.txt +39 -0
  22. data/doc/release_notes/5.16.0.txt +110 -0
  23. data/doc/release_notes/5.17.0.txt +31 -0
  24. data/doc/release_notes/5.18.0.txt +69 -0
  25. data/doc/release_notes/5.19.0.txt +28 -0
  26. data/doc/release_notes/5.20.0.txt +89 -0
  27. data/doc/release_notes/5.21.0.txt +87 -0
  28. data/doc/release_notes/5.22.0.txt +48 -0
  29. data/doc/release_notes/5.23.0.txt +56 -0
  30. data/doc/release_notes/5.24.0.txt +56 -0
  31. data/doc/release_notes/5.25.0.txt +32 -0
  32. data/doc/release_notes/5.26.0.txt +35 -0
  33. data/doc/release_notes/5.27.0.txt +21 -0
  34. data/doc/release_notes/5.28.0.txt +16 -0
  35. data/doc/release_notes/5.29.0.txt +22 -0
  36. data/doc/release_notes/5.30.0.txt +20 -0
  37. data/doc/release_notes/5.31.0.txt +148 -0
  38. data/doc/release_notes/5.32.0.txt +46 -0
  39. data/doc/release_notes/5.33.0.txt +24 -0
  40. data/doc/release_notes/5.34.0.txt +40 -0
  41. data/doc/release_notes/5.35.0.txt +56 -0
  42. data/doc/release_notes/5.36.0.txt +60 -0
  43. data/doc/release_notes/5.37.0.txt +30 -0
  44. data/doc/release_notes/5.38.0.txt +28 -0
  45. data/doc/release_notes/5.9.0.txt +99 -0
  46. data/doc/security.rdoc +10 -0
  47. data/doc/sharding.rdoc +42 -28
  48. data/doc/sql.rdoc +12 -0
  49. data/doc/testing.rdoc +24 -17
  50. data/doc/transactions.rdoc +78 -0
  51. data/doc/validations.rdoc +2 -2
  52. data/lib/sequel/adapters/ado.rb +26 -18
  53. data/lib/sequel/adapters/ado/access.rb +2 -2
  54. data/lib/sequel/adapters/ado/mssql.rb +5 -8
  55. data/lib/sequel/adapters/amalgalite.rb +1 -1
  56. data/lib/sequel/adapters/jdbc.rb +71 -27
  57. data/lib/sequel/adapters/jdbc/mysql.rb +6 -6
  58. data/lib/sequel/adapters/jdbc/oracle.rb +7 -6
  59. data/lib/sequel/adapters/jdbc/postgresql.rb +17 -28
  60. data/lib/sequel/adapters/jdbc/sqlanywhere.rb +5 -6
  61. data/lib/sequel/adapters/jdbc/sqlite.rb +33 -2
  62. data/lib/sequel/adapters/jdbc/sqlserver.rb +4 -3
  63. data/lib/sequel/adapters/jdbc/transactions.rb +14 -28
  64. data/lib/sequel/adapters/mysql.rb +14 -15
  65. data/lib/sequel/adapters/mysql2.rb +5 -3
  66. data/lib/sequel/adapters/odbc.rb +4 -6
  67. data/lib/sequel/adapters/oracle.rb +7 -7
  68. data/lib/sequel/adapters/postgres.rb +52 -16
  69. data/lib/sequel/adapters/shared/access.rb +16 -12
  70. data/lib/sequel/adapters/shared/db2.rb +5 -0
  71. data/lib/sequel/adapters/shared/mssql.rb +41 -18
  72. data/lib/sequel/adapters/shared/mysql.rb +66 -19
  73. data/lib/sequel/adapters/shared/oracle.rb +29 -23
  74. data/lib/sequel/adapters/shared/postgres.rb +341 -95
  75. data/lib/sequel/adapters/shared/sqlanywhere.rb +23 -10
  76. data/lib/sequel/adapters/shared/sqlite.rb +174 -21
  77. data/lib/sequel/adapters/sqlanywhere.rb +33 -17
  78. data/lib/sequel/adapters/sqlite.rb +78 -68
  79. data/lib/sequel/adapters/tinytds.rb +14 -6
  80. data/lib/sequel/adapters/utils/emulate_offset_with_reverse_and_count.rb +2 -5
  81. data/lib/sequel/adapters/utils/mysql_mysql2.rb +5 -1
  82. data/lib/sequel/connection_pool.rb +2 -6
  83. data/lib/sequel/connection_pool/sharded_single.rb +7 -4
  84. data/lib/sequel/connection_pool/sharded_threaded.rb +32 -21
  85. data/lib/sequel/connection_pool/single.rb +1 -1
  86. data/lib/sequel/connection_pool/threaded.rb +26 -11
  87. data/lib/sequel/core.rb +327 -319
  88. data/lib/sequel/database/connecting.rb +7 -8
  89. data/lib/sequel/database/logging.rb +7 -1
  90. data/lib/sequel/database/misc.rb +68 -34
  91. data/lib/sequel/database/query.rb +6 -4
  92. data/lib/sequel/database/schema_generator.rb +31 -11
  93. data/lib/sequel/database/schema_methods.rb +32 -22
  94. data/lib/sequel/database/transactions.rb +129 -25
  95. data/lib/sequel/dataset.rb +4 -2
  96. data/lib/sequel/dataset/actions.rb +34 -23
  97. data/lib/sequel/dataset/features.rb +34 -0
  98. data/lib/sequel/dataset/graph.rb +27 -11
  99. data/lib/sequel/dataset/misc.rb +17 -3
  100. data/lib/sequel/dataset/placeholder_literalizer.rb +50 -21
  101. data/lib/sequel/dataset/prepared_statements.rb +96 -26
  102. data/lib/sequel/dataset/query.rb +43 -8
  103. data/lib/sequel/dataset/sql.rb +189 -41
  104. data/lib/sequel/deprecated.rb +3 -1
  105. data/lib/sequel/exceptions.rb +2 -0
  106. data/lib/sequel/extensions/_pretty_table.rb +1 -2
  107. data/lib/sequel/extensions/any_not_empty.rb +45 -0
  108. data/lib/sequel/extensions/caller_logging.rb +79 -0
  109. data/lib/sequel/extensions/columns_introspection.rb +1 -2
  110. data/lib/sequel/extensions/connection_expiration.rb +6 -6
  111. data/lib/sequel/extensions/connection_validator.rb +7 -6
  112. data/lib/sequel/extensions/constant_sql_override.rb +65 -0
  113. data/lib/sequel/extensions/constraint_validations.rb +53 -28
  114. data/lib/sequel/extensions/core_refinements.rb +2 -0
  115. data/lib/sequel/extensions/duplicate_columns_handler.rb +2 -0
  116. data/lib/sequel/extensions/escaped_like.rb +100 -0
  117. data/lib/sequel/extensions/eval_inspect.rb +3 -1
  118. data/lib/sequel/extensions/exclude_or_null.rb +68 -0
  119. data/lib/sequel/extensions/fiber_concurrency.rb +24 -0
  120. data/lib/sequel/extensions/index_caching.rb +9 -7
  121. data/lib/sequel/extensions/integer64.rb +3 -1
  122. data/lib/sequel/extensions/looser_typecasting.rb +3 -3
  123. data/lib/sequel/extensions/migration.rb +13 -6
  124. data/lib/sequel/extensions/named_timezones.rb +84 -23
  125. data/lib/sequel/extensions/pg_array.rb +87 -79
  126. data/lib/sequel/extensions/pg_array_ops.rb +14 -6
  127. data/lib/sequel/extensions/pg_enum.rb +34 -18
  128. data/lib/sequel/extensions/pg_extended_date_support.rb +34 -14
  129. data/lib/sequel/extensions/pg_hstore.rb +6 -0
  130. data/lib/sequel/extensions/pg_hstore_ops.rb +2 -0
  131. data/lib/sequel/extensions/pg_inet.rb +15 -5
  132. data/lib/sequel/extensions/pg_interval.rb +2 -0
  133. data/lib/sequel/extensions/pg_json.rb +387 -123
  134. data/lib/sequel/extensions/pg_json_ops.rb +168 -0
  135. data/lib/sequel/extensions/pg_range.rb +20 -10
  136. data/lib/sequel/extensions/pg_range_ops.rb +2 -0
  137. data/lib/sequel/extensions/pg_row.rb +3 -2
  138. data/lib/sequel/extensions/pg_row_ops.rb +24 -0
  139. data/lib/sequel/extensions/pg_static_cache_updater.rb +2 -2
  140. data/lib/sequel/extensions/pg_timestamptz.rb +2 -0
  141. data/lib/sequel/extensions/query.rb +1 -0
  142. data/lib/sequel/extensions/run_transaction_hooks.rb +72 -0
  143. data/lib/sequel/extensions/s.rb +2 -0
  144. data/lib/sequel/extensions/schema_dumper.rb +13 -7
  145. data/lib/sequel/extensions/sequel_4_dataset_methods.rb +4 -2
  146. data/lib/sequel/extensions/server_block.rb +18 -7
  147. data/lib/sequel/extensions/sql_comments.rb +2 -2
  148. data/lib/sequel/extensions/symbol_aref_refinement.rb +2 -0
  149. data/lib/sequel/extensions/symbol_as_refinement.rb +2 -0
  150. data/lib/sequel/extensions/to_dot.rb +9 -3
  151. data/lib/sequel/model.rb +3 -1
  152. data/lib/sequel/model/associations.rb +403 -69
  153. data/lib/sequel/model/base.rb +170 -90
  154. data/lib/sequel/model/plugins.rb +105 -0
  155. data/lib/sequel/plugins/after_initialize.rb +1 -1
  156. data/lib/sequel/plugins/association_dependencies.rb +3 -3
  157. data/lib/sequel/plugins/association_lazy_eager_option.rb +66 -0
  158. data/lib/sequel/plugins/association_multi_add_remove.rb +85 -0
  159. data/lib/sequel/plugins/association_pks.rb +74 -22
  160. data/lib/sequel/plugins/association_proxies.rb +6 -2
  161. data/lib/sequel/plugins/auto_validations.rb +36 -17
  162. data/lib/sequel/plugins/blacklist_security.rb +1 -2
  163. data/lib/sequel/plugins/boolean_subsets.rb +4 -1
  164. data/lib/sequel/plugins/caching.rb +3 -0
  165. data/lib/sequel/plugins/class_table_inheritance.rb +62 -34
  166. data/lib/sequel/plugins/composition.rb +13 -9
  167. data/lib/sequel/plugins/csv_serializer.rb +28 -9
  168. data/lib/sequel/plugins/defaults_setter.rb +2 -2
  169. data/lib/sequel/plugins/dirty.rb +60 -22
  170. data/lib/sequel/plugins/eager_graph_eager.rb +139 -0
  171. data/lib/sequel/plugins/empty_failure_backtraces.rb +38 -0
  172. data/lib/sequel/plugins/finder.rb +2 -2
  173. data/lib/sequel/plugins/forbid_lazy_load.rb +216 -0
  174. data/lib/sequel/plugins/hook_class_methods.rb +17 -5
  175. data/lib/sequel/plugins/insert_conflict.rb +72 -0
  176. data/lib/sequel/plugins/instance_specific_default.rb +113 -0
  177. data/lib/sequel/plugins/inverted_subsets.rb +2 -2
  178. data/lib/sequel/plugins/json_serializer.rb +21 -14
  179. data/lib/sequel/plugins/lazy_attributes.rb +1 -1
  180. data/lib/sequel/plugins/list.rb +22 -10
  181. data/lib/sequel/plugins/many_through_many.rb +1 -1
  182. data/lib/sequel/plugins/nested_attributes.rb +27 -5
  183. data/lib/sequel/plugins/pg_array_associations.rb +12 -9
  184. data/lib/sequel/plugins/pg_auto_constraint_validations.rb +149 -61
  185. data/lib/sequel/plugins/prepared_statements.rb +6 -12
  186. data/lib/sequel/plugins/prepared_statements_safe.rb +1 -3
  187. data/lib/sequel/plugins/rcte_tree.rb +20 -22
  188. data/lib/sequel/plugins/sharding.rb +13 -7
  189. data/lib/sequel/plugins/single_table_inheritance.rb +20 -15
  190. data/lib/sequel/plugins/skip_saving_columns.rb +108 -0
  191. data/lib/sequel/plugins/static_cache.rb +36 -17
  192. data/lib/sequel/plugins/static_cache_cache.rb +53 -0
  193. data/lib/sequel/plugins/string_stripper.rb +1 -1
  194. data/lib/sequel/plugins/subclasses.rb +2 -0
  195. data/lib/sequel/plugins/subset_conditions.rb +2 -2
  196. data/lib/sequel/plugins/tactical_eager_loading.rb +73 -2
  197. data/lib/sequel/plugins/throw_failures.rb +110 -0
  198. data/lib/sequel/plugins/tree.rb +49 -31
  199. data/lib/sequel/plugins/typecast_on_load.rb +3 -2
  200. data/lib/sequel/plugins/validation_class_methods.rb +11 -5
  201. data/lib/sequel/plugins/validation_helpers.rb +2 -2
  202. data/lib/sequel/sql.rb +120 -30
  203. data/lib/sequel/timezones.rb +55 -14
  204. data/lib/sequel/version.rb +6 -1
  205. metadata +101 -361
  206. data/Rakefile +0 -151
  207. data/doc/release_notes/4.0.0.txt +0 -262
  208. data/doc/release_notes/4.1.0.txt +0 -85
  209. data/doc/release_notes/4.10.0.txt +0 -226
  210. data/doc/release_notes/4.11.0.txt +0 -147
  211. data/doc/release_notes/4.12.0.txt +0 -105
  212. data/doc/release_notes/4.13.0.txt +0 -169
  213. data/doc/release_notes/4.14.0.txt +0 -68
  214. data/doc/release_notes/4.15.0.txt +0 -56
  215. data/doc/release_notes/4.16.0.txt +0 -36
  216. data/doc/release_notes/4.17.0.txt +0 -38
  217. data/doc/release_notes/4.18.0.txt +0 -36
  218. data/doc/release_notes/4.19.0.txt +0 -45
  219. data/doc/release_notes/4.2.0.txt +0 -129
  220. data/doc/release_notes/4.20.0.txt +0 -79
  221. data/doc/release_notes/4.21.0.txt +0 -94
  222. data/doc/release_notes/4.22.0.txt +0 -72
  223. data/doc/release_notes/4.23.0.txt +0 -65
  224. data/doc/release_notes/4.24.0.txt +0 -99
  225. data/doc/release_notes/4.25.0.txt +0 -181
  226. data/doc/release_notes/4.26.0.txt +0 -44
  227. data/doc/release_notes/4.27.0.txt +0 -78
  228. data/doc/release_notes/4.28.0.txt +0 -57
  229. data/doc/release_notes/4.29.0.txt +0 -41
  230. data/doc/release_notes/4.3.0.txt +0 -40
  231. data/doc/release_notes/4.30.0.txt +0 -37
  232. data/doc/release_notes/4.31.0.txt +0 -57
  233. data/doc/release_notes/4.32.0.txt +0 -132
  234. data/doc/release_notes/4.33.0.txt +0 -88
  235. data/doc/release_notes/4.34.0.txt +0 -86
  236. data/doc/release_notes/4.35.0.txt +0 -130
  237. data/doc/release_notes/4.36.0.txt +0 -116
  238. data/doc/release_notes/4.37.0.txt +0 -50
  239. data/doc/release_notes/4.38.0.txt +0 -67
  240. data/doc/release_notes/4.39.0.txt +0 -127
  241. data/doc/release_notes/4.4.0.txt +0 -92
  242. data/doc/release_notes/4.40.0.txt +0 -179
  243. data/doc/release_notes/4.41.0.txt +0 -77
  244. data/doc/release_notes/4.42.0.txt +0 -221
  245. data/doc/release_notes/4.43.0.txt +0 -87
  246. data/doc/release_notes/4.44.0.txt +0 -125
  247. data/doc/release_notes/4.45.0.txt +0 -370
  248. data/doc/release_notes/4.46.0.txt +0 -404
  249. data/doc/release_notes/4.47.0.txt +0 -56
  250. data/doc/release_notes/4.48.0.txt +0 -293
  251. data/doc/release_notes/4.49.0.txt +0 -222
  252. data/doc/release_notes/4.5.0.txt +0 -34
  253. data/doc/release_notes/4.6.0.txt +0 -30
  254. data/doc/release_notes/4.7.0.txt +0 -103
  255. data/doc/release_notes/4.8.0.txt +0 -175
  256. data/doc/release_notes/4.9.0.txt +0 -190
  257. data/spec/adapter_spec.rb +0 -4
  258. data/spec/adapters/db2_spec.rb +0 -170
  259. data/spec/adapters/mssql_spec.rb +0 -804
  260. data/spec/adapters/mysql_spec.rb +0 -1041
  261. data/spec/adapters/oracle_spec.rb +0 -327
  262. data/spec/adapters/postgres_spec.rb +0 -4000
  263. data/spec/adapters/spec_helper.rb +0 -43
  264. data/spec/adapters/sqlanywhere_spec.rb +0 -97
  265. data/spec/adapters/sqlite_spec.rb +0 -600
  266. data/spec/bin_spec.rb +0 -269
  267. data/spec/core/connection_pool_spec.rb +0 -1228
  268. data/spec/core/database_spec.rb +0 -2673
  269. data/spec/core/dataset_spec.rb +0 -5419
  270. data/spec/core/deprecated_spec.rb +0 -70
  271. data/spec/core/expression_filters_spec.rb +0 -1344
  272. data/spec/core/mock_adapter_spec.rb +0 -722
  273. data/spec/core/object_graph_spec.rb +0 -306
  274. data/spec/core/placeholder_literalizer_spec.rb +0 -166
  275. data/spec/core/schema_generator_spec.rb +0 -214
  276. data/spec/core/schema_spec.rb +0 -1820
  277. data/spec/core/spec_helper.rb +0 -23
  278. data/spec/core/version_spec.rb +0 -7
  279. data/spec/core_extensions_spec.rb +0 -762
  280. data/spec/core_model_spec.rb +0 -2
  281. data/spec/core_spec.rb +0 -1
  282. data/spec/deprecation_helper.rb +0 -30
  283. data/spec/extensions/accessed_columns_spec.rb +0 -51
  284. data/spec/extensions/active_model_spec.rb +0 -99
  285. data/spec/extensions/after_initialize_spec.rb +0 -24
  286. data/spec/extensions/arbitrary_servers_spec.rb +0 -109
  287. data/spec/extensions/association_dependencies_spec.rb +0 -125
  288. data/spec/extensions/association_pks_spec.rb +0 -423
  289. data/spec/extensions/association_proxies_spec.rb +0 -100
  290. data/spec/extensions/auto_literal_strings_spec.rb +0 -205
  291. data/spec/extensions/auto_validations_spec.rb +0 -202
  292. data/spec/extensions/blacklist_security_spec.rb +0 -95
  293. data/spec/extensions/blank_spec.rb +0 -69
  294. data/spec/extensions/boolean_readers_spec.rb +0 -93
  295. data/spec/extensions/boolean_subsets_spec.rb +0 -47
  296. data/spec/extensions/caching_spec.rb +0 -273
  297. data/spec/extensions/class_table_inheritance_spec.rb +0 -568
  298. data/spec/extensions/column_conflicts_spec.rb +0 -75
  299. data/spec/extensions/column_select_spec.rb +0 -129
  300. data/spec/extensions/columns_introspection_spec.rb +0 -90
  301. data/spec/extensions/columns_updated_spec.rb +0 -35
  302. data/spec/extensions/composition_spec.rb +0 -248
  303. data/spec/extensions/connection_expiration_spec.rb +0 -133
  304. data/spec/extensions/connection_validator_spec.rb +0 -127
  305. data/spec/extensions/constraint_validations_plugin_spec.rb +0 -300
  306. data/spec/extensions/constraint_validations_spec.rb +0 -395
  307. data/spec/extensions/core_refinements_spec.rb +0 -528
  308. data/spec/extensions/csv_serializer_spec.rb +0 -183
  309. data/spec/extensions/current_datetime_timestamp_spec.rb +0 -27
  310. data/spec/extensions/dataset_associations_spec.rb +0 -365
  311. data/spec/extensions/dataset_source_alias_spec.rb +0 -51
  312. data/spec/extensions/date_arithmetic_spec.rb +0 -181
  313. data/spec/extensions/datetime_parse_to_time_spec.rb +0 -169
  314. data/spec/extensions/def_dataset_method_spec.rb +0 -100
  315. data/spec/extensions/defaults_setter_spec.rb +0 -141
  316. data/spec/extensions/delay_add_association_spec.rb +0 -73
  317. data/spec/extensions/dirty_spec.rb +0 -189
  318. data/spec/extensions/duplicate_columns_handler_spec.rb +0 -104
  319. data/spec/extensions/eager_each_spec.rb +0 -62
  320. data/spec/extensions/empty_array_consider_nulls_spec.rb +0 -24
  321. data/spec/extensions/error_splitter_spec.rb +0 -18
  322. data/spec/extensions/error_sql_spec.rb +0 -20
  323. data/spec/extensions/eval_inspect_spec.rb +0 -74
  324. data/spec/extensions/finder_spec.rb +0 -260
  325. data/spec/extensions/force_encoding_spec.rb +0 -126
  326. data/spec/extensions/freeze_datasets_spec.rb +0 -31
  327. data/spec/extensions/graph_each_spec.rb +0 -113
  328. data/spec/extensions/hook_class_methods_spec.rb +0 -380
  329. data/spec/extensions/identifier_mangling_spec.rb +0 -201
  330. data/spec/extensions/implicit_subquery_spec.rb +0 -58
  331. data/spec/extensions/index_caching_spec.rb +0 -66
  332. data/spec/extensions/inflector_spec.rb +0 -183
  333. data/spec/extensions/input_transformer_spec.rb +0 -69
  334. data/spec/extensions/insert_returning_select_spec.rb +0 -72
  335. data/spec/extensions/instance_filters_spec.rb +0 -79
  336. data/spec/extensions/instance_hooks_spec.rb +0 -246
  337. data/spec/extensions/integer64_spec.rb +0 -22
  338. data/spec/extensions/inverted_subsets_spec.rb +0 -33
  339. data/spec/extensions/json_serializer_spec.rb +0 -336
  340. data/spec/extensions/lazy_attributes_spec.rb +0 -183
  341. data/spec/extensions/list_spec.rb +0 -275
  342. data/spec/extensions/looser_typecasting_spec.rb +0 -43
  343. data/spec/extensions/many_through_many_spec.rb +0 -2177
  344. data/spec/extensions/migration_spec.rb +0 -840
  345. data/spec/extensions/modification_detection_spec.rb +0 -93
  346. data/spec/extensions/mssql_optimistic_locking_spec.rb +0 -92
  347. data/spec/extensions/named_timezones_spec.rb +0 -109
  348. data/spec/extensions/nested_attributes_spec.rb +0 -703
  349. data/spec/extensions/null_dataset_spec.rb +0 -85
  350. data/spec/extensions/optimistic_locking_spec.rb +0 -127
  351. data/spec/extensions/pagination_spec.rb +0 -116
  352. data/spec/extensions/pg_array_associations_spec.rb +0 -802
  353. data/spec/extensions/pg_array_ops_spec.rb +0 -144
  354. data/spec/extensions/pg_array_spec.rb +0 -398
  355. data/spec/extensions/pg_auto_constraint_validations_spec.rb +0 -165
  356. data/spec/extensions/pg_enum_spec.rb +0 -113
  357. data/spec/extensions/pg_extended_date_support_spec.rb +0 -126
  358. data/spec/extensions/pg_hstore_ops_spec.rb +0 -238
  359. data/spec/extensions/pg_hstore_spec.rb +0 -219
  360. data/spec/extensions/pg_inet_ops_spec.rb +0 -102
  361. data/spec/extensions/pg_inet_spec.rb +0 -72
  362. data/spec/extensions/pg_interval_spec.rb +0 -103
  363. data/spec/extensions/pg_json_ops_spec.rb +0 -289
  364. data/spec/extensions/pg_json_spec.rb +0 -262
  365. data/spec/extensions/pg_loose_count_spec.rb +0 -23
  366. data/spec/extensions/pg_range_ops_spec.rb +0 -60
  367. data/spec/extensions/pg_range_spec.rb +0 -487
  368. data/spec/extensions/pg_row_ops_spec.rb +0 -61
  369. data/spec/extensions/pg_row_plugin_spec.rb +0 -60
  370. data/spec/extensions/pg_row_spec.rb +0 -363
  371. data/spec/extensions/pg_static_cache_updater_spec.rb +0 -93
  372. data/spec/extensions/pg_timestamptz_spec.rb +0 -17
  373. data/spec/extensions/prepared_statements_safe_spec.rb +0 -66
  374. data/spec/extensions/prepared_statements_spec.rb +0 -182
  375. data/spec/extensions/pretty_table_spec.rb +0 -123
  376. data/spec/extensions/query_spec.rb +0 -94
  377. data/spec/extensions/rcte_tree_spec.rb +0 -381
  378. data/spec/extensions/round_timestamps_spec.rb +0 -39
  379. data/spec/extensions/s_spec.rb +0 -60
  380. data/spec/extensions/schema_caching_spec.rb +0 -64
  381. data/spec/extensions/schema_dumper_spec.rb +0 -868
  382. data/spec/extensions/select_remove_spec.rb +0 -38
  383. data/spec/extensions/sequel_4_dataset_methods_spec.rb +0 -121
  384. data/spec/extensions/serialization_modification_detection_spec.rb +0 -98
  385. data/spec/extensions/serialization_spec.rb +0 -365
  386. data/spec/extensions/server_block_spec.rb +0 -97
  387. data/spec/extensions/server_logging_spec.rb +0 -45
  388. data/spec/extensions/sharding_spec.rb +0 -189
  389. data/spec/extensions/shared_caching_spec.rb +0 -151
  390. data/spec/extensions/single_table_inheritance_spec.rb +0 -347
  391. data/spec/extensions/singular_table_names_spec.rb +0 -22
  392. data/spec/extensions/skip_create_refresh_spec.rb +0 -18
  393. data/spec/extensions/spec_helper.rb +0 -61
  394. data/spec/extensions/split_array_nil_spec.rb +0 -24
  395. data/spec/extensions/split_values_spec.rb +0 -57
  396. data/spec/extensions/sql_comments_spec.rb +0 -33
  397. data/spec/extensions/sql_expr_spec.rb +0 -59
  398. data/spec/extensions/static_cache_spec.rb +0 -410
  399. data/spec/extensions/string_agg_spec.rb +0 -90
  400. data/spec/extensions/string_date_time_spec.rb +0 -95
  401. data/spec/extensions/string_stripper_spec.rb +0 -68
  402. data/spec/extensions/subclasses_spec.rb +0 -79
  403. data/spec/extensions/subset_conditions_spec.rb +0 -38
  404. data/spec/extensions/symbol_aref_refinement_spec.rb +0 -28
  405. data/spec/extensions/symbol_as_refinement_spec.rb +0 -21
  406. data/spec/extensions/synchronize_sql_spec.rb +0 -124
  407. data/spec/extensions/table_select_spec.rb +0 -83
  408. data/spec/extensions/tactical_eager_loading_spec.rb +0 -141
  409. data/spec/extensions/thread_local_timezones_spec.rb +0 -67
  410. data/spec/extensions/timestamps_spec.rb +0 -209
  411. data/spec/extensions/to_dot_spec.rb +0 -153
  412. data/spec/extensions/touch_spec.rb +0 -226
  413. data/spec/extensions/tree_spec.rb +0 -284
  414. data/spec/extensions/typecast_on_load_spec.rb +0 -86
  415. data/spec/extensions/unlimited_update_spec.rb +0 -21
  416. data/spec/extensions/update_or_create_spec.rb +0 -83
  417. data/spec/extensions/update_primary_key_spec.rb +0 -105
  418. data/spec/extensions/update_refresh_spec.rb +0 -59
  419. data/spec/extensions/uuid_spec.rb +0 -101
  420. data/spec/extensions/validate_associated_spec.rb +0 -52
  421. data/spec/extensions/validation_class_methods_spec.rb +0 -1040
  422. data/spec/extensions/validation_contexts_spec.rb +0 -31
  423. data/spec/extensions/validation_helpers_spec.rb +0 -525
  424. data/spec/extensions/whitelist_security_spec.rb +0 -157
  425. data/spec/extensions/xml_serializer_spec.rb +0 -213
  426. data/spec/files/bad_down_migration/001_create_alt_basic.rb +0 -4
  427. data/spec/files/bad_down_migration/002_create_alt_advanced.rb +0 -4
  428. data/spec/files/bad_timestamped_migrations/1273253849_create_sessions.rb +0 -9
  429. data/spec/files/bad_timestamped_migrations/1273253851_create_nodes.rb +0 -9
  430. data/spec/files/bad_timestamped_migrations/1273253853_3_create_users.rb +0 -3
  431. data/spec/files/bad_up_migration/001_create_alt_basic.rb +0 -4
  432. data/spec/files/bad_up_migration/002_create_alt_advanced.rb +0 -3
  433. data/spec/files/convert_to_timestamp_migrations/001_create_sessions.rb +0 -9
  434. data/spec/files/convert_to_timestamp_migrations/002_create_nodes.rb +0 -9
  435. data/spec/files/convert_to_timestamp_migrations/003_3_create_users.rb +0 -4
  436. data/spec/files/convert_to_timestamp_migrations/1273253850_create_artists.rb +0 -9
  437. data/spec/files/convert_to_timestamp_migrations/1273253852_create_albums.rb +0 -9
  438. data/spec/files/double_migration/001_create_sessions.rb +0 -9
  439. data/spec/files/double_migration/002_create_nodes.rb +0 -19
  440. data/spec/files/double_migration/003_3_create_users.rb +0 -4
  441. data/spec/files/duplicate_integer_migrations/001_create_alt_advanced.rb +0 -4
  442. data/spec/files/duplicate_integer_migrations/001_create_alt_basic.rb +0 -4
  443. data/spec/files/duplicate_timestamped_migrations/1273253849_create_sessions.rb +0 -9
  444. data/spec/files/duplicate_timestamped_migrations/1273253853_create_nodes.rb +0 -9
  445. data/spec/files/duplicate_timestamped_migrations/1273253853_create_users.rb +0 -4
  446. data/spec/files/empty_migration/001_create_sessions.rb +0 -9
  447. data/spec/files/empty_migration/002_create_nodes.rb +0 -0
  448. data/spec/files/empty_migration/003_3_create_users.rb +0 -4
  449. data/spec/files/integer_migrations/001_create_sessions.rb +0 -9
  450. data/spec/files/integer_migrations/002_create_nodes.rb +0 -9
  451. data/spec/files/integer_migrations/003_3_create_users.rb +0 -4
  452. data/spec/files/interleaved_timestamped_migrations/1273253849_create_sessions.rb +0 -9
  453. data/spec/files/interleaved_timestamped_migrations/1273253850_create_artists.rb +0 -9
  454. data/spec/files/interleaved_timestamped_migrations/1273253851_create_nodes.rb +0 -9
  455. data/spec/files/interleaved_timestamped_migrations/1273253852_create_albums.rb +0 -9
  456. data/spec/files/interleaved_timestamped_migrations/1273253853_3_create_users.rb +0 -4
  457. data/spec/files/missing_integer_migrations/001_create_alt_basic.rb +0 -4
  458. data/spec/files/missing_integer_migrations/003_create_alt_advanced.rb +0 -4
  459. data/spec/files/missing_timestamped_migrations/1273253849_create_sessions.rb +0 -9
  460. data/spec/files/missing_timestamped_migrations/1273253853_3_create_users.rb +0 -4
  461. data/spec/files/reversible_migrations/001_reversible.rb +0 -5
  462. data/spec/files/reversible_migrations/002_reversible.rb +0 -5
  463. data/spec/files/reversible_migrations/003_reversible.rb +0 -5
  464. data/spec/files/reversible_migrations/004_reversible.rb +0 -5
  465. data/spec/files/reversible_migrations/005_reversible.rb +0 -10
  466. data/spec/files/reversible_migrations/006_reversible.rb +0 -10
  467. data/spec/files/reversible_migrations/007_reversible.rb +0 -10
  468. data/spec/files/timestamped_migrations/1273253849_create_sessions.rb +0 -9
  469. data/spec/files/timestamped_migrations/1273253851_create_nodes.rb +0 -9
  470. data/spec/files/timestamped_migrations/1273253853_3_create_users.rb +0 -4
  471. data/spec/files/transaction_specified_migrations/001_create_alt_basic.rb +0 -4
  472. data/spec/files/transaction_specified_migrations/002_create_basic.rb +0 -4
  473. data/spec/files/transaction_unspecified_migrations/001_create_alt_basic.rb +0 -3
  474. data/spec/files/transaction_unspecified_migrations/002_create_basic.rb +0 -3
  475. data/spec/files/uppercase_timestamped_migrations/1273253849_CREATE_SESSIONS.RB +0 -9
  476. data/spec/files/uppercase_timestamped_migrations/1273253851_CREATE_NODES.RB +0 -9
  477. data/spec/files/uppercase_timestamped_migrations/1273253853_3_CREATE_USERS.RB +0 -4
  478. data/spec/guards_helper.rb +0 -58
  479. data/spec/integration/associations_test.rb +0 -2513
  480. data/spec/integration/database_test.rb +0 -113
  481. data/spec/integration/dataset_test.rb +0 -1880
  482. data/spec/integration/eager_loader_test.rb +0 -687
  483. data/spec/integration/migrator_test.rb +0 -262
  484. data/spec/integration/model_test.rb +0 -203
  485. data/spec/integration/plugin_test.rb +0 -2302
  486. data/spec/integration/prepared_statement_test.rb +0 -398
  487. data/spec/integration/schema_test.rb +0 -869
  488. data/spec/integration/spec_helper.rb +0 -64
  489. data/spec/integration/timezone_test.rb +0 -86
  490. data/spec/integration/transaction_test.rb +0 -354
  491. data/spec/integration/type_test.rb +0 -127
  492. data/spec/model/association_reflection_spec.rb +0 -803
  493. data/spec/model/associations_spec.rb +0 -4538
  494. data/spec/model/base_spec.rb +0 -817
  495. data/spec/model/class_dataset_methods_spec.rb +0 -146
  496. data/spec/model/dataset_methods_spec.rb +0 -198
  497. data/spec/model/eager_loading_spec.rb +0 -2262
  498. data/spec/model/hooks_spec.rb +0 -370
  499. data/spec/model/inflector_spec.rb +0 -26
  500. data/spec/model/model_spec.rb +0 -953
  501. data/spec/model/plugins_spec.rb +0 -318
  502. data/spec/model/record_spec.rb +0 -2107
  503. data/spec/model/spec_helper.rb +0 -45
  504. data/spec/model/validations_spec.rb +0 -193
  505. data/spec/model_no_assoc_spec.rb +0 -1
  506. data/spec/model_spec.rb +0 -1
  507. data/spec/plugin_spec.rb +0 -1
  508. data/spec/sequel_coverage.rb +0 -15
  509. data/spec/sequel_warning.rb +0 -4
  510. data/spec/spec_config.rb +0 -12
@@ -1,214 +0,0 @@
1
- require_relative "spec_helper"
2
-
3
- describe Sequel::Schema::CreateTableGenerator do
4
- before do
5
- @generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
6
- string(:title).must_be_nil
7
- column(:body, :text).must_be_nil
8
- foreign_key(:parent_id).must_be_nil
9
- primary_key(:id).must_be_nil
10
- check('price > 100').must_be_nil
11
- constraint(:xxx){{:yyy => :zzz}}.must_be_nil
12
- index(:title).must_be_nil
13
- index([:title, :body], :unique => true).must_be_nil
14
- foreign_key(:node_id, :nodes).must_be_nil
15
- foreign_key(:deferrable_node_id, :nodes, :deferrable => true).must_be_nil
16
- primary_key([:title, :parent_id], :name => :cpk).must_be_nil
17
- foreign_key([:node_id, :prop_id], :nodes_props, :name => :cfk).must_be_nil
18
- end
19
- @columns, @indexes, @constraints = @generator.columns, @generator.indexes, @generator.constraints
20
- end
21
-
22
- it "should respond to everything" do
23
- @generator.respond_to?(:foo).must_equal true
24
- end
25
-
26
- it "should respond adding types" do
27
- c = Class.new(Sequel::Schema::CreateTableGenerator)
28
- c2 = Class.new
29
- def c2.to_s; 'Foo' end
30
- c.add_type_method(c2)
31
- gen = c.new(Sequel.mock) do
32
- Foo :bar
33
- end
34
-
35
- gen.columns.first.values_at(:name, :type).must_equal [:bar, c2]
36
- end
37
-
38
- it "should primary key column first" do
39
- @columns.first[:name].must_equal :id
40
- @columns.first[:primary_key].must_equal true
41
- @columns[3][:name].must_equal :parent_id
42
- @columns[3][:primary_key].must_be_nil
43
- end
44
-
45
- it "should respect existing column order if primary_key :keep_order is used" do
46
- generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
47
- string :title
48
- primary_key :id, :keep_order=>true
49
- end
50
-
51
- columns = generator.columns
52
- columns.last[:name].must_equal :id
53
- columns.last[:primary_key].must_equal true
54
- columns.first[:name].must_equal :title
55
- columns.first[:primary_key].must_be_nil
56
- end
57
-
58
- it "should handle SQL::Identifier and SQL::QualifiedIdentifier as foreign_key arguments" do
59
- generator = Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
60
- foreign_key :a_id, Sequel.identifier(:as)
61
- foreign_key :b_id, Sequel.qualify(:c, :b)
62
- end
63
-
64
- columns = generator.columns
65
- columns.first.values_at(:name, :table).must_equal [:a_id, Sequel.identifier(:as)]
66
- columns.last.values_at(:name, :table).must_equal [:b_id, Sequel.qualify(:c, :b)]
67
- end
68
-
69
- it "counts definitions correctly" do
70
- @columns.size.must_equal 6
71
- @indexes.size.must_equal 2
72
- @constraints.size.must_equal 4
73
- end
74
-
75
- it "retrieves primary key name" do
76
- @generator.primary_key_name.must_equal :id
77
- end
78
-
79
- it "keeps columns in order" do
80
- @columns[1][:name].must_equal :title
81
- @columns[1][:type].must_equal :string
82
- @columns[2][:name].must_equal :body
83
- @columns[2][:type].must_equal :text
84
- end
85
-
86
- it "creates foreign key column" do
87
- @columns[3][:name].must_equal :parent_id
88
- @columns[3][:type].must_equal Integer
89
- @columns[4][:name].must_equal :node_id
90
- @columns[4][:type].must_equal Integer
91
- end
92
-
93
- it "creates deferrable altered foreign key column" do
94
- @columns[5][:name].must_equal :deferrable_node_id
95
- @columns[5][:type].must_equal Integer
96
- @columns[5][:deferrable].must_equal true
97
- end
98
-
99
- it "uses table for foreign key columns, if specified" do
100
- @columns[3][:table].must_be_nil
101
- @columns[4][:table].must_equal :nodes
102
- @constraints[3][:table].must_equal :nodes_props
103
- end
104
-
105
- it "finds columns" do
106
- [:title, :body, :parent_id, :id].each do |col|
107
- @generator.has_column?(col).must_equal true
108
- end
109
- @generator.has_column?(:foo).wont_equal true
110
- end
111
-
112
- it "creates constraints" do
113
- @constraints[0][:name].must_be_nil
114
- @constraints[0][:type].must_equal :check
115
- @constraints[0][:check].must_equal ['price > 100']
116
-
117
- @constraints[1][:name].must_equal :xxx
118
- @constraints[1][:type].must_equal :check
119
- @constraints[1][:check].must_be_kind_of Proc
120
-
121
- @constraints[2][:name].must_equal :cpk
122
- @constraints[2][:type].must_equal :primary_key
123
- @constraints[2][:columns].must_equal [ :title, :parent_id ]
124
-
125
- @constraints[3][:name].must_equal :cfk
126
- @constraints[3][:type].must_equal :foreign_key
127
- @constraints[3][:columns].must_equal [ :node_id, :prop_id ]
128
- @constraints[3][:table].must_equal :nodes_props
129
- end
130
-
131
- it "creates indexes" do
132
- @indexes[0][:columns].must_equal [:title]
133
- @indexes[1][:columns].must_equal [:title, :body]
134
- end
135
- end
136
-
137
- describe Sequel::Schema::AlterTableGenerator do
138
- before do
139
- @generator = Sequel::Schema::AlterTableGenerator.new(Sequel.mock) do
140
- add_column(:aaa, :text).must_be_nil
141
- drop_column(:bbb).must_be_nil
142
- rename_column(:ccc, :ho).must_be_nil
143
- set_column_type(:ddd, :float).must_be_nil
144
- set_column_default(:eee, 1).must_be_nil
145
- add_index([:fff, :ggg]).must_be_nil
146
- drop_index(:hhh).must_be_nil
147
- drop_index(:hhh, :name=>:blah_blah).must_be_nil
148
- add_full_text_index(:blah).must_be_nil
149
- add_spatial_index(:geom).must_be_nil
150
- add_index(:blah, :type => :hash).must_be_nil
151
- add_index(:blah, :where => {:something => true}).must_be_nil
152
- add_constraint(:con1, 'fred > 100').must_be_nil
153
- drop_constraint(:con2).must_be_nil
154
- add_unique_constraint([:aaa, :bbb, :ccc], :name => :con3).must_be_nil
155
- add_primary_key(:id).must_be_nil
156
- add_foreign_key(:node_id, :nodes).must_be_nil
157
- add_primary_key([:aaa, :bbb]).must_be_nil
158
- add_foreign_key([:node_id, :prop_id], :nodes_props).must_be_nil
159
- add_foreign_key([:node_id, :prop_id], :nodes_props, :name => :fkey).must_be_nil
160
- drop_foreign_key(:node_id).must_be_nil
161
- drop_foreign_key([:node_id, :prop_id]).must_be_nil
162
- drop_foreign_key([:node_id, :prop_id], :name => :fkey).must_be_nil
163
- end
164
- end
165
-
166
- it "should generate operation records" do
167
- @generator.operations.must_equal [
168
- {:op => :add_column, :name => :aaa, :type => :text},
169
- {:op => :drop_column, :name => :bbb},
170
- {:op => :rename_column, :name => :ccc, :new_name => :ho},
171
- {:op => :set_column_type, :name => :ddd, :type => :float},
172
- {:op => :set_column_default, :name => :eee, :default => 1},
173
- {:op => :add_index, :columns => [:fff, :ggg]},
174
- {:op => :drop_index, :columns => [:hhh]},
175
- {:op => :drop_index, :columns => [:hhh], :name=>:blah_blah},
176
- {:op => :add_index, :columns => [:blah], :type => :full_text},
177
- {:op => :add_index, :columns => [:geom], :type => :spatial},
178
- {:op => :add_index, :columns => [:blah], :type => :hash},
179
- {:op => :add_index, :columns => [:blah], :where => {:something => true}},
180
- {:op => :add_constraint, :type => :check, :name => :con1, :check => ['fred > 100']},
181
- {:op => :drop_constraint, :name => :con2},
182
- {:op => :add_constraint, :type => :unique, :name => :con3, :columns => [:aaa, :bbb, :ccc]},
183
- {:op => :add_column, :name => :id, :type => Integer, :primary_key=>true, :auto_increment=>true},
184
- {:op => :add_column, :name => :node_id, :type => Integer, :table=>:nodes},
185
- {:op => :add_constraint, :type => :primary_key, :columns => [:aaa, :bbb]},
186
- {:op => :add_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :table => :nodes_props},
187
- {:op => :add_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :table => :nodes_props, :name => :fkey},
188
- {:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id]},
189
- {:op => :drop_column, :name => :node_id},
190
- {:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id]},
191
- {:op => :drop_constraint, :type => :foreign_key, :columns => [:node_id, :prop_id], :name => :fkey},
192
- ]
193
- end
194
- end
195
-
196
- describe "Sequel::Schema::CreateTableGenerator generic type methods" do
197
- it "should store the type class in :type for each column" do
198
- Sequel::Schema::CreateTableGenerator.new(Sequel.mock) do
199
- String :a
200
- Integer :b
201
- Fixnum :c
202
- Bignum :d
203
- Float :e
204
- BigDecimal :f
205
- Date :g
206
- DateTime :h
207
- Time :i
208
- Numeric :j
209
- File :k
210
- TrueClass :l
211
- FalseClass :m
212
- end.columns.map{|c| c[:type]}.must_equal [String, Integer, Integer, :Bignum, Float, BigDecimal, Date, DateTime, Time, Numeric, File, TrueClass, FalseClass]
213
- end
214
- end
@@ -1,1820 +0,0 @@
1
- require_relative "spec_helper"
2
-
3
- describe "DB#create_table" do
4
- before do
5
- @db = Sequel.mock
6
- end
7
-
8
- it "should accept the table name" do
9
- @db.create_table(:cats){}.must_be_nil
10
- @db.sqls.must_equal ['CREATE TABLE cats ()']
11
- end
12
-
13
- with_symbol_splitting "should accept the table name with splittable symbols" do
14
- @db.create_table(:cats__cats) {}
15
- @db.sqls.must_equal ['CREATE TABLE cats.cats ()']
16
- end
17
-
18
- it "should accept the table name in multiple formats" do
19
- @db.create_table(Sequel[:cats][:cats]) {}
20
- @db.create_table("cats__cats1") {}
21
- @db.create_table(Sequel.identifier(:cats__cats2)) {}
22
- @db.create_table(Sequel.qualify(:cats3, :cats)) {}
23
- @db.sqls.must_equal ['CREATE TABLE cats.cats ()', 'CREATE TABLE cats__cats1 ()', 'CREATE TABLE cats__cats2 ()', 'CREATE TABLE cats3.cats ()']
24
- end
25
-
26
- it "should raise an error if the table name argument is not valid" do
27
- proc{@db.create_table(1) {}}.must_raise(Sequel::Error)
28
- proc{@db.create_table(Sequel.as(:cats, :c)) {}}.must_raise(Sequel::Error)
29
- end
30
-
31
- it "should remove cached schema entry" do
32
- @db.instance_variable_set(:@schemas, {'cats'=>[]})
33
- @db.create_table(:cats){Integer :a}
34
- @db.instance_variable_get(:@schemas).must_be :empty?
35
- end
36
-
37
- it "should accept multiple columns" do
38
- @db.create_table(:cats) do
39
- column :id, :integer
40
- column :name, :text
41
- end
42
- @db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
43
- end
44
-
45
- it "should accept method calls as data types" do
46
- @db.create_table(:cats) do
47
- integer :id
48
- text :name
49
- end
50
- @db.sqls.must_equal ['CREATE TABLE cats (id integer, name text)']
51
- end
52
-
53
- it "should transform types given as ruby classes to database-specific types" do
54
- @db.create_table(:cats) do
55
- String :a
56
- Integer :b
57
- Fixnum :c
58
- Bignum :d
59
- Float :e
60
- BigDecimal :f
61
- Date :g
62
- DateTime :h
63
- Time :i
64
- Numeric :j
65
- File :k
66
- TrueClass :l
67
- FalseClass :m
68
- column :n, Integer
69
- primary_key :o, :type=>String
70
- foreign_key :p, :f, :type=>Date
71
- end
72
- @db.sqls.must_equal ['CREATE TABLE cats (o varchar(255) PRIMARY KEY AUTOINCREMENT, a varchar(255), b integer, c integer, d bigint, e double precision, f numeric, g date, h timestamp, i timestamp, j numeric, k blob, l boolean, m boolean, n integer, p date REFERENCES f)']
73
- end
74
-
75
- it "should transform types given as ruby classes to database-specific types" do
76
- @db.default_string_column_size = 50
77
- @db.create_table(:cats) do
78
- String :a
79
- String :a2, :size=>13
80
- String :a3, :fixed=>true
81
- String :a4, :size=>13, :fixed=>true
82
- String :a5, :text=>true
83
- varchar :a6
84
- varchar :a7, :size=>13
85
- end
86
- @db.sqls.must_equal ['CREATE TABLE cats (a varchar(50), a2 varchar(13), a3 char(50), a4 char(13), a5 text, a6 varchar(50), a7 varchar(13))']
87
- end
88
-
89
- it "should allow the use of modifiers with ruby class types" do
90
- @db.create_table(:cats) do
91
- String :a, :size=>50
92
- String :b, :text=>true
93
- String :c, :fixed=>true, :size=>40
94
- Time :d, :only_time=>true
95
- BigDecimal :e, :size=>[11,2]
96
- end
97
- @db.sqls.must_equal ['CREATE TABLE cats (a varchar(50), b text, c char(40), d time, e numeric(11, 2))']
98
- end
99
-
100
- it "should allow the use of modifiers with ruby class types" do
101
- c = Class.new
102
- def c.name; 'Fixnum'; end
103
- @db.create_table(:cats) do
104
- column :a, c
105
- end
106
- @db.sqls.must_equal ['CREATE TABLE cats (a integer)']
107
- end
108
-
109
- it "should raise an error if you use a ruby class that isn't handled" do
110
- proc{@db.create_table(:cats){column :a, Class}}.must_raise(Sequel::Error)
111
- end
112
-
113
- it "should accept primary key definition" do
114
- @db.create_table(:cats) do
115
- primary_key :id
116
- end
117
- @db.sqls.must_equal ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT)']
118
-
119
- @db.create_table(:cats) do
120
- primary_key :id, :serial, :auto_increment => false
121
- end
122
- @db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
123
-
124
- @db.create_table(:cats) do
125
- primary_key :id, :type => :serial, :auto_increment => false
126
- end
127
- @db.sqls.must_equal ['CREATE TABLE cats (id serial PRIMARY KEY)']
128
-
129
- @db.create_table(:cats) do
130
- Integer :a
131
- primary_key :id
132
- end
133
- @db.sqls.must_equal ['CREATE TABLE cats (id integer PRIMARY KEY AUTOINCREMENT, a integer)']
134
-
135
- @db.create_table(:cats) do
136
- Integer :a
137
- primary_key :id, :keep_order=>true
138
- end
139
- @db.sqls.must_equal ['CREATE TABLE cats (a integer, id integer PRIMARY KEY AUTOINCREMENT)']
140
- end
141
-
142
- it "should allow naming primary key constraint with :primary_key_constraint_name option" do
143
- @db.create_table(:cats) do
144
- primary_key :id, :primary_key_constraint_name=>:foo
145
- end
146
- @db.sqls.must_equal ['CREATE TABLE cats (id integer CONSTRAINT foo PRIMARY KEY AUTOINCREMENT)']
147
- end
148
-
149
- it "should automatically set primary key column NOT NULL if database doesn't do it automatically" do
150
- def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
151
- @db.create_table(:cats) do
152
- primary_key :id
153
- end
154
- @db.sqls.must_equal ['CREATE TABLE cats (id integer NOT NULL PRIMARY KEY AUTOINCREMENT)']
155
- end
156
-
157
- it "should automatically set primary key column NOT NULL when adding constraint if database doesn't do it automatically" do
158
- def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
159
- @db.create_table(:cats) do
160
- String :id
161
- primary_key [:id]
162
- end
163
- @db.sqls.must_equal ['CREATE TABLE cats (id varchar(255) NOT NULL, PRIMARY KEY (id))']
164
- end
165
-
166
- it "should handling splitting named column constraints into table constraints if unsupported" do
167
- def @db.supports_named_column_constraints?; false end
168
- @db.create_table(:cats) do
169
- primary_key :id, :primary_key_constraint_name=>:foo
170
- foreign_key :cat_id, :cats, :unique=>true, :unique_constraint_name=>:bar, :foreign_key_constraint_name=>:baz, :deferrable=>true, :key=>:foo_id, :on_delete=>:cascade, :on_update=>:restrict
171
- end
172
- @db.sqls.must_equal ['CREATE TABLE cats (id integer AUTOINCREMENT, cat_id integer, CONSTRAINT foo PRIMARY KEY (id), CONSTRAINT baz FOREIGN KEY (cat_id) REFERENCES cats(foo_id) ON DELETE CASCADE ON UPDATE RESTRICT DEFERRABLE INITIALLY DEFERRED, CONSTRAINT bar UNIQUE (cat_id))']
173
- end
174
-
175
- it "should accept and literalize default values" do
176
- @db.create_table(:cats) do
177
- integer :id, :default => 123
178
- text :name, :default => "abc'def"
179
- end
180
- @db.sqls.must_equal ["CREATE TABLE cats (id integer DEFAULT 123, name text DEFAULT 'abc''def')"]
181
- end
182
-
183
- it "should accept not null definition" do
184
- @db.create_table(:cats) do
185
- integer :id
186
- text :name, :null => false
187
- text :name2, :allow_null => false
188
- end
189
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NOT NULL, name2 text NOT NULL)"]
190
- end
191
-
192
- it "should accept null definition" do
193
- @db.create_table(:cats) do
194
- integer :id
195
- text :name, :null => true
196
- text :name2, :allow_null => true
197
- end
198
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text NULL, name2 text NULL)"]
199
- end
200
-
201
- it "should accept unique definition" do
202
- @db.create_table(:cats) do
203
- integer :id
204
- text :name, :unique => true
205
- end
206
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text UNIQUE)"]
207
- end
208
-
209
- it "should allow naming unique constraint with :unique_constraint_name option" do
210
- @db.create_table(:cats) do
211
- text :name, :unique => true, :unique_constraint_name=>:foo
212
- end
213
- @db.sqls.must_equal ["CREATE TABLE cats (name text CONSTRAINT foo UNIQUE)"]
214
- end
215
-
216
- it "should handle not deferred unique constraints" do
217
- @db.create_table(:cats) do
218
- integer :id
219
- text :name
220
- unique :name, :deferrable=>false
221
- end
222
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) NOT DEFERRABLE)"]
223
- end
224
-
225
- it "should handle deferred unique constraints" do
226
- @db.create_table(:cats) do
227
- integer :id
228
- text :name
229
- unique :name, :deferrable=>true
230
- end
231
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY DEFERRED)"]
232
- end
233
-
234
- it "should handle deferred initially immediate unique constraints" do
235
- @db.create_table(:cats) do
236
- integer :id
237
- text :name
238
- unique :name, :deferrable=>:immediate
239
- end
240
- @db.sqls.must_equal ["CREATE TABLE cats (id integer, name text, UNIQUE (name) DEFERRABLE INITIALLY IMMEDIATE)"]
241
- end
242
-
243
- it "should accept unsigned definition" do
244
- @db.create_table(:cats) do
245
- integer :value, :unsigned => true
246
- end
247
- @db.sqls.must_equal ["CREATE TABLE cats (value integer UNSIGNED)"]
248
- end
249
-
250
- it "should accept [SET|ENUM](...) types" do
251
- @db.create_table(:cats) do
252
- set :color, :elements => ['black', 'tricolor', 'grey']
253
- end
254
- @db.sqls.must_equal ["CREATE TABLE cats (color set('black', 'tricolor', 'grey'))"]
255
- end
256
-
257
- it "should accept varchar size" do
258
- @db.create_table(:cats) do
259
- varchar :name
260
- end
261
- @db.sqls.must_equal ["CREATE TABLE cats (name varchar(255))"]
262
- @db.create_table(:cats) do
263
- varchar :name, :size => 51
264
- end
265
- @db.sqls.must_equal ["CREATE TABLE cats (name varchar(51))"]
266
- end
267
-
268
- it "should use double precision for double type" do
269
- @db.create_table(:cats) do
270
- double :name
271
- end
272
- @db.sqls.must_equal ["CREATE TABLE cats (name double precision)"]
273
- end
274
-
275
- it "should accept foreign keys without options" do
276
- @db.create_table(:cats) do
277
- foreign_key :project_id
278
- end
279
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer)"]
280
- end
281
-
282
- it "should accept foreign keys with options" do
283
- @db.create_table(:cats) do
284
- foreign_key :project_id, :table => :projects
285
- end
286
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects)"]
287
- end
288
-
289
- it "should accept foreign keys with separate table argument" do
290
- @db.create_table(:cats) do
291
- foreign_key :project_id, :projects, :default=>3
292
- end
293
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer DEFAULT 3 REFERENCES projects)"]
294
- end
295
-
296
- it "should allowing naming foreign key constraint with :foreign_key_constraint_name option" do
297
- @db.create_table(:cats) do
298
- foreign_key :project_id, :projects, :foreign_key_constraint_name=>:foo
299
- end
300
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer CONSTRAINT foo REFERENCES projects)"]
301
- end
302
-
303
- it "should raise an error if the table argument to foreign_key isn't a hash, symbol, or nil" do
304
- proc{@db.create_table(:cats){foreign_key :project_id, Object.new, :default=>3}}.must_raise(Sequel::Error)
305
- end
306
-
307
- it "should accept foreign keys with arbitrary keys" do
308
- @db.create_table(:cats) do
309
- foreign_key :project_id, :table => :projects, :key => :id
310
- end
311
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(id))"]
312
-
313
- @db.create_table(:cats) do
314
- foreign_key :project_id, :table => :projects, :key => :zzz
315
- end
316
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects(zzz))"]
317
- end
318
-
319
- it "should accept foreign keys with ON DELETE clause" do
320
- @db.create_table(:cats) do
321
- foreign_key :project_id, :table => :projects, :on_delete => :restrict
322
- end
323
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE RESTRICT)"]
324
-
325
- @db.create_table(:cats) do
326
- foreign_key :project_id, :table => :projects, :on_delete => :cascade
327
- end
328
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)"]
329
-
330
- @db.create_table(:cats) do
331
- foreign_key :project_id, :table => :projects, :on_delete => :no_action
332
- end
333
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION)"]
334
-
335
- @db.create_table(:cats) do
336
- foreign_key :project_id, :table => :projects, :on_delete => :set_null
337
- end
338
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET NULL)"]
339
-
340
- @db.create_table(:cats) do
341
- foreign_key :project_id, :table => :projects, :on_delete => :set_default
342
- end
343
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE SET DEFAULT)"]
344
-
345
- @db.create_table(:cats) do
346
- foreign_key :project_id, :table => :projects, :on_delete => 'NO ACTION FOO'
347
- end
348
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE NO ACTION FOO)"]
349
- end
350
-
351
- it "should accept foreign keys with ON UPDATE clause" do
352
- @db.create_table(:cats) do
353
- foreign_key :project_id, :table => :projects, :on_update => :restrict
354
- end
355
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE RESTRICT)"]
356
-
357
- @db.create_table(:cats) do
358
- foreign_key :project_id, :table => :projects, :on_update => :cascade
359
- end
360
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE CASCADE)"]
361
-
362
- @db.create_table(:cats) do
363
- foreign_key :project_id, :table => :projects, :on_update => :no_action
364
- end
365
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE NO ACTION)"]
366
-
367
- @db.create_table(:cats) do
368
- foreign_key :project_id, :table => :projects, :on_update => :set_null
369
- end
370
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET NULL)"]
371
-
372
- @db.create_table(:cats) do
373
- foreign_key :project_id, :table => :projects, :on_update => :set_default
374
- end
375
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT)"]
376
-
377
- @db.create_table(:cats) do
378
- foreign_key :project_id, :table => :projects, :on_update => 'SET DEFAULT FOO'
379
- end
380
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON UPDATE SET DEFAULT FOO)"]
381
- end
382
-
383
- it "should accept foreign keys with deferrable option" do
384
- @db.create_table(:cats) do
385
- foreign_key :project_id, :projects, :deferrable=>true
386
- end
387
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects DEFERRABLE INITIALLY DEFERRED)"]
388
- end
389
-
390
- it "should accept collation" do
391
- @db.create_table(:cats) do
392
- String :name, :collate => :utf8_bin
393
- end
394
- @db.sqls.must_equal ['CREATE TABLE cats (name varchar(255) COLLATE utf8_bin)']
395
- end
396
-
397
- it "should accept collation as a String, treated literally" do
398
- @db.create_table(:cats) do
399
- String :name, :collate => '"utf8_bin"'
400
- end
401
- @db.sqls.must_equal ['CREATE TABLE cats (name varchar(255) COLLATE "utf8_bin")']
402
- end
403
-
404
- it "should accept inline index definition" do
405
- @db.create_table(:cats) do
406
- integer :id, :index => true
407
- end
408
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
409
- end
410
-
411
- it "should accept inline index definition with a hash of options" do
412
- @db.create_table(:cats) do
413
- integer :id, :index => {:unique=>true}
414
- end
415
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
416
- end
417
-
418
- it "should accept inline index definition for foreign keys" do
419
- @db.create_table(:cats) do
420
- foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => true
421
- end
422
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
423
- "CREATE INDEX cats_project_id_index ON cats (project_id)"]
424
- end
425
-
426
- it "should accept inline index definition for foreign keys with a hash of options" do
427
- @db.create_table(:cats) do
428
- foreign_key :project_id, :table => :projects, :on_delete => :cascade, :index => {:unique=>true}
429
- end
430
- @db.sqls.must_equal ["CREATE TABLE cats (project_id integer REFERENCES projects ON DELETE CASCADE)",
431
- "CREATE UNIQUE INDEX cats_project_id_index ON cats (project_id)"]
432
- end
433
-
434
- it "should accept index definitions" do
435
- @db.create_table(:cats) do
436
- integer :id
437
- index :id
438
- end
439
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)"]
440
- end
441
-
442
- it "should accept unique constraint definitions" do
443
- @db.create_table(:cats) do
444
- text :name
445
- unique :name
446
- end
447
- @db.sqls.must_equal ["CREATE TABLE cats (name text, UNIQUE (name))"]
448
- end
449
-
450
- it "should accept partial index definitions" do
451
- def @db.supports_partial_indexes?() true end
452
- @db.create_table(:cats) do
453
- integer :id
454
- index :id, :where=>proc{id > 1}
455
- end
456
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id) WHERE (id > 1)"]
457
- end
458
-
459
- it "should raise an error if partial indexes are not supported" do
460
- proc do
461
- @db.create_table(:cats) do
462
- integer :id
463
- index :id, :where=>proc{id > 1}
464
- end
465
- end.must_raise(Sequel::Error)
466
- end
467
-
468
- it "should not raise on index error for unsupported index definitions if ignore_index_errors is used" do
469
- @db.create_table(:cats, :ignore_index_errors=>true) do
470
- text :name
471
- full_text_index :name
472
- end
473
- end
474
-
475
- it "should raise on full-text index definitions" do
476
- proc {
477
- @db.create_table(:cats) do
478
- text :name
479
- full_text_index :name
480
- end
481
- }.must_raise(Sequel::Error)
482
- end
483
-
484
- it "should raise on spatial index definitions" do
485
- proc {
486
- @db.create_table(:cats) do
487
- point :geom
488
- spatial_index :geom
489
- end
490
- }.must_raise(Sequel::Error)
491
- end
492
-
493
- it "should raise on partial index definitions" do
494
- proc {
495
- @db.create_table(:cats) do
496
- text :name
497
- index :name, :where => {:something => true}
498
- end
499
- }.must_raise(Sequel::Error)
500
- end
501
-
502
- it "should raise index definitions with type" do
503
- proc {
504
- @db.create_table(:cats) do
505
- text :name
506
- index :name, :type => :hash
507
- end
508
- }.must_raise(Sequel::Error)
509
- end
510
-
511
- it "should ignore errors if the database raises an error on an index creation statement and the :ignore_index_errors option is used" do
512
- @db.define_singleton_method(:execute_ddl){|*a| raise Sequel::DatabaseError if /blah/.match(a.first); super(*a)}
513
- lambda{@db.create_table(:cats){Integer :id; index :blah; index :id}}.must_raise(Sequel::DatabaseError)
514
- @db.sqls.must_equal ['CREATE TABLE cats (id integer)']
515
- @db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}
516
- @db.sqls.must_equal ['CREATE TABLE cats (id integer)', 'CREATE INDEX cats_id_index ON cats (id)']
517
- end
518
-
519
- it "should not use savepoints around index creation if running inside a transaction if :ignore_index_errors option is used" do
520
- @db.define_singleton_method(:execute_ddl){|*a| super(*a); raise Sequel::DatabaseError if /blah/.match(a.first)}
521
- @db.transaction{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}
522
- @db.sqls.must_equal ["BEGIN", "CREATE TABLE cats (id integer)", "CREATE INDEX cats_blah_index ON cats (blah)", "CREATE INDEX cats_id_index ON cats (id)", "COMMIT"]
523
- end
524
-
525
- it "should use savepoints around index creation if running inside a transaction if :ignore_index_errors option is used and transactional schema modifications are supported" do
526
- @db.define_singleton_method(:supports_transactional_ddl?){true}
527
- @db.define_singleton_method(:execute_ddl){|*a| super(*a); raise Sequel::DatabaseError if /blah/.match(a.first)}
528
- @db.transaction{@db.create_table(:cats, :ignore_index_errors=>true){Integer :id; index :blah; index :id}}
529
- @db.sqls.must_equal ["BEGIN", "CREATE TABLE cats (id integer)", "SAVEPOINT autopoint_1", "CREATE INDEX cats_blah_index ON cats (blah)", "ROLLBACK TO SAVEPOINT autopoint_1", "SAVEPOINT autopoint_1", "CREATE INDEX cats_id_index ON cats (id)", "RELEASE SAVEPOINT autopoint_1", "COMMIT"]
530
- end
531
-
532
- it "should accept multiple index definitions" do
533
- @db.create_table(:cats) do
534
- integer :id
535
- index :id
536
- index :name
537
- end
538
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_id_index ON cats (id)", "CREATE INDEX cats_name_index ON cats (name)"]
539
- end
540
-
541
- it "should accept functional indexes" do
542
- @db.create_table(:cats) do
543
- integer :id
544
- index Sequel.function(:lower, :name)
545
- end
546
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower_name__index ON cats (lower(name))"]
547
- end
548
-
549
- it "should accept indexes with identifiers" do
550
- @db.create_table(:cats) do
551
- integer :id
552
- index Sequel.identifier(:lower__name)
553
- end
554
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX cats_lower__name_index ON cats (lower__name)"]
555
- end
556
-
557
- it "should accept custom index names" do
558
- @db.create_table(:cats) do
559
- integer :id
560
- index :id, :name => 'abc'
561
- end
562
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE INDEX abc ON cats (id)"]
563
- end
564
-
565
- it "should accept unique index definitions" do
566
- @db.create_table(:cats) do
567
- integer :id
568
- index :id, :unique => true
569
- end
570
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_index ON cats (id)"]
571
- end
572
-
573
- it "should accept composite index definitions" do
574
- @db.create_table(:cats) do
575
- integer :id
576
- index [:id, :name], :unique => true
577
- end
578
- @db.sqls.must_equal ["CREATE TABLE cats (id integer)", "CREATE UNIQUE INDEX cats_id_name_index ON cats (id, name)"]
579
- end
580
-
581
- it "should accept unnamed constraint definitions with blocks" do
582
- @db.create_table(:cats) do
583
- integer :score
584
- check{(x > 0) & (y < 1)}
585
- end
586
- @db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK ((x > 0) AND (y < 1)))"]
587
- end
588
-
589
- it "should accept unnamed constraint definitions with function calls" do
590
- @db.create_table(:cats) do
591
- integer :score
592
- check{f(x)}
593
- end
594
- @db.sqls.must_equal ["CREATE TABLE cats (score integer, CHECK (f(x)))"]
595
- end
596
-
597
- it "should accept unnamed constraint definitions" do
598
- @db.create_table(:cats) do
599
- check 'price < ?', 100
600
- end
601
- @db.sqls.must_equal ["CREATE TABLE cats (CHECK (price < 100))"]
602
- end
603
-
604
- it "should accept arrays of pairs constraints" do
605
- @db.create_table(:cats) do
606
- check [[:price, 100]]
607
- end
608
- @db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
609
- end
610
-
611
- it "should accept hash constraints" do
612
- @db.create_table(:cats) do
613
- check :price=>100
614
- end
615
- @db.sqls.must_equal ["CREATE TABLE cats (CHECK (price = 100))"]
616
- end
617
-
618
- it "should accept array constraints" do
619
- @db.create_table(:cats) do
620
- check [Sequel.expr(:x) > 0, Sequel.expr(:y) < 1]
621
- end
622
- @db.sqls.must_equal ["CREATE TABLE cats (CHECK ((x > 0) AND (y < 1)))"]
623
- end
624
-
625
- it "should accept expression constraints" do
626
- @db.create_table(:cats) do
627
- check Sequel.&(Sequel.expr(:x) > 0, Sequel.expr(:y) < 1)
628
- end
629
- @db.sqls.must_equal ["CREATE TABLE cats (CHECK ((x > 0) AND (y < 1)))"]
630
- end
631
-
632
- it "should accept named constraint definitions" do
633
- @db.create_table(:cats) do
634
- integer :score
635
- constraint :valid_score, 'score <= 100'
636
- end
637
- @db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100))"]
638
- end
639
-
640
- it "should accept named constraint definitions with options" do
641
- @db.create_table(:cats) do
642
- integer :score
643
- constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
644
- end
645
- @db.sqls.must_equal ["CREATE TABLE cats (score integer, CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED)"]
646
- end
647
-
648
- it "should accept named constraint definitions with block" do
649
- @db.create_table(:cats) do
650
- constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
651
- end
652
- @db.sqls.must_equal ["CREATE TABLE cats (CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1)))"]
653
- end
654
-
655
- it "should raise an error if an invalid constraint type is used" do
656
- proc{@db.create_table(:cats){unique [:a, :b], :type=>:bb}}.must_raise(Sequel::Error)
657
- end
658
-
659
- it "should accept composite primary keys" do
660
- @db.create_table(:cats) do
661
- integer :a
662
- integer :b
663
- primary_key [:a, :b]
664
- end
665
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, PRIMARY KEY (a, b))"]
666
- end
667
-
668
- it "should accept named composite primary keys" do
669
- @db.create_table(:cats) do
670
- integer :a
671
- integer :b
672
- primary_key [:a, :b], :name => :cpk
673
- end
674
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cpk PRIMARY KEY (a, b))"]
675
- end
676
-
677
- it "should accept composite foreign keys" do
678
- @db.create_table(:cats) do
679
- integer :a
680
- integer :b
681
- foreign_key [:a, :b], :abc
682
- end
683
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc)"]
684
- end
685
-
686
- it "should accept named composite foreign keys" do
687
- @db.create_table(:cats) do
688
- integer :a
689
- integer :b
690
- foreign_key [:a, :b], :abc, :name => :cfk
691
- end
692
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, CONSTRAINT cfk FOREIGN KEY (a, b) REFERENCES abc)"]
693
- end
694
-
695
- it "should accept composite foreign keys with arbitrary keys" do
696
- @db.create_table(:cats) do
697
- integer :a
698
- integer :b
699
- foreign_key [:a, :b], :abc, :key => [:real_a, :real_b]
700
- end
701
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(real_a, real_b))"]
702
-
703
- @db.create_table(:cats) do
704
- integer :a
705
- integer :b
706
- foreign_key [:a, :b], :abc, :key => [:z, :x]
707
- end
708
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(z, x))"]
709
- end
710
-
711
- it "should accept composite foreign keys with on delete and on update clauses" do
712
- @db.create_table(:cats) do
713
- integer :a
714
- integer :b
715
- foreign_key [:a, :b], :abc, :on_delete => :cascade
716
- end
717
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE CASCADE)"]
718
-
719
- @db.create_table(:cats) do
720
- integer :a
721
- integer :b
722
- foreign_key [:a, :b], :abc, :on_update => :no_action
723
- end
724
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON UPDATE NO ACTION)"]
725
-
726
- @db.create_table(:cats) do
727
- integer :a
728
- integer :b
729
- foreign_key [:a, :b], :abc, :on_delete => :restrict, :on_update => :set_default
730
- end
731
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc ON DELETE RESTRICT ON UPDATE SET DEFAULT)"]
732
-
733
- @db.create_table(:cats) do
734
- integer :a
735
- integer :b
736
- foreign_key [:a, :b], :abc, :key => [:x, :y], :on_delete => :set_null, :on_update => :set_null
737
- end
738
- @db.sqls.must_equal ["CREATE TABLE cats (a integer, b integer, FOREIGN KEY (a, b) REFERENCES abc(x, y) ON DELETE SET NULL ON UPDATE SET NULL)"]
739
- end
740
-
741
- it "should accept an :as option to create a table from the results of a dataset" do
742
- @db.create_table(:cats, :as=>@db[:a])
743
- @db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
744
- end
745
-
746
- it "should accept an :as option to create a table from a SELECT string" do
747
- @db.create_table(:cats, :as=>'SELECT * FROM a')
748
- @db.sqls.must_equal ['CREATE TABLE cats AS SELECT * FROM a']
749
- end
750
-
751
- it "should raise an Error if both a block and an :as argument are given" do
752
- proc{@db.create_table(:cats, :as=>@db[:a]){}}.must_raise(Sequel::Error)
753
- end
754
- end
755
-
756
- describe "DB#create_table!" do
757
- before do
758
- @db = Sequel.mock
759
- end
760
-
761
- it "should create the table if it does not exist" do
762
- @db.define_singleton_method(:table_exists?){|a| false}
763
- @db.create_table!(:cats){|*a|}.must_be_nil
764
- @db.sqls.must_equal ['CREATE TABLE cats ()']
765
- end
766
-
767
- it "should drop the table before creating it if it already exists" do
768
- @db.define_singleton_method(:table_exists?){|a| true}
769
- @db.create_table!(:cats){|*a|}
770
- @db.sqls.must_equal ['DROP TABLE cats', 'CREATE TABLE cats ()']
771
- end
772
-
773
- it "should use IF EXISTS if the database supports it" do
774
- @db.define_singleton_method(:supports_drop_table_if_exists?){true}
775
- @db.create_table!(:cats){|*a|}
776
- @db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'CREATE TABLE cats ()']
777
- end
778
- end
779
-
780
- describe "DB#create_table?" do
781
- before do
782
- @db = Sequel.mock
783
- end
784
-
785
- it "should not create the table if the table already exists" do
786
- @db.define_singleton_method(:table_exists?){|a| true}
787
- @db.create_table?(:cats){|*a|}.must_be_nil
788
- @db.sqls.must_equal []
789
- end
790
-
791
- it "should create the table if the table doesn't already exist" do
792
- @db.define_singleton_method(:table_exists?){|a| false}
793
- @db.create_table?(:cats){|*a|}
794
- @db.sqls.must_equal ['CREATE TABLE cats ()']
795
- end
796
-
797
- it "should use IF NOT EXISTS if the database supports that" do
798
- @db.define_singleton_method(:supports_create_table_if_not_exists?){true}
799
- @db.create_table?(:cats){|*a|}
800
- @db.sqls.must_equal ['CREATE TABLE IF NOT EXISTS cats ()']
801
- end
802
-
803
- it "should not use IF NOT EXISTS if the indexes are created" do
804
- @db.define_singleton_method(:table_exists?){|a| false}
805
- @db.define_singleton_method(:supports_create_table_if_not_exists?){true}
806
- @db.create_table?(:cats){|*a| Integer :a, :index=>true}
807
- @db.sqls.must_equal ['CREATE TABLE cats (a integer)', 'CREATE INDEX cats_a_index ON cats (a)']
808
-
809
- @db.define_singleton_method(:table_exists?){|a| true}
810
- @db.create_table?(:cats){|*a| Integer :a, :index=>true}
811
- @db.sqls.must_equal []
812
- end
813
- end
814
-
815
- describe "DB#create_join_table" do
816
- before do
817
- @db = Sequel.mock
818
- end
819
-
820
- it "should take a hash with foreign keys and table name values" do
821
- @db.create_join_table(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
822
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
823
- end
824
-
825
- it "should be able to have values be a hash of options" do
826
- @db.create_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
827
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NULL REFERENCES cats, dog_id integer DEFAULT 0 NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
828
- end
829
-
830
- it "should be able to pass a second hash of table options" do
831
- @db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :temp=>true)
832
- @db.sqls.must_equal ['CREATE TEMPORARY TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
833
- end
834
-
835
- it "should recognize :name option in table options" do
836
- @db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
837
- @db.sqls.must_equal ['CREATE TABLE f (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX f_dog_id_cat_id_index ON f (dog_id, cat_id)']
838
- end
839
-
840
- it "should recognize :index_options option in table options" do
841
- @db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :index_options=>{:name=>:foo_index})
842
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX foo_index ON cats_dogs (dog_id, cat_id)']
843
- end
844
-
845
- it "should recognize :no_index option in table options" do
846
- @db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
847
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))']
848
- end
849
-
850
- it "should recognize :no_primary_key option in table options" do
851
- @db.create_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :no_primary_key=>true)
852
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs)', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
853
- end
854
-
855
- it "should raise an error if the hash doesn't have 2 entries with table names" do
856
- proc{@db.create_join_table({})}.must_raise(Sequel::Error)
857
- proc{@db.create_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
858
- proc{@db.create_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
859
- proc{@db.create_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
860
- end
861
- end
862
-
863
- describe "DB#create_join_table?" do
864
- before do
865
- @db = Sequel.mock
866
- end
867
-
868
- it "should create the table if it does not already exist" do
869
- @db.define_singleton_method(:table_exists?){|a| false}
870
- @db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
871
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
872
- end
873
-
874
- it "should not create the table if it already exists" do
875
- @db.define_singleton_method(:table_exists?){|a| true}
876
- @db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
877
- @db.sqls.must_equal []
878
- end
879
-
880
- it "should not use IF NOT EXISTS" do
881
- @db.define_singleton_method(:table_exists?){|a| false}
882
- @db.define_singleton_method(:supports_create_table_if_not_exists?){true}
883
- @db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
884
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
885
-
886
- @db.define_singleton_method(:table_exists?){|a| true}
887
- @db.create_join_table?(:cat_id=>:cats, :dog_id=>:dogs)
888
- @db.sqls.must_equal []
889
- end
890
-
891
- it "should not use IF NOT EXISTS if no_index is used" do
892
- @db.define_singleton_method(:supports_create_table_if_not_exists?){true}
893
- @db.create_join_table?({:cat_id=>:cats, :dog_id=>:dogs}, :no_index=>true)
894
- @db.sqls.must_equal ['CREATE TABLE IF NOT EXISTS cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))']
895
- end
896
- end
897
-
898
- describe "DB#create_join_table!" do
899
- before do
900
- @db = Sequel.mock
901
- end
902
-
903
- it "should drop the table first if it already exists" do
904
- @db.define_singleton_method(:table_exists?){|a| true}
905
- @db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
906
- @db.sqls.must_equal ['DROP TABLE cats_dogs', 'CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
907
- end
908
-
909
- it "should not drop the table if it doesn't exists" do
910
- @db.define_singleton_method(:table_exists?){|a| false}
911
- @db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
912
- @db.sqls.must_equal ['CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
913
- end
914
-
915
- it "should use IF EXISTS if the database supports it" do
916
- @db.define_singleton_method(:supports_drop_table_if_exists?){true}
917
- @db.create_join_table!(:cat_id=>:cats, :dog_id=>:dogs)
918
- @db.sqls.must_equal ['DROP TABLE IF EXISTS cats_dogs', 'CREATE TABLE cats_dogs (cat_id integer NOT NULL REFERENCES cats, dog_id integer NOT NULL REFERENCES dogs, PRIMARY KEY (cat_id, dog_id))', 'CREATE INDEX cats_dogs_dog_id_cat_id_index ON cats_dogs (dog_id, cat_id)']
919
- end
920
- end
921
-
922
- describe "DB#drop_join_table" do
923
- before do
924
- @db = Sequel.mock
925
- end
926
-
927
- it "should take a hash with foreign keys and table name values and drop the table" do
928
- @db.drop_join_table(:cat_id=>:cats, :dog_id=>:dogs).must_be_nil
929
- @db.sqls.must_equal ['DROP TABLE cats_dogs']
930
- end
931
-
932
- it "should be able to have values be a hash of options" do
933
- @db.drop_join_table(:cat_id=>{:table=>:cats, :null=>true}, :dog_id=>{:table=>:dogs, :default=>0})
934
- @db.sqls.must_equal ['DROP TABLE cats_dogs']
935
- end
936
-
937
- it "should respect a second hash of table options" do
938
- @db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :cascade=>true)
939
- @db.sqls.must_equal ['DROP TABLE cats_dogs CASCADE']
940
- end
941
-
942
- it "should respect :name option for table name" do
943
- @db.drop_join_table({:cat_id=>:cats, :dog_id=>:dogs}, :name=>:f)
944
- @db.sqls.must_equal ['DROP TABLE f']
945
- end
946
-
947
- it "should raise an error if the hash doesn't have 2 entries with table names" do
948
- proc{@db.drop_join_table({})}.must_raise(Sequel::Error)
949
- proc{@db.drop_join_table({:cat_id=>:cats})}.must_raise(Sequel::Error)
950
- proc{@db.drop_join_table({:cat_id=>:cats, :human_id=>:humans, :dog_id=>:dog})}.must_raise(Sequel::Error)
951
- proc{@db.drop_join_table({:cat_id=>:cats, :dog_id=>{}})}.must_raise(Sequel::Error)
952
- end
953
- end
954
-
955
- describe "DB#drop_table" do
956
- before do
957
- @db = Sequel.mock
958
- end
959
-
960
- it "should generate a DROP TABLE statement" do
961
- @db.drop_table(:cats).must_be_nil
962
- @db.sqls.must_equal ['DROP TABLE cats']
963
- end
964
-
965
- it "should drop multiple tables at once" do
966
- @db.drop_table :cats, :dogs
967
- @db.sqls.must_equal ['DROP TABLE cats', 'DROP TABLE dogs']
968
- end
969
-
970
- it "should take an options hash and support the :cascade option" do
971
- @db.drop_table :cats, :dogs, :cascade=>true
972
- @db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
973
- end
974
- end
975
-
976
- describe "DB#drop_table?" do
977
- before do
978
- @db = Sequel.mock
979
- end
980
-
981
- it "should drop the table if it exists" do
982
- @db.define_singleton_method(:table_exists?){|a| true}
983
- @db.drop_table?(:cats).must_be_nil
984
- @db.sqls.must_equal ["DROP TABLE cats"]
985
- end
986
-
987
- it "should do nothing if the table does not exist" do
988
- @db.define_singleton_method(:table_exists?){|a| false}
989
- @db.drop_table?(:cats)
990
- @db.sqls.must_equal []
991
- end
992
-
993
- it "should operate on multiple tables at once" do
994
- @db.define_singleton_method(:table_exists?){|a| a == :cats}
995
- @db.drop_table? :cats, :dogs
996
- @db.sqls.must_equal ['DROP TABLE cats']
997
- end
998
-
999
- it "should take an options hash and support the :cascade option" do
1000
- @db.define_singleton_method(:table_exists?){|a| true}
1001
- @db.drop_table? :cats, :dogs, :cascade=>true
1002
- @db.sqls.must_equal ['DROP TABLE cats CASCADE', 'DROP TABLE dogs CASCADE']
1003
- end
1004
-
1005
- it "should use IF NOT EXISTS if the database supports that" do
1006
- @db.define_singleton_method(:supports_drop_table_if_exists?){true}
1007
- @db.drop_table? :cats, :dogs
1008
- @db.sqls.must_equal ['DROP TABLE IF EXISTS cats', 'DROP TABLE IF EXISTS dogs']
1009
- end
1010
-
1011
- it "should use IF NOT EXISTS with CASCADE if the database supports that" do
1012
- @db.define_singleton_method(:supports_drop_table_if_exists?){true}
1013
- @db.drop_table? :cats, :dogs, :cascade=>true
1014
- @db.sqls.must_equal ['DROP TABLE IF EXISTS cats CASCADE', 'DROP TABLE IF EXISTS dogs CASCADE']
1015
- end
1016
- end
1017
-
1018
- describe "DB#alter_table" do
1019
- before do
1020
- @db = Sequel.mock
1021
- end
1022
-
1023
- it "should allow adding not null constraint via set_column_allow_null with false argument" do
1024
- @db.alter_table(:cats) do
1025
- set_column_allow_null :score, false
1026
- end.must_be_nil
1027
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
1028
- end
1029
-
1030
- it "should allow removing not null constraint via set_column_allow_null with true argument" do
1031
- @db.alter_table(:cats) do
1032
- set_column_allow_null :score, true
1033
- end
1034
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
1035
- end
1036
-
1037
- it "should allow adding not null constraint via set_column_not_null" do
1038
- @db.alter_table(:cats) do
1039
- set_column_not_null :score
1040
- end
1041
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET NOT NULL"]
1042
- end
1043
-
1044
- it "should allow removing not null constraint via set_column_allow_null without argument" do
1045
- @db.alter_table(:cats) do
1046
- set_column_allow_null :score
1047
- end
1048
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score DROP NOT NULL"]
1049
- end
1050
-
1051
- it "should support add_column" do
1052
- @db.alter_table(:cats) do
1053
- add_column :score, :integer
1054
- end
1055
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN score integer"]
1056
- end
1057
-
1058
- it "should support add_constraint" do
1059
- @db.alter_table(:cats) do
1060
- add_constraint :valid_score, 'score <= 100'
1061
- end
1062
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100)"]
1063
- end
1064
-
1065
- it "should support add_constraint with options" do
1066
- @db.alter_table(:cats) do
1067
- add_constraint({:name=>:valid_score, :deferrable=>true}, 'score <= 100')
1068
- end
1069
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT valid_score CHECK (score <= 100) DEFERRABLE INITIALLY DEFERRED"]
1070
- end
1071
-
1072
- it "should support add_constraint with block" do
1073
- @db.alter_table(:cats) do
1074
- add_constraint(:blah_blah){(x.sql_number > 0) & (y.sql_number < 1)}
1075
- end
1076
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT blah_blah CHECK ((x > 0) AND (y < 1))"]
1077
- end
1078
-
1079
- it "should support add_unique_constraint" do
1080
- @db.alter_table(:cats) do
1081
- add_unique_constraint [:a, :b]
1082
- end
1083
- @db.sqls.must_equal ["ALTER TABLE cats ADD UNIQUE (a, b)"]
1084
-
1085
- @db.alter_table(:cats) do
1086
- add_unique_constraint [:a, :b], :name => :ab_uniq
1087
- end
1088
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT ab_uniq UNIQUE (a, b)"]
1089
- end
1090
-
1091
- it "should support add_foreign_key" do
1092
- @db.alter_table(:cats) do
1093
- add_foreign_key :node_id, :nodes
1094
- end
1095
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN node_id integer REFERENCES nodes"]
1096
- end
1097
-
1098
- it "should support add_foreign_key with composite foreign keys" do
1099
- @db.alter_table(:cats) do
1100
- add_foreign_key [:node_id, :prop_id], :nodes_props
1101
- end
1102
- @db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
1103
-
1104
- @db.alter_table(:cats) do
1105
- add_foreign_key [:node_id, :prop_id], :nodes_props, :name => :cfk
1106
- end
1107
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cfk FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props"]
1108
-
1109
- @db.alter_table(:cats) do
1110
- add_foreign_key [:node_id, :prop_id], :nodes_props, :key => [:nid, :pid]
1111
- end
1112
- @db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props(nid, pid)"]
1113
-
1114
- @db.alter_table(:cats) do
1115
- add_foreign_key [:node_id, :prop_id], :nodes_props, :on_delete => :restrict, :on_update => :cascade
1116
- end
1117
- @db.sqls.must_equal ["ALTER TABLE cats ADD FOREIGN KEY (node_id, prop_id) REFERENCES nodes_props ON DELETE RESTRICT ON UPDATE CASCADE"]
1118
- end
1119
-
1120
- it "should support add_index" do
1121
- @db.alter_table(:cats) do
1122
- add_index :name
1123
- end
1124
- @db.sqls.must_equal ["CREATE INDEX cats_name_index ON cats (name)"]
1125
- end
1126
-
1127
- it "should ignore errors if the database raises an error on an add_index call and the :ignore_errors option is used" do
1128
- @db.define_singleton_method(:execute_ddl){|*a| raise Sequel::DatabaseError}
1129
- lambda{@db.add_index(:cats, :id)}.must_raise(Sequel::DatabaseError)
1130
- @db.add_index(:cats, :id, :ignore_errors=>true)
1131
- @db.sqls.must_equal []
1132
- end
1133
-
1134
- it "should support add_primary_key" do
1135
- @db.alter_table(:cats) do
1136
- add_primary_key :id
1137
- end
1138
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN id integer PRIMARY KEY AUTOINCREMENT"]
1139
- end
1140
-
1141
- it "should support add_primary_key with composite primary keys" do
1142
- @db.alter_table(:cats) do
1143
- add_primary_key [:id, :type]
1144
- end
1145
- @db.sqls.must_equal ["ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
1146
-
1147
- @db.alter_table(:cats) do
1148
- add_primary_key [:id, :type], :name => :cpk
1149
- end
1150
- @db.sqls.must_equal ["ALTER TABLE cats ADD CONSTRAINT cpk PRIMARY KEY (id, type)"]
1151
- end
1152
-
1153
- it "should set primary key column NOT NULL when using add_primary_key if database doesn't handle it" do
1154
- def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
1155
- @db.alter_table(:cats) do
1156
- add_primary_key :id
1157
- end
1158
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN id integer NOT NULL PRIMARY KEY AUTOINCREMENT"]
1159
- end
1160
-
1161
- it "should set primary key column NOT NULL when adding primary key constraint if database doesn't handle it" do
1162
- def @db.can_add_primary_key_constraint_on_nullable_columns?; false end
1163
- @db.alter_table(:cats) do
1164
- add_primary_key [:id, :type]
1165
- end
1166
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN id SET NOT NULL", "ALTER TABLE cats ALTER COLUMN type SET NOT NULL", "ALTER TABLE cats ADD PRIMARY KEY (id, type)"]
1167
- end
1168
-
1169
- it "should support drop_column" do
1170
- @db.alter_table(:cats) do
1171
- drop_column :score
1172
- end
1173
- @db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score"]
1174
- end
1175
-
1176
- it "should support drop_column with :cascade=>true option" do
1177
- @db.alter_table(:cats) do
1178
- drop_column :score, :cascade=>true
1179
- end
1180
- @db.sqls.must_equal ["ALTER TABLE cats DROP COLUMN score CASCADE"]
1181
- end
1182
-
1183
- it "should support drop_constraint" do
1184
- @db.alter_table(:cats) do
1185
- drop_constraint :valid_score
1186
- end
1187
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score"]
1188
- end
1189
-
1190
- it "should support drop_constraint with :cascade=>true option" do
1191
- @db.alter_table(:cats) do
1192
- drop_constraint :valid_score, :cascade=>true
1193
- end
1194
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT valid_score CASCADE"]
1195
- end
1196
-
1197
- it "should support drop_foreign_key" do
1198
- def @db.foreign_key_list(table_name)
1199
- [{:name=>:cats_node_id_fkey, :columns=>[:node_id]}]
1200
- end
1201
- @db.alter_table(:cats) do
1202
- drop_foreign_key :node_id
1203
- end
1204
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_fkey", "ALTER TABLE cats DROP COLUMN node_id"]
1205
- end
1206
-
1207
- it "should support drop_foreign_key with :foreign_key_constraint_name option" do
1208
- @db.alter_table(:cats) do
1209
- drop_foreign_key :node_id, :foreign_key_constraint_name=>:foo
1210
- end
1211
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT foo", "ALTER TABLE cats DROP COLUMN node_id"]
1212
- end
1213
-
1214
- it "should support drop_foreign_key with :name option" do
1215
- @db.alter_table(:cats) do
1216
- drop_foreign_key :node_id, :name=>:foo
1217
- end
1218
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT foo", "ALTER TABLE cats DROP COLUMN node_id"]
1219
- end
1220
-
1221
- it "should support drop_foreign_key with composite foreign keys" do
1222
- def @db.foreign_key_list(table_name)
1223
- [{:name=>:cats_node_id_prop_id_fkey, :columns=>[:node_id, :prop_id]}]
1224
- end
1225
- @db.alter_table(:cats) do
1226
- drop_foreign_key [:node_id, :prop_id]
1227
- end
1228
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cats_node_id_prop_id_fkey"]
1229
-
1230
- @db.alter_table(:cats) do
1231
- drop_foreign_key [:node_id, :prop_id], :name => :cfk
1232
- end
1233
- @db.sqls.must_equal ["ALTER TABLE cats DROP CONSTRAINT cfk"]
1234
- end
1235
-
1236
- it "should have drop_foreign_key raise Error if no name is found" do
1237
- def @db.foreign_key_list(table_name)
1238
- [{:name=>:cats_node_id_fkey, :columns=>[:foo_id]}]
1239
- end
1240
- lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
1241
- end
1242
-
1243
- it "should have drop_foreign_key raise Error if multiple foreign keys found" do
1244
- def @db.foreign_key_list(table_name)
1245
- [{:name=>:cats_node_id_fkey, :columns=>[:node_id]}, {:name=>:cats_node_id_fkey2, :columns=>[:node_id]}]
1246
- end
1247
- lambda{@db.alter_table(:cats){drop_foreign_key :node_id}}.must_raise(Sequel::Error)
1248
- end
1249
-
1250
- it "should support drop_index" do
1251
- @db.alter_table(:cats) do
1252
- drop_index :name
1253
- end
1254
- @db.sqls.must_equal ["DROP INDEX cats_name_index"]
1255
- end
1256
-
1257
- it "should support drop_index with a given name" do
1258
- @db.alter_table(:cats) do
1259
- drop_index :name, :name=>:blah_blah
1260
- end
1261
- @db.sqls.must_equal ["DROP INDEX blah_blah"]
1262
- end
1263
-
1264
- it "should support rename_column" do
1265
- @db.alter_table(:cats) do
1266
- rename_column :name, :old_name
1267
- end
1268
- @db.sqls.must_equal ["ALTER TABLE cats RENAME COLUMN name TO old_name"]
1269
- end
1270
-
1271
- it "should support set_column_default" do
1272
- @db.alter_table(:cats) do
1273
- set_column_default :score, 3
1274
- end
1275
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score SET DEFAULT 3"]
1276
- end
1277
-
1278
- it "should support set_column_type" do
1279
- @db.alter_table(:cats) do
1280
- set_column_type :score, :real
1281
- end
1282
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE real"]
1283
- end
1284
-
1285
- it "should support set_column_type with options" do
1286
- @db.alter_table(:cats) do
1287
- set_column_type :score, :integer, :unsigned=>true
1288
- set_column_type :score, :varchar, :size=>30
1289
- set_column_type :score, :enum, :elements=>['a', 'b']
1290
- end
1291
- @db.sqls.must_equal ["ALTER TABLE cats ALTER COLUMN score TYPE integer UNSIGNED",
1292
- "ALTER TABLE cats ALTER COLUMN score TYPE varchar(30)",
1293
- "ALTER TABLE cats ALTER COLUMN score TYPE enum('a', 'b')"]
1294
- end
1295
-
1296
- it "should combine operations into a single query if the database supports it" do
1297
- @db.define_singleton_method(:supports_combining_alter_table_ops?){true}
1298
- @db.alter_table(:cats) do
1299
- add_column :a, Integer
1300
- drop_column :b
1301
- set_column_not_null :c
1302
- rename_column :d, :e
1303
- set_column_default :f, 'g'
1304
- set_column_type :h, Integer
1305
- add_constraint(:i){a > 1}
1306
- drop_constraint :j
1307
- end
1308
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN a integer, DROP COLUMN b, ALTER COLUMN c SET NOT NULL, RENAME COLUMN d TO e, ALTER COLUMN f SET DEFAULT 'g', ALTER COLUMN h TYPE integer, ADD CONSTRAINT i CHECK (a > 1), DROP CONSTRAINT j"]
1309
- end
1310
-
1311
- it "should combine operations into consecutive groups of combinable operations if the database supports combining operations" do
1312
- @db.define_singleton_method(:supports_combining_alter_table_ops?){true}
1313
- @db.alter_table(:cats) do
1314
- add_column :a, Integer
1315
- drop_column :b
1316
- set_column_not_null :c
1317
- rename_column :d, :e
1318
- add_index :e
1319
- set_column_default :f, 'g'
1320
- set_column_type :h, Integer
1321
- add_constraint(:i){a > 1}
1322
- drop_constraint :j
1323
- end
1324
- @db.sqls.must_equal ["ALTER TABLE cats ADD COLUMN a integer, DROP COLUMN b, ALTER COLUMN c SET NOT NULL, RENAME COLUMN d TO e",
1325
- "CREATE INDEX cats_e_index ON cats (e)",
1326
- "ALTER TABLE cats ALTER COLUMN f SET DEFAULT 'g', ALTER COLUMN h TYPE integer, ADD CONSTRAINT i CHECK (a > 1), DROP CONSTRAINT j"]
1327
- end
1328
-
1329
- end
1330
-
1331
- describe "Database#create_table" do
1332
- before do
1333
- @db = Sequel.mock
1334
- end
1335
-
1336
- it "should construct proper SQL" do
1337
- @db.create_table :test do
1338
- primary_key :id, :integer, :null => false
1339
- column :name, :text
1340
- index :name, :unique => true
1341
- end
1342
- @db.sqls.must_equal ['CREATE TABLE test (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
1343
- 'CREATE UNIQUE INDEX test_name_index ON test (name)']
1344
- end
1345
-
1346
- it "should create a temporary table" do
1347
- @db.create_table :test_tmp, :temp => true do
1348
- primary_key :id, :integer, :null => false
1349
- column :name, :text
1350
- index :name, :unique => true
1351
- end
1352
-
1353
- @db.sqls.must_equal ['CREATE TEMPORARY TABLE test_tmp (id integer NOT NULL PRIMARY KEY AUTOINCREMENT, name text)',
1354
- 'CREATE UNIQUE INDEX test_tmp_name_index ON test_tmp (name)']
1355
- end
1356
- end
1357
-
1358
- describe "Database#alter_table" do
1359
- before do
1360
- @db = Sequel.mock
1361
- end
1362
-
1363
- it "should construct proper SQL" do
1364
- @db.alter_table :xyz do
1365
- add_column :aaa, :text, :null => false, :unique => true
1366
- drop_column :bbb
1367
- rename_column :ccc, :ddd
1368
- set_column_type :eee, :integer
1369
- set_column_default :hhh, 'abcd'
1370
- add_index :fff, :unique => true
1371
- drop_index :ggg
1372
- end
1373
-
1374
- @db.sqls.must_equal ['ALTER TABLE xyz ADD COLUMN aaa text NOT NULL UNIQUE',
1375
- 'ALTER TABLE xyz DROP COLUMN bbb',
1376
- 'ALTER TABLE xyz RENAME COLUMN ccc TO ddd',
1377
- 'ALTER TABLE xyz ALTER COLUMN eee TYPE integer',
1378
- "ALTER TABLE xyz ALTER COLUMN hhh SET DEFAULT 'abcd'",
1379
- 'CREATE UNIQUE INDEX xyz_fff_index ON xyz (fff)',
1380
- 'DROP INDEX xyz_ggg_index']
1381
- end
1382
- end
1383
-
1384
- describe "Database#add_column" do
1385
- it "should construct proper SQL" do
1386
- db = Sequel.mock
1387
- db.add_column(:test, :name, :text, :unique => true).must_be_nil
1388
- db.sqls.must_equal ['ALTER TABLE test ADD COLUMN name text UNIQUE']
1389
- end
1390
- end
1391
-
1392
- describe "Database#drop_column" do
1393
- before do
1394
- @db = Sequel.mock
1395
- end
1396
-
1397
- it "should construct proper SQL" do
1398
- @db.drop_column(:test, :name).must_be_nil
1399
- @db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name']
1400
- end
1401
-
1402
- it "should use CASCADE for :cascade=>true option" do
1403
- @db.drop_column :test, :name, :cascade=>true
1404
- @db.sqls.must_equal ['ALTER TABLE test DROP COLUMN name CASCADE']
1405
- end
1406
- end
1407
-
1408
- describe "Database#rename_column" do
1409
- before do
1410
- @db = Sequel.mock
1411
- end
1412
-
1413
- it "should construct proper SQL" do
1414
- @db.rename_column(:test, :abc, :def).must_be_nil
1415
- @db.sqls.must_equal ['ALTER TABLE test RENAME COLUMN abc TO def']
1416
- end
1417
- end
1418
-
1419
- describe "Database#set_column_type" do
1420
- before do
1421
- @db = Sequel.mock
1422
- end
1423
-
1424
- it "should construct proper SQL" do
1425
- @db.set_column_type(:test, :name, :integer).must_be_nil
1426
- @db.sqls.must_equal ['ALTER TABLE test ALTER COLUMN name TYPE integer']
1427
- end
1428
- end
1429
-
1430
- describe "Database#set_column_default" do
1431
- before do
1432
- @db = Sequel.mock
1433
- end
1434
-
1435
- it "should construct proper SQL" do
1436
- @db.set_column_default(:test, :name, 'zyx').must_be_nil
1437
- @db.sqls.must_equal ["ALTER TABLE test ALTER COLUMN name SET DEFAULT 'zyx'"]
1438
- end
1439
- end
1440
-
1441
- describe "Database#add_index" do
1442
- before do
1443
- @db = Sequel.mock
1444
- end
1445
-
1446
- it "should construct proper SQL" do
1447
- @db.add_index(:test, :name, :unique => true).must_be_nil
1448
- @db.sqls.must_equal ['CREATE UNIQUE INDEX test_name_index ON test (name)']
1449
- end
1450
-
1451
- it "should accept multiple columns" do
1452
- @db.add_index :test, [:one, :two]
1453
- @db.sqls.must_equal ['CREATE INDEX test_one_two_index ON test (one, two)']
1454
- end
1455
- end
1456
-
1457
- describe "Database#drop_index" do
1458
- before do
1459
- @db = Sequel.mock
1460
- end
1461
-
1462
- it "should construct proper SQL" do
1463
- @db.drop_index(:test, :name).must_be_nil
1464
- @db.sqls.must_equal ['DROP INDEX test_name_index']
1465
- end
1466
-
1467
- end
1468
-
1469
- describe "Database#drop_table" do
1470
- before do
1471
- @db = Sequel.mock
1472
- end
1473
-
1474
- it "should construct proper SQL" do
1475
- @db.drop_table(:test).must_be_nil
1476
- @db.sqls.must_equal ['DROP TABLE test']
1477
- end
1478
-
1479
- it "should accept multiple table names" do
1480
- @db.drop_table :a, :bb, :ccc
1481
- @db.sqls.must_equal ['DROP TABLE a', 'DROP TABLE bb', 'DROP TABLE ccc']
1482
- end
1483
- end
1484
-
1485
- describe "Database#rename_table" do
1486
- before do
1487
- @db = Sequel.mock
1488
- end
1489
-
1490
- it "should construct proper SQL" do
1491
- @db.rename_table(:abc, :xyz).must_be_nil
1492
- @db.sqls.must_equal ['ALTER TABLE abc RENAME TO xyz']
1493
- end
1494
- end
1495
-
1496
- describe "Database#create_view" do
1497
- before do
1498
- @db = Sequel.mock
1499
- end
1500
-
1501
- it "should construct proper SQL with raw SQL" do
1502
- @db.create_view(:test, "SELECT * FROM xyz").must_be_nil
1503
- @db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
1504
- @db.create_view Sequel.identifier(:test), "SELECT * FROM xyz"
1505
- @db.sqls.must_equal ['CREATE VIEW test AS SELECT * FROM xyz']
1506
- end
1507
-
1508
- it "should construct proper SQL with dataset" do
1509
- @db.create_view :test, @db[:items].select(:a, :b).order(:c)
1510
- @db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
1511
- end
1512
-
1513
- it "should handle :columns option" do
1514
- @db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[:d, :e]
1515
- @db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
1516
- @db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>%w'd e'
1517
- @db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
1518
- @db.create_view :test, @db[:items].select(:a, :b).order(:c), :columns=>[Sequel.identifier('d'), Sequel.lit('e')]
1519
- @db.sqls.must_equal ['CREATE VIEW test (d, e) AS SELECT a, b FROM items ORDER BY c']
1520
- end
1521
-
1522
- it "should handle :check option" do
1523
- @db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>true
1524
- @db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH CHECK OPTION']
1525
- @db.create_view :test, @db[:items].select(:a, :b).order(:c), :check=>:local
1526
- @db.sqls.must_equal ['CREATE VIEW test AS SELECT a, b FROM items ORDER BY c WITH LOCAL CHECK OPTION']
1527
- end
1528
-
1529
- with_symbol_splitting "should handle create_or_replace_view with splittable symbols" do
1530
- @db.create_or_replace_view :sch__test, "SELECT * FROM xyz"
1531
- @db.sqls.must_equal ['DROP VIEW sch.test', 'CREATE VIEW sch.test AS SELECT * FROM xyz']
1532
- end
1533
-
1534
- it "should handle create_or_replace_view" do
1535
- @db.create_or_replace_view :test, @db[:items].select(:a, :b).order(:c)
1536
- @db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
1537
- @db.create_or_replace_view Sequel.identifier(:test), @db[:items].select(:a, :b).order(:c)
1538
- @db.sqls.must_equal ['DROP VIEW test', 'CREATE VIEW test AS SELECT a, b FROM items ORDER BY c']
1539
- end
1540
-
1541
- it "should use CREATE OR REPLACE VIEW if such syntax is supported" do
1542
- def @db.supports_create_or_replace_view?() true end
1543
- @db.create_or_replace_view :test, @db[:items]
1544
- @db.sqls.must_equal ['CREATE OR REPLACE VIEW test AS SELECT * FROM items']
1545
- end
1546
- end
1547
-
1548
- describe "Database#drop_view" do
1549
- before do
1550
- @db = Sequel.mock
1551
- end
1552
-
1553
- with_symbol_splitting "should construct proper SQL for splittable symbols" do
1554
- @db.drop_view(:sch__test).must_be_nil
1555
- @db.sqls.must_equal ['DROP VIEW sch.test']
1556
- end
1557
-
1558
- it "should construct proper SQL" do
1559
- @db.drop_view :test
1560
- @db.drop_view Sequel.identifier(:test)
1561
- @db.drop_view Sequel.qualify(:sch, :test)
1562
- @db.sqls.must_equal ['DROP VIEW test', 'DROP VIEW test', 'DROP VIEW sch.test']
1563
- end
1564
-
1565
- it "should drop multiple views at once" do
1566
- @db.drop_view :cats, :dogs
1567
- @db.sqls.must_equal ['DROP VIEW cats', 'DROP VIEW dogs']
1568
- end
1569
-
1570
- it "should support the :cascade option" do
1571
- @db.drop_view :cats, :dogs, :cascade=>true
1572
- @db.sqls.must_equal ['DROP VIEW cats CASCADE', 'DROP VIEW dogs CASCADE']
1573
- end
1574
-
1575
- it "should support the :if_exists option" do
1576
- @db.drop_view :cats, :dogs, :if_exists=>true
1577
- @db.sqls.must_equal ['DROP VIEW IF EXISTS cats', 'DROP VIEW IF EXISTS dogs']
1578
- end
1579
- end
1580
-
1581
- describe "Database#alter_table_sql" do
1582
- it "should raise error for an invalid op" do
1583
- proc {Sequel.mock.send(:alter_table_sql, :mau, :op => :blah)}.must_raise(Sequel::Error)
1584
- end
1585
- end
1586
-
1587
- describe "Schema Parser" do
1588
- before do
1589
- @sqls = []
1590
- @db = Sequel::Database.new
1591
- end
1592
-
1593
- it "should raise an error if there are no columns" do
1594
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1595
- []
1596
- end
1597
- proc{@db.schema(:x)}.must_raise(Sequel::Error)
1598
- end
1599
-
1600
- it "should cache data by default" do
1601
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1602
- [[:a, {}]]
1603
- end
1604
- @db.schema(:x).must_be_same_as(@db.schema(:x))
1605
- end
1606
-
1607
- it "should not cache data if :reload=>true is given" do
1608
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1609
- [[:a, {}]]
1610
- end
1611
- @db.schema(:x).wont_be_same_as(@db.schema(:x, :reload=>true))
1612
- end
1613
-
1614
- it "should not cache schema metadata if cache_schema is false" do
1615
- @db.cache_schema = false
1616
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1617
- [[:a, {}]]
1618
- end
1619
- @db.schema(:x).wont_be_same_as(@db.schema(:x))
1620
- end
1621
-
1622
- it "should freeze string values in resulting hash" do
1623
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1624
- [[:a, {:oid=>1, :db_type=>'integer'.dup, :default=>"'a'".dup, :ruby_default=>'a'.dup}]]
1625
- end
1626
- c = @db.schema(:x)[0][1]
1627
- c[:db_type].frozen?.must_equal true
1628
- c[:default].frozen?.must_equal true
1629
- c[:ruby_default].frozen?.must_equal true
1630
- end
1631
-
1632
- it "should provide options if given a table name" do
1633
- c = nil
1634
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1635
- c = [t, opts]
1636
- [[:a, {:db_type=>t.to_s}]]
1637
- end
1638
- @db.schema(:x)
1639
- c.must_equal ["x", {}]
1640
- @db.schema(Sequel[:s][:x])
1641
- c.must_equal ["x", {:schema=>"s"}]
1642
- ds = @db[Sequel[:s][:y]]
1643
- @db.schema(ds)
1644
- c.must_equal ["y", {:schema=>"s", :dataset=>ds}]
1645
- end
1646
-
1647
- with_symbol_splitting "should provide options if given a table name with splittable symbols" do
1648
- c = nil
1649
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1650
- c = [t, opts]
1651
- [[:a, {:db_type=>t.to_s}]]
1652
- end
1653
- @db.schema(:s__x)
1654
- c.must_equal ["x", {:schema=>"s"}]
1655
- ds = @db[:s__y]
1656
- @db.schema(ds)
1657
- c.must_equal ["y", {:schema=>"s", :dataset=>ds}]
1658
- end
1659
-
1660
- it "should parse the schema correctly for a single table" do
1661
- sqls = @sqls
1662
- proc{@db.schema(:x)}.must_raise(Sequel::Error)
1663
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1664
- sqls << t
1665
- [[:a, {:db_type=>t.to_s}]]
1666
- end
1667
- @db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
1668
- @sqls.must_equal ['x']
1669
- @db.schema(:x).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
1670
- @sqls.must_equal ['x']
1671
- @db.schema(:x, :reload=>true).must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}]]
1672
- @sqls.must_equal ['x', 'x']
1673
- end
1674
-
1675
- it "should dedup :db_type strings" do
1676
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1677
- [[:a, {:db_type=>t.to_s.dup}], [:b, {:db_type=>t.to_s.dup}]]
1678
- end
1679
- sch = @db.schema(:x)
1680
- sch.must_equal [[:a, {:db_type=>"x", :ruby_default=>nil}], [:b, {:db_type=>"x", :ruby_default=>nil}]]
1681
- sch[0][1][:db_type].must_be_same_as(sch[1][1][:db_type])
1682
- end if RUBY_VERSION >= '2.5'
1683
-
1684
- it "should set :auto_increment to true by default if unset and a single integer primary key is used" do
1685
- @db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}]]}
1686
- @db.schema(:x).first.last[:auto_increment].must_equal true
1687
- end
1688
-
1689
- it "should not set :auto_increment if already set" do
1690
- @db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer', :auto_increment=>false}]]}
1691
- @db.schema(:x).first.last[:auto_increment].must_equal false
1692
- end
1693
-
1694
- it "should set :auto_increment to false by default if unset and a single nonintegery primary key is used" do
1695
- @db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'varchar'}]]}
1696
- @db.schema(:x).first.last[:auto_increment].must_equal false
1697
- end
1698
-
1699
- it "should set :auto_increment to false by default if unset and a composite primary key" do
1700
- @db.define_singleton_method(:schema_parse_table){|*| [[:a, {:primary_key=>true, :db_type=>'integer'}], [:b, {:primary_key=>true, :db_type=>'integer'}]]}
1701
- @db.schema(:x).first.last[:auto_increment].must_equal false
1702
- @db.schema(:x).last.last[:auto_increment].must_equal false
1703
- end
1704
-
1705
- it "should set :auto_increment to true by default if set and not the first column" do
1706
- @db.define_singleton_method(:schema_parse_table){|*| [[:b, {}], [:a, {:primary_key=>true, :db_type=>'integer'}]]}
1707
- @db.schema(:x).last.last[:auto_increment].must_equal true
1708
- end
1709
-
1710
- it "should convert various types of table name arguments" do
1711
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1712
- [[t, opts]]
1713
- end
1714
- s1 = @db.schema(:x)
1715
- s1.must_equal [['x', {:ruby_default=>nil}]]
1716
- @db.schema(:x).object_id.must_equal s1.object_id
1717
- @db.schema(Sequel.identifier(:x)).object_id.must_equal s1.object_id
1718
-
1719
- s2 = @db.schema(Sequel[:x][:y])
1720
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil}]]
1721
- @db.schema(Sequel[:x][:y]).object_id.must_equal s2.object_id
1722
- @db.schema(Sequel.qualify(:x, :y)).object_id.must_equal s2.object_id
1723
-
1724
- s2 = @db.schema(Sequel.qualify(:v, Sequel[:x][:y]))
1725
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.identifier('v')}]]
1726
- @db.schema(Sequel.qualify(:v, Sequel[:x][:y])).object_id.must_equal s2.object_id
1727
- @db.schema(Sequel.qualify(Sequel[:v][:x], :y)).object_id.must_equal s2.object_id
1728
-
1729
- s2 = @db.schema(Sequel.qualify(Sequel[:u][:v], Sequel[:x][:y]))
1730
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.qualify('u', 'v')}]]
1731
- @db.schema(Sequel.qualify(Sequel[:u][:v], Sequel[:x][:y])).object_id.must_equal s2.object_id
1732
- @db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.must_equal s2.object_id
1733
- end
1734
-
1735
- with_symbol_splitting "should convert splittable symbol arguments" do
1736
- @db.define_singleton_method(:schema_parse_table) do |t, opts|
1737
- [[t, opts]]
1738
- end
1739
- s1 = @db.schema(:x)
1740
- s1.must_equal [['x', {:ruby_default=>nil}]]
1741
- @db.schema(:x).object_id.must_equal s1.object_id
1742
- @db.schema(Sequel.identifier(:x)).object_id.must_equal s1.object_id
1743
-
1744
- s2 = @db.schema(:x__y)
1745
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil}]]
1746
- @db.schema(:x__y).object_id.must_equal s2.object_id
1747
- @db.schema(Sequel.qualify(:x, :y)).object_id.must_equal s2.object_id
1748
-
1749
- s2 = @db.schema(Sequel.qualify(:v, :x__y))
1750
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.identifier('v')}]]
1751
- @db.schema(Sequel.qualify(:v, :x__y)).object_id.must_equal s2.object_id
1752
- @db.schema(Sequel.qualify(:v__x, :y)).object_id.must_equal s2.object_id
1753
-
1754
- s2 = @db.schema(Sequel.qualify(:u__v, :x__y))
1755
- s2.must_equal [['y', {:schema=>'x', :ruby_default=>nil, :information_schema_schema=>Sequel.qualify('u', 'v')}]]
1756
- @db.schema(Sequel.qualify(:u__v, :x__y)).object_id.must_equal s2.object_id
1757
- @db.schema(Sequel.qualify(Sequel.qualify(:u, :v), Sequel.qualify(:x, :y))).object_id.must_equal s2.object_id
1758
- end
1759
-
1760
- it "should correctly parse all supported data types" do
1761
- sm = Module.new do
1762
- def schema_parse_table(t, opts)
1763
- [[:x, {:db_type=>t.to_s, :type=>schema_column_type(t.to_s)}]]
1764
- end
1765
- end
1766
- @db.extend(sm)
1767
- @db.schema(:tinyint).first.last[:type].must_equal :integer
1768
- @db.schema(:int).first.last[:type].must_equal :integer
1769
- @db.schema(:integer).first.last[:type].must_equal :integer
1770
- @db.schema(:bigint).first.last[:type].must_equal :integer
1771
- @db.schema(:smallint).first.last[:type].must_equal :integer
1772
- @db.schema(:character).first.last[:type].must_equal :string
1773
- @db.schema(:"character varying").first.last[:type].must_equal :string
1774
- @db.schema(:varchar).first.last[:type].must_equal :string
1775
- @db.schema(:"varchar(255)").first.last[:type].must_equal :string
1776
- @db.schema(:"varchar(255)").first.last[:max_length].must_equal 255
1777
- @db.schema(:text).first.last[:type].must_equal :string
1778
- @db.schema(:date).first.last[:type].must_equal :date
1779
- @db.schema(:datetime).first.last[:type].must_equal :datetime
1780
- @db.schema(:timestamp).first.last[:type].must_equal :datetime
1781
- @db.schema(:"timestamp with time zone").first.last[:type].must_equal :datetime
1782
- @db.schema(:"timestamp without time zone").first.last[:type].must_equal :datetime
1783
- @db.schema(:time).first.last[:type].must_equal :time
1784
- @db.schema(:"time with time zone").first.last[:type].must_equal :time
1785
- @db.schema(:"time without time zone").first.last[:type].must_equal :time
1786
- @db.schema(:boolean).first.last[:type].must_equal :boolean
1787
- @db.schema(:real).first.last[:type].must_equal :float
1788
- @db.schema(:float).first.last[:type].must_equal :float
1789
- @db.schema(:double).first.last[:type].must_equal :float
1790
- @db.schema(:"double(1,2)").first.last[:type].must_equal :float
1791
- @db.schema(:"double precision").first.last[:type].must_equal :float
1792
- @db.schema(:number).first.last[:type].must_equal :decimal
1793
- @db.schema(:numeric).first.last[:type].must_equal :decimal
1794
- @db.schema(:decimal).first.last[:type].must_equal :decimal
1795
- @db.schema(:"number(10,0)").first.last[:type].must_equal :integer
1796
- @db.schema(:"numeric(10, 10)").first.last[:type].must_equal :decimal
1797
- @db.schema(:"decimal(10,1)").first.last[:type].must_equal :decimal
1798
- @db.schema(:bytea).first.last[:type].must_equal :blob
1799
- @db.schema(:blob).first.last[:type].must_equal :blob
1800
- @db.schema(:image).first.last[:type].must_equal :blob
1801
- @db.schema(:nchar).first.last[:type].must_equal :string
1802
- @db.schema(:nvarchar).first.last[:type].must_equal :string
1803
- @db.schema(:ntext).first.last[:type].must_equal :string
1804
- @db.schema(:smalldatetime).first.last[:type].must_equal :datetime
1805
- @db.schema(:binary).first.last[:type].must_equal :blob
1806
- @db.schema(:varbinary).first.last[:type].must_equal :blob
1807
- @db.schema(:enum).first.last[:type].must_equal :enum
1808
-
1809
- @db = Sequel.mock(:host=>'postgres')
1810
- @db.extend(sm)
1811
- @db.schema(:interval).first.last[:type].must_equal :interval
1812
- @db.schema(:citext).first.last[:type].must_equal :string
1813
-
1814
- @db = Sequel.mock(:host=>'mysql')
1815
- @db.extend(sm)
1816
- @db.schema(:set).first.last[:type].must_equal :set
1817
- @db.schema(:mediumint).first.last[:type].must_equal :integer
1818
- @db.schema(:mediumtext).first.last[:type].must_equal :string
1819
- end
1820
- end