createk_data_migrator 0.1.1

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99b4fed781f0d902d6405fae0316299baf15c356
4
+ data.tar.gz: e54954cf3272cff56b956a694709cc23c02e89d9
5
+ SHA512:
6
+ metadata.gz: 077aa2d2b7a6c5d83bfa3629c18012a55f5f4192dd54e83bcaa829b56051beb9be472fdbbc10133b6657c5bddc9a4d5ec97e275803953e5a1b6d34da1e53d1f7
7
+ data.tar.gz: 58f5a649f4bf550c083697b7e21fb4a2f7d021f89c3b0459b0fb92bfad1f0638eb84ed72ff849b6271cf12a13c5e0f96f957532e53fc2d17986283bccaeefb20
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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.0
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in data_migrator.gemspec
4
+ gem 'byebug'
5
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 TODO: Write your name
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,58 @@
1
+ # DataMigrator Useage
2
+
3
+ ## Installing DataMigrator
4
+
5
+ Add the following to your Gemfile
6
+
7
+ Add `data_migrator`
8
+
9
+ Then run `bundle install`
10
+
11
+ Once DataMigrator is installed you will need to generate the `data_migrations`
12
+ table that stores the versioning of data migrations.
13
+
14
+ Run the following from the command line. `rails generate data_migrator`
15
+
16
+ Then run `rake db:migrate`
17
+
18
+ DataMigrator should now be installed & ready to be used
19
+
20
+ ## Data Migration
21
+
22
+ Data migrations are used within an application to make ammendments & changes to data.
23
+ For example if a set collection of `SomeModel`'s need their `status` changing to
24
+ `active`.
25
+
26
+ ## Creating the migration itself
27
+
28
+ We can create data migrations using a pre-defined Rails generator that is ran
29
+ from the command line. `rails generate data_migration change_some_models_state_to_active`.
30
+ This will then create a data migration file within the `db/migrate_data` folder.
31
+
32
+ By default this file will contain an `up` and a `down` method, although a
33
+ `rollback` task not yet been implemented for data migrations.
34
+
35
+ Enter some code that you would like to be run within the `up` method.
36
+
37
+ ```
38
+ class ChangeSomeModelsStateToActive < ActiveRecord::Migration
39
+ def up
40
+ some_models = SomeModel.find(some_model_ids)
41
+ some_models.update_all(state: 'active')
42
+ end
43
+
44
+ def down
45
+ end
46
+ end
47
+ ```
48
+
49
+ ## Running the migration
50
+
51
+ Outstanding data migrations can be manually ran with the command `rake db:data:migrate`.
52
+
53
+ ## Ensuring the same data migration is not run more than once
54
+
55
+ The implementation is inherited from the Rails way of creating the usual database
56
+ migrations. This means when a data migration is ran, a record of which migration
57
+ is inserted into the `data_migrations` table. This table is then checked prior
58
+ to running any future data migration, thus stopping duplicate entries.
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 "createk_data_migrator"
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,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'createk_data_migrator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "createk_data_migrator"
8
+ spec.version = CreatekDataMigrator::VERSION
9
+ spec.authors = ["Reiss Johnson"]
10
+ spec.email = ["reissjohnson@me.com"]
11
+
12
+ spec.summary = %q{A Gem used for creating data migration tasks}
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.12"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ spec.add_development_dependency "rspec", "~> 3.0"
22
+ end
@@ -0,0 +1,7 @@
1
+ class CreatekDataMigrator
2
+ class Railtie < Rails::Railtie
3
+ rake_tasks do
4
+ load 'tasks/data_migrate.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class CreatekDataMigrator
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,19 @@
1
+ require "createk_data_migrator/version"
2
+ require "createk_data_migrator/railtie" if defined?(Rails)
3
+
4
+ class CreatekDataMigrator
5
+ class << self
6
+ attr_writer :data_migrations_path
7
+
8
+ def migrate
9
+ begin
10
+ old_paths = ActiveRecord::Tasks::DatabaseTasks.migrations_paths
11
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = [Rails.root.join('db/migrate_data')]
12
+
13
+ ActiveRecord::Tasks::DatabaseTasks.migrate
14
+ ensure
15
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = old_paths
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class CreatekDataMigrator
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ include Rails::Generators::Migration
7
+ source_root File.expand_path('../templates', __FILE__)
8
+ desc "add the migrations"
9
+ namespace 'createk_data_migrator'
10
+
11
+ def self.next_migration_number(path)
12
+ unless @prev_migration_nr
13
+ @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
14
+ else
15
+ @prev_migration_nr += 1
16
+ end
17
+ @prev_migration_nr.to_s
18
+ end
19
+
20
+ def copy_migrations
21
+ migration_template "create_data_migrations_table.rb", "db/migrate/create_data_migrations_table.rb"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,27 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/active_record'
3
+ require 'rails/generators/actions/create_migration'
4
+
5
+ class DataMigrationGenerator < Rails::Generators::NamedBase
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+ include Rails::Generators::Migration
8
+
9
+ def create_data_migration
10
+ migration_template('data_migration_template.rb', "db/migrate_data/#{file_name}.rb")
11
+ end
12
+
13
+ private
14
+
15
+ def self.next_migration_number(dirname)
16
+ ActiveRecord::Generators::Base.next_migration_number(dirname)
17
+ end
18
+
19
+ def migration_class_name
20
+ @name.camelize
21
+ end
22
+
23
+ def migration_version
24
+ return unless Rails.version >= '5'
25
+ ['4.2']
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ class CreateDataMigrationsTable < ActiveRecord::Migration['4.2']
2
+ def self.up
3
+ create_table :data_migrations, id: false do |t|
4
+ t.string :version
5
+ t.index :version, unique: true
6
+ end
7
+ end
8
+
9
+ def self.down
10
+ drop_table :data_migrations
11
+ end
12
+ end
@@ -0,0 +1,17 @@
1
+ require 'createk_data_migrator'
2
+
3
+ namespace :db do
4
+ desc 'Run Data Migrations'
5
+ namespace :data do
6
+ task migrate: :environment do
7
+ begin
8
+ old_table_name = ActiveRecord::Base.schema_migrations_table_name
9
+ ActiveRecord::Base.schema_migrations_table_name = 'data_migrations'
10
+
11
+ CreatekDataMigrator.migrate
12
+ ensure
13
+ ActiveRecord::Base.schema_migrations_table_name = old_table_name
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
2
+ def up
3
+ end
4
+
5
+ def down
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: createk_data_migrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Reiss Johnson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - reissjohnson@me.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - createk_data_migrator.gemspec
72
+ - lib/createk_data_migrator.rb
73
+ - lib/createk_data_migrator/railtie.rb
74
+ - lib/createk_data_migrator/version.rb
75
+ - lib/generators/createk_data_migrator_generator.rb
76
+ - lib/generators/data_migration_generator.rb
77
+ - lib/generators/templates/create_data_migrations_table.rb.tt
78
+ - lib/tasks/data_migrate.rake
79
+ - lib/templates/data_migration_template.rb.tt
80
+ homepage:
81
+ licenses: []
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.1
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: A Gem used for creating data migration tasks
103
+ test_files: []