activerecord-spanner-adapter 0.3.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/CODEOWNERS +7 -0
- data/.github/sync-repo-settings.yaml +16 -0
- data/.github/workflows/acceptance-tests-on-emulator.yaml +45 -0
- data/.github/workflows/acceptance-tests-on-production.yaml +49 -0
- data/.github/workflows/ci.yaml +33 -0
- data/.github/workflows/nightly-acceptance-tests-on-emulator.yaml +52 -0
- data/.github/workflows/nightly-acceptance-tests-on-production.yaml +35 -0
- data/.github/workflows/nightly-unit-tests.yaml +40 -0
- data/.github/workflows/release-please-label.yml +25 -0
- data/.github/workflows/release-please.yml +39 -0
- data/.github/workflows/rubocop.yaml +31 -0
- data/.gitignore +67 -5
- data/.kokoro/populate-secrets.sh +77 -0
- data/.kokoro/release.cfg +33 -0
- data/.kokoro/release.sh +15 -0
- data/.kokoro/trampoline_v2.sh +489 -0
- data/.rubocop.yml +46 -0
- data/.toys/release.rb +18 -0
- data/.trampolinerc +48 -0
- data/.yardopts +11 -0
- data/CHANGELOG.md +55 -0
- data/CODE_OF_CONDUCT.md +40 -0
- data/CONTRIBUTING.md +79 -0
- data/Gemfile +9 -4
- data/LICENSE +6 -6
- data/README.md +66 -30
- data/Rakefile +79 -3
- data/SECURITY.md +7 -0
- data/acceptance/cases/associations/has_many_associations_test.rb +119 -0
- data/acceptance/cases/associations/has_many_through_associations_test.rb +63 -0
- data/acceptance/cases/associations/has_one_associations_test.rb +79 -0
- data/acceptance/cases/associations/has_one_through_associations_test.rb +98 -0
- data/acceptance/cases/interleaved_associations/has_many_associations_using_interleaved_test.rb +211 -0
- data/acceptance/cases/migration/change_schema_test.rb +433 -0
- data/acceptance/cases/migration/change_table_test.rb +115 -0
- data/acceptance/cases/migration/column_attributes_test.rb +122 -0
- data/acceptance/cases/migration/column_positioning_test.rb +48 -0
- data/acceptance/cases/migration/columns_test.rb +201 -0
- data/acceptance/cases/migration/command_recorder_test.rb +406 -0
- data/acceptance/cases/migration/create_join_table_test.rb +216 -0
- data/acceptance/cases/migration/ddl_batching_test.rb +80 -0
- data/acceptance/cases/migration/foreign_key_test.rb +297 -0
- data/acceptance/cases/migration/index_test.rb +211 -0
- data/acceptance/cases/migration/references_foreign_key_test.rb +259 -0
- data/acceptance/cases/migration/references_index_test.rb +135 -0
- data/acceptance/cases/migration/references_statements_test.rb +166 -0
- data/acceptance/cases/migration/rename_column_test.rb +96 -0
- data/acceptance/cases/models/calculation_query_test.rb +128 -0
- data/acceptance/cases/models/generated_column_test.rb +126 -0
- data/acceptance/cases/models/mutation_test.rb +122 -0
- data/acceptance/cases/models/query_test.rb +171 -0
- data/acceptance/cases/sessions/session_not_found_test.rb +121 -0
- data/acceptance/cases/transactions/optimistic_locking_test.rb +141 -0
- data/acceptance/cases/transactions/read_only_transactions_test.rb +130 -0
- data/acceptance/cases/transactions/read_write_transactions_test.rb +248 -0
- data/acceptance/cases/type/all_types_test.rb +172 -0
- data/acceptance/cases/type/binary_test.rb +59 -0
- data/acceptance/cases/type/boolean_test.rb +31 -0
- data/acceptance/cases/type/date_test.rb +32 -0
- data/acceptance/cases/type/date_time_test.rb +30 -0
- data/acceptance/cases/type/float_test.rb +27 -0
- data/acceptance/cases/type/integer_test.rb +44 -0
- data/acceptance/cases/type/json_test.rb +34 -0
- data/acceptance/cases/type/numeric_test.rb +27 -0
- data/acceptance/cases/type/string_test.rb +79 -0
- data/acceptance/cases/type/text_test.rb +30 -0
- data/acceptance/cases/type/time_test.rb +87 -0
- data/acceptance/models/account.rb +13 -0
- data/acceptance/models/address.rb +9 -0
- data/acceptance/models/album.rb +12 -0
- data/acceptance/models/all_types.rb +8 -0
- data/acceptance/models/author.rb +11 -0
- data/acceptance/models/club.rb +12 -0
- data/acceptance/models/comment.rb +9 -0
- data/acceptance/models/customer.rb +9 -0
- data/acceptance/models/department.rb +9 -0
- data/acceptance/models/firm.rb +10 -0
- data/acceptance/models/member.rb +13 -0
- data/acceptance/models/member_type.rb +9 -0
- data/acceptance/models/membership.rb +10 -0
- data/acceptance/models/organization.rb +9 -0
- data/acceptance/models/post.rb +10 -0
- data/acceptance/models/singer.rb +10 -0
- data/acceptance/models/track.rb +20 -0
- data/acceptance/models/transaction.rb +9 -0
- data/acceptance/schema/schema.rb +147 -0
- data/acceptance/test_helper.rb +261 -0
- data/activerecord-spanner-adapter.gemspec +32 -17
- data/assets/solidus-db.png +0 -0
- data/benchmarks/README.md +17 -0
- data/benchmarks/Rakefile +14 -0
- data/benchmarks/application.rb +308 -0
- data/benchmarks/config/database.yml +8 -0
- data/benchmarks/config/environment.rb +12 -0
- data/benchmarks/db/migrate/01_create_tables.rb +25 -0
- data/benchmarks/db/schema.rb +29 -0
- data/benchmarks/models/album.rb +9 -0
- data/benchmarks/models/singer.rb +9 -0
- data/bin/console +6 -7
- data/examples/rails/README.md +262 -0
- data/examples/snippets/README.md +29 -0
- data/examples/snippets/Rakefile +57 -0
- data/examples/snippets/array-data-type/README.md +45 -0
- data/examples/snippets/array-data-type/Rakefile +13 -0
- data/examples/snippets/array-data-type/application.rb +45 -0
- data/examples/snippets/array-data-type/config/database.yml +8 -0
- data/examples/snippets/array-data-type/db/migrate/01_create_tables.rb +24 -0
- data/examples/snippets/array-data-type/db/schema.rb +26 -0
- data/examples/snippets/array-data-type/db/seeds.rb +5 -0
- data/examples/snippets/array-data-type/models/entity_with_array_types.rb +18 -0
- data/examples/snippets/bin/create_emulator_instance.rb +18 -0
- data/examples/snippets/bulk-insert/README.md +21 -0
- data/examples/snippets/bulk-insert/Rakefile +13 -0
- data/examples/snippets/bulk-insert/application.rb +64 -0
- data/examples/snippets/bulk-insert/config/database.yml +8 -0
- data/examples/snippets/bulk-insert/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/bulk-insert/db/schema.rb +26 -0
- data/examples/snippets/bulk-insert/db/seeds.rb +5 -0
- data/examples/snippets/bulk-insert/models/album.rb +9 -0
- data/examples/snippets/bulk-insert/models/singer.rb +9 -0
- data/examples/snippets/commit-timestamp/README.md +18 -0
- data/examples/snippets/commit-timestamp/Rakefile +13 -0
- data/examples/snippets/commit-timestamp/application.rb +53 -0
- data/examples/snippets/commit-timestamp/config/database.yml +8 -0
- data/examples/snippets/commit-timestamp/db/migrate/01_create_tables.rb +26 -0
- data/examples/snippets/commit-timestamp/db/schema.rb +29 -0
- data/examples/snippets/commit-timestamp/db/seeds.rb +5 -0
- data/examples/snippets/commit-timestamp/models/album.rb +9 -0
- data/examples/snippets/commit-timestamp/models/singer.rb +9 -0
- data/examples/snippets/config/environment.rb +21 -0
- data/examples/snippets/create-records/README.md +12 -0
- data/examples/snippets/create-records/Rakefile +13 -0
- data/examples/snippets/create-records/application.rb +42 -0
- data/examples/snippets/create-records/config/database.yml +8 -0
- data/examples/snippets/create-records/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/create-records/db/schema.rb +26 -0
- data/examples/snippets/create-records/db/seeds.rb +5 -0
- data/examples/snippets/create-records/models/album.rb +9 -0
- data/examples/snippets/create-records/models/singer.rb +9 -0
- data/examples/snippets/date-data-type/README.md +19 -0
- data/examples/snippets/date-data-type/Rakefile +13 -0
- data/examples/snippets/date-data-type/application.rb +35 -0
- data/examples/snippets/date-data-type/config/database.yml +8 -0
- data/examples/snippets/date-data-type/db/migrate/01_create_tables.rb +20 -0
- data/examples/snippets/date-data-type/db/schema.rb +21 -0
- data/examples/snippets/date-data-type/db/seeds.rb +16 -0
- data/examples/snippets/date-data-type/models/singer.rb +8 -0
- data/examples/snippets/generated-column/README.md +41 -0
- data/examples/snippets/generated-column/Rakefile +13 -0
- data/examples/snippets/generated-column/application.rb +37 -0
- data/examples/snippets/generated-column/config/database.yml +8 -0
- data/examples/snippets/generated-column/db/migrate/01_create_tables.rb +23 -0
- data/examples/snippets/generated-column/db/schema.rb +21 -0
- data/examples/snippets/generated-column/db/seeds.rb +18 -0
- data/examples/snippets/generated-column/models/singer.rb +8 -0
- data/examples/snippets/hints/README.md +19 -0
- data/examples/snippets/hints/Rakefile +13 -0
- data/examples/snippets/hints/application.rb +47 -0
- data/examples/snippets/hints/config/database.yml +8 -0
- data/examples/snippets/hints/db/migrate/01_create_tables.rb +23 -0
- data/examples/snippets/hints/db/schema.rb +28 -0
- data/examples/snippets/hints/db/seeds.rb +29 -0
- data/examples/snippets/hints/models/album.rb +9 -0
- data/examples/snippets/hints/models/singer.rb +9 -0
- data/examples/snippets/migrations/README.md +43 -0
- data/examples/snippets/migrations/Rakefile +13 -0
- data/examples/snippets/migrations/application.rb +26 -0
- data/examples/snippets/migrations/config/database.yml +8 -0
- data/examples/snippets/migrations/db/migrate/01_create_tables.rb +28 -0
- data/examples/snippets/migrations/db/schema.rb +33 -0
- data/examples/snippets/migrations/db/seeds.rb +5 -0
- data/examples/snippets/migrations/models/album.rb +10 -0
- data/examples/snippets/migrations/models/singer.rb +10 -0
- data/examples/snippets/migrations/models/track.rb +9 -0
- data/examples/snippets/mutations/README.md +34 -0
- data/examples/snippets/mutations/Rakefile +13 -0
- data/examples/snippets/mutations/application.rb +47 -0
- data/examples/snippets/mutations/config/database.yml +8 -0
- data/examples/snippets/mutations/db/migrate/01_create_tables.rb +22 -0
- data/examples/snippets/mutations/db/schema.rb +27 -0
- data/examples/snippets/mutations/db/seeds.rb +25 -0
- data/examples/snippets/mutations/models/album.rb +9 -0
- data/examples/snippets/mutations/models/singer.rb +9 -0
- data/examples/snippets/optimistic-locking/README.md +12 -0
- data/examples/snippets/optimistic-locking/Rakefile +13 -0
- data/examples/snippets/optimistic-locking/application.rb +48 -0
- data/examples/snippets/optimistic-locking/config/database.yml +8 -0
- data/examples/snippets/optimistic-locking/db/migrate/01_create_tables.rb +26 -0
- data/examples/snippets/optimistic-locking/db/schema.rb +29 -0
- data/examples/snippets/optimistic-locking/db/seeds.rb +25 -0
- data/examples/snippets/optimistic-locking/models/album.rb +9 -0
- data/examples/snippets/optimistic-locking/models/singer.rb +9 -0
- data/examples/snippets/partitioned-dml/README.md +16 -0
- data/examples/snippets/partitioned-dml/Rakefile +13 -0
- data/examples/snippets/partitioned-dml/application.rb +48 -0
- data/examples/snippets/partitioned-dml/config/database.yml +8 -0
- data/examples/snippets/partitioned-dml/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/partitioned-dml/db/schema.rb +26 -0
- data/examples/snippets/partitioned-dml/db/seeds.rb +29 -0
- data/examples/snippets/partitioned-dml/models/album.rb +9 -0
- data/examples/snippets/partitioned-dml/models/singer.rb +9 -0
- data/examples/snippets/quickstart/README.md +26 -0
- data/examples/snippets/quickstart/Rakefile +13 -0
- data/examples/snippets/quickstart/application.rb +51 -0
- data/examples/snippets/quickstart/config/database.yml +8 -0
- data/examples/snippets/quickstart/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/quickstart/db/schema.rb +26 -0
- data/examples/snippets/quickstart/db/seeds.rb +24 -0
- data/examples/snippets/quickstart/models/album.rb +9 -0
- data/examples/snippets/quickstart/models/singer.rb +9 -0
- data/examples/snippets/read-only-transactions/README.md +13 -0
- data/examples/snippets/read-only-transactions/Rakefile +13 -0
- data/examples/snippets/read-only-transactions/application.rb +77 -0
- data/examples/snippets/read-only-transactions/config/database.yml +8 -0
- data/examples/snippets/read-only-transactions/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/read-only-transactions/db/schema.rb +26 -0
- data/examples/snippets/read-only-transactions/db/seeds.rb +24 -0
- data/examples/snippets/read-only-transactions/models/album.rb +9 -0
- data/examples/snippets/read-only-transactions/models/singer.rb +9 -0
- data/examples/snippets/read-write-transactions/README.md +12 -0
- data/examples/snippets/read-write-transactions/Rakefile +13 -0
- data/examples/snippets/read-write-transactions/application.rb +39 -0
- data/examples/snippets/read-write-transactions/config/database.yml +8 -0
- data/examples/snippets/read-write-transactions/db/migrate/01_create_tables.rb +22 -0
- data/examples/snippets/read-write-transactions/db/schema.rb +27 -0
- data/examples/snippets/read-write-transactions/db/seeds.rb +25 -0
- data/examples/snippets/read-write-transactions/models/album.rb +9 -0
- data/examples/snippets/read-write-transactions/models/singer.rb +9 -0
- data/examples/snippets/stale-reads/README.md +27 -0
- data/examples/snippets/stale-reads/Rakefile +13 -0
- data/examples/snippets/stale-reads/application.rb +63 -0
- data/examples/snippets/stale-reads/config/database.yml +8 -0
- data/examples/snippets/stale-reads/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/stale-reads/db/schema.rb +26 -0
- data/examples/snippets/stale-reads/db/seeds.rb +24 -0
- data/examples/snippets/stale-reads/models/album.rb +9 -0
- data/examples/snippets/stale-reads/models/singer.rb +9 -0
- data/examples/snippets/timestamp-data-type/README.md +17 -0
- data/examples/snippets/timestamp-data-type/Rakefile +13 -0
- data/examples/snippets/timestamp-data-type/application.rb +42 -0
- data/examples/snippets/timestamp-data-type/config/database.yml +8 -0
- data/examples/snippets/timestamp-data-type/db/migrate/01_create_tables.rb +21 -0
- data/examples/snippets/timestamp-data-type/db/schema.rb +21 -0
- data/examples/snippets/timestamp-data-type/db/seeds.rb +6 -0
- data/examples/snippets/timestamp-data-type/models/meeting.rb +19 -0
- data/examples/solidus/README.md +172 -0
- data/lib/active_record/connection_adapters/spanner/database_statements.rb +244 -266
- data/lib/active_record/connection_adapters/spanner/quoting.rb +42 -50
- data/lib/active_record/connection_adapters/spanner/schema_cache.rb +43 -0
- data/lib/active_record/connection_adapters/spanner/schema_creation.rb +125 -9
- data/lib/active_record/connection_adapters/spanner/schema_definitions.rb +122 -0
- data/lib/active_record/connection_adapters/spanner/schema_dumper.rb +19 -0
- data/lib/active_record/connection_adapters/spanner/schema_statements.rb +553 -139
- data/lib/active_record/connection_adapters/spanner/type_metadata.rb +37 -0
- data/lib/active_record/connection_adapters/spanner_adapter.rb +185 -78
- data/lib/active_record/tasks/spanner_database_tasks.rb +74 -0
- data/lib/active_record/type/spanner/array.rb +32 -0
- data/lib/active_record/type/spanner/bytes.rb +26 -0
- data/lib/active_record/type/spanner/spanner_active_record_converter.rb +33 -0
- data/lib/active_record/type/spanner/time.rb +37 -0
- data/lib/activerecord-spanner-adapter.rb +23 -0
- data/lib/activerecord_spanner_adapter/base.rb +238 -0
- data/lib/activerecord_spanner_adapter/connection.rb +350 -0
- data/lib/activerecord_spanner_adapter/errors.rb +13 -0
- data/lib/activerecord_spanner_adapter/foreign_key.rb +29 -0
- data/lib/activerecord_spanner_adapter/index/column.rb +38 -0
- data/lib/activerecord_spanner_adapter/index.rb +80 -0
- data/lib/activerecord_spanner_adapter/information_schema.rb +262 -0
- data/lib/activerecord_spanner_adapter/primary_key.rb +31 -0
- data/lib/activerecord_spanner_adapter/table/column.rb +59 -0
- data/lib/activerecord_spanner_adapter/table.rb +61 -0
- data/lib/activerecord_spanner_adapter/transaction.rb +154 -0
- data/lib/activerecord_spanner_adapter/version.rb +9 -0
- data/lib/arel/visitors/spanner.rb +111 -0
- data/lib/spanner_client_ext.rb +107 -0
- data/renovate.json +5 -0
- metadata +405 -34
- data/.travis.yml +0 -5
- data/lib/active_record/connection_adapters/spanner/client.rb +0 -190
- data/lib/active_record/connection_adapters/spanner.rb +0 -10
- data/lib/activerecord-spanner-adapter/version.rb +0 -3
@@ -0,0 +1,111 @@
|
|
1
|
+
# Copyright 2020 Google LLC
|
2
|
+
#
|
3
|
+
# Use of this source code is governed by an MIT-style
|
4
|
+
# license that can be found in the LICENSE file or at
|
5
|
+
# https://opensource.org/licenses/MIT.
|
6
|
+
|
7
|
+
module Arel # :nodoc: all
|
8
|
+
module Visitors
|
9
|
+
class StalenessHint
|
10
|
+
attr_reader :value
|
11
|
+
|
12
|
+
def initialize value
|
13
|
+
@value = value
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class StatementHint
|
18
|
+
attr_reader :value
|
19
|
+
|
20
|
+
def initialize value
|
21
|
+
@value = value
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Spanner < Arel::Visitors::ToSql
|
26
|
+
def compile node, collector = Arel::Collectors::SQLString.new
|
27
|
+
collector.class.module_eval { attr_accessor :hints }
|
28
|
+
collector.class.module_eval { attr_accessor :table_hints }
|
29
|
+
collector.class.module_eval { attr_accessor :join_hints }
|
30
|
+
collector.hints = {}
|
31
|
+
collector.table_hints = {}
|
32
|
+
collector.join_hints = {}
|
33
|
+
sql, binds = accept(node, collector).value
|
34
|
+
sql = collector.hints[:statement_hint].value + sql if collector.hints[:statement_hint]
|
35
|
+
binds << collector.hints[:staleness] if collector.hints[:staleness]
|
36
|
+
[sql, binds]
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
BIND_BLOCK = proc { |i| "@p#{i}" }
|
42
|
+
private_constant :BIND_BLOCK
|
43
|
+
|
44
|
+
def bind_block
|
45
|
+
BIND_BLOCK
|
46
|
+
end
|
47
|
+
|
48
|
+
def visit_table_hint v, collector
|
49
|
+
value = v.delete_prefix("table_hint:").strip
|
50
|
+
# TODO: This does not support FORCE_INDEX hints that reference an index that contains '@{' in the name.
|
51
|
+
start_of_hint_index = value.rindex "@{"
|
52
|
+
table_name = value[0, start_of_hint_index]
|
53
|
+
table_hint = value[start_of_hint_index, value.length]
|
54
|
+
collector.table_hints[table_name] = table_hint if table_name && table_hint
|
55
|
+
end
|
56
|
+
|
57
|
+
def visit_statement_hint v, collector
|
58
|
+
collector.hints[:statement_hint] = \
|
59
|
+
StatementHint.new v.delete_prefix("statement_hint:")
|
60
|
+
end
|
61
|
+
|
62
|
+
# rubocop:disable Naming/MethodName
|
63
|
+
def visit_Arel_Nodes_OptimizerHints o, collector
|
64
|
+
o.expr.each do |v|
|
65
|
+
visit_table_hint v, collector if v.start_with? "table_hint:"
|
66
|
+
visit_statement_hint v, collector if v.start_with? "statement_hint:"
|
67
|
+
if v.start_with? "max_staleness:"
|
68
|
+
collector.hints[:staleness] = \
|
69
|
+
StalenessHint.new max_staleness: v.delete_prefix("max_staleness:").to_f
|
70
|
+
next
|
71
|
+
end
|
72
|
+
if v.start_with? "exact_staleness:"
|
73
|
+
collector.hints[:staleness] = \
|
74
|
+
StalenessHint.new exact_staleness: v.delete_prefix("exact_staleness:").to_f
|
75
|
+
next
|
76
|
+
end
|
77
|
+
if v.start_with? "min_read_timestamp:"
|
78
|
+
time = Time.xmlschema v.delete_prefix("min_read_timestamp:")
|
79
|
+
collector.hints[:staleness] = \
|
80
|
+
StalenessHint.new min_read_timestamp: time
|
81
|
+
next
|
82
|
+
end
|
83
|
+
next unless v.start_with? "read_timestamp:"
|
84
|
+
time = Time.xmlschema v.delete_prefix("read_timestamp:")
|
85
|
+
collector.hints[:staleness] = \
|
86
|
+
StalenessHint.new read_timestamp: time
|
87
|
+
end
|
88
|
+
collector
|
89
|
+
end
|
90
|
+
|
91
|
+
def visit_Arel_Table o, collector
|
92
|
+
return super unless collector.table_hints[o.name]
|
93
|
+
if o.table_alias
|
94
|
+
collector << quote_table_name(o.name) << collector.table_hints[o.name] \
|
95
|
+
<< " " << quote_table_name(o.table_alias)
|
96
|
+
else
|
97
|
+
collector << quote_table_name(o.name) << collector.table_hints[o.name]
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def visit_Arel_Nodes_BindParam o, collector
|
102
|
+
# Do not generate a query parameter if the value should be set to the PENDING_COMMIT_TIMESTAMP(), as that is
|
103
|
+
# not supported as a parameter value by Cloud Spanner.
|
104
|
+
return collector << "PENDING_COMMIT_TIMESTAMP()" \
|
105
|
+
if o.value.type.is_a?(ActiveRecord::Type::Spanner::Time) && o.value.value == :commit_timestamp
|
106
|
+
collector.add_bind(o.value, &bind_block)
|
107
|
+
end
|
108
|
+
# rubocop:enable Naming/MethodName
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Copyright 2020 Google LLC
|
2
|
+
#
|
3
|
+
# Use of this source code is governed by an MIT-style
|
4
|
+
# license that can be found in the LICENSE file or at
|
5
|
+
# https://opensource.org/licenses/MIT.
|
6
|
+
|
7
|
+
|
8
|
+
module Google
|
9
|
+
module Cloud
|
10
|
+
module Spanner
|
11
|
+
class Project
|
12
|
+
def create_session instance_id, database_id, labels: nil
|
13
|
+
ensure_service!
|
14
|
+
|
15
|
+
grpc = @service.create_session(
|
16
|
+
Admin::Database::V1::DatabaseAdmin::Paths.database_path(
|
17
|
+
project: project, instance: instance_id, database: database_id
|
18
|
+
),
|
19
|
+
labels: labels
|
20
|
+
)
|
21
|
+
Session.from_grpc grpc, @service
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Session
|
26
|
+
def commit_transaction transaction, mutations = []
|
27
|
+
ensure_service!
|
28
|
+
|
29
|
+
resp = service.commit(
|
30
|
+
path,
|
31
|
+
mutations,
|
32
|
+
transaction_id: transaction.transaction_id
|
33
|
+
)
|
34
|
+
@last_updated_at = Time.now
|
35
|
+
Convert.timestamp_to_time resp.commit_timestamp
|
36
|
+
end
|
37
|
+
|
38
|
+
# Create a single-use transaction selector.
|
39
|
+
def self.single_use_transaction opts
|
40
|
+
return nil if opts.nil? || opts.empty?
|
41
|
+
|
42
|
+
exact_timestamp = Convert.time_to_timestamp opts[:read_timestamp]
|
43
|
+
exact_staleness = Convert.number_to_duration opts[:exact_staleness]
|
44
|
+
min_read_timestamp = Convert.time_to_timestamp opts[:min_read_timestamp]
|
45
|
+
max_staleness = Convert.number_to_duration opts[:max_staleness]
|
46
|
+
|
47
|
+
V1::TransactionSelector.new(single_use:
|
48
|
+
V1::TransactionOptions.new(read_only:
|
49
|
+
V1::TransactionOptions::ReadOnly.new({
|
50
|
+
strong: opts[:strong],
|
51
|
+
read_timestamp: exact_timestamp,
|
52
|
+
exact_staleness: exact_staleness,
|
53
|
+
min_read_timestamp: min_read_timestamp,
|
54
|
+
max_staleness: max_staleness,
|
55
|
+
return_read_timestamp: true
|
56
|
+
}.delete_if { |_, v| v.nil? })))
|
57
|
+
end
|
58
|
+
|
59
|
+
def create_snapshot strong: nil,
|
60
|
+
timestamp: nil, read_timestamp: nil,
|
61
|
+
staleness: nil, exact_staleness: nil
|
62
|
+
validate_snapshot_args! strong: strong, timestamp: timestamp,
|
63
|
+
read_timestamp: read_timestamp,
|
64
|
+
staleness: staleness,
|
65
|
+
exact_staleness: exact_staleness
|
66
|
+
ensure_service!
|
67
|
+
|
68
|
+
snp_grpc = service.create_snapshot \
|
69
|
+
path, timestamp: (timestamp || read_timestamp),
|
70
|
+
staleness: (staleness || exact_staleness)
|
71
|
+
Snapshot.from_grpc snp_grpc, self
|
72
|
+
end
|
73
|
+
|
74
|
+
def create_pdml
|
75
|
+
ensure_service!
|
76
|
+
pdml_grpc = service.create_pdml path
|
77
|
+
Transaction.from_grpc pdml_grpc, self
|
78
|
+
end
|
79
|
+
|
80
|
+
private
|
81
|
+
|
82
|
+
##
|
83
|
+
# Check for valid snapshot arguments
|
84
|
+
def validate_snapshot_args! strong: nil,
|
85
|
+
timestamp: nil, read_timestamp: nil,
|
86
|
+
staleness: nil, exact_staleness: nil
|
87
|
+
valid_args_count = [
|
88
|
+
strong, timestamp, read_timestamp, staleness, exact_staleness
|
89
|
+
].compact.count
|
90
|
+
return true if valid_args_count <= 1
|
91
|
+
raise ArgumentError,
|
92
|
+
"Can only provide one of the following arguments: " \
|
93
|
+
"(strong, timestamp, read_timestamp, staleness, " \
|
94
|
+
"exact_staleness)"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
class Results
|
99
|
+
attr_reader :metadata
|
100
|
+
end
|
101
|
+
|
102
|
+
class Transaction
|
103
|
+
attr_accessor :seqno, :commit
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|