imigrate 0.9.0

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.
Files changed (70) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +24 -0
  3. data/.rspec +2 -0
  4. data/.travis.yml +9 -0
  5. data/Gemfile +4 -0
  6. data/LICENSE +21 -0
  7. data/README.md +104 -0
  8. data/Rakefile +2 -0
  9. data/imigrate.gemspec +25 -0
  10. data/lib/generators/imigrate.rb +30 -0
  11. data/lib/generators/templates/data_migration.rb +8 -0
  12. data/lib/imigrate.rb +9 -0
  13. data/lib/imigrate/activerecord_ext.rb +19 -0
  14. data/lib/imigrate/railtie.rb +10 -0
  15. data/lib/imigrate/version.rb +3 -0
  16. data/lib/tasks/capistrano.rake +17 -0
  17. data/lib/tasks/databases.rake +60 -0
  18. data/spec/dummy/README.rdoc +28 -0
  19. data/spec/dummy/Rakefile +6 -0
  20. data/spec/dummy/app/assets/images/.keep +0 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  25. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  26. data/spec/dummy/app/mailers/.keep +0 -0
  27. data/spec/dummy/app/models/.keep +0 -0
  28. data/spec/dummy/app/models/concerns/.keep +0 -0
  29. data/spec/dummy/app/models/user.rb +2 -0
  30. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  31. data/spec/dummy/bin/bundle +3 -0
  32. data/spec/dummy/bin/rails +4 -0
  33. data/spec/dummy/bin/rake +4 -0
  34. data/spec/dummy/config.ru +4 -0
  35. data/spec/dummy/config/application.rb +23 -0
  36. data/spec/dummy/config/boot.rb +5 -0
  37. data/spec/dummy/config/database.yml +25 -0
  38. data/spec/dummy/config/environment.rb +5 -0
  39. data/spec/dummy/config/environments/development.rb +39 -0
  40. data/spec/dummy/config/environments/production.rb +78 -0
  41. data/spec/dummy/config/environments/test.rb +39 -0
  42. data/spec/dummy/config/initializers/assets.rb +8 -0
  43. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  44. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  45. data/spec/dummy/config/initializers/data_migrator.rb +1 -0
  46. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  47. data/spec/dummy/config/initializers/inflections.rb +16 -0
  48. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  49. data/spec/dummy/config/initializers/session_store.rb +3 -0
  50. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  51. data/spec/dummy/config/locales/en.yml +23 -0
  52. data/spec/dummy/config/routes.rb +56 -0
  53. data/spec/dummy/config/secrets.yml +22 -0
  54. data/spec/dummy/db/data/20150202182709_populate_user.rb +9 -0
  55. data/spec/dummy/db/data/20150202183455_populate_user2.rb +9 -0
  56. data/spec/dummy/db/development.sqlite3 +0 -0
  57. data/spec/dummy/db/migrate/20150202164939_create_user.rb +8 -0
  58. data/spec/dummy/db/migrate_test/20150202174939_modify_user.rb +5 -0
  59. data/spec/dummy/db/schema.rb +27 -0
  60. data/spec/dummy/db/test.sqlite3 +0 -0
  61. data/spec/dummy/lib/assets/.keep +0 -0
  62. data/spec/dummy/log/.keep +0 -0
  63. data/spec/dummy/public/404.html +67 -0
  64. data/spec/dummy/public/422.html +67 -0
  65. data/spec/dummy/public/500.html +66 -0
  66. data/spec/dummy/public/favicon.ico +0 -0
  67. data/spec/lib/generators/generator_spec.rb +17 -0
  68. data/spec/rake_tasks_spec.rb +147 -0
  69. data/spec/spec_helper.rb +18 -0
  70. metadata +181 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d379610f46ae849894f48c9f025e87d5f4868895
