dynomite 1.2.7 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (123) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +17 -2
  3. data/CHANGELOG.md +18 -0
  4. data/Gemfile +1 -5
  5. data/LICENSE.txt +22 -0
  6. data/README.md +6 -190
  7. data/Rakefile +13 -1
  8. data/dynomite.gemspec +9 -2
  9. data/exe/dynomite +14 -0
  10. data/lib/dynomite/associations/association.rb +126 -0
  11. data/lib/dynomite/associations/belongs_to.rb +35 -0
  12. data/lib/dynomite/associations/has_and_belongs_to_many.rb +19 -0
  13. data/lib/dynomite/associations/has_many.rb +19 -0
  14. data/lib/dynomite/associations/has_one.rb +19 -0
  15. data/lib/dynomite/associations/many_association.rb +257 -0
  16. data/lib/dynomite/associations/single_association.rb +157 -0
  17. data/lib/dynomite/associations.rb +248 -0
  18. data/lib/dynomite/autoloader.rb +25 -0
  19. data/lib/dynomite/cli.rb +48 -0
  20. data/lib/dynomite/client.rb +118 -0
  21. data/lib/dynomite/command.rb +89 -0
  22. data/lib/dynomite/completer/script.rb +6 -0
  23. data/lib/dynomite/completer/script.sh +10 -0
  24. data/lib/dynomite/completer.rb +159 -0
  25. data/lib/dynomite/config.rb +39 -0
  26. data/lib/dynomite/core.rb +18 -19
  27. data/lib/dynomite/engine.rb +45 -0
  28. data/lib/dynomite/erb.rb +5 -3
  29. data/lib/dynomite/error.rb +12 -0
  30. data/lib/dynomite/help/completion.md +20 -0
  31. data/lib/dynomite/help/completion_script.md +3 -0
  32. data/lib/dynomite/help/migrate.md +3 -0
  33. data/lib/dynomite/help.rb +9 -0
  34. data/lib/dynomite/install.rb +4 -0
  35. data/lib/dynomite/item/abstract.rb +15 -0
  36. data/lib/dynomite/item/components.rb +33 -0
  37. data/lib/dynomite/item/dsl.rb +101 -0
  38. data/lib/dynomite/item/id.rb +41 -0
  39. data/lib/dynomite/item/indexes/finder.rb +58 -0
  40. data/lib/dynomite/item/indexes/index.rb +21 -0
  41. data/lib/dynomite/item/indexes/primary_index.rb +18 -0
  42. data/lib/dynomite/item/indexes.rb +25 -0
  43. data/lib/dynomite/item/locking.rb +53 -0
  44. data/lib/dynomite/item/magic_fields.rb +66 -0
  45. data/lib/dynomite/item/primary_key.rb +85 -0
  46. data/lib/dynomite/item/query/delegates.rb +28 -0
  47. data/lib/dynomite/item/query/params/base.rb +42 -0
  48. data/lib/dynomite/item/query/params/expression_attribute.rb +79 -0
  49. data/lib/dynomite/item/query/params/filter.rb +41 -0
  50. data/lib/dynomite/item/query/params/function/attribute_exists.rb +21 -0
  51. data/lib/dynomite/item/query/params/function/attribute_type.rb +30 -0
  52. data/lib/dynomite/item/query/params/function/base.rb +33 -0
  53. data/lib/dynomite/item/query/params/function/begins_with.rb +32 -0
  54. data/lib/dynomite/item/query/params/function/contains.rb +7 -0
  55. data/lib/dynomite/item/query/params/function/size_fn.rb +37 -0
  56. data/lib/dynomite/item/query/params/helpers.rb +94 -0
  57. data/lib/dynomite/item/query/params/key_condition.rb +34 -0
  58. data/lib/dynomite/item/query/params.rb +115 -0
  59. data/lib/dynomite/item/query/partiql/executer.rb +72 -0
  60. data/lib/dynomite/item/query/partiql.rb +67 -0
  61. data/lib/dynomite/item/query/relation/chain.rb +125 -0
  62. data/lib/dynomite/item/query/relation/comparision_expression.rb +21 -0
  63. data/lib/dynomite/item/query/relation/comparision_map.rb +19 -0
  64. data/lib/dynomite/item/query/relation/delete.rb +38 -0
  65. data/lib/dynomite/item/query/relation/ids.rb +21 -0
  66. data/lib/dynomite/item/query/relation/math.rb +19 -0
  67. data/lib/dynomite/item/query/relation/where_field.rb +32 -0
  68. data/lib/dynomite/item/query/relation/where_group.rb +78 -0
  69. data/lib/dynomite/item/query/relation.rb +127 -0
  70. data/lib/dynomite/item/query.rb +7 -0
  71. data/lib/dynomite/item/read/find.rb +196 -0
  72. data/lib/dynomite/item/read/find_with_event.rb +42 -0
  73. data/lib/dynomite/item/read.rb +90 -0
  74. data/lib/dynomite/item/sti.rb +43 -0
  75. data/lib/dynomite/item/table_namespace.rb +43 -0
  76. data/lib/dynomite/item/typecaster.rb +106 -0
  77. data/lib/dynomite/item/waiter_methods.rb +18 -0
  78. data/lib/dynomite/item/write/base.rb +15 -0
  79. data/lib/dynomite/item/write/delete_item.rb +14 -0
  80. data/lib/dynomite/item/write/put_item.rb +99 -0
  81. data/lib/dynomite/item/write/update_item.rb +73 -0
  82. data/lib/dynomite/item/write.rb +204 -0
  83. data/lib/dynomite/item.rb +113 -286
  84. data/lib/dynomite/migration/dsl/accessor.rb +19 -0
  85. data/lib/dynomite/migration/dsl/index/base.rb +42 -0
  86. data/lib/dynomite/migration/dsl/index/gsi.rb +59 -0
  87. data/lib/dynomite/migration/dsl/index/lsi.rb +27 -0
  88. data/lib/dynomite/migration/dsl/index.rb +72 -0
  89. data/lib/dynomite/migration/dsl/primary_key.rb +62 -0
  90. data/lib/dynomite/migration/dsl/provisioned_throughput.rb +38 -0
  91. data/lib/dynomite/migration/dsl.rb +89 -142
  92. data/lib/dynomite/migration/file_info.rb +28 -0
  93. data/lib/dynomite/migration/generator.rb +30 -16
  94. data/lib/dynomite/migration/helpers.rb +7 -0
  95. data/lib/dynomite/migration/internal/migrate/create_schema_migrations.rb +17 -0
  96. data/lib/dynomite/migration/internal/models/schema_migration.rb +6 -0
  97. data/lib/dynomite/migration/runner.rb +178 -0
  98. data/lib/dynomite/migration/templates/create_table.rb +7 -23
  99. data/lib/dynomite/migration/templates/delete_table.rb +7 -0
  100. data/lib/dynomite/migration/templates/update_table.rb +3 -18
  101. data/lib/dynomite/migration.rb +53 -10
  102. data/lib/dynomite/reserved_words.rb +13 -3
  103. data/lib/dynomite/seed.rb +12 -0
  104. data/lib/dynomite/types.rb +22 -0
  105. data/lib/dynomite/version.rb +1 -1
  106. data/lib/dynomite/waiter.rb +40 -0
  107. data/lib/dynomite.rb +11 -17
  108. data/lib/generators/application_item/application_item_generator.rb +30 -0
  109. data/lib/generators/application_item/templates/application_item.rb.tt +4 -0
  110. data/lib/jets/commands/dynamodb_command.rb +29 -0
  111. data/lib/jets/commands/help/generate.md +33 -0
  112. data/lib/jets/commands/help/migrate.md +3 -0
  113. metadata +201 -17
  114. data/docs/migrations/long-example.rb +0 -127
  115. data/docs/migrations/short-example.rb +0 -40
  116. data/lib/dynomite/db_config.rb +0 -121
  117. data/lib/dynomite/errors.rb +0 -15
  118. data/lib/dynomite/log.rb +0 -15
  119. data/lib/dynomite/migration/common.rb +0 -86
  120. data/lib/dynomite/migration/dsl/base_secondary_index.rb +0 -73
  121. data/lib/dynomite/migration/dsl/global_secondary_index.rb +0 -4
  122. data/lib/dynomite/migration/dsl/local_secondary_index.rb +0 -8
  123. data/lib/dynomite/migration/executor.rb +0 -38
