dynamodb-migration 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9bcbd5523525b31582b3fdc18c2d9eb567d00547
4
+ data.tar.gz: 2c939f83c5e65d2d92314addf257450790d16495
5
+ SHA512:
6
+ metadata.gz: 550f89961218ccebc933cf2f5e5ab177ef88c49b4704bba5ea434e8237e5a6e9192521cfa7066adba920e0ae5a6681159fcc7a5bcc804cd6df06fe89bc3792ea
7
+ data.tar.gz: a3ca25874298df3a0423eb12957b3a9ed569b097c3015a5b21a81456418d1943d954f19fdfe305d853247af5f345cd35a406d9630a59fa84bab43d9b33f1a936
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at henry.lawson@foinq.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in dynamodb-migration.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Henry Lawson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,105 @@
1
+ # Dynamodb::Migration
2
+
3
+ Allows for the creation of simple DynamoDB commands that will be executed only once against a DynamoDB database to allow you to "migrate" the schema of the database over time. This is a simple implementation for DynamoDB, similar to tools such as FlywayDB and Active Record Migrations.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'dynamodb-migration'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install dynamodb-migration
20
+
21
+ ## Usage
22
+
23
+ In a rake task or in your applications start up, simply add:
24
+
25
+ ```ruby
26
+ options = {
27
+ client: dynamodb-client, # an Aws::DynamoDB::Client instance
28
+ path: '~/my_project/migrations' # the full path to the folder where your migration classes will live
29
+ }
30
+ DynamoDB::Migration.run_all_migrations(options)
31
+ ```
32
+
33
+ Or if you are using a Sinatra application, in your `config.ru`:
34
+
35
+ ```ruby
36
+ # the full path to the folder where your migration classes will live
37
+ # we are assuming you will place your migrations in a "migrations" folder
38
+ # next to config.ru
39
+ set :migrations_path, File.join(File.dirname(__FILE__), 'migrations')
40
+
41
+ # registering the below requires the "dynamodb-client" gem, alternatively
42
+ # you can return a Aws::DynamoDB::Client instance from a method named
43
+ # `dynamodb_client`
44
+ register DynamoDB::Client
45
+
46
+ # registering this extension will automatically run migrations when the app
47
+ # starts up
48
+ register DynamoDB::Migration
49
+ ```
50
+
51
+ To define a migration, create a class in your `migrations` folder (defined
52
+ above with the `path` option) such as the one below:
53
+
54
+
55
+ ```ruby
56
+ # migrations/20150215181100_create_table_users.rb
57
+
58
+ class CreateTableUsers < DynamoDB::Migration::Unit
59
+ def update
60
+ client.create_table(
61
+ table_name: "users",
62
+ attribute_definitions: [
63
+ {
64
+ attribute_name: "username",
65
+ attribute_type: "S",
66
+ },
67
+ ],
68
+ key_schema: [
69
+ {
70
+ attribute_name: "username",
71
+ key_type: "HASH",
72
+ },
73
+ ],
74
+ provisioned_throughput: {
75
+ read_capacity_units: 1,
76
+ write_capacity_units: 1,
77
+ },
78
+ stream_specification: {
79
+ stream_enabled: true,
80
+ stream_view_type: "NEW_AND_OLD_IMAGES",
81
+ },
82
+ )
83
+ end
84
+ end
85
+ ```
86
+
87
+ DynamoDB::Migration will detect this class and execute once against your
88
+ DynamoDB instance. It will record the execution in a table called `migrations`
89
+ which it creates and maintains internally.
90
+
91
+ ## Development
92
+
93
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
94
+
95
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
96
+
97
+ ## Contributing
98
+
99
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/dynamodb-migration. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
100
+
101
+
102
+ ## License
103
+
104
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
105
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "dynamodb/migration"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dynamodb/migration/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dynamodb-migration"
8
+ spec.version = Dynamodb::Migration::VERSION
9
+ spec.authors = ["Henry Lawson"]
10
+ spec.email = ["henry.lawson@foinq.com"]
11
+
12
+ spec.summary = %q{A simple DynamoDB migration tool.}
13
+ spec.description = %q{Allows for the creation of simple DynamoDB commands that will be executed only once against a DynamoDB database to allow you to "migrate" the schema of the database over time. This is a simple implementation for DynamoDB, similar to tools such as FlywayDB and Active Record Migrations.}
14
+ spec.homepage = "https://github.com/henrylawson/dynamodb-migration"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency "aws-sdk", "~> 2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
@@ -0,0 +1,23 @@
1
+ require "dynamodb/migration/version"
2
+ require "dynamodb/migration/execute"
3
+ require "dynamodb/migration/unit"
4
+
5
+ module Dynamodb
6
+ module Migration
7
+ def self.registered(app)
8
+ options = {
9
+ client: app.dynamodb_client,
10
+ path: app.settings.migrations_path,
11
+ }
12
+ run_all_migrations(options)
13
+ end
14
+
15
+ def self.run_all_migrations(options)
16
+ Dir.glob("#{options[:path]}/**/*.rb").each do |file|
17
+ require file
18
+ end
19
+ Execute.new(options[:client])
20
+ .update_all
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,132 @@
1
+ module DynamoDB
2
+ module Migration
3
+ class Execute
4
+ def initialize(client)
5
+ @client = client
6
+ end
7
+
8
+ def update_all
9
+ ensure_migrations_table_exists
10
+ migration_classes.each do |clazz|
11
+ apply_migration(clazz)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ attr_reader :client
18
+
19
+ def apply_migration(clazz)
20
+ return if migration_executed?(clazz)
21
+ record_start_migration(clazz)
22
+ migration = clazz.new
23
+ migration.client = client
24
+ migration.update
25
+ record_successful_migration(clazz)
26
+ rescue Aws::DynamoDB::Errors::ServiceError => e
27
+ record_failed_migration(clazz)
28
+ raise
29
+ end
30
+
31
+ def record_failed_migration(clazz)
32
+ client.delete_item({
33
+ table_name: "migrations",
34
+ key: {
35
+ "file" => clazz_filename(clazz),
36
+ },
37
+ condition_expression: "completed = :false",
38
+ expression_attribute_values: {
39
+ ":false" => false
40
+ }
41
+ })
42
+ end
43
+
44
+ def record_start_migration(clazz)
45
+ client.put_item({
46
+ table_name: "migrations",
47
+ item: {
48
+ "file" => clazz_filename(clazz),
49
+ "executed_at" => Time.now.iso8601,
50
+ "created_at" => Time.now.iso8601,
51
+ "updated_at" => Time.now.iso8601,
52
+ "completed" => false,
53
+ },
54
+ return_values: "NONE",
55
+ })
56
+ end
57
+
58
+ def record_successful_migration(clazz)
59
+ client.update_item({
60
+ table_name: "migrations",
61
+ key: {
62
+ "file" => clazz_filename(clazz),
63
+ },
64
+ update_expression: "SET completed = :true",
65
+ condition_expression: "completed = :false",
66
+ expression_attribute_values: {
67
+ ":false" => false,
68
+ ":true" => true,
69
+ }
70
+ })
71
+ end
72
+
73
+ def clazz_filename(clazz)
74
+ full_filename = clazz.instance_methods(false)
75
+ .map { |m| clazz.instance_method(m).source_location }
76
+ .compact
77
+ .map { |m| m.first }
78
+ .uniq
79
+ .first
80
+ File.basename(full_filename)
81
+ end
82
+
83
+ def migration_classes
84
+ ObjectSpace.each_object(Migration.singleton_class)
85
+ .reject { |c| c == Migration }
86
+ end
87
+
88
+ def migration_executed?(clazz)
89
+ client.get_item({
90
+ table_name: "migrations",
91
+ key: {
92
+ "file" => clazz_filename(clazz),
93
+ },
94
+ attributes_to_get: ["file"],
95
+ consistent_read: true,
96
+ }).item
97
+ end
98
+
99
+ def ensure_migrations_table_exists
100
+ client.create_table(
101
+ table_name: "migrations",
102
+ attribute_definitions: [
103
+ {
104
+ attribute_name: "file",
105
+ attribute_type: "S",
106
+ },
107
+ ],
108
+ key_schema: [
109
+ {
110
+ attribute_name: "file",
111
+ key_type: "HASH",
112
+ },
113
+ ],
114
+ provisioned_throughput: {
115
+ read_capacity_units: 1,
116
+ write_capacity_units: 1,
117
+ },
118
+ stream_specification: {
119
+ stream_enabled: true,
120
+ stream_view_type: "NEW_AND_OLD_IMAGES",
121
+ },
122
+ ) unless table_exists?(client, 'migrations')
123
+ end
124
+
125
+ def table_exists?(client, table_name)
126
+ client.describe_table(table_name: table_name)
127
+ rescue Aws::DynamoDB::Errors::ResourceNotFoundException => e
128
+ false
129
+ end
130
+ end
131
+ end
132
+ end
@@ -0,0 +1,11 @@
1
+ module DynamoDB
2
+ module Migration
3
+ class Unit
4
+ attr_accessor :client
5
+
6
+ def update
7
+ raise 'Update method not implemented by migration class'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,5 @@
1
+ module Dynamodb
2
+ module Migration
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dynamodb-migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Henry Lawson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Allows for the creation of simple DynamoDB commands that will be executed
70
+ only once against a DynamoDB database to allow you to "migrate" the schema of the
71
+ database over time. This is a simple implementation for DynamoDB, similar to tools
72
+ such as FlywayDB and Active Record Migrations.
73
+ email:
74
+ - henry.lawson@foinq.com
75
+ executables: []
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".travis.yml"
82
+ - CODE_OF_CONDUCT.md
83
+ - Gemfile
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - dynamodb-migration.gemspec
90
+ - lib/dynamodb/migration.rb
91
+ - lib/dynamodb/migration/execute.rb
92
+ - lib/dynamodb/migration/unit.rb
93
+ - lib/dynamodb/migration/version.rb
94
+ homepage: https://github.com/henrylawson/dynamodb-migration
95
+ licenses:
96
+ - MIT
97
+ metadata: {}
98
+ post_install_message:
99
+ rdoc_options: []
100
+ require_paths:
101
+ - lib
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 2.4.5
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: A simple DynamoDB migration tool.
118
+ test_files: []