activerecord-spanner-adapter 1.4.2 → 1.4.3

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cfd752e3287084b088f8d8e7ba09d2644ec8bb3c5f699b80425ed7babc74ac1f
4
- data.tar.gz: 2c72dad4930f696935f5a8ebe61db261da7ac11d3f8149b22f7bbe0bae900755
3
+ metadata.gz: ecc586f0beee665e45413d64af184d69ed9814b17465ab68be04767c3312d69a
4
+ data.tar.gz: 061c3c4b433b673ab848b8f5e67652a0e3f991b99fdae1ed7fdc118e622e3139
5
5
  SHA512:
6
- metadata.gz: 77e3afc2326448c8e7de3e838baf09d87beb631c28129c2588e14ae675cd20ea0bf4c18e89d2e1ec456fb519530dd9d968838fcfc2cab32d5dc66dd5fb586025
7
- data.tar.gz: e4525d57eab407c0122a26ec412533e29e3ef51e942577a0686e36d22ddaf1a0243d30cc46ee350386b4ba292c9dc4a81dd7215e0beba5fc0eb08577f89a5ed1
6
+ metadata.gz: 6c69f17a06d188ca69fe942756ded71f245a0398d67fa0c1e0bb915cc03a8cd51147851edcf352697670b3999539b178c1d1542d30659ef6110b096faa3c36d9
7
+ data.tar.gz: 339ae0975ff7ac256cba1d98b68379a47de1d7187f82e2ebfdd83ad0c940c3ca3af52c4bc5e30f334ba63e48d001baebe373e9a121f4593ad09daf941f7e3e22
@@ -1,3 +1,3 @@
1
1
  {
2
- ".": "1.4.2"
2
+ ".": "1.4.3"
3
3
  }
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ### 1.4.3 (2023-06-09)
4
+
5
+ #### Bug Fixes
6
+
7
+ * unquote string default value ([#253](https://github.com/googleapis/ruby-spanner-activerecord/issues/253))
8
+
3
9
  ### 1.4.2 (2023-06-01)
4
10
 
5
11
  #### Bug Fixes
@@ -7,15 +7,47 @@ module Models
7
7
  class DefaultValueTest < SpannerAdapter::TestCase
8
8
  include TestHelpers::WithSeparateDatabase
9
9
 
10
- class DynamicItem < ActiveRecord::Base; end
10
+ class LiteralValue < ActiveRecord::Base; end
11
+ class ExpressionValue < ActiveRecord::Base; end
11
12
 
12
- def test_dynamic_default_values
13
- connection.create_table :dynamic_items do |t|
13
+ def test_literal_default_values
14
+ default = OpenStruct.new(
15
+ col_string: "default",
16
+ col_int64: 123,
17
+ col_float64: 1.23,
18
+ col_numeric: BigDecimal("1.23"),
19
+ col_bool: true,
20
+ col_date: Date.new(2023, 5, 9),
21
+ col_timestamp: DateTime.new(2023, 5, 9, 1, 2, 3),
22
+ )
23
+
24
+ connection.create_table :literal_values do |t|
25
+ t.column :col_string, :string, default: default.col_string
26
+ t.column :col_int64, :bigint, default: default.col_int64
27
+ t.column :col_float64, :float, default: default.col_float64
28
+ t.column :col_numeric, :numeric, default: default.col_numeric
29
+ t.column :col_bool, :boolean, default: default.col_bool
30
+ t.column :col_date, :date, default: default.col_date
31
+ t.column :col_timestamp, :datetime, default: default.col_timestamp
32
+ end
33
+
34
+ item = LiteralValue.new
35
+ default.each_pair { |col, expected| assert_equal(expected, item[col]) }
36
+ item.save!
37
+ default.each_pair { |col, expected| assert_equal(expected, item[col]) }
38
+ item.reload
39
+ default.each_pair { |col, expected| assert_equal(expected, item[col]) }
40
+ end
41
+
42
+ def test_expression_default_values
43
+ connection.create_table :expression_values do |t|
44
+ t.column :col_numeric, :numeric, default: -> { "NUMERIC '1.23'" }
14
45
  t.column :col_timestamp, :datetime, default: -> { "CURRENT_TIMESTAMP()" }
15
46
  end
16
47
 
17
- item = DynamicItem.create!
48
+ item = ExpressionValue.create!
18
49
  item.reload
50
+ assert_equal(BigDecimal("1.23"), item.col_numeric)
19
51
  assert(item.col_timestamp)
20
52
  end
21
53
  end
@@ -87,6 +87,10 @@ module ActiveRecordSpannerAdapter
87
87
  default = nil
88
88
  end
89
89
 
90
+ if default && type == "STRING"
91
+ default = unquote_string default
92
+ end
93
+
90
94
  Table::Column.new \
91
95
  table_name,
92
96
  column_name,
@@ -286,8 +290,70 @@ module ActiveRecordSpannerAdapter
286
290
  [matched[1], limit]
287
291
  end
288
292
 
293
+ def unquote_string value
294
+ return unquote_raw_string value, 1 if value[0] == "r" || value[0] == "R"
295
+ unescape_string unquote_raw_string value
296
+ end
297
+
289
298
  private
290
299
 
300
+ def unquote_raw_string value, prefix_length = 0
301
+ triple_quote_range = prefix_length..(prefix_length + 2)
302
+ if value[triple_quote_range] == '"""' || value[triple_quote_range] == "'''"
303
+ value[(prefix_length + 3)...-3]
304
+ else
305
+ value[(prefix_length + 1)...-1]
306
+ end
307
+ end
308
+
309
+ def unescape_string value # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity
310
+ str = ""
311
+ i = 0
312
+
313
+ while i < value.length
314
+ case value[i]
315
+ when "\\"
316
+ i += 1
317
+ case value[i]
318
+ when "a" then str += "\a"
319
+ when "b" then str += "\b"
320
+ when "f" then str += "\f"
321
+ when "n" then str += "\n"
322
+ when "r" then str += "\r"
323
+ when "t" then str += "\t"
324
+ when "v" then str += "\v"
325
+ when "\\" then str += "\\"
326
+ when "?" then str += "?"
327
+ when "'" then str += "'"
328
+ when '"' then str += '"'
329
+ when "`" then str += "`"
330
+ when "0".."7"
331
+ str += unescape_unicode value, i, 3, 8
332
+ i += 2
333
+ when "x", "X"
334
+ str += unescape_unicode value, i + 1, 2, 16
335
+ i += 2
336
+ when "u"
337
+ str += unescape_unicode value, i + 1, 4, 16
338
+ i += 4
339
+ when "U"
340
+ str += unescape_unicode value, i + 1, 8, 16
341
+ i += 8
342
+ end
343
+ else
344
+ str += value[i]
345
+ end
346
+
347
+ i += 1
348
+ end
349
+
350
+ str
351
+ end
352
+
353
+ def unescape_unicode value, start, length, base
354
+ [value[start...(start + length)].to_i(base)].pack "U"
355
+ end
356
+
291
357
  def column_options table_name, column_name
292
358
  sql = +"SELECT COLUMN_NAME, OPTION_NAME, OPTION_TYPE, OPTION_VALUE"
293
359
  sql << " FROM INFORMATION_SCHEMA.COLUMN_OPTIONS"
@@ -5,5 +5,5 @@
5
5
  # https://opensource.org/licenses/MIT.
6
6
 
7
7
  module ActiveRecordSpannerAdapter
8
- VERSION = "1.4.2".freeze
8
+ VERSION = "1.4.3".freeze
9
9
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-spanner-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.2
4
+ version: 1.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Google LLC
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-06-01 00:00:00.000000000 Z
11
+ date: 2023-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: google-cloud-spanner