dynamoid 3.14.0 → 3.15.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +34 -0
- data/README.md +82 -53
- data/lib/dynamoid/adapter_plugin/aws_sdk_v3/item_updater.rb +1 -2
- data/lib/dynamoid/adapter_plugin/aws_sdk_v3.rb +2 -69
- data/lib/dynamoid/config.rb +10 -1
- data/lib/dynamoid/criteria/chain.rb +56 -12
- data/lib/dynamoid/criteria.rb +1 -0
- data/lib/dynamoid/dirty.rb +10 -14
- data/lib/dynamoid/document.rb +66 -2
- data/lib/dynamoid/dumping.rb +7 -5
- data/lib/dynamoid/fields/declare.rb +6 -2
- data/lib/dynamoid/fields.rb +69 -49
- data/lib/dynamoid/finders.rb +44 -0
- data/lib/dynamoid/identity_map.rb +3 -2
- data/lib/dynamoid/indexes.rb +50 -37
- data/lib/dynamoid/persistence/import.rb +15 -1
- data/lib/dynamoid/persistence/inc.rb +3 -5
- data/lib/dynamoid/persistence/save.rb +9 -3
- data/lib/dynamoid/persistence/update_fields.rb +1 -1
- data/lib/dynamoid/persistence/upsert.rb +1 -1
- data/lib/dynamoid/persistence.rb +106 -47
- data/lib/dynamoid/primary_key_type_mapping.rb +8 -1
- data/lib/dynamoid/test_helper.rb +62 -0
- data/lib/dynamoid/transactions/mutation/builders/put_request_builder.rb +37 -0
- data/lib/dynamoid/transactions/mutation/import.rb +9 -7
- data/lib/dynamoid/transactions/mutation/increment.rb +15 -7
- data/lib/dynamoid/transactions/mutation/save.rb +11 -22
- data/lib/dynamoid/transactions/mutation.rb +9 -1
- data/lib/dynamoid/type_casting.rb +3 -5
- data/lib/dynamoid/undumping.rb +41 -24
- data/lib/dynamoid/version.rb +1 -1
- data/lib/dynamoid.rb +3 -1
- metadata +6 -4
|
@@ -30,7 +30,7 @@ module Dynamoid
|
|
|
30
30
|
|
|
31
31
|
raw_attributes = update_item
|
|
32
32
|
@model_class.new(undump_attributes(raw_attributes))
|
|
33
|
-
rescue Dynamoid::Errors::ConditionalCheckFailedException
|
|
33
|
+
rescue Dynamoid::Errors::ConditionalCheckFailedException # rubocop:disable Lint/SuppressedException
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
private
|
|
@@ -28,7 +28,7 @@ module Dynamoid
|
|
|
28
28
|
|
|
29
29
|
raw_attributes = update_item
|
|
30
30
|
@model_class.new(undump_attributes(raw_attributes))
|
|
31
|
-
rescue Dynamoid::Errors::ConditionalCheckFailedException
|
|
31
|
+
rescue Dynamoid::Errors::ConditionalCheckFailedException # rubocop:disable Lint/SuppressedException
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
private
|
data/lib/dynamoid/persistence.rb
CHANGED
|
@@ -12,7 +12,6 @@ require 'dynamoid/persistence/inc'
|
|
|
12
12
|
require 'dynamoid/persistence/update_validations'
|
|
13
13
|
require 'dynamoid/persistence/item_updater_with_dumping'
|
|
14
14
|
|
|
15
|
-
# encoding: utf-8
|
|
16
15
|
module Dynamoid
|
|
17
16
|
# Persistence is responsible for dumping objects to and marshalling objects from the data store. It tries to reserialize
|
|
18
17
|
# values to be of the same type as when they were passed in, based on the fields in the class.
|
|
@@ -70,21 +69,22 @@ module Dynamoid
|
|
|
70
69
|
#
|
|
71
70
|
# User.create_table(table_name: 'users', read_capacity: 200, write_capacity: 40)
|
|
72
71
|
#
|
|
73
|
-
# Dynamoid creates a table
|
|
74
|
-
#
|
|
75
|
-
# table status changes to +ACTIVE+ and a table becomes available. That's
|
|
76
|
-
# why Dynamoid is polling a table status and returns results only when a
|
|
77
|
-
# table becomes available.
|
|
72
|
+
# Dynamoid creates a table asynchronously by default, returning +self+
|
|
73
|
+
# immediately without waiting for the table to become available.
|
|
78
74
|
#
|
|
79
|
-
#
|
|
80
|
-
#
|
|
81
|
-
#
|
|
82
|
-
# Dynamoid stops polling and returns +true+.
|
|
75
|
+
# To wait until a table is created and available, pass +sync: true+:
|
|
76
|
+
#
|
|
77
|
+
# User.create_table(sync: true)
|
|
83
78
|
#
|
|
84
|
-
#
|
|
85
|
-
#
|
|
79
|
+
# When +sync: true+ is specified, Dynamoid polls the table status until it
|
|
80
|
+
# becomes available. Tables with Global Secondary Indexes (GSIs) always
|
|
81
|
+
# poll synchronously; tables with Local Secondary Indexes (LSIs) default to
|
|
82
|
+
# +sync: true+ unless +sync: false+ is passed explicitly.
|
|
86
83
|
#
|
|
87
|
-
#
|
|
84
|
+
# Polling is configured with +Dynamoid::Config.sync_retry_max_times+ and
|
|
85
|
+
# +Dynamoid::Config.sync_retry_wait_seconds+ configuration options. If the
|
|
86
|
+
# retry limit is reached while the table is still creating, Dynamoid stops
|
|
87
|
+
# polling and returns without raising an exception.
|
|
88
88
|
#
|
|
89
89
|
# Subsequent method calls for the same table will be ignored.
|
|
90
90
|
#
|
|
@@ -101,7 +101,8 @@ module Dynamoid
|
|
|
101
101
|
# @option options [Hash] :global_secondary_indexes
|
|
102
102
|
# @option options [true|false] :sync specifies should the method call be synchronous and wait until a table is completely created
|
|
103
103
|
#
|
|
104
|
-
# @
|
|
104
|
+
# @raise [Dynamoid::Errors::UnsupportedKeyType] if the partition key or sort key field uses a non-scalar type not supported by DynamoDB keys
|
|
105
|
+
# @return [Class] self
|
|
105
106
|
# @since 0.4.0
|
|
106
107
|
def create_table(options = {})
|
|
107
108
|
range_key_hash = if range_key
|
|
@@ -465,7 +466,7 @@ module Dynamoid
|
|
|
465
466
|
# @return [Model class] self
|
|
466
467
|
def inc(hash_key_value, range_key_value = nil, counters)
|
|
467
468
|
# It's similar to Rails' #update_counters.
|
|
468
|
-
Inc.call(self, hash_key_value, range_key_value, counters)
|
|
469
|
+
Inc.call(self, hash_key_value, range_key_value, **counters)
|
|
469
470
|
self
|
|
470
471
|
end
|
|
471
472
|
|
|
@@ -550,6 +551,46 @@ module Dynamoid
|
|
|
550
551
|
|
|
551
552
|
nil
|
|
552
553
|
end
|
|
554
|
+
|
|
555
|
+
# Execute a modifying PartiQL statement (+INSERT+, +UPDATE+, or +DELETE+).
|
|
556
|
+
#
|
|
557
|
+
# Executes the given PartiQL statement directly against DynamoDB and
|
|
558
|
+
# returns +nil+.
|
|
559
|
+
#
|
|
560
|
+
# Modifications bypass Dynamoid's model lifecycle: validations, callbacks,
|
|
561
|
+
# dirty tracking, automatic timestamps, and optimistic locking are
|
|
562
|
+
# skipped. Table names in the statement must match the underlying DynamoDB
|
|
563
|
+
# table name (such as +User.table_name+). Bind parameter values are passed
|
|
564
|
+
# directly to DynamoDB without type casting or conversion to DynamoDB's
|
|
565
|
+
# underlying storage format (for example, +:datetime+ and +:date+ values
|
|
566
|
+
# must be passed in their stored representation, such as numeric Unix
|
|
567
|
+
# epoch timestamps or ISO 8601 strings).
|
|
568
|
+
#
|
|
569
|
+
# @param statement [String] PartiQL statement
|
|
570
|
+
# @param parameters [Array] a list of bind parameters (optional)
|
|
571
|
+
# @return nil
|
|
572
|
+
#
|
|
573
|
+
# @example Insert an item
|
|
574
|
+
# User.execute_partiql(
|
|
575
|
+
# "INSERT INTO \"#{User.table_name}\" VALUE { 'id': ?, 'name': ?, 'age': ? }",
|
|
576
|
+
# ['101', 'Alice', 28]
|
|
577
|
+
# )
|
|
578
|
+
#
|
|
579
|
+
# @example Update an item
|
|
580
|
+
# User.execute_partiql(
|
|
581
|
+
# "UPDATE \"#{User.table_name}\" SET age = ? WHERE id = ?",
|
|
582
|
+
# [29, '101']
|
|
583
|
+
# )
|
|
584
|
+
#
|
|
585
|
+
# @example Delete an item
|
|
586
|
+
# User.execute_partiql(
|
|
587
|
+
# "DELETE FROM \"#{User.table_name}\" WHERE id = ?",
|
|
588
|
+
# ['101']
|
|
589
|
+
# )
|
|
590
|
+
def execute_partiql(statement, parameters = [])
|
|
591
|
+
Dynamoid.adapter.execute(statement, parameters)
|
|
592
|
+
nil
|
|
593
|
+
end
|
|
553
594
|
end
|
|
554
595
|
|
|
555
596
|
# Update document timestamps.
|
|
@@ -861,9 +902,9 @@ module Dynamoid
|
|
|
861
902
|
# collections if attribute is a set.
|
|
862
903
|
#
|
|
863
904
|
# ```
|
|
864
|
-
# user.update! do |
|
|
865
|
-
#
|
|
866
|
-
#
|
|
905
|
+
# user.update! do |u|
|
|
906
|
+
# u.add(age: 1, followers_count: 5)
|
|
907
|
+
# u.add(hobbies: ['skying', 'climbing'])
|
|
867
908
|
# end
|
|
868
909
|
# ```
|
|
869
910
|
#
|
|
@@ -871,16 +912,16 @@ module Dynamoid
|
|
|
871
912
|
# substructs one collection from another.
|
|
872
913
|
#
|
|
873
914
|
# ```
|
|
874
|
-
# user.update! do |
|
|
875
|
-
#
|
|
915
|
+
# user.update! do |u|
|
|
916
|
+
# u.delete(hobbies: ['skying'])
|
|
876
917
|
# end
|
|
877
918
|
# ```
|
|
878
919
|
#
|
|
879
920
|
# Operation +set+ just changes an attribute value:
|
|
880
921
|
#
|
|
881
922
|
# ```
|
|
882
|
-
# user.update! do |
|
|
883
|
-
#
|
|
923
|
+
# user.update! do |u|
|
|
924
|
+
# u.set(age: 21)
|
|
884
925
|
# end
|
|
885
926
|
# ```
|
|
886
927
|
#
|
|
@@ -896,8 +937,8 @@ module Dynamoid
|
|
|
896
937
|
# Can update a model conditionaly:
|
|
897
938
|
#
|
|
898
939
|
# ```
|
|
899
|
-
# user.update!(if: { age: 20 }) do |
|
|
900
|
-
#
|
|
940
|
+
# user.update!(if: { age: 20 }) do |u|
|
|
941
|
+
# u.add(age: 1)
|
|
901
942
|
# end
|
|
902
943
|
# ```
|
|
903
944
|
#
|
|
@@ -907,23 +948,31 @@ module Dynamoid
|
|
|
907
948
|
#
|
|
908
949
|
# ```
|
|
909
950
|
# user = User.create(name: 'Tylor')
|
|
910
|
-
# user.update!(unless_exists: [:age]) do |
|
|
911
|
-
#
|
|
951
|
+
# user.update!(unless_exists: [:age]) do |u|
|
|
952
|
+
# u.set(age: 18)
|
|
912
953
|
# end
|
|
913
954
|
# ```
|
|
914
955
|
#
|
|
915
956
|
# If a document doesn't meet conditions it raises
|
|
916
957
|
# +Dynamoid::Errors::StaleObjectError+ exception.
|
|
917
958
|
#
|
|
959
|
+
# If any of the +before_*+ callbacks throws +:abort+ the updating is
|
|
960
|
+
# cancelled and +update!+ raises +Dynamoid::Errors::RecordNotSaved+.
|
|
961
|
+
#
|
|
918
962
|
# It will increment the +lock_version+ attribute if a table has the column,
|
|
919
963
|
# but will not check it. Thus, a concurrent +save+ call will never cause an
|
|
920
964
|
# +update!+ to fail, but an +update!+ may cause a concurrent +save+ to
|
|
921
965
|
# fail.
|
|
922
966
|
#
|
|
923
967
|
# @param conditions [Hash] Conditions on model attributes to make a conditional update (optional)
|
|
968
|
+
# @raise [Dynamoid::Errors::StaleObjectError] if the document does not meet the specified conditions or no longer exists
|
|
969
|
+
# @raise [Dynamoid::Errors::RecordNotSaved] if an +update+ callback aborts the update
|
|
924
970
|
# @return [Dynamoid::Document] self
|
|
925
971
|
def update!(conditions = {})
|
|
972
|
+
aborted = true
|
|
973
|
+
|
|
926
974
|
run_callbacks(:update) do
|
|
975
|
+
aborted = false
|
|
927
976
|
options = {}
|
|
928
977
|
if range_key
|
|
929
978
|
value = read_attribute(range_key)
|
|
@@ -944,8 +993,8 @@ module Dynamoid
|
|
|
944
993
|
|
|
945
994
|
update_item_options = options.merge(conditions: conditions)
|
|
946
995
|
|
|
947
|
-
new_attrs = Dynamoid.adapter.update_item(table_name, partition_key_dumped, update_item_options) do |
|
|
948
|
-
item_updater = ItemUpdaterWithDumping.new(self.class,
|
|
996
|
+
new_attrs = Dynamoid.adapter.update_item(table_name, partition_key_dumped, update_item_options) do |u|
|
|
997
|
+
item_updater = ItemUpdaterWithDumping.new(self.class, u)
|
|
949
998
|
|
|
950
999
|
item_updater.add(lock_version: 1) if self.class.attributes[:lock_version]
|
|
951
1000
|
|
|
@@ -953,7 +1002,7 @@ module Dynamoid
|
|
|
953
1002
|
item_updater.set(updated_at: DateTime.now.in_time_zone(Time.zone))
|
|
954
1003
|
end
|
|
955
1004
|
|
|
956
|
-
yield
|
|
1005
|
+
yield u
|
|
957
1006
|
end
|
|
958
1007
|
load(Undumping.undump_attributes(new_attrs, self.class.attributes))
|
|
959
1008
|
rescue Dynamoid::Errors::ConditionalCheckFailedException
|
|
@@ -964,6 +1013,10 @@ module Dynamoid
|
|
|
964
1013
|
end
|
|
965
1014
|
end
|
|
966
1015
|
|
|
1016
|
+
if aborted
|
|
1017
|
+
raise Dynamoid::Errors::RecordNotSaved, self
|
|
1018
|
+
end
|
|
1019
|
+
|
|
967
1020
|
self
|
|
968
1021
|
end
|
|
969
1022
|
|
|
@@ -978,9 +1031,9 @@ module Dynamoid
|
|
|
978
1031
|
# collections if attribute is a set.
|
|
979
1032
|
#
|
|
980
1033
|
# ```
|
|
981
|
-
# user.update do |
|
|
982
|
-
#
|
|
983
|
-
#
|
|
1034
|
+
# user.update do |u|
|
|
1035
|
+
# u.add(age: 1, followers_count: 5)
|
|
1036
|
+
# u.add(hobbies: ['skying', 'climbing'])
|
|
984
1037
|
# end
|
|
985
1038
|
# ```
|
|
986
1039
|
#
|
|
@@ -988,8 +1041,8 @@ module Dynamoid
|
|
|
988
1041
|
# substructs one collection from another.
|
|
989
1042
|
#
|
|
990
1043
|
# ```
|
|
991
|
-
# user.update do |
|
|
992
|
-
#
|
|
1044
|
+
# user.update do |u|
|
|
1045
|
+
# u.delete(hobbies: ['skying'])
|
|
993
1046
|
# end
|
|
994
1047
|
# ```
|
|
995
1048
|
#
|
|
@@ -997,24 +1050,24 @@ module Dynamoid
|
|
|
997
1050
|
# removed at all:
|
|
998
1051
|
#
|
|
999
1052
|
# ```
|
|
1000
|
-
# user.update do |
|
|
1001
|
-
#
|
|
1053
|
+
# user.update do |u|
|
|
1054
|
+
# u.delete(age: nil)
|
|
1002
1055
|
# end
|
|
1003
1056
|
# ```
|
|
1004
1057
|
#
|
|
1005
1058
|
# or even without useless value at all:
|
|
1006
1059
|
#
|
|
1007
1060
|
# ```
|
|
1008
|
-
# user.update do |
|
|
1009
|
-
#
|
|
1061
|
+
# user.update do |u|
|
|
1062
|
+
# u.delete(:age)
|
|
1010
1063
|
# end
|
|
1011
1064
|
# ```
|
|
1012
1065
|
#
|
|
1013
1066
|
# Operation +set+ just changes an attribute value:
|
|
1014
1067
|
#
|
|
1015
1068
|
# ```
|
|
1016
|
-
# user.update do |
|
|
1017
|
-
#
|
|
1069
|
+
# user.update do |u|
|
|
1070
|
+
# u.set(age: 21)
|
|
1018
1071
|
# end
|
|
1019
1072
|
# ```
|
|
1020
1073
|
#
|
|
@@ -1026,8 +1079,8 @@ module Dynamoid
|
|
|
1026
1079
|
# Can update a model conditionaly:
|
|
1027
1080
|
#
|
|
1028
1081
|
# ```
|
|
1029
|
-
# user.update(if: { age: 20 }) do |
|
|
1030
|
-
#
|
|
1082
|
+
# user.update(if: { age: 20 }) do |u|
|
|
1083
|
+
# u.add(age: 1)
|
|
1031
1084
|
# end
|
|
1032
1085
|
# ```
|
|
1033
1086
|
#
|
|
@@ -1037,12 +1090,13 @@ module Dynamoid
|
|
|
1037
1090
|
#
|
|
1038
1091
|
# ```
|
|
1039
1092
|
# user = User.create(name: 'Tylor')
|
|
1040
|
-
# user.update(unless_exists: [:age]) do |
|
|
1041
|
-
#
|
|
1093
|
+
# user.update(unless_exists: [:age]) do |u|
|
|
1094
|
+
# u.set(age: 18)
|
|
1042
1095
|
# end
|
|
1043
1096
|
# ```
|
|
1044
1097
|
#
|
|
1045
|
-
# If a document doesn't meet conditions
|
|
1098
|
+
# If a document doesn't meet conditions or any of the +before_*+ callbacks
|
|
1099
|
+
# throws +:abort+ it just returns +false+. Otherwise it returns +true+.
|
|
1046
1100
|
#
|
|
1047
1101
|
# It will increment the +lock_version+ attribute if a table has the column,
|
|
1048
1102
|
# but will not check it. Thus, a concurrent +save+ call will never cause an
|
|
@@ -1054,7 +1108,7 @@ module Dynamoid
|
|
|
1054
1108
|
def update(conditions = {}, &block)
|
|
1055
1109
|
update!(conditions, &block)
|
|
1056
1110
|
true
|
|
1057
|
-
rescue Dynamoid::Errors::StaleObjectError
|
|
1111
|
+
rescue Dynamoid::Errors::StaleObjectError, Dynamoid::Errors::RecordNotSaved
|
|
1058
1112
|
false
|
|
1059
1113
|
end
|
|
1060
1114
|
|
|
@@ -1103,7 +1157,12 @@ module Dynamoid
|
|
|
1103
1157
|
increment(attribute, by)
|
|
1104
1158
|
change = read_attribute(attribute) - (attribute_was(attribute) || 0)
|
|
1105
1159
|
|
|
1106
|
-
|
|
1160
|
+
if touch
|
|
1161
|
+
run_callbacks :touch do
|
|
1162
|
+
self.class.inc(hash_key, range_value, attribute => change, touch: touch)
|
|
1163
|
+
clear_attribute_changes(attribute)
|
|
1164
|
+
end
|
|
1165
|
+
else
|
|
1107
1166
|
self.class.inc(hash_key, range_value, attribute => change, touch: touch)
|
|
1108
1167
|
clear_attribute_changes(attribute)
|
|
1109
1168
|
end
|
|
@@ -4,7 +4,7 @@ module Dynamoid
|
|
|
4
4
|
# @private
|
|
5
5
|
class PrimaryKeyTypeMapping
|
|
6
6
|
def self.dynamodb_type(type, options)
|
|
7
|
-
if type.is_a?(
|
|
7
|
+
if type.is_a?(Module)
|
|
8
8
|
type = type.respond_to?(:dynamoid_field_type) ? type.dynamoid_field_type : :string
|
|
9
9
|
end
|
|
10
10
|
|
|
@@ -27,6 +27,13 @@ module Dynamoid
|
|
|
27
27
|
options[:store_as_string]
|
|
28
28
|
end
|
|
29
29
|
string_format ? :string : :number
|
|
30
|
+
when :binary
|
|
31
|
+
native_binary = if options[:store_as_native_binary].nil?
|
|
32
|
+
Dynamoid::Config.store_binary_as_native
|
|
33
|
+
else
|
|
34
|
+
options[:store_as_native_binary]
|
|
35
|
+
end
|
|
36
|
+
native_binary ? :binary : :string
|
|
30
37
|
else
|
|
31
38
|
raise Errors::UnsupportedKeyType, "#{type} cannot be used as a type of table key attribute"
|
|
32
39
|
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dynamoid
|
|
4
|
+
module TestHelper
|
|
5
|
+
module_function
|
|
6
|
+
|
|
7
|
+
# Provision tables for all loaded models (or specified models).
|
|
8
|
+
#
|
|
9
|
+
# Dynamoid::TestHelper.create_tables
|
|
10
|
+
# Dynamoid::TestHelper.create_tables(User, Post)
|
|
11
|
+
#
|
|
12
|
+
def create_tables(*models)
|
|
13
|
+
if models.empty?
|
|
14
|
+
models_dir = Dynamoid.config.models_dir
|
|
15
|
+
|
|
16
|
+
if models_dir
|
|
17
|
+
unless Dir.exist?(models_dir)
|
|
18
|
+
raise Dynamoid::Errors::Error, "Dynamoid.config.models_dir #{models_dir.inspect} does not exist"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
Dir[File.join(models_dir, '**/*.rb')].sort.each { |f| require f }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
models = Dynamoid.included_models
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
models.uniq(&:table_name).each do |model|
|
|
28
|
+
model.create_table(sync: true)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Fast item cleanup between tests.
|
|
33
|
+
#
|
|
34
|
+
# Dynamoid::TestHelper.truncate_tables
|
|
35
|
+
# Dynamoid::TestHelper.truncate_tables(User, Post)
|
|
36
|
+
#
|
|
37
|
+
def truncate_tables(*models)
|
|
38
|
+
models = Dynamoid.included_models if models.empty?
|
|
39
|
+
|
|
40
|
+
models.map(&:table_name).uniq.each do |table|
|
|
41
|
+
Dynamoid.adapter.truncate(table)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Delete all tables belonging to the current namespace.
|
|
46
|
+
#
|
|
47
|
+
# Dynamoid::TestHelper.drop_tables
|
|
48
|
+
#
|
|
49
|
+
def drop_tables
|
|
50
|
+
namespace = Dynamoid.config.namespace
|
|
51
|
+
if namespace.to_s.empty?
|
|
52
|
+
raise Dynamoid::Errors::Error, 'Dynamoid.config.namespace must be set to prevent dropping non-test tables'
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
prefix = "#{namespace}_"
|
|
56
|
+
tables = Dynamoid.adapter.list_tables.select { |t| t.start_with?(prefix) }
|
|
57
|
+
|
|
58
|
+
tables.each { |t| Dynamoid.adapter.delete_table(t) }
|
|
59
|
+
Dynamoid.adapter.tables.clear
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Dynamoid
|
|
4
|
+
module Transactions
|
|
5
|
+
class Mutation
|
|
6
|
+
# @private
|
|
7
|
+
module Builders
|
|
8
|
+
class PutRequestBuilder
|
|
9
|
+
def initialize(model_class, item)
|
|
10
|
+
@model_class = model_class
|
|
11
|
+
@item = item
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def request
|
|
15
|
+
# Require primary key not to exist yet so existing items are never overwritten.
|
|
16
|
+
expression_attribute_names = { '#_h' => @model_class.hash_key }
|
|
17
|
+
condition = 'attribute_not_exists(#_h)'
|
|
18
|
+
|
|
19
|
+
if @model_class.range_key?
|
|
20
|
+
expression_attribute_names['#_r'] = @model_class.range_key
|
|
21
|
+
condition += ' AND attribute_not_exists(#_r)'
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
{
|
|
25
|
+
put: {
|
|
26
|
+
item: @item,
|
|
27
|
+
table_name: @model_class.table_name,
|
|
28
|
+
condition_expression: condition,
|
|
29
|
+
expression_attribute_names: expression_attribute_names
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base'
|
|
4
|
+
require_relative 'builders/put_request_builder'
|
|
4
5
|
|
|
5
6
|
module Dynamoid
|
|
6
7
|
module Transactions
|
|
@@ -21,7 +22,9 @@ module Dynamoid
|
|
|
21
22
|
end
|
|
22
23
|
|
|
23
24
|
model = @model_class.build(attributes)
|
|
24
|
-
|
|
25
|
+
if model.hash_key.nil? && hash_key_string?
|
|
26
|
+
model.hash_key = SecureRandom.uuid
|
|
27
|
+
end
|
|
25
28
|
model
|
|
26
29
|
end
|
|
27
30
|
end
|
|
@@ -63,12 +66,7 @@ module Dynamoid
|
|
|
63
66
|
attributes_dumped = Dynamoid::Dumping.dump_attributes(model.attributes, @model_class.attributes)
|
|
64
67
|
attributes_dumped = sanitize_item(attributes_dumped)
|
|
65
68
|
|
|
66
|
-
|
|
67
|
-
put: {
|
|
68
|
-
item: attributes_dumped,
|
|
69
|
-
table_name: @model_class.table_name
|
|
70
|
-
}
|
|
71
|
-
}
|
|
69
|
+
Builders::PutRequestBuilder.new(@model_class, attributes_dumped).request
|
|
72
70
|
end
|
|
73
71
|
end
|
|
74
72
|
|
|
@@ -78,6 +76,10 @@ module Dynamoid
|
|
|
78
76
|
raise Dynamoid::Errors::MissingHashKey if model.hash_key.nil?
|
|
79
77
|
raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && model.range_value.nil?
|
|
80
78
|
end
|
|
79
|
+
|
|
80
|
+
def hash_key_string?
|
|
81
|
+
@model_class.attributes[@model_class.hash_key][:type] == :string
|
|
82
|
+
end
|
|
81
83
|
end
|
|
82
84
|
end
|
|
83
85
|
end
|
|
@@ -19,13 +19,10 @@ module Dynamoid
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def on_registration
|
|
22
|
-
@
|
|
23
|
-
@model.
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
inc_action = Inc.new(@model.class, @model.hash_key, @model.range_value, { @attribute => change, touch: @touch })
|
|
27
|
-
inc_action.on_registration
|
|
28
|
-
@action_requests = inc_action.action_requests
|
|
22
|
+
if @touch
|
|
23
|
+
@model.run_callbacks(:touch) { increment_and_build_action_requests }
|
|
24
|
+
else
|
|
25
|
+
increment_and_build_action_requests
|
|
29
26
|
end
|
|
30
27
|
end
|
|
31
28
|
|
|
@@ -57,6 +54,17 @@ module Dynamoid
|
|
|
57
54
|
def action_requests # rubocop:disable Style/TrivialAccessors
|
|
58
55
|
@action_requests
|
|
59
56
|
end
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
def increment_and_build_action_requests
|
|
61
|
+
@model.increment(@attribute, @by)
|
|
62
|
+
change = @model.read_attribute(@attribute) - (@model.send(:attribute_was, @attribute) || 0)
|
|
63
|
+
|
|
64
|
+
inc_action = Inc.new(@model.class, @model.hash_key, @model.range_value, { @attribute => change, touch: @touch })
|
|
65
|
+
inc_action.on_registration
|
|
66
|
+
@action_requests = inc_action.action_requests
|
|
67
|
+
end
|
|
60
68
|
end
|
|
61
69
|
end
|
|
62
70
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base'
|
|
4
|
+
require_relative 'builders/put_request_builder'
|
|
4
5
|
require_relative 'builders/update_request_builder'
|
|
5
6
|
|
|
6
7
|
module Dynamoid
|
|
@@ -36,6 +37,10 @@ module Dynamoid
|
|
|
36
37
|
@model.run_callbacks(:save) do
|
|
37
38
|
@model.run_callbacks(callback_name) do
|
|
38
39
|
@model.run_callbacks(:validate) do
|
|
40
|
+
if @was_new_record && @model.hash_key.nil? && hash_key_string?
|
|
41
|
+
@model.hash_key = SecureRandom.uuid
|
|
42
|
+
end
|
|
43
|
+
|
|
39
44
|
validate_primary_key!
|
|
40
45
|
|
|
41
46
|
@aborted = false
|
|
@@ -48,10 +53,6 @@ module Dynamoid
|
|
|
48
53
|
raise Dynamoid::Errors::RecordNotSaved, @model
|
|
49
54
|
end
|
|
50
55
|
|
|
51
|
-
if @was_new_record && @model.hash_key.nil?
|
|
52
|
-
@model.hash_key = SecureRandom.uuid
|
|
53
|
-
end
|
|
54
|
-
|
|
55
56
|
if @model.class.attributes[:lock_version]
|
|
56
57
|
if @model.lock_version.nil? && @model.new_record?
|
|
57
58
|
@model.lock_version = 1
|
|
@@ -104,33 +105,21 @@ module Dynamoid
|
|
|
104
105
|
private
|
|
105
106
|
|
|
106
107
|
def validate_primary_key!
|
|
107
|
-
raise Dynamoid::Errors::MissingHashKey if
|
|
108
|
+
raise Dynamoid::Errors::MissingHashKey if @model.hash_key.nil?
|
|
108
109
|
raise Dynamoid::Errors::MissingRangeKey if @model_class.range_key? && @model.range_value.nil?
|
|
109
110
|
end
|
|
110
111
|
|
|
112
|
+
def hash_key_string?
|
|
113
|
+
@model_class.attributes[@model_class.hash_key][:type] == :string
|
|
114
|
+
end
|
|
115
|
+
|
|
111
116
|
def action_request_to_create
|
|
112
117
|
touch_model_timestamps(skip_created_at: false)
|
|
113
118
|
|
|
114
119
|
attributes_dumped = Dynamoid::Dumping.dump_attributes(@model.attributes, @model_class.attributes)
|
|
115
120
|
attributes_dumped = sanitize_item(attributes_dumped)
|
|
116
121
|
|
|
117
|
-
|
|
118
|
-
expression_attribute_names = { '#_h' => @model_class.hash_key }
|
|
119
|
-
condition = 'attribute_not_exists(#_h)'
|
|
120
|
-
|
|
121
|
-
if @model_class.range_key?
|
|
122
|
-
expression_attribute_names['#_r'] = @model_class.range_key
|
|
123
|
-
condition += ' AND attribute_not_exists(#_r)'
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
{
|
|
127
|
-
put: {
|
|
128
|
-
item: attributes_dumped,
|
|
129
|
-
table_name: @model_class.table_name,
|
|
130
|
-
condition_expression: condition,
|
|
131
|
-
expression_attribute_names: expression_attribute_names
|
|
132
|
-
}
|
|
133
|
-
}
|
|
122
|
+
Builders::PutRequestBuilder.new(@model_class, attributes_dumped).request
|
|
134
123
|
end
|
|
135
124
|
|
|
136
125
|
def action_request_to_update
|
|
@@ -443,7 +443,7 @@ module Dynamoid
|
|
|
443
443
|
# @param range_key [Scalar value] range key value (optional)
|
|
444
444
|
# @param attributes [Hash]
|
|
445
445
|
# @return [nil]
|
|
446
|
-
def upsert(model_class, hash_key, range_key = nil, attributes)
|
|
446
|
+
def upsert(model_class, hash_key, range_key = nil, attributes)
|
|
447
447
|
action = Upsert.new(model_class, hash_key, range_key, attributes)
|
|
448
448
|
register_action action
|
|
449
449
|
end
|
|
@@ -910,6 +910,14 @@ module Dynamoid
|
|
|
910
910
|
# Since DynamoDB limits the total number of actions per transaction,
|
|
911
911
|
# each model created via +#import+ consumes one action from this limit.
|
|
912
912
|
#
|
|
913
|
+
# There are the following differences between transactional and
|
|
914
|
+
# non-transactional +#import+:
|
|
915
|
+
# - transactional +#import+ checks primary key uniqueness and rolls back
|
|
916
|
+
# the transaction, raising
|
|
917
|
+
# +Aws::DynamoDB::Errors::TransactionCanceledException+, when a primary
|
|
918
|
+
# key already exists (non-transactional +#import+ overwrites existing
|
|
919
|
+
# items with the same key)
|
|
920
|
+
#
|
|
913
921
|
# @param model_class [Class] a model class
|
|
914
922
|
# @param array_of_attributes [Array<Hash>] attributes of models
|
|
915
923
|
# @return [Array<Dynamoid::Document>] created models
|
|
@@ -37,7 +37,7 @@ module Dynamoid
|
|
|
37
37
|
when :serialized then SerializedTypeCaster
|
|
38
38
|
when :boolean then BooleanTypeCaster
|
|
39
39
|
when :binary then BinaryTypeCaster
|
|
40
|
-
when
|
|
40
|
+
when Module then CustomTypeCaster
|
|
41
41
|
end
|
|
42
42
|
|
|
43
43
|
if type_caster_class.present?
|
|
@@ -72,7 +72,7 @@ module Dynamoid
|
|
|
72
72
|
|
|
73
73
|
class IntegerTypeCaster < Base
|
|
74
74
|
def process(value)
|
|
75
|
-
# rubocop:disable Lint/DuplicateBranch
|
|
75
|
+
# rubocop:disable-next Lint/DuplicateBranch
|
|
76
76
|
if value == true
|
|
77
77
|
1
|
|
78
78
|
elsif value == false
|
|
@@ -86,13 +86,12 @@ module Dynamoid
|
|
|
86
86
|
else
|
|
87
87
|
value.to_i
|
|
88
88
|
end
|
|
89
|
-
# rubocop:enable Lint/DuplicateBranch
|
|
90
89
|
end
|
|
91
90
|
end
|
|
92
91
|
|
|
93
92
|
class NumberTypeCaster < Base
|
|
94
93
|
def process(value)
|
|
95
|
-
# rubocop:disable Lint/DuplicateBranch
|
|
94
|
+
# rubocop:disable-next Lint/DuplicateBranch
|
|
96
95
|
if value == true
|
|
97
96
|
1
|
|
98
97
|
elsif value == false
|
|
@@ -108,7 +107,6 @@ module Dynamoid
|
|
|
108
107
|
else
|
|
109
108
|
value.to_d
|
|
110
109
|
end
|
|
111
|
-
# rubocop:enable Lint/DuplicateBranch
|
|
112
110
|
end
|
|
113
111
|
end
|
|
114
112
|
|