rake-task-migration 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: 5f9eb0afb17227ac6a80a7692db5a2a688c5c78f
4
+ data.tar.gz: b80fe4c75a32824bc7d4b8176a4af77568ae3edf
5
+ SHA512:
6
+ metadata.gz: e9834db6eddde4d669271d8d5e42526d7bcd3ce629f0ad39cc327eb2b27c1c1294e289070da69d7aa2e0160ceb1fcc757d5c9d7c3046574a54f5c36874ba9cce
7
+ data.tar.gz: c0752318635b5eafdf61615d19afc457af2eab1946c32ffc175c0369995a5be83f756e123c6cc44cb7e7033ea00dd8e48e2de94b760dc7c2a4253085ee8efc19
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Michael Zaccari
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,99 @@
1
+ [![Build Status](https://travis-ci.org/mzaccari/rake-task-migrations.svg?branch=master)](https://travis-ci.org/mzaccari/rake-task-migrations)
2
+ [![Code Climate](https://codeclimate.com/github/mzaccari/rake-task-migrations/badges/gpa.svg)](https://codeclimate.com/github/mzaccari/rake-task-migrations)
3
+ [![Test Coverage](https://codeclimate.com/github/mzaccari/rake-task-migrations/badges/coverage.svg)](https://codeclimate.com/github/mzaccari/rake-task-migrations/coverage)
4
+
5
+ # Rake Task Migrations
6
+
7
+ Heavily based on the `seed_migration` gem [found here](https://github.com/harrystech/seed_migration).
8
+
9
+ For rails projects that need to run tasks on deployment that don't quite fit in the `db:migrate` and `seed:migrate` categories, this gem migrates specified rake tasks and ensures they only run once.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'rake-task-migration'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Install and run the internal migrations
24
+
25
+ $ bundle exec rake task_migration:install:migrations
26
+ $ bundle exec rake db:migrate
27
+
28
+ That will create the table to keep track of rake task migrations.
29
+
30
+ ## Usage
31
+
32
+ Create the `lib/tasks/migrations.rake` file and add your tasks:
33
+
34
+ ```ruby
35
+ namespace :migrations do
36
+ task :migrate_user_names => :environment do
37
+ User.find_each do |user|
38
+ user.update_attributes(name: "#{user.first_name} #{user.last_name}")
39
+ end
40
+ end
41
+ end
42
+ ```
43
+
44
+ Then run the migration for your rake tasks:
45
+
46
+ ```
47
+ $ bundle exec rake tasks:migrate
48
+ == migrate_user_names: migrating =============================================
49
+ == migrate_user_names: migrated (0.0191s) ====================================
50
+ ```
51
+
52
+ Each rake task is run only once.
53
+
54
+ ## Configuration
55
+
56
+ Use an initializer file for configuration.
57
+
58
+ ### List of available configurations :
59
+
60
+ - `migration_table_name (default = 'rake_task_migrations')`
61
+ - `migration_namespace (default = :migrations)`
62
+
63
+ #### Example:
64
+
65
+ ```ruby
66
+ # config/initializers/rake_task_migration.rb
67
+
68
+ Rake::TaskMigration.config do |config|
69
+ config.migration_table_name = 'table_name'
70
+ config.migration_namespace = 'namespace'
71
+ end
72
+ ```
73
+
74
+ ## Runnings tests
75
+
76
+ ```bash
77
+ export RAILS_ENV=test
78
+ bundle exec rake app:db:create app:db:migrate
79
+ bundle exec rspec spec
80
+ ```
81
+
82
+ * Rubies: 1.9.3, 2.0, 2.1, 2.2, JRuby 1.7.x, JRuby 9.x
83
+ * Rails: 3.2, 4.0, 4.1, 4.2
84
+ * Databases: MySQL, SQLite, PostgreSQL
85
+
86
+ For more information see the [travic-ci config](https://github.com/mzaccari/rake-task-migrations/blob/master/.travis.yml).
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create new Pull Request
95
+
96
+ ## License
97
+
98
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
99
+
data/Rakefile ADDED
@@ -0,0 +1,26 @@
1
+ Dir['lib/tasks/*.rake'].each { |rake| load rake }
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
8
+
9
+ task test: :spec
10
+
11
+ begin
12
+ require 'rubocop/rake_task'
13
+ RuboCop::RakeTask.new
14
+ rescue LoadError
15
+ desc 'Run RuboCop'
16
+ task :rubocop do
17
+ $stderr.puts 'Rubocop is disabled'
18
+ end
19
+ end
20
+
21
+ task default: [:spec, :rubocop]
22
+
23
+ APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
24
+ load 'rails/tasks/engine.rake'
25
+
26
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,5 @@
1
+ class RakeTaskMigration < ActiveRecord::Base
2
+ self.table_name = Rake::TaskMigration.migration_table_name
3
+
4
+ validates :version, :runtime, :migrated_on, presence: true
5
+ end
@@ -0,0 +1,9 @@
1
+ class CreateRakeTaskMigrations < ActiveRecord::Migration
2
+ def change
3
+ create_table Rake::TaskMigration.migration_table_name do |t|
4
+ t.string :version
5
+ t.integer :runtime
6
+ t.datetime :migrated_on
7
+ end
8
+ end
9
+ end
@@ -0,0 +1 @@
1
+ require 'rake/task_migration'
@@ -0,0 +1 @@
1
+ require 'rake/task_migration'
@@ -0,0 +1,39 @@
1
+ require 'rake'
2
+ require 'rake/task_migration/version'
3
+ require 'rake/task_migration/engine'
4
+ require 'benchmark'
5
+
6
+ module Rake
7
+ module TaskMigration
8
+ autoload :Migrator, 'rake/task_migration/migrator'
9
+
10
+ DEFAULT_TABLE_NAME = 'rake_task_migrations'
11
+ DEFAULT_NAMESPACE = :migrations
12
+
13
+ class << self
14
+ mattr_accessor :migration_table_name
15
+ mattr_accessor :migration_namespace
16
+
17
+ self.migration_table_name = DEFAULT_TABLE_NAME
18
+ self.migration_namespace = DEFAULT_NAMESPACE
19
+
20
+ def config
21
+ yield self
22
+ end
23
+
24
+ def migrate
25
+ Migrator.migrate(tasks)
26
+ end
27
+
28
+ def tasks
29
+ with_namespace { |namespace| return namespace.tasks }
30
+ end
31
+
32
+ def with_namespace
33
+ Rake.application.in_namespace(migration_namespace) do |namespace|
34
+ yield namespace if block_given?
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,9 @@
1
+ require 'rails'
2
+
3
+ module Rake
4
+ module TaskMigration
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Rake::TaskMigration
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,88 @@
1
+ module Rake
2
+ module TaskMigration
3
+ class Migrator
4
+ class << self
5
+ def migrate(tasks)
6
+ new(tasks).migrate
7
+ end
8
+
9
+ def rake_task_migrations_table_name
10
+ Rake::TaskMigration.migration_table_name
11
+ end
12
+
13
+ def get_all_tasks(connection = ActiveRecord::Base.connection)
14
+ ActiveSupport::Deprecation.silence do
15
+ if connection.table_exists?(rake_task_migrations_table_name)
16
+ RakeTaskMigration.all.map { |x| x.version.to_s }.sort
17
+ else
18
+ []
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ attr_reader :tasks
25
+
26
+ def initialize(tasks)
27
+ @tasks = Array(tasks)
28
+ end
29
+
30
+ def migrate
31
+ pending_tasks.each { |task| migrate_task(task.name.to_s) }
32
+ end
33
+
34
+ private
35
+
36
+ def migrate_task(task)
37
+ fail "#{task} has already been migrated." if RakeTaskMigration.where(version: task).first
38
+
39
+ announce "#{task}: migrating"
40
+
41
+ ActiveRecord::Base.transaction do
42
+ time = Benchmark.measure do
43
+ invoke(task)
44
+ end
45
+
46
+ # Create record
47
+ migration = RakeTaskMigration.new
48
+ migration.version = task
49
+ migration.runtime = time.real.to_i
50
+ migration.migrated_on = DateTime.now
51
+ begin
52
+ migration.save!
53
+ rescue StandardError => e
54
+ puts e
55
+ end
56
+
57
+ announce "#{task}: migrated (#{format('%.4fs', time.real)})"
58
+ end
59
+ end
60
+
61
+ def invoke(task)
62
+ Rake::Task[task].invoke
63
+ end
64
+
65
+ def pending_tasks
66
+ already_migrated = migrated
67
+ tasks.reject { |task| already_migrated.include?(task.name) }
68
+ end
69
+
70
+ def migrated
71
+ @migrated_tasks || load_migrated
72
+ end
73
+
74
+ def load_migrated
75
+ @migrated_tasks = Set.new(self.class.get_all_tasks)
76
+ end
77
+
78
+ def write(text)
79
+ puts text
80
+ end
81
+
82
+ def announce(text)
83
+ length = [0, 75 - text.length].max
84
+ write format('== %s %s', text, '=' * length)
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,5 @@
1
+ module Rake
2
+ module TaskMigration
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
@@ -0,0 +1,13 @@
1
+ namespace :tasks do
2
+ task migrate: :environment do
3
+ Rake::TaskMigration.migrate
4
+ end
5
+ end
6
+
7
+ namespace :task_migration do
8
+ namespace :install do
9
+ task :migrations do
10
+ Rake::Task['rake_task_migration:install:migrations'].invoke
11
+ end
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-task-migration
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Zaccari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-11 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: '3.2'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '3.2'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6'
33
+ - !ruby/object:Gem::Dependency
34
+ name: test-unit
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '3.0'
40
+ type: :development
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '3.0'
47
+ description: Rails gem for rake task migrations
48
+ email:
49
+ - michael.zaccari@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - app/models/rake_task_migration.rb
58
+ - db/migrate/20160210030451_create_rake_task_migrations.rb
59
+ - lib/rake-task-migration.rb
60
+ - lib/rake/task/migration.rb
61
+ - lib/rake/task_migration.rb
62
+ - lib/rake/task_migration/engine.rb
63
+ - lib/rake/task_migration/migrator.rb
64
+ - lib/rake/task_migration/version.rb
65
+ - lib/tasks/task_migration.rake
66
+ homepage: https://github.com/mzaccari/rake-task-migration
67
+ licenses:
68
+ - MIT
69
+ metadata: {}
70
+ post_install_message:
71
+ rdoc_options: []
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 2.4.5.1
87
+ signing_key:
88
+ specification_version: 4
89
+ summary: Rake Task Migrations
90
+ test_files: []
91
+ has_rdoc: