dyna_model 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6178c869f10e2306bf62d4d1b4beb5bd7f183a07
4
- data.tar.gz: 6cb0b80e057ca46dfe1164d96140e7a582a95b42
3
+ metadata.gz: a14772713df5d414c446ff2512cbfab7cd74ac4f
4
+ data.tar.gz: f8bf4f7078c3ec8adfbdf7243c54ea386b2f5976
5
5
  SHA512:
6
- metadata.gz: 92abe2b742564589f72c04605c9dd158cd2e081a47e99eef68e8e65310930695a1e019a4700d25918d6be6e7e0761c4254c94f5c5a63d43a19b5dad185e1751a
7
- data.tar.gz: 5aecbbecaf7dc0b3ffaaa39ea374c717063e242a38b1716fb0ab270749173d508c1e83c421321cb8ab4276a0277c9712bf06892239370681478fef4c89cb0872
6
+ metadata.gz: 28c730d1944e150b62855c5060ce95f99ad51512d3807611f9a0229df5fa2df89888aea6ce6431f442e72e9017e03e34b9cb048b9ef1238cd045103c2a07a6e6
7
+ data.tar.gz: b2866435dfd853401dd0e23d92103ad5a4a1ba40e01704eef94a4387723f8179ae27937b1a84f82125f488d089692976a32956afd34c685f7a4fd84937df7b35
@@ -0,0 +1,95 @@
1
+ #
2
+ # Persist DynaModel records for a particular model to S3 for extra backup.
3
+ #
4
+ # The DynamoDB backup model (using EMR to read and write to S3 is not incremental and quickly takes way too long
5
+ # and defeats the purpose of a backup)
6
+ #
7
+ # This is not intended to be used for models with high frequency writes but as a way to incrementally backup models
8
+ # that contain mission critical data (although S3 backups are not guarenteed to be durable since you probably want to delay
9
+ # the S3 write to a backround task).
10
+ #
11
+ module DynaModel
12
+ module Extensions
13
+ module S3Backup
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ after_save :backup_dyna_model_record_to_s3
18
+ end
19
+
20
+ def backup_dyna_model_record_to_s3
21
+ if self.class.dyna_model_s3_backup_config
22
+ if self.class.dyna_model_s3_backup_config[:after_save]
23
+ self.class.dyna_model_s3_backup_config[:after_save].call(self)
24
+ else
25
+ self.write_dyna_model_s3_backup!
26
+ end
27
+ end
28
+ end
29
+
30
+ def write_dyna_model_s3_backup!
31
+ self.class.dyna_model_s3_backup_bucket.objects[File.join(self.class.dyna_model_s3_backup_config[:prefix], "#{self.guid}.json")].write(self.to_dyna_model_s3_backup_json)
32
+ end
33
+
34
+ def to_dyna_model_s3_backup_json
35
+ ActiveSupport::JSON.encode({
36
+ class: self.class.to_s,
37
+ attributes: self.attributes
38
+ })
39
+ end
40
+
41
+ module ClassMethods
42
+
43
+ def dyna_model_s3_backup(options={})
44
+ raise "DynaModel::Extensions::S3Backup requires a bucket." unless options[:bucket]
45
+ options[:prefix] ||= self.to_s.underscore
46
+ #options[:after_save] = lambda { |obj| ... }
47
+ @@dyna_model_s3_backup_config = options
48
+ end
49
+
50
+ def dyna_model_s3_backup_client
51
+ @@dyna_model_s3_backup_client ||= AWS::S3.new
52
+ end
53
+
54
+ def dyna_model_s3_backup_config
55
+ @@dyna_model_s3_backup_config
56
+ end
57
+
58
+ def dyna_model_s3_backup_bucket
59
+ self.dyna_model_s3_backup_client.buckets[self.dyna_model_s3_backup_config[:bucket]]
60
+ end
61
+
62
+ def enable_dyna_model_s3_backup_versioning!
63
+ self.dyna_model_s3_backup_bucket.enable_versioning
64
+ end
65
+
66
+ def suspend_dyna_model_s3_backup_versioning!
67
+ self.dyna_model_s3_backup_bucket.suspend_versioning
68
+ end
69
+
70
+ def create_dyna_model_s3_backup_bucket!
71
+ self.dyna_model_s3_backup_client.buckets.create(self.dyna_model_s3_backup_config[:bucket])
72
+ end
73
+
74
+ # TODO: improve for high scale... ability to resume
75
+ def import_from_dyna_model_s3_backup
76
+ self.dyna_model_s3_backup_bucket.objects.with_prefix(self.dyna_model_s3_backup_config[:prefix]).each_batch do |batch|
77
+ batch.each do |item|
78
+ puts "Found #{item.key}"
79
+ obj_json = ActiveSupport::JSON.decode(item.read)
80
+ obj = obj_json["class"].constantize.new
81
+ obj.attributes = obj_json["attributes"]
82
+ if obj.save
83
+ puts "Saved #{obj.guid}."
84
+ else
85
+ puts "Failed to save #{obj.guid}. #{obj.errors.full_messages.to_sentence}"
86
+ end
87
+ end
88
+ end
89
+ end
90
+
91
+ end
92
+
93
+ end
94
+ end
95
+ end
@@ -117,6 +117,7 @@ module DynaModel
117
117
  end
118
118
 
119
119
  def read_first(hash_value, options={})
120
+ options[:limit] = 1
120
121
  self.read_range(hash_value, options).first
121
122
  end
122
123
 
@@ -20,8 +20,8 @@ module DynaModel
20
20
  AWS::Record::Attributes::IntegerAttr => "N",
21
21
  AWS::Record::Attributes::FloatAttr => "N",
22
22
  AWS::Record::Attributes::BooleanAttr => "S",
23
- AWS::Record::Attributes::DateTimeAttr => "N",
24
- AWS::Record::Attributes::DateAttr => "N",
23
+ AWS::Record::Attributes::DateTimeAttr => "S",
24
+ AWS::Record::Attributes::DateAttr => "S",
25
25
  AWS::Record::Attributes::SerializedAttr => "B"
26
26
  }
27
27
 
@@ -1,3 +1,3 @@
1
1
  module DynaModel
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/dyna_model.rb CHANGED
@@ -15,6 +15,7 @@ require "dyna_model/query"
15
15
  require "dyna_model/response"
16
16
  require "dyna_model/validations"
17
17
  require "dyna_model/extensions/symbol"
18
+ require "dyna_model/extensions/s3_backup"
18
19
  require "dyna_model/document"
19
20
 
20
21
  module DynaModel
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dyna_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cary Dunn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-07 00:00:00.000000000 Z
11
+ date: 2014-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -143,6 +143,7 @@ files:
143
143
  - lib/dyna_model/config.rb
144
144
  - lib/dyna_model/config/options.rb
145
145
  - lib/dyna_model/document.rb
146
+ - lib/dyna_model/extensions/s3_backup.rb
146
147
  - lib/dyna_model/extensions/symbol.rb
147
148
  - lib/dyna_model/persistence.rb
148
149
  - lib/dyna_model/query.rb