sequel 5.70.0 → 5.80.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (85) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG +134 -0
  3. data/README.rdoc +7 -5
  4. data/doc/dataset_basics.rdoc +1 -1
  5. data/doc/mass_assignment.rdoc +1 -1
  6. data/doc/migration.rdoc +15 -0
  7. data/doc/opening_databases.rdoc +6 -2
  8. data/doc/querying.rdoc +6 -1
  9. data/doc/release_notes/5.71.0.txt +21 -0
  10. data/doc/release_notes/5.72.0.txt +33 -0
  11. data/doc/release_notes/5.73.0.txt +66 -0
  12. data/doc/release_notes/5.74.0.txt +45 -0
  13. data/doc/release_notes/5.75.0.txt +35 -0
  14. data/doc/release_notes/5.76.0.txt +86 -0
  15. data/doc/release_notes/5.77.0.txt +63 -0
  16. data/doc/release_notes/5.78.0.txt +67 -0
  17. data/doc/release_notes/5.79.0.txt +28 -0
  18. data/doc/release_notes/5.80.0.txt +40 -0
  19. data/doc/schema_modification.rdoc +2 -2
  20. data/doc/testing.rdoc +4 -2
  21. data/lib/sequel/adapters/ibmdb.rb +1 -1
  22. data/lib/sequel/adapters/jdbc/h2.rb +3 -0
  23. data/lib/sequel/adapters/jdbc/hsqldb.rb +2 -0
  24. data/lib/sequel/adapters/jdbc/postgresql.rb +3 -0
  25. data/lib/sequel/adapters/jdbc/sqlanywhere.rb +15 -0
  26. data/lib/sequel/adapters/jdbc/sqlserver.rb +4 -0
  27. data/lib/sequel/adapters/jdbc.rb +10 -6
  28. data/lib/sequel/adapters/mysql2.rb +2 -2
  29. data/lib/sequel/adapters/odbc/mssql.rb +1 -1
  30. data/lib/sequel/adapters/postgres.rb +6 -5
  31. data/lib/sequel/adapters/shared/db2.rb +12 -0
  32. data/lib/sequel/adapters/shared/mssql.rb +30 -2
  33. data/lib/sequel/adapters/shared/mysql.rb +68 -3
  34. data/lib/sequel/adapters/shared/oracle.rb +4 -6
  35. data/lib/sequel/adapters/shared/postgres.rb +116 -6
  36. data/lib/sequel/adapters/shared/sqlanywhere.rb +10 -4
  37. data/lib/sequel/adapters/shared/sqlite.rb +20 -3
  38. data/lib/sequel/adapters/sqlite.rb +42 -3
  39. data/lib/sequel/connection_pool.rb +4 -2
  40. data/lib/sequel/database/misc.rb +3 -2
  41. data/lib/sequel/database/schema_methods.rb +11 -4
  42. data/lib/sequel/database/transactions.rb +6 -0
  43. data/lib/sequel/dataset/actions.rb +8 -6
  44. data/lib/sequel/dataset/dataset_module.rb +1 -1
  45. data/lib/sequel/dataset/features.rb +10 -1
  46. data/lib/sequel/dataset/graph.rb +1 -0
  47. data/lib/sequel/dataset/query.rb +58 -9
  48. data/lib/sequel/dataset/sql.rb +47 -34
  49. data/lib/sequel/exceptions.rb +5 -0
  50. data/lib/sequel/extensions/any_not_empty.rb +2 -2
  51. data/lib/sequel/extensions/async_thread_pool.rb +7 -0
  52. data/lib/sequel/extensions/auto_cast_date_and_time.rb +94 -0
  53. data/lib/sequel/extensions/caller_logging.rb +2 -1
  54. data/lib/sequel/extensions/duplicate_columns_handler.rb +10 -9
  55. data/lib/sequel/extensions/index_caching.rb +5 -1
  56. data/lib/sequel/extensions/migration.rb +64 -14
  57. data/lib/sequel/extensions/named_timezones.rb +1 -1
  58. data/lib/sequel/extensions/pg_array.rb +10 -0
  59. data/lib/sequel/extensions/pg_auto_parameterize_in_array.rb +110 -0
  60. data/lib/sequel/extensions/pg_extended_date_support.rb +4 -4
  61. data/lib/sequel/extensions/pg_json_ops.rb +52 -0
  62. data/lib/sequel/extensions/pg_range.rb +2 -2
  63. data/lib/sequel/extensions/pg_timestamptz.rb +27 -3
  64. data/lib/sequel/extensions/provenance.rb +108 -0
  65. data/lib/sequel/extensions/round_timestamps.rb +1 -1
  66. data/lib/sequel/extensions/schema_caching.rb +1 -1
  67. data/lib/sequel/extensions/sqlite_json_ops.rb +76 -18
  68. data/lib/sequel/extensions/transaction_connection_validator.rb +78 -0
  69. data/lib/sequel/model/associations.rb +9 -2
  70. data/lib/sequel/model/base.rb +26 -13
  71. data/lib/sequel/model/exceptions.rb +15 -3
  72. data/lib/sequel/plugins/column_encryption.rb +27 -6
  73. data/lib/sequel/plugins/defaults_setter.rb +16 -0
  74. data/lib/sequel/plugins/list.rb +5 -2
  75. data/lib/sequel/plugins/mssql_optimistic_locking.rb +8 -38
  76. data/lib/sequel/plugins/optimistic_locking.rb +9 -42
  77. data/lib/sequel/plugins/optimistic_locking_base.rb +55 -0
  78. data/lib/sequel/plugins/paged_operations.rb +181 -0
  79. data/lib/sequel/plugins/pg_auto_constraint_validations.rb +5 -1
  80. data/lib/sequel/plugins/pg_xmin_optimistic_locking.rb +109 -0
  81. data/lib/sequel/plugins/rcte_tree.rb +7 -4
  82. data/lib/sequel/plugins/static_cache_cache.rb +5 -1
  83. data/lib/sequel/plugins/validation_helpers.rb +1 -1
  84. data/lib/sequel/version.rb +1 -1
  85. metadata +44 -3