4
+ data.tar.gz: becab09bfc09e91b4190ace6b85f752c30ea1153
5
+ SHA512:
6
+ metadata.gz: 1a71515b63577eeb473251cd465d045f9f236dbf6b85cd4707c9025e70fd67802268af166271a0a6b7b6f8712a4d383ca26437bfe1c7245888490b9717f55679
7
+ data.tar.gz: 3a4ce18ebc1e9886fda72121b787533dc06e8dd81a7acabf5e4c8bfbda191d8a5ace271b02674ea4929076651abaf6db63d651707c76fd3484794a9b44b7a3e7
@@ -0,0 +1,24 @@
1
+
2
+ .bundle
3
+ bundler/
4
+ db/*.sqlite3*
5
+ log/*.log
6
+ *.log
7
+ tmp/**/*
8
+ tmp/*
9
+ doc/*
10
+ *.swp
11
+ *~
12
+ .DS_Store
13
+ #*#
14
+ .autotest
15
+ rspec.html
16
+ public/uploads/*
17
+ .sass-cache/*
18
+ .project
19
+ .idea/*
20
+ .rvmrc
21
+ spec/report/*
22
+ sauce_connect.log*
23
+ .vagrant/
24
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --colour
2
+ --format doc
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ cache: bundler
3
+ rvm:
4
+ - 2.1
5
+
6
+ script:
7
+ - bundle exec rspec spec/
8
+
9
+ sudo: false
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in imigrate.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2011 Andrew J Vargo
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.
@@ -0,0 +1,104 @@
1
+ I migrate
2
+ ====
3
+
4
+ Run Rails data migrations.
5
+
6
+ Data migrations are stored in db/data. They act like schema
7
+ migrations, except they should be reserved for data migrations.
8
+
9
+ Problems this gem is solving :
10
+ ----------------------
11
+
12
+ ### Migrating data using model which has attribute not created in database
13
+
14
+ Example:
15
+
16
+ * Migration adding a new attribute to the model User.
17
+ * Migration migrating data to populate User models.
18
+ * Migration adding a new attribute to the model User.
19
+
20
+ The second migration will break because the model User now use a new attribute
21
+ which will not be created in the database at this time.
22
+
23
+ Using this gem, you can now run data migrations after executing all schema migrations.
24
+
25
+ ### Using a staging environment using the production database
26
+
27
+ In this case when we deploy to production, we need to run again migration
28
+ of the data because models in production was still not using the last schema.
29
+
30
+ Example:
31
+ When adding a new attribute to the model user, while begin in staging,
32
+ the production will not create the user as it does not know about it.
33
+
34
+ Using this gem, you can specify a prefix (`env_prefix`) for the version registered in
35
+ the data versioning table. (example: 'staging' or 'production')
36
+
37
+ ### Ensure migration are runnable multiple times
38
+
39
+ As run migration on staging and in production, this mean migration should
40
+ runnable multiple times agains the same database. You can simplify call multiple
41
+ times `rake db:data:migrate` to verify migrations are correct.
42
+
43
+
44
+ What's it do?
45
+ -------------
46
+
47
+ Data migrations are stored in db/data. They act like schema
48
+ migrations, except they should be reserved for data migrations.
49
+ Running any of the provided rake tasks also
50
+ creates a data schema table to mirror the usual schema migrations
51
+ table to track all the goodness.
52
+
53
+ Data migrations can be created at the same time as schema migrations,
54
+ or independently.
55
+
56
+ Rails 4.1 and Ruby 2.0
57
+ --------------------
58
+
59
+ Imigrate is Rails 4.1 and Ruby > 2.0 compatible.
60
+
61
+ Installation
62
+ ------------
63
+ Add the gem to your project
64
+
65
+ # Gemfile
66
+ gem 'imigrate'
67
+
68
+ Then `bundle install` and you are ready to go.
69
+
70
+ So you know, when you use one of the provide rake tasks, a table
71
+ called 'data_migrations' will be created in your database. This
72
+ is to mirror the way the standard 'db' rake tasks work.
73
+
74
+ Usage
75
+ -----
76
+
77
+ ### Generating Migrations
78
+
79
+ You can generate a data migration as you would a schema migration:
80
+
81
+ rails g data_migration add_this_to_that
82
+
83
+ ### Specify the a prefix to the data versioning table
84
+
85
+ Imigrate.env_prefix = 'staging'
86
+
87
+ ### Rake Tasks
88
+
89
+ $> rake -T data
90
+ rake db:data:forward # Pushes the schema to the next version (specify steps w/ STEP=n)
91
+ rake db:data:migrate # Migrate data migrations (options: VERSION=x, VERBOSE=false)
92
+ rake db:data:migrate:down # Runs the "down" for a given migration VERSION
93
+ rake db:data:migrate:redo # Rollbacks the database one migration and re migrate up (options: STEP=x, VERSIO...
94
+ rake db:data:migrate:status # Display status of data migrations
95
+ rake db:data:migrate:up # Runs the "up" for a given migration VERSION
96
+ rake db:data:rollback # Rolls the schema back to the previous version (specify steps w/ STEP=n)
97
+ rake db:data:version # Retrieves the current schema version number for data migrations
98
+
99
+
100
+ Credits
101
+ -------
102
+
103
+ Inspired from https://github.com/ajvargo/data-migrate
104
+
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "imigrate/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "imigrate"
7
+ s.version = Imigrate::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Matthieu Paret"]
10
+ s.email = ["matthieu@ifeelgoods.com"]
11
+ s.homepage = "https://github.com/ifeelgoods/imigrate"
12
+ s.summary = %q{Rake tasks to migrate data for Rails application.}
13
+ s.description = %q{Rake tasks to migrate data for Rails application.}
14
+
15
+ s.add_development_dependency "rails", "~> 4.1.0"
16
+ s.add_development_dependency "rspec-rails", "~> 3.1.0"
17
+ s.add_development_dependency "sqlite3"
18
+ s.add_development_dependency "generator_spec"
19
+ s.add_development_dependency "database_cleaner"
20
+
21
+ s.files = `git ls-files`.split("\n")
22
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
23
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
24
+ s.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,30 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/named_base'
3
+ require 'rails/generators/migration'
4
+
5
+ module Imigrate
6
+ module Generators
7
+ class DataMigrationGenerator < Rails::Generators::NamedBase #:nodoc:
8
+ namespace "data_migration"
9
+ include Rails::Generators::Migration
10
+
11
+ def self.source_root
12
+ @_data_migrator_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
13
+ end
14
+
15
+ def create_data_migration
16
+ migration_template "data_migration.rb", "db/data/#{file_name}.rb"
17
+ end
18
+
19
+ protected
20
+
21
+ def self.next_migration_number(dirname)
22
+ if ActiveRecord::Base.timestamped_migrations
23
+ Time.new.utc.strftime("%Y%m%d%H%M%S")
24
+ else
25
+ "%.3d" % (current_migration_number(dirname) + 1)
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,8 @@
1
+ class <%= migration_class_name %> < ActiveRecord::Migration
2
+ def self.up
3
+ end
4
+
5
+ def self.down
6
+ raise ActiveRecord::IrreversibleMigration
7
+ end
8
+ end
@@ -0,0 +1,9 @@
1
+ require_relative 'imigrate/railtie'
2
+ require_relative 'generators/imigrate'
3
+
4
+ module Imigrate
5
+ mattr_accessor :env_prefix
6
+ mattr_accessor :activated
7
+ SCHEMA_MIGRATIONS_TABLE_NAME = 'data_migrations'
8
+ MIGRATION_PATH = 'db/data'
9
+ end
@@ -0,0 +1,19 @@
1
+ #require it, only when needed!
2
+
3
+ if Imigrate.activated == true
4
+ class ActiveRecord::MigrationProxy
5
+ def initialize(name, version, filename, scope)
6
+ if Imigrate.env_prefix.present?
7
+ version = "#{Imigrate.env_prefix}_#{version}"
8
+ end
9
+
10
+ super(name, version, filename, scope)
11
+ end
12
+ end
13
+ class ActiveRecord::Migrator
14
+ private
15
+ def ran?(migration)
16
+ migrated.include?(migration.version)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,10 @@
1
+ require 'active_record'
2
+ require 'active_record/migration'
3
+
4
+ module Imigrate
5
+ class Railtie < Rails::Railtie
6
+ rake_tasks do
7
+ load File.join(File.dirname(__FILE__), '..', 'tasks/databases.rake')
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Imigrate
2
+ VERSION = "0.9.0"
3
+ end
@@ -0,0 +1,17 @@
1
+ namespace :deploy do
2
+ namespace :migrate do
3
+ desc 'Runs rake db:data:migrate if migrations are set'
4
+ task :data => [:set_rails_env] do
5
+ on primary fetch(:migration_role) do
6
+ within release_path do
7
+ with rails_env: fetch(:rails_env) do
8
+ migrate_env = fetch(:migrate_env, "")
9
+ execute :rake, "db:data:migrate #{migrate_env} "
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ after 'deploy:migrate', 'deploy:migrate:data'
17
+ end
@@ -0,0 +1,60 @@
1
+ namespace :db do
2
+ namespace :migrate do
3
+ desc 'Migrate schema and data migrations (options: VERSION=x, VERBOSE=false)'
4
+ task :all do
5
+ system('rake db:migrate')
6
+ system('rake db:data:migrate')
7
+ end
8
+ end
9
+
10
+ namespace :data do
11
+ task :load_config do
12
+ Imigrate.activated = true
13
+ require_relative '../imigrate/activerecord_ext'
14
+ ActiveRecord::Tasks::DatabaseTasks.migrations_paths = Imigrate::MIGRATION_PATH
15
+ ActiveRecord::Base.schema_migrations_table_name = Imigrate::SCHEMA_MIGRATIONS_TABLE_NAME
16
+ end
17
+
18
+ desc 'Migrate data migrations (options: VERSION=x, VERBOSE=false)'
19
+ task :migrate => [:environment, :load_config] do
20
+ Rake::Task["db:migrate"].invoke
21
+ end
22
+
23
+ namespace :migrate do
24
+ desc 'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
25
+ task :redo => [:environment, :load_config] do
26
+ Rake::Task["db:migrate:redo"].invoke
27
+ end
28
+
29
+ desc 'Runs the "up" for a given migration VERSION.'
30
+ task :up => [:environment, :load_config] do
31
+ Rake::Task["db:migrate:up"].invoke
32
+ end
33
+
34
+ desc 'Runs the "down" for a given migration VERSION.'
35
+ task :down => [:environment, :load_config] do
36
+ Rake::Task["db:migrate:down"].invoke
37
+ end
38
+
39
+ desc "Retrieves the current schema version number for data migrations"
40
+ task :status => [:environment, :load_config] do
41
+ Rake::Task["db:migrate:status"].invoke
42
+ end
43
+ end
44
+
45
+ desc "Display status of data migrations"
46
+ task :version => [:environment, :load_config] do
47
+ Rake::Task["db:version"].invoke
48
+ end
49
+
50
+ desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
51
+ task :rollback => [:environment, :load_config] do
52
+ Rake::Task["db:rollback"].invoke
53
+ end
54
+
55
+ desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
56
+ task :forward => [:environment, :load_config] do
57
+ Rake::Task["db:forward"].invoke
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
File without changes
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */