rails-data-migrations 1.0.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: 3d872d014e8e922e77c643a401cb07a55c07a628
4
+ data.tar.gz: b53d95b58987918de1e0ea4c1e170ab77f144b31
5
+ SHA512:
6
+ metadata.gz: 79f12c676d358a522870252b83f169681d060b01cc91acb397540a8c53e098ee5c4a36eb4ced215c690da16e5f4e1da08c1c26e85879bddaab83944c7bcce07a
7
+ data.tar.gz: 6a2c2a431f8e0def8f8f4e0408acf5d14f89ddf2f7710c4a5b73c14a8a0ca01779f802d9a72bfe24cab2c28b93a00c5f13a9f91b2ed712e1e4694bb26b99f2e0
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.idea/
11
+ spec/db/
12
+ gemfiles/
13
+ *.gem
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.1
5
+ before_install: gem install bundler -v 1.13.6
data/Appraisals ADDED
@@ -0,0 +1,15 @@
1
+ appraise 'rails-4.0' do
2
+ gem 'rails', '~> 4.0'
3
+ end
4
+
5
+ appraise 'rails-4.1' do
6
+ gem 'rails', '~> 4.1'
7
+ end
8
+
9
+ appraise 'rails-4.2' do
10
+ gem 'rails', '~> 4.2'
11
+ end
12
+
13
+ appraise 'rails-5.0' do
14
+ gem 'rails', '~> 5.0'
15
+ end
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-data-migrations.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Offgrid Electric
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,69 @@
1
+ # Rails Data Migrations
2
+
3
+ ## Why?
4
+
5
+ Have you ever run into a problem when alongside with DB schema migrations (managed by `rake db:migrate` in Rails)
6
+ you have to often change your DB content, as well? If you read this, you probably tried to use schema migrations do change your data after schema changes, but this is not a recommended way and sometimes data changes could take a long time, so they will block your app at the deploy time.
7
+ Another approach is to use [rake tasks](https://robots.thoughtbot.com/data-migrations-in-rails) to run your changes after `db:migrate` or even independently. But this could also become a mess after some time if you have multiple developers in your project, and you need to change your data often.
8
+
9
+ This is our solution we came up with in our company - run data migration tasks in a `db:migrate`-like manner
10
+
11
+ ## Usage
12
+
13
+ To create a data migration you need to run:
14
+ ```
15
+ rails generate data_migration migration_name
16
+ ```
17
+
18
+ and this will create a `migration_name.rb` file in `db/data_migrations` folder with a following content:
19
+ ```ruby
20
+ class MigrationName < DataMigration
21
+ def up
22
+ # put your code here
23
+ end
24
+ end
25
+ ```
26
+
27
+ so all we need to do is to put some ruby code inside the `up` method.
28
+
29
+ Finally, at the release time, you need to run
30
+ ```
31
+ rake data:migrate
32
+ ```
33
+
34
+ This will run all pending data migrations and store migration history in `data_migrations` table. You're all set.
35
+
36
+ ## Rails Support
37
+
38
+ Rails 4.0 and higher
39
+
40
+ ## Installation
41
+
42
+ Add this line to your application's Gemfile:
43
+
44
+ ```ruby
45
+ gem 'rails-data-migrations'
46
+ ```
47
+
48
+ And then execute:
49
+
50
+ $ bundle
51
+
52
+ Or install it yourself as:
53
+
54
+ $ gem install rails-data-migrations
55
+
56
+ ## Contributing
57
+
58
+ 1. Fork it
59
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
60
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
61
+ 4. Run tests (`appraisal install && appraisal rake`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
64
+
65
+
66
+ ## License
67
+
68
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
69
+
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
@@ -0,0 +1,11 @@
1
+ require 'rails/version'
2
+
3
+ module ActiveRecord
4
+ class DataMigration < (Rails::VERSION::MAJOR >= 5 ? Migration[5.0] : Migration)
5
+ # base class (extend with any useful helpers)
6
+
7
+ def down
8
+ raise IrreversibleMigration
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'rails/generators'
2
+
3
+ class DataMigrationGenerator < Rails::Generators::NamedBase
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def create_migration_file
7
+ migration_file_name = "#{RailsDataMigrations::Migrator.migrations_path}/#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{file_name}.rb"
8
+ copy_file 'data_migration_generator.rb', migration_file_name do |content|
9
+ content.sub(/ClassName/, file_name.camelize)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ class ClassName < ActiveRecord::DataMigration
2
+ def up
3
+ # put your code here
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_record'
2
+ require 'active_record/data_migration'
3
+ require 'rails_data_migrations/version'
4
+ require 'rails_data_migrations/log_entry'
5
+ require 'rails_data_migrations/migrator'
6
+ require 'rails_data_migrations/railtie'
@@ -0,0 +1,13 @@
1
+ module RailsDataMigrations
2
+ class LogEntry < ::ActiveRecord::SchemaMigration
3
+ class << self
4
+ def table_name
5
+ ActiveRecord::Base.table_name_prefix + 'data_migrations' + ActiveRecord::Base.table_name_suffix
6
+ end
7
+
8
+ def index_name
9
+ "#{table_name_prefix}unique_data_migrations#{table_name_suffix}"
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,32 @@
1
+ module RailsDataMigrations
2
+ class Migrator < ::ActiveRecord::Migrator
3
+
4
+ def record_version_state_after_migrating(version)
5
+ if down?
6
+ migrated.delete(version)
7
+ LogEntry.where(version: version.to_s).delete_all
8
+ else
9
+ migrated << version
10
+ LogEntry.create!(version: version.to_s)
11
+ end
12
+ end
13
+
14
+ class << self
15
+ def get_all_versions(connection = ActiveRecord::Base.connection)
16
+ if connection.data_source_exists?(schema_migrations_table_name)
17
+ LogEntry.all.map { |x| x.version.to_i }.sort
18
+ else
19
+ []
20
+ end
21
+ end
22
+
23
+ def schema_migrations_table_name
24
+ LogEntry.table_name
25
+ end
26
+
27
+ def migrations_path
28
+ 'db/data_migrations'
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ module RailsDataMigrations
4
+ class Railtie < ::Rails::Railtie
5
+ rake_tasks do
6
+ load File.join(File.dirname(__FILE__), '..', 'tasks/data_migrations.rake')
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module RailsDataMigrations
2
+ VERSION = '1.0.0'
3
+ end
@@ -0,0 +1,51 @@
1
+ require 'rake'
2
+
3
+ namespace :data do
4
+ def migrations_path
5
+ RailsDataMigrations::Migrator.migrations_path
6
+ end
7
+
8
+ def apply_single_migration(direction, version)
9
+ raise 'VERSION is required' unless version
10
+ RailsDataMigrations::Migrator.run(direction, migrations_path, version.to_i)
11
+ end
12
+
13
+ task init_migration: :environment do
14
+ RailsDataMigrations::LogEntry.create_table
15
+ end
16
+
17
+ desc 'Apply pending data migrations'
18
+ task migrate: :init_migration do
19
+ filter = RailsDataMigrations::Migrator.current_version
20
+ versions = RailsDataMigrations::Migrator.migrations(migrations_path)
21
+ .select { |migration| migration.version > filter }
22
+ .map(&:version)
23
+ versions.sort.each do |version|
24
+ apply_single_migration(:up, version)
25
+ end
26
+ end
27
+
28
+ namespace :migrate do
29
+ desc 'Apply single data migration using VERSION'
30
+ task up: :init_migration do
31
+ apply_single_migration(:up, ENV['VERSION'])
32
+ end
33
+
34
+ desc 'Revert single data migration using VERSION'
35
+ task down: :init_migration do
36
+ apply_single_migration(:down, ENV['VERSION'])
37
+ end
38
+
39
+ desc 'Skip single data migration using VERSION'
40
+ task skip: :init_migration do
41
+ version = ENV['VERSION'].to_i
42
+ raise 'VERSION is required' unless version > 0
43
+ if RailsDataMigrations::LogEntry.where(version: version).any?
44
+ puts "data migration #{version} was already applied."
45
+ else
46
+ RailsDataMigrations::LogEntry.create!(version: version)
47
+ puts "data migration #{version} was skipped."
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rails_data_migrations/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rails-data-migrations'
7
+ spec.version = RailsDataMigrations::VERSION
8
+ spec.authors = ['Sergey Glukhov']
9
+ spec.email = ['sergey.glukhov@gmail.com']
10
+
11
+ spec.summary = %q{Run your data migration tasks in a db:migrate-like manner}
12
+ spec.homepage = 'https://github.com/OffgridElectric/rails-data-migrations'
13
+ spec.license = 'MIT'
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
16
+ f.match(%r{^(test|spec|features)/})
17
+ end
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_runtime_dependency 'rails', '>= 4.0.0'
22
+
23
+ spec.add_development_dependency 'bundler', '~> 1.13'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'rspec', '3.5.0'
26
+ spec.add_development_dependency 'sqlite3', '~> 1.3'
27
+ spec.add_development_dependency 'appraisal', '~> 2.1'
28
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-data-migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergey Glukhov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-11-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 4.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 4.0.0
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.13'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.13'
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.5.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.5.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: sqlite3
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '2.1'
97
+ description:
98
+ email:
99
+ - sergey.glukhov@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Appraisals
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - lib/active_record/data_migration.rb
113
+ - lib/generators/data_migration_generator.rb
114
+ - lib/generators/templates/data_migration_generator.rb
115
+ - lib/rails-data-migrations.rb
116
+ - lib/rails_data_migrations/log_entry.rb
117
+ - lib/rails_data_migrations/migrator.rb
118
+ - lib/rails_data_migrations/railtie.rb
119
+ - lib/rails_data_migrations/version.rb
120
+ - lib/tasks/data_migrations.rake
121
+ - rails-data-migrations.gemspec
122
+ homepage: https://github.com/OffgridElectric/rails-data-migrations
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.6.7
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Run your data migration tasks in a db:migrate-like manner
146
+ test_files: []