@@ -43,7 +43,7 @@ module Sequel
43
43
  add_graph_aliases distinct except exclude exclude_having
44
44
  filter for_update from from_self graph grep group group_and_count group_append group_by having intersect invert
45
45
  limit lock_style naked offset or order order_append order_by order_more order_prepend qualify
46
- reverse reverse_order select select_all select_append select_group select_more server
46
+ reverse reverse_order select select_all select_append select_group select_more select_prepend server
47
47
  set_graph_aliases unfiltered ungraphed ungrouped union
48
48
  unlimited unordered where with with_recursive with_sql
49
49
  METHS
@@ -129,6 +129,7 @@ module Sequel
129
129
  def distinct(*args, &block)
130
130
  virtual_row_columns(args, block)
131
131
  if args.empty?
132
+ return self if opts[:distinct] == EMPTY_ARRAY
132
133
  cached_dataset(:_distinct_ds){clone(:distinct => EMPTY_ARRAY)}
133
134
  else
134
135
  raise(InvalidOperation, "DISTINCT ON not supported") unless supports_distinct_on?
@@ -230,6 +231,7 @@ module Sequel
230
231
  #
231
232
  # DB[:table].for_update # SELECT * FROM table FOR UPDATE
232
233
  def for_update
234
+ return self if opts[:lock] == :update
233
235
  cached_dataset(:_for_update_ds){lock_style(:update)}
234
236
  end
235
237
 
@@ -641,6 +643,7 @@ module Sequel
641
643
  # DB.from(:a, DB[:b].where(Sequel[:a][:c]=>Sequel[:b][:d]).lateral)
642
644
  # # SELECT * FROM a, LATERAL (SELECT * FROM b WHERE (a.c = b.d))
643
645
  def lateral
646
+ return self if opts[:lateral]
644
647
  cached_dataset(:_lateral_ds){clone(:lateral=>true)}
645
648
  end
646
649
 
@@ -744,6 +747,7 @@ module Sequel
744
747
  # ds.all # => [{2=>:id}]
745
748
  # ds.naked.all # => [{:id=>2}]
746
749
  def naked
750
+ return self unless opts[:row_proc]
747
751
  cached_dataset(:_naked_ds){with_row_proc(nil)}
748
752
  end
749
753
 
@@ -753,6 +757,7 @@ module Sequel
753
757
  # DB[:items].for_update.nowait
754
758
  # # SELECT * FROM items FOR UPDATE NOWAIT
755
759
  def nowait
760
+ return self if opts[:nowait]
756
761
  cached_dataset(:_nowait_ds) do
757
762
  raise(Error, 'This dataset does not support raises errors instead of waiting for locked rows') unless supports_nowait?
758
763
  clone(:nowait=>true)
@@ -878,6 +883,7 @@ module Sequel
878
883
  # end
879
884
  def returning(*values)
880
885
  if values.empty?
886
+ return self if opts[:returning] == EMPTY_ARRAY
881
887
  cached_dataset(:_returning_ds) do
882
888
  raise Error, "RETURNING is not supported on #{db.database_type}" unless supports_returning?(:insert)
883
889
  clone(:returning=>EMPTY_ARRAY)
@@ -930,6 +936,7 @@ module Sequel
930
936
  # DB[:items].select_all(:items, :foo) # SELECT items.*, foo.* FROM items
931
937
  def select_all(*tables)
932
938
  if tables.empty?