@@ -1,86 +0,0 @@
1
- # Common methods to the *SecondaryIndex classes that handle gsi and lsi methods
2
- # as well a the Dsl class that handles create_table and update_table methods.
3
- class Dynomite::Migration::Dsl
4
- module Common
5
- # http://docs.aws.amazon.com/sdkforruby/api/Aws/DynamoDB/Types/KeySchemaElement.html
6
- # partition_key is required
7
- def partition_key(identifier)
8
- @partition_key_identifier = identifier # for later use. useful for conventional_index_name
9
- adjust_schema_and_attributes(identifier, "hash")
10
- end
11
-
12
- # sort_key is optional
13
- def sort_key(identifier)
14
- @sort_key_identifier = identifier # for later use. useful for conventional_index_name
15
- adjust_schema_and_attributes(identifier, "range")
16
- end
17
-
18
- # Parameters:
19
- # identifier: "id:string" or "id"
20
- # key_type: "hash" or "range"
21
- #
22
- # Adjusts the parameters for create_table to add the
23
- # partition_key and sort_key
24
- def adjust_schema_and_attributes(identifier, key_type)
25
- name, attribute_type = identifier.split(':')
26
- attribute_type = "string" if attribute_type.nil?
27
-
28
- partition_key = {
29
- attribute_name: name,
30
- key_type: key_type.upcase
31
- }
32
- @key_schema << partition_key
33
-
34
- attribute_definition = {
35
- attribute_name: name,
36
- attribute_type: Dynomite::ATTRIBUTE_TYPES[attribute_type]
37
- }
38
- @attribute_definitions << attribute_definition
39
- end
40
-
41
- # t.provisioned_throughput(5) # both
42
- # t.provisioned_throughput(:read, 5)
43
- # t.provisioned_throughput(:write, 5)
44
- # t.provisioned_throughput(:both, 5)
45
- def provisioned_throughput(*params)
46
- case params.size
47
- when 0 # reader method
48
- return @provisioned_throughput # early return
49
- when 1
50
- # @provisioned_throughput_set_called useful for update_table
51
- # only provide a provisioned_throughput settings if explicitly called for update_table
52
- @provisioned_throughput_set_called = true
53
- arg = params[0]
54
- if arg.is_a?(Hash)
55
- # Case:
56
- # provisioned_throughput(
57
- # read_capacity_units: 10,
58
- # write_capacity_units: 10
59
- # )
60
- @provisioned_throughput = arg # set directly
61
- return # early return
62
- else # assume parameter is an Integer
63
- # Case: provisioned_throughput(10)
64
- capacity_type = :both
65
- capacity_units = arg
66
- end
67
- when 2
68
- @provisioned_throughput_set_called = true
69
- # Case: provisioned_throughput(:read, 5)
70
- capacity_type, capacity_units = params
71
- end
72
-
73
- map = {
74
- read: :read_capacity_units,
75
- write: :write_capacity_units,
76
- }
77
-
78
- if capacity_type == :both
79
- @provisioned_throughput[map[:read]] = capacity_units
80
- @provisioned_throughput[map[:write]] = capacity_units
81
- else
82
- @provisioned_throughput[capacity_type] = capacity_units
83
- end
84
- end
85
- end
86
- end
@@ -1,73 +0,0 @@
1
- # Base class for LocalSecondaryIndex and GlobalSecondaryIndex
2
- class Dynomite::Migration::Dsl
3
- class BaseSecondaryIndex
4
- include Common
5
-
6
- attr_accessor :action, :key_schema, :attribute_definitions
7
- attr_accessor :index_name
8
- def initialize(action, index_name=nil, &block)
9
- @action = action.to_sym
10
- # for gsi action can be: :create, :update, :delete
11
- # for lsi action is always: :create
12
- @index_name = index_name
13
- @block = block
14
-
15
- # Dsl fills these atttributes in as methods are called within
16
- # the block
17
- @key_schema = []
18
- @attribute_definitions = []
19
- # default provisioned_throughput
20
- @provisioned_throughput = {
21
- read_capacity_units: 5,
22
- write_capacity_units: 5
23
- }
24
- end
25
-
26
- def index_name
27
- @index_name || conventional_index_name
28
- end
29
-
30
- def conventional_index_name
31
- # @partition_key_identifier and @sort_key_identifier are set as immediately
32
- # when the partition_key and sort_key methods are called in the dsl block.
33
- # Usually look like this:
34
- #
35
- # @partition_key_identifier: post_id:string
36
- # @sort_key_identifier: updated_at:string
37
- #
38
- # We strip the :string portion in case it is provided
39
- #
40
- partition_key = @partition_key_identifier.split(':').first
41
- sort_key = @sort_key_identifier.split(':').first if @sort_key_identifier
42
- [partition_key, sort_key, "index"].compact.join('-')
43
- end
44
-
45
- def evaluate
46
- return if @evaluated
47
- @block.call(self) if @block
48
- @evaluated = true
49
- end
50
-
51
- def params
52
- evaluate # lazy evaluation: wait until as long as possible before evaluating code block
53
-
54
- params = { index_name: index_name } # required for all actions
55
-
56
- if @action == :create
57
- params[:key_schema] = @key_schema # required for create action
58
- # hardcode to ALL for now
59
- params[:projection] = { # required
60
- projection_type: "ALL", # accepts ALL, KEYS_ONLY, INCLUDE
61
- # non_key_attributes: ["NonKeyAttributeName"],
62
- }
63
- end
64
-
65
- if self.is_a?(GlobalSecondaryIndex) && [:create, :update].include?(@action)
66
- # provisioned_throughput is required for gsi index definition
67
- params[:provisioned_throughput] = @provisioned_throughput
68
- end
69
-
70
- params
71
- end
72
- end
73
- end
@@ -1,4 +0,0 @@
1
- class Dynomite::Migration::Dsl
2
- class GlobalSecondaryIndex < BaseSecondaryIndex
3
- end
4
- end
@@ -1,8 +0,0 @@
1
- class Dynomite::Migration::Dsl
2
- class LocalSecondaryIndex < BaseSecondaryIndex
3
- def initialize(index_name=nil, &block)
4
- # Can only create local secondary index when creating a table
5
- super(:create, index_name, &block)
6
- end
7
- end
8
- end
@@ -1,38 +0,0 @@
1
- class Dynomite::Migration
2
- class Executor
3
- include Dynomite::DbConfig
4
-
5
- # Examples:
6
- # Executor.new(:create_table, params) or
7
- # Executor.new(:update_table, params)
8
- #
9
- # The params are generated frmo the dsl.params
10
- attr_accessor :table_name
11
- def initialize(table_name, method_name, params)
12
- @table_name = table_name
13
- @method_name = method_name # create_table or update_table
14
- @params = params
15
- end
16
-
17
- def run
18
- begin
19
- # Examples:
20
- # result = db.create_table(@params)
21
- # result = db.update_table(@params)
22
-
23
- # Leaving this verbose output in here until this DSL is more hardened to help debug
24
- unless ENV['DYNOMITE_ENV'] == 'test'
25
- puts "Calling #{@method_name} with params:"
26
- pp @params
27
- end
28
-
29
- return if ENV['DYNOMITE_DRY'] # dry run flag
30
- result = db.send(@method_name, @params)
31
-
32
- puts "DynamoDB Table: #{@table_name} Status: #{result.table_description.table_status}"
33
- rescue Aws::DynamoDB::Errors::ServiceError => error
34
- puts "Unable to #{@method_name.to_s.gsub('_',' ')}: #{error.message}".color(:red)
35
- end
36
- end
37
- end
38
- end