data_migrater 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: f0a68cf4d21de0ee3a7406e759ee2e845fa34c15
4
+ data.tar.gz: 5dc9b57fe7f9ef317d4fc0deee61130d390aa939
5
+ SHA512:
6
+ metadata.gz: 3d5760d59787acb7288fb4521d4942d97be558c75801a7f1fae58b8e92de9f1fab7e23a01d30587ee12056641dafc4bac3c291c90b56ef94efb05057b2a2425c
7
+ data.tar.gz: d43f33644a45da85afc3a2d320dc158384da82014a4b193634285d700032ce391d1fa45a5c63801deeacb7d68e4866bea77ff2ebad9fa90922b240b796ef9622
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ # 0.1.0
2
+
3
+ - Rails 4 support.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 GetNinjas
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,47 @@
1
+ # Data Migrater
2
+
3
+ Data Migration is like Migrate, but it will changes the data of your tables,
4
+ not your schema. This gems creates a file where you can write code to make
5
+ change along you schema changes.
6
+
7
+ ## Install
8
+
9
+ Extracts the necessary files including a migrate that create a table used
10
+ to keep you data migration version.
11
+
12
+ ```bash
13
+ rails g data_migrater:install
14
+ ```
15
+
16
+ Then execute the migrations to create the table.
17
+
18
+ ```bash
19
+ rake db:migrate
20
+ ```
21
+
22
+ ## Usage
23
+
24
+ Now we can create a data migration with `name` we want.
25
+
26
+ ```bash
27
+ rails g data_migrater:create name
28
+ ```
29
+
30
+ Check your `db/data_migrate` folder, the data migration will be there.
31
+ Next time your application run, all pending data migration will be execute.
32
+
33
+ ## Test
34
+
35
+ Before send pull request, check if specs is passing.
36
+
37
+ ```bash
38
+ rspec spec
39
+ ```
40
+
41
+ ## Code Style
42
+
43
+ And check if the code style is good.
44
+
45
+ ```bash
46
+ rubocop --debug --display-cop-names
47
+ ```
@@ -0,0 +1,8 @@
1
+ module DataMigrater
2
+ end
3
+
4
+ require 'data_migration'
5
+ require 'data_migrater/collection'
6
+ require 'data_migrater/migration'
7
+ require 'data_migrater/migrator'
8
+ require 'data_migrater/version'
@@ -0,0 +1,25 @@
1
+ module DataMigrater
2
+ class Collection
3
+ def initialize(path = "#{Rails.root}/db/data_migrate")
4
+ @path = path
5
+ end
6
+
7
+ def migrations
8
+ Dir.entries(@path).sort.map do |migration_file|
9
+ migration_for migration_file
10
+ end.compact
11
+ end
12
+
13
+ private
14
+
15
+ def migration_for(file)
16
+ if file =~ migration_pattern
17
+ DataMigrater::Migration.new $1.to_i, $2, "#{$1}_#{$2}"
18
+ end
19
+ end
20
+
21
+ def migration_pattern
22
+ /^([0-9]+)_([_a-z0-9]+)\.rb$/
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,35 @@
1
+ module DataMigrater
2
+ class Migration
3
+ attr_reader :version, :name, :filename
4
+
5
+ def initialize(version, name, filename, path = "#{Rails.root}/db/data_migrate")
6
+ @filename = filename
7
+ @name = name
8
+ @path = path
9
+ @version = version
10
+ end
11
+
12
+ def execute
13
+ data_migration = DataMigration.new version: @version
14
+
15
+ return false unless data_migration.valid?
16
+
17
+ begin
18
+ data_migration.save!
19
+ migration.execute
20
+ rescue StandardError => e
21
+ data_migration.destroy
22
+
23
+ raise e
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def migration
30
+ require_dependency "#{@path}/#{filename}.rb"
31
+
32
+ name.camelize.constantize.new
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,38 @@
1
+ module DataMigrater
2
+ class Migrator
3
+ def initialize(collection = DataMigrater::Collection.new)
4
+ @collection = collection
5
+ end
6
+
7
+ def migrate
8
+ ActiveRecord::Base.transaction do
9
+ if execute?
10
+ data_migrations.each(&:execute)
11
+ else
12
+ pending_migrations.each do |migration|
13
+ puts " #{migration.version} #{migration.name}"
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def execute?
22
+ return false if pending_migrations.any?
23
+
24
+ DataMigration.table_exists? && data_migrations.any?
25
+ end
26
+
27
+ def data_migrations
28
+ @data_migrations ||= @collection.migrations
29
+ end
30
+
31
+ def pending_migrations
32
+ migrations_paths = ActiveRecord::Migrator.migrations_paths
33
+ migrator = ActiveRecord::Migrator.open migrations_paths
34
+
35
+ @pending_migrations ||= migrator.pending_migrations
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,3 @@
1
+ module DataMigrater
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,3 @@
1
+ class DataMigration < ActiveRecord::Base
2
+ validates :version, uniqueness: true
3
+ end
@@ -0,0 +1,16 @@
1
+ module DataMigrater
2
+ class CreateGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ argument :name, type: :string
6
+
7
+ desc "create a skeleton data migration."
8
+
9
+ def create_migrate_file
10
+ tmpl = 'db/data_migrate/data_migrate.rb.erb'
11
+ version = Time.zone.now.strftime '%Y%m%d%H%M%S'
12
+
13
+ template tmpl, "db/data_migrate/#{version}_#{name.underscore}.rb"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,22 @@
1
+ module DataMigrater
2
+ class InstallGenerator < Rails::Generators::Base
3
+ source_root File.expand_path('../templates', __FILE__)
4
+
5
+ desc "creates an initializer and copy necessary files."
6
+
7
+ def create_data_folder
8
+ FileUtils.mkdir_p 'db/data_migrate'
9
+ FileUtils.touch 'db/data_migrate/.keep'
10
+ end
11
+
12
+ def copy_migrate
13
+ version = Time.zone.now.strftime '%Y%m%d%H%M%S'
14
+
15
+ template 'db/migrate/create_data_migrations.rb', "db/migrate/#{version}_create_data_migrations.rb"
16
+ end
17
+
18
+ def copy_initializer
19
+ template 'config/initializers/data_migrater.rb', 'config/initializers/data_migrater.rb'
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require 'data_migrater'
2
+
3
+ DataMigrater::Migrator.new.migrate unless Rails.env.test?
@@ -0,0 +1,4 @@
1
+ class <%= name.camelize %>
2
+ def execute
3
+ end
4
+ end
@@ -0,0 +1,7 @@
1
+ class CreateDataMigrations < ActiveRecord::Migration
2
+ def change
3
+ create_table :data_migrations do |t|
4
+ t.string :version, null: false
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMigrater::Collection do
4
+ let(:path) { 'db/data_migrate' }
5
+
6
+ before do
7
+ FileUtils.mkdir_p path
8
+
9
+ allow(Rails).to receive(:root) { '.' }
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf 'db'
14
+ end
15
+
16
+ describe '.migrations' do
17
+ let(:file_1) { '20151205090800_add_column' }
18
+ let(:file_2) { '20151205090801_rename_column' }
19
+ let(:file_3) { 'invalid_20151205090802_remove_column' }
20
+
21
+ subject { described_class.new path }
22
+
23
+ before do
24
+ FileUtils.touch "#{path}/#{file_1}.rb"
25
+ FileUtils.touch "#{path}/#{file_2}.rb"
26
+ FileUtils.touch "#{path}/#{file_3}.rb"
27
+ end
28
+
29
+ it 'returns valid migrations' do
30
+ expect(subject.migrations.size).to eq 2
31
+
32
+ expect(subject.migrations.first.name).to eq 'add_column'
33
+ expect(subject.migrations.first.version).to eq 20151205090800
34
+
35
+ expect(subject.migrations.last.name).to eq 'rename_column'
36
+ expect(subject.migrations.last.version).to eq 20151205090801
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,15 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMigration do
4
+ let(:version) { '19841023113000' }
5
+
6
+ it { expect(DataMigration.new version: version).to be_valid }
7
+
8
+ it 'validates uniqueness' do
9
+ DataMigration.create! version: version
10
+
11
+ expect {
12
+ DataMigration.create! version: version
13
+ }.to raise_error ActiveRecord::RecordInvalid
14
+ end
15
+ end
@@ -0,0 +1,117 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMigrater::Migrator do
4
+ before do
5
+ FileUtils.mkdir_p 'db/migrate'
6
+
7
+ allow(Rails).to receive(:root) { '.' }
8
+ end
9
+
10
+ after do
11
+ FileUtils.rm_rf 'db'
12
+ end
13
+
14
+ let(:connection) { ActiveRecord::Base.connection }
15
+
16
+ it 'has a version number' do
17
+ expect(DataMigrater::VERSION).not_to be_nil
18
+ end
19
+
20
+ describe '.migrate' do
21
+ let(:data_migration) { double }
22
+ let(:data_migrations) { [data_migration] }
23
+ let(:insert) { "insert into data_migrations(version) values('21161023010203')" }
24
+ let(:select) { 'select version from data_migrations' }
25
+ let(:version) { lambda { connection.select_one(select)['version'] } }
26
+
27
+ before do
28
+ connection.insert_sql insert
29
+ end
30
+
31
+ context 'when has no pending migration' do
32
+ context 'when data migration table does not exists' do
33
+ let(:data_migration) { double }
34
+ let(:collection) { double migrations: [data_migration] }
35
+
36
+ before do
37
+ allow(DataMigration).to receive(:table_exists?) { false }
38
+ end
39
+
40
+ subject { described_class.new collection }
41
+
42
+ it 'does not executes data migration' do
43
+ expect(data_migration).to_not receive :execute
44
+
45
+ subject.migrate
46
+ end
47
+ end
48
+
49
+ context 'when data migration table exists' do
50
+ context 'but there is no data migration on collection' do
51
+ let(:collection) { double migrations: [] }
52
+
53
+ subject { described_class.new collection }
54
+
55
+ it { expect { subject.migrate }.to_not change { version.call } }
56
+ end
57
+
58
+ context 'and has data migration on collection' do
59
+ let(:data_migration_2) { double }
60
+ let(:collection) { double migrations: [data_migration, data_migration_2] }
61
+
62
+ it 'executes migration' do
63
+ expect(data_migration).to receive :execute
64
+ expect(data_migration_2).to receive :execute
65
+
66
+ described_class.new(collection).migrate
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ context 'when migration throws exception' do
73
+ before do
74
+ allow(data_migration).to receive(:execute).and_raise
75
+ end
76
+
77
+ it 'keeps the actual version' do
78
+ begin
79
+ subject.migrate
80
+ rescue
81
+ end
82
+
83
+ expect(version.call).to eq '21161023010203'
84
+ end
85
+ end
86
+
87
+ context 'when there are migration on db/migrate' do
88
+ let(:collection) { double migrations: [data_migration] }
89
+
90
+ context 'pending' do
91
+ let(:next_version) { version.call.to_i + 1 }
92
+ let(:next_migration) { "#{Rails.root}/db/migrate/#{next_version}_migration.rb" }
93
+
94
+ before { FileUtils.touch next_migration }
95
+ after { FileUtils.rm next_migration }
96
+
97
+ it 'does not run the data migrations' do
98
+ expect(data_migration).to_not receive :execute
99
+
100
+ described_class.new(collection).migrate
101
+ end
102
+ end
103
+
104
+ context 'executed' do
105
+ before do
106
+ allow_any_instance_of(ActiveRecord::Migrator).to receive(:pending_migrations) { [] }
107
+ end
108
+
109
+ it 'runs the data migrations' do
110
+ expect(data_migration).to receive :execute
111
+
112
+ described_class.new(collection).migrate
113
+ end
114
+ end
115
+ end
116
+ end
117
+ end
@@ -0,0 +1,74 @@
1
+ require 'spec_helper'
2
+
3
+ describe DataMigrater::Migration do
4
+ let(:name) { 'add_column_to_users_table' }
5
+ let(:path) { 'db/data_migrate' }
6
+ let(:version) { '20151205090800' }
7
+ let(:filename) { "#{version}_#{name}" }
8
+
9
+ class AddColumnToUsersTable
10
+ end
11
+
12
+ before do
13
+ FileUtils.mkdir_p path
14
+ FileUtils.touch "#{path}/#{filename}.rb"
15
+ end
16
+
17
+ after do
18
+ FileUtils.rm_rf 'db'
19
+ end
20
+
21
+ describe "#execute" do
22
+ let(:migration) { double.as_null_object }
23
+
24
+ subject { described_class.new version, name, filename, path }
25
+
26
+ before do
27
+ allow(AddColumnToUsersTable).to receive(:new) { migration }
28
+ end
29
+
30
+ context 'when migration was already run' do
31
+ before do
32
+ DataMigration.create version: version
33
+ end
34
+
35
+ specify { expect(subject.execute).to be_falsy }
36
+
37
+ it 'does not executes' do
38
+ expect(migration).to_not receive :execute
39
+
40
+ subject.execute
41
+ end
42
+ end
43
+
44
+ context 'when migration was not run before' do
45
+ it 'executes the migration' do
46
+ expect(migration).to receive :execute
47
+
48
+ subject.execute
49
+ end
50
+
51
+ it 'creates a new data_migrations' do
52
+ subject.execute
53
+
54
+ expect(DataMigration.exists? version: version).to be_truthy
55
+ end
56
+
57
+ context 'and some error is raised' do
58
+ before do
59
+ allow(migration).to receive(:execute).and_raise RuntimeError
60
+ end
61
+
62
+ specify { expect { subject.execute }.to raise_error RuntimeError }
63
+
64
+ it 'removes data migration object' do
65
+ begin
66
+ subject.execute
67
+ rescue
68
+ expect(DataMigration.exists? version: version).to be_falsy
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,20 @@
1
+ ENV['RAILS_ENV'] ||= 'test'
2
+
3
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
4
+
5
+ require 'active_record/railtie'
6
+ require 'data_migrater'
7
+
8
+ ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
9
+
10
+ ActiveRecord::Schema.define do
11
+ create_table :data_migrations do |t|
12
+ t.string :version, null: false
13
+ end
14
+ end
15
+
16
+ RSpec.configure do |config|
17
+ config.before do
18
+ DataMigration.destroy_all
19
+ end
20
+ end
metadata ADDED
@@ -0,0 +1,165 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_migrater
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - GetNinjas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '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: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Generates Data Migrations on Migrate style.
112
+ email: tech@getninjas.com.br
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - CHANGELOG.md
118
+ - LICENSE
119
+ - README.md
120
+ - lib/data_migrater.rb
121
+ - lib/data_migrater/collection.rb
122
+ - lib/data_migrater/migration.rb
123
+ - lib/data_migrater/migrator.rb
124
+ - lib/data_migrater/version.rb
125
+ - lib/data_migration.rb
126
+ - lib/generators/data_migrater/create_generator.rb
127
+ - lib/generators/data_migrater/install_generator.rb
128
+ - lib/generators/data_migrater/templates/config/initializers/data_migrater.rb
129
+ - lib/generators/data_migrater/templates/db/data_migrate/data_migrate.rb.erb
130
+ - lib/generators/data_migrater/templates/db/migrate/create_data_migrations.rb
131
+ - spec/collection_spec.rb
132
+ - spec/data_migration_spec.rb
133
+ - spec/migrater_spec.rb
134
+ - spec/migration_spec.rb
135
+ - spec/spec_helper.rb
136
+ homepage: https://github.com/getninjas/data_migrater
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5.1
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: A Data Migrator gem
160
+ test_files:
161
+ - spec/collection_spec.rb
162
+ - spec/data_migration_spec.rb
163
+ - spec/migrater_spec.rb
164
+ - spec/migration_spec.rb
165
+ - spec/spec_helper.rb