939
+ return self unless opts[:select]
933
940
  cached_dataset(:_select_all_ds){clone(:select => nil)}
934
941
  else
935
942
  select(*tables.map{|t| i, a = split_alias(t); a || i}.map!{|t| SQL::ColumnAll.new(t)}.freeze)
@@ -944,14 +951,8 @@ module Sequel
944
951
  # DB[:items].select(:a).select_append(:b) # SELECT a, b FROM items
945
952
  # DB[:items].select_append(:b) # SELECT *, b FROM items
946
953
  def select_append(*columns, &block)
947
- cur_sel = @opts[:select]
948
- if !cur_sel || cur_sel.empty?
949
- unless supports_select_all_and_column?
950
- return select_all(*(Array(@opts[:from]) + Array(@opts[:join]))).select_append(*columns, &block)
951
- end
952
- cur_sel = [WILDCARD]
953
- end
954
- select(*(cur_sel + columns), &block)
954
+ virtual_row_columns(columns, block)
955
+ select(*(_current_select(true) + columns))
955
956
  end
956
957
 
957
958
  # Set both the select and group clauses with the given +columns+.
@@ -973,6 +974,18 @@ module Sequel
973
974
  select_append(*columns, &block)
974
975
  end
975
976
 
977
+ # Returns a copy of the dataset with the given columns added
978
+ # to the existing selected columns. If no columns are currently selected,
979
+ # it will select the columns given in addition to *.
980
+ #
981
+ # DB[:items].select(:a).select(:b) # SELECT b FROM items
982
+ # DB[:items].select(:a).select_prepend(:b) # SELECT b, a FROM items
983
+ # DB[:items].select_prepend(:b) # SELECT b, * FROM items
984
+ def select_prepend(*columns, &block)
985
+ virtual_row_columns(columns, block)
986
+ select(*(columns + _current_select(false)))
987
+ end
988
+
976
989
  # Set the server for this dataset to use. Used to pick a specific database
977
990
  # shard to run a query against, or to override the default (where SELECT uses
978
991
  # :read_only database and all other queries use the :default database). This
@@ -999,6 +1012,7 @@ module Sequel
999
1012
 
1000
1013
  # Specify that the check for limits/offsets when updating/deleting be skipped for the dataset.
1001
1014
  def skip_limit_check
1015
+ return self if opts[:skip_limit_check]
1002
1016
  cached_dataset(:_skip_limit_check_ds) do
1003
1017
  clone(:skip_limit_check=>true)
1004
1018
  end
@@ -1006,6 +1020,7 @@ module Sequel
1006
1020
 
1007
1021
  # Skip locked rows when returning results from this dataset.
1008
1022
  def skip_locked
1023
+ return self if opts[:skip_locked]
1009
1024
  cached_dataset(:_skip_locked_ds) do
1010
1025
  raise(Error, 'This dataset does not support skipping locked rows') unless supports_skip_locked?
1011
1026
  clone(:skip_locked=>true)
@@ -1017,6 +1032,7 @@ module Sequel
1017
1032
  # DB[:items].group(:a).having(a: 1).where(:b).unfiltered
1018
1033
  # # SELECT * FROM items GROUP BY a
1019
1034
  def unfiltered
1035
+ return self unless opts[:where] || opts[:having]
1020
1036
  cached_dataset(:_unfiltered_ds){clone(:where => nil, :having => nil)}
1021
1037
  end
1022
1038
 
@@ -1025,6 +1041,7 @@ module Sequel
1025
1041
  # DB[:items].group(:a).having(a: 1).where(:b).ungrouped
1026
1042
  # # SELECT * FROM items WHERE b
1027
1043
  def ungrouped
1044
+ return self unless opts[:group] || opts[:having]
1028
1045
  cached_dataset(:_ungrouped_ds){clone(:group => nil, :having => nil)}
1029
1046
  end
1030
1047
 
@@ -1052,6 +1069,7 @@ module Sequel
1052
1069
  #
1053
1070
  # DB[:items].limit(10, 20).unlimited # SELECT * FROM items
1054
1071
  def unlimited
1072
+ return self unless opts[:limit] || opts[:offset]
1055
1073
  cached_dataset(:_unlimited_ds){clone(:limit=>nil, :offset=>nil)}
1056
1074
  end
1057
1075
 
@@ -1059,6 +1077,7 @@ module Sequel
1059
1077
  #
1060
1078
  # DB[:items].order(:a).unordered # SELECT * FROM items
1061
1079
  def unordered
1080
+ return self unless opts[:order]
1062
1081
  cached_dataset(:_unordered_ds){clone(:order=>nil)}
1063
1082
  end
1064
1083
 
@@ -1353,6 +1372,36 @@ module Sequel
1353
1372
  end
1354
1373
  # :nocov:
1355
1374
 
1375
+ # A frozen array for the currently selected columns.
1376
+ def _current_select(allow_plain_wildcard)
1377
+ cur_sel = @opts[:select]
1378
+
1379
+ if !cur_sel || cur_sel.empty?
1380
+ cur_sel = if allow_plain_wildcard && supports_select_all_and_column?
1381
+ [WILDCARD].freeze
1382
+ else
1383
+ _current_select_column_all
1384
+ end
1385
+ elsif !allow_plain_wildcard && cur_sel.include?(WILDCARD)
1386
+ cur_sel = cur_sel.dup
1387
+ index = cur_sel.index(WILDCARD)
1388
+ cur_sel.delete(WILDCARD)
1389
+ _current_select_column_all.each_with_index do |ca, i|
1390
+ cur_sel.insert(index+i, ca)
1391
+ end
1392
+ cur_sel.freeze
1393
+ end
1394
+
1395
+ cur_sel
1396
+ end
1397
+
1398
+ # An array of SQL::ColumnAll objects for all FROM and JOIN tables. Used for select_append
1399
+ # and select_prepend.
1400
+ def _current_select_column_all
1401
+ tables = Array(@opts[:from]) + Array(@opts[:join])
1402
+ tables.map{|t| i, a = split_alias(t); a || i}.map!{|t| SQL::ColumnAll.new(t)}.freeze
1403
+ end
1404
+
1356
1405
  # If invert is true, invert the condition.
1357
1406
  def _invert_filter(cond, invert)
1358
1407
  if invert
@@ -82,7 +82,7 @@ module Sequel
82
82
  when DateTime
83
83
  literal_datetime_append(sql, v)
84
84
  when Date
85
- sql << literal_date(v)
85
+ literal_date_append(sql, v)
86
86
  when Dataset
87
87
  literal_dataset_append(sql, v)
88
88
  else
@@ -115,6 +115,33 @@ module Sequel
115
115
  sql
116
116
  end
117
117
 
118
+ # Literalize a date or time value, as a SQL string value with no
119
+ # typecasting. If +raw+ is true, remove the surrounding single
120
+ # quotes. This is designed for usage by bound argument code that
121
+ # can work even if the auto_cast_date_and_time extension is
122
+ # used (either manually or implicitly in the related adapter).
123
+ def literal_date_or_time(dt, raw=false)
124
+ value = case dt
125
+ when SQLTime
126
+ literal_sqltime(dt)
127
+ when Time
128
+ literal_time(dt)
129
+ when DateTime
130
+ literal_datetime(dt)
131
+ when Date
132
+ literal_date(dt)
133
+ else
134
+ raise TypeError, "unsupported type: #{dt.inspect}"
135
+ end
136
+
137
+ if raw
138
+ value.sub!(/\A'/, '')
139
+ value.sub!(/'\z/, '')
140
+ end
141
+
142
+ value
143
+ end
144
+
118
145
  # Returns an array of insert statements for inserting multiple records.
119
146
  # This method is used by +multi_insert+ to format insert statements and
120
147
  # expects a keys array and and an array of value arrays.
@@ -1104,9 +1131,14 @@ module Sequel
1104
1131
  :"t#{number}"
1105
1132
  end
1106
1133
 
1107
- # The strftime format to use when literalizing the time.
1134
+ # The strftime format to use when literalizing time (Sequel::SQLTime) values.
1135
+ def default_time_format
1136
+ "'%H:%M:%S.%6N'"
1137
+ end
1138
+
1139
+ # The strftime format to use when literalizing timestamp (Time/DateTime) values.
1108
1140
  def default_timestamp_format
1109
- requires_sql_standard_datetimes? ? "TIMESTAMP '%Y-%m-%d %H:%M:%S%N%z'" : "'%Y-%m-%d %H:%M:%S%N%z'"
1141
+ "'%Y-%m-%d %H:%M:%S.%6N'"
1110
1142
  end
1111
1143
 
1112
1144
  def delete_delete_sql(sql)
@@ -1169,43 +1201,23 @@ module Sequel
1169
1201
  {1 => ((op == :IN) ? 0 : 1)}
1170
1202
  end
1171
1203
 
1172
- # Format the timestamp based on the default_timestamp_format, with a couple
1173
- # of modifiers. First, allow %N to be used for fractions seconds (if the
1174
- # database supports them), and override %z to always use a numeric offset
1175
- # of hours and minutes.
1204
+ # Format the timestamp based on the default_timestamp_format.
1176
1205
  def format_timestamp(v)
1177
- v2 = db.from_application_timestamp(v)
1178
- fmt = default_timestamp_format.gsub(/%[Nz]/) do |m|
1179
- if m == '%N'
1180
- # Ruby 1.9 supports %N in timestamp formats, but Sequel has supported %N
1181
- # for longer in a different way, where the . is already appended and only 6
1182
- # decimal places are used by default.
1183
- format_timestamp_usec(v.is_a?(DateTime) ? v.sec_fraction*(1000000) : v.usec) if supports_timestamp_usecs?
1184
- else
1185
- if supports_timestamp_timezones?
1186
- # Would like to just use %z format, but it doesn't appear to work on Windows
1187
- # Instead, the offset fragment is constructed manually
1188
- minutes = (v2.is_a?(DateTime) ? v2.offset * 1440 : v2.utc_offset/60).to_i
1189
- format_timestamp_offset(*minutes.divmod(60))
1190
- end
1191
- end
1192
- end
1193
- v2.strftime(fmt)
1206
+ db.from_application_timestamp(v).strftime(default_timestamp_format)
1194
1207
  end
1195
1208
 
1196
- # Return the SQL timestamp fragment to use for the timezone offset.
1197
- def format_timestamp_offset(hour, minute)
1198
- sprintf("%+03i%02i", hour, minute)
1199
- end
1209
+ # :nocov:
1200
1210
 
1201
1211
  # Return the SQL timestamp fragment to use for the fractional time part.
1202
1212
  # Should start with the decimal point. Uses 6 decimal places by default.
1203
1213
  def format_timestamp_usec(usec, ts=timestamp_precision)
1214
+ # SEQUEL6: Remove
1204
1215
  unless ts == 6
1205
1216
  usec = usec/(10 ** (6 - ts))
1206
1217
  end
1207
1218
  sprintf(".%0#{ts}d", usec)
1208
1219
  end
1220
+ # :nocov:
1209
1221
 
1210
1222
  # Append literalization of identifier to SQL string, considering regular strings
1211
1223
  # as SQL identifiers instead of SQL strings.
@@ -1347,11 +1359,12 @@ module Sequel
1347
1359
 
1348
1360
  # SQL fragment for Date, using the ISO8601 format.
1349
1361
  def literal_date(v)
1350
- if requires_sql_standard_datetimes?
1351
- v.strftime("DATE '%Y-%m-%d'")
1352
- else
1353
- v.strftime("'%Y-%m-%d'")
1354
- end
1362
+ v.strftime("'%Y-%m-%d'")
1363
+ end
1364
+
1365
+ # Append literalization of date to SQL string.
1366
+ def literal_date_append(sql, v)
1367
+ sql << literal_date(v)
1355
1368
  end
1356
1369
 
1357
1370
  # SQL fragment for DateTime
@@ -1414,7 +1427,7 @@ module Sequel
1414
1427
 
1415
1428
  # SQL fragment for Sequel::SQLTime, containing just the time part
1416
1429
  def literal_sqltime(v)
1417
- v.strftime("'%H:%M:%S#{format_timestamp_usec(v.usec, sqltime_precision) if supports_timestamp_usecs?}'")
1430
+ v.strftime(default_time_format)
1418
1431
  end
1419
1432
 
1420
1433
  # Append literalization of Sequel::SQLTime to SQL string.
@@ -18,6 +18,11 @@ module Sequel
18
18
  end
19
19
  end
20
20
  end
21
+
22
+ (
23
+ # Error raised when there is a failed attempt to acquire an advisory lock.
24
+ AdvisoryLockError = Class.new(Error)
25
+ ).name
21
26
 
22
27
  (
23
28
  # Error raised when the adapter requested doesn't exist or can't be loaded.
@@ -32,8 +32,8 @@
32
32
  module Sequel
33
33
  module AnyNotEmpty
34
34
  # If a block is not given, return whether the dataset is not empty.
35
- def any?
36
- if defined?(yield)
35
+ def any?(*a)
36
+ if !a.empty? || defined?(yield)
37
37
  super
38
38
  else
39
39
  !empty?
@@ -176,6 +176,13 @@
176
176
  # +:preempt_async_thread+ Database option before loading the
177
177
  # async_thread_pool extension.
178
178
  #
179
+ # Note that the async_thread_pool extension creates the thread pool
180
+ # when it is loaded into the Database. If you fork after loading
181
+ # the extension, the extension will not work, as fork does not
182
+ # copy the thread pools. If you are using a forking webserver
183
+ # (or any other system that forks worker processes), load this
184
+ # extension in each child process, do not load it before forking.
185
+ #
179
186
  # Related module: Sequel::Database::AsyncThreadPool::DatasetMethods
180
187
 
181
188
 
@@ -0,0 +1,94 @@
1
+ # frozen-string-literal: true
2
+ #
3
+ # The auto_cast_date_and_time extension uses SQL standard type casting
4
+ # when literalizing date, time, and timestamp values:
5
+ #
6
+ # DB.literal(Time.now)
7
+ # # => "TIMESTAMP '...'"
8
+ #
9
+ # DB.literal(Date.today)
10
+ # # => "DATE '...'"
11
+ #
12
+ # DB.literal(Sequel::SQLTime.create(10, 20, 30))
13
+ # # => "TIME '10:20:30.000000'"
14
+ #
15
+ # The default behavior of Sequel on adapters that do not require the
16
+ # SQL standard behavior is to format the date or time value without:
17
+ # casting
18
+ #
19
+ # DB.literal(Sequel::SQLTime.create(10, 20, 30))
20
+ # # => "'10:20:30.000000'"
21
+ #
22
+ # However, then the database cannot determine the type of the string,
23
+ # and must perform some implicit casting. If implicit casting cannot
24
+ # be used, it will probably treat the value as a string:
25
+ #
26
+ # DB.get(Time.now).class
27
+ # # Without auto_cast_date_and_time: String
28
+ # # With auto_cast_date_and_time: Time
29
+ #
30
+ # Note that not all databases support this extension. PostgreSQL and
31
+ # MySQL support it, but SQLite and Microsoft SQL Server do not.
32
+ #
33
+ # You can load this extension into specific datasets:
34
+ #
35
+ # ds = DB[:table]
36
+ # ds = ds.extension(:auto_cast_date_and_time)
37
+ #
38
+ # Or you can load it into all of a database's datasets, which
39
+ # is probably the desired behavior if you are using this extension:
40
+ #
41
+ # DB.extension(:auto_cast_date_and_time)
42
+ #
43
+ # Related module: Sequel::AutoCastDateAndTime
44
+
45
+ #
46
+ module Sequel
47
+ module AutoCastDateAndTime
48
+ # :nocov:
49
+
50
+ # Mark the datasets as requiring sql standard date times. This is only needed
51
+ # for backwards compatibility.
52
+ def requires_sql_standard_datetimes?
53
+ # SEQUEL6: Remove
54
+ true
55
+ end
56
+ # :nocov:
57
+
58
+ private
59
+
60
+ # Explicitly cast SQLTime objects to TIME.
61
+ def literal_sqltime_append(sql, v)
62
+ sql << "TIME "
63
+ super
64
+ end
65
+
66
+ # Explicitly cast Time objects to TIMESTAMP.
67
+ def literal_time_append(sql, v)
68
+ sql << literal_datetime_timestamp_cast
69
+ super
70
+ end
71
+
72
+ # Explicitly cast DateTime objects to TIMESTAMP.
73
+ def literal_datetime_append(sql, v)
74
+ sql << literal_datetime_timestamp_cast
75
+ super
76
+ end
77
+
78
+ # Explicitly cast Date objects to DATE.
79
+ def literal_date_append(sql, v)
80
+ sql << "DATE "
81
+ super
82
+ end
83
+
84
+ # The default cast string to use for Time/DateTime objects.
85
+ # Respects existing method if already defined.
86
+ def literal_datetime_timestamp_cast
87
+ return super if defined?(super)
88
+ 'TIMESTAMP '
89
+ end
90
+ end
91
+
92
+ Dataset.register_extension(:auto_cast_date_and_time, AutoCastDateAndTime)
93
+ end
94
+
@@ -36,6 +36,7 @@ require 'rbconfig'
36
36
  module Sequel
37
37
  module CallerLogging
38
38
  SEQUEL_LIB_PATH = (File.expand_path('../../..', __FILE__) + '/').freeze
39
+ RUBY_STDLIB = RbConfig::CONFIG["rubylibdir"]
39
40
 
40
41
  # A regexp of caller lines to ignore, in addition to internal Sequel and Ruby code.
41
42
  attr_accessor :caller_logging_ignore
@@ -59,7 +60,7 @@ module Sequel
59
60
  ignore = caller_logging_ignore
60
61
  c = caller.find do |line|
61
62
  !(line.start_with?(SEQUEL_LIB_PATH) ||
62
- line.start_with?(RbConfig::CONFIG["rubylibdir"]) ||
63
+ line.start_with?(RUBY_STDLIB) ||
63
64
  (ignore && line =~ ignore))
64
65
  end
65
66
 
@@ -14,12 +14,12 @@
14
14
  #
15
15
  # ds = DB[:items].extension(:duplicate_columns_handler)
16
16
  #
17
- # A database option is introduced: :on_duplicate_columns. It accepts a Symbol
18
- # or any object that responds to :call.
17
+ # If the Database option :on_duplicate_columns is set, it configures how this
18
+ # extension works. The value should be # or any object that responds to :call.
19
19
  #
20
- # on_duplicate_columns: :raise
21
- # on_duplicate_columns: :warn
22
- # on_duplicate_columns: :ignore
20
+ # on_duplicate_columns: :raise # or 'raise'
21
+ # on_duplicate_columns: :warn # or 'warn'
22
+ # on_duplicate_columns: :ignore # or anything unrecognized
23
23
  # on_duplicate_columns: lambda{|columns| arbitrary_condition? ? :raise : :warn}
24
24
  #
25
25
  # You may also configure duplicate columns handling for a specific dataset:
@@ -30,9 +30,10 @@
30
30
  # ds.on_duplicate_columns{|columns| arbitrary_condition? ? :raise : :warn}
31
31
  # ds.on_duplicate_columns(lambda{|columns| arbitrary_condition? ? :raise : :warn})
32
32
  #
33
- # If :raise is specified, a Sequel::DuplicateColumnError is raised.
34
- # If :warn is specified, you will receive a warning via +warn+.
33
+ # If :raise or 'raise' is specified, a Sequel::DuplicateColumnError is raised.
34
+ # If :warn or 'warn' is specified, you will receive a warning via +warn+.
35
35
  # If a callable is specified, it will be called.
36
+ # For other values, duplicate columns are ignored (Sequel's default behavior)
36
37
  # If no on_duplicate_columns is specified, the default is :warn.
37
38
  #
38
39
  # Related module: Sequel::DuplicateColumnsHandler
@@ -64,9 +65,9 @@ module Sequel
64
65
  message = "#{caller(*CALLER_ARGS).first}: One or more duplicate columns present in #{cols.inspect}"
65
66
 
66
67
  case duplicate_columns_handler_type(cols)
67
- when :raise
68
+ when :raise, 'raise'
68
69
  raise DuplicateColumnError, message
69
- when :warn
70
+ when :warn, 'warn'
70
71
  warn message
71
72
  end
72
73
  end
@@ -56,7 +56,11 @@ module Sequel
56
56
 
57
57
  # Dump the index cache to the filename given in Marshal format.
58
58
  def dump_index_cache(file)
59
- File.open(file, 'wb'){|f| f.write(Marshal.dump(@indexes))}
59
+ indexes = {}
60
+ @indexes.sort.each do |k, v|
61
+ indexes[k] = v
62
+ end
63
+ File.open(file, 'wb'){|f| f.write(Marshal.dump(indexes))}
60
64
  nil
61
65
  end
62
66
 
@@ -159,6 +159,19 @@ module Sequel
159
159
  migration.up = block
160
160
  migration.down = MigrationReverser.new.reverse(&block)
161
161
  end
162
+
163
+ # Creates a revert migration. This is the same as creating
164
+ # the same block with +down+, but it also calls the block and attempts
165
+ # to create a +up+ block that will reverse the changes made by
166
+ # the block. This is designed to revert the changes in the
167
+ # provided block.
168
+ #
169
+ # There are no guarantees that this will work perfectly
170
+ # in all cases, but it works for some simple cases.
171
+ def revert(&block)
172
+ migration.down = block
173
+ migration.up = MigrationReverser.new.reverse(&block)
174
+ end
162
175
  end
163
176
 
164
177
  # Handles the reversing of reversible migrations. Basically records
@@ -270,6 +283,10 @@ module Sequel
270
283
  def rename_column(name, new_name)
271
284
  @actions << [:rename_column, new_name, name]
272
285
  end
286
+
287
+ def set_column_allow_null(name, allow_null=true)
288
+ @actions << [:set_column_allow_null, name, !allow_null]
289
+ end
273
290
  end
274
291
 
275
292
  # The preferred method for writing Sequel migrations, using a DSL:
@@ -386,6 +403,11 @@ module Sequel
386
403
  migrator_class(directory).new(db, directory, opts).is_current?
387
404
  end
388
405
 
406
+ # Lock ID to use for advisory locks when running migrations
407
+ # "sequel-migration".codepoints.reduce(:*) % (2**63)
408
+ MIGRATION_ADVISORY_LOCK_ID = 4966325471869609408
409
+ private_constant :MIGRATION_ADVISORY_LOCK_ID
410
+
389
411
  # Migrates the supplied database using the migration files in the specified directory. Options:
390
412
  # :allow_missing_migration_files :: Don't raise an error if there are missing migration files.
391
413
  # It is very risky to use this option, since it can result in
@@ -399,6 +421,8 @@ module Sequel
399
421
  # :table :: The table containing the schema version (default: :schema_info for integer migrations and
400
422
  # :schema_migrations for timestamped migrations).
401
423
  # :target :: The target version to which to migrate. If not given, migrates to the maximum version.
424
+ # :use_advisory_lock :: Use advisory locks in migrations (only use this if Sequel supports advisory
425
+ # locks for the database).
402
426
  #
403
427
  # Examples:
404
428
  # Sequel::Migrator.run(DB, "migrations")
@@ -406,7 +430,11 @@ module Sequel
406
430
  # Sequel::Migrator.run(DB, "app1/migrations", column: :app2_version)
407
431
  # Sequel::Migrator.run(DB, "app2/migrations", column: :app2_version, table: :schema_info2)
408
432
  def self.run(db, directory, opts=OPTS)
409
- migrator_class(directory).new(db, directory, opts).run
433
+ if opts[:use_advisory_lock]
434
+ db.with_advisory_lock(MIGRATION_ADVISORY_LOCK_ID){run(db, directory, opts.merge(:use_advisory_lock=>false))}
435
+ else
436
+ migrator_class(directory).new(db, directory, opts).run
437
+ end
410
438
  end
411
439
 
412
440
  # Choose the Migrator subclass to use. Uses the TimestampMigrator
@@ -478,11 +506,7 @@ module Sequel
478
506
  @use_transactions
479
507
  end
480
508
 
481
- if use_trans
482
- db.transaction(&block)
483
- else
484
- yield
485
- end
509
+ db.transaction(:skip_transaction=>use_trans == false, &block)
486
510
  end
487
511
 
488
512
  # Load the migration file, raising an exception if the file does not define
@@ -680,6 +704,13 @@ module Sequel
680
704
  @migration_tuples = get_migration_tuples
681
705
  end
682
706
 
707
+ # Apply the migration in the given file path. See Migrator.run for the
708
+ # available options. Additionally, this method supports the :direction
709
+ # option for whether to run the migration up (default) or down.
710
+ def self.run_single(db, path, opts=OPTS)
711
+ new(db, File.dirname(path), opts).run_single(path, opts[:direction] || :up)
712
+ end
713
+
683
714
  # The timestamp migrator is current if there are no migrations to apply
684
715
  # in either direction.
685
716
  def is_current?
@@ -689,20 +720,39 @@ module Sequel
689
720
  # Apply all migration tuples on the database
690
721
  def run
691
722
  migration_tuples.each do |m, f, direction|
692
- t = Time.now
693
- db.log_info("Begin applying migration #{f}, direction: #{direction}")
694
- checked_transaction(m) do
695
- m.apply(db, direction)
696
- fi = f.downcase
697
- direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
698
- end
699
- db.log_info("Finished applying migration #{f}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
723
+ apply_migration(m, f, direction)
700
724
  end
701
725
  nil
702
726
  end
703
727
 
728
+ # Apply single migration tuple at the given path with the given direction
729
+ # on the database.
730
+ def run_single(path, direction)
731
+ migration = load_migration_file(path)
732
+ file_name = File.basename(path)
733
+ already_applied = applied_migrations.include?(file_name.downcase)
734
+
735
+ return if direction == :up ? already_applied : !already_applied
736
+
737
+ apply_migration(migration, file_name, direction)
738
+ nil
739
+ end
740
+
704
741
  private
705
742
 
743
+ # Apply a single migration with the given filename in the given direction.
744
+ def apply_migration(migration, file_name, direction)
745
+ fi = file_name.downcase
746
+ t = Time.now
747
+
748
+ db.log_info("Begin applying migration #{file_name}, direction: #{direction}")
749
+ checked_transaction(migration) do
750
+ migration.apply(db, direction)
751
+ direction == :up ? ds.insert(column=>fi) : ds.where(column=>fi).delete
752
+ end
753
+ db.log_info("Finished applying migration #{file_name}, direction: #{direction}, took #{sprintf('%0.6f', Time.now - t)} seconds")
754
+ end
755
+
706
756
  # Convert the schema_info table to the new schema_migrations table format,
707
757
  # using the version of the schema_info table and the current migration files.
708
758
  def convert_from_schema_info
@@ -136,7 +136,7 @@ module Sequel
136
136
  v = output_timezone.utc_to_local(v.new_offset(0))
137
137
 
138
138
  # Force DateTime output instead of TZInfo::DateTimeWithOffset
139
- DateTime.jd(v.jd, v.hour, v.minute, v.second + v.sec_fraction, v.offset, v.start)
139
+ DateTime.civil(v.year, v.month, v.day, v.hour, v.minute, v.second + v.sec_fraction, v.offset, v.start)
140
140
  end
141
141
  # :nodoc:
142
142
  # :nocov:
@@ -233,6 +233,16 @@ module Sequel
233
233
  a
234
234
  when String
235
235
  bound_variable_array_string(a)
236
+ when Float
237
+ if a.infinite?
238
+ a > 0 ? '"Infinity"' : '"-Infinity"'
239
+ elsif a.nan?
240
+ '"NaN"'
241
+ else
242
+ literal(a)
243
+ end
244
+ when Time, Date
245
+ @default_dataset.literal_date_or_time(a)
236
246
  else
237
247
  if (s = bound_variable_arg(a, nil)).is_a?(String)
238
248
  bound_variable_array_string(s)