rake-migrations 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 3f287b3ea554d83ddf5ca6e8be12a00a0d51330a
4
+ data.tar.gz: 5b91b36fa62c01d3b905e048b88c5c60eb4a7263
5
+ SHA512:
6
+ metadata.gz: 42cd0935e07bdbcf5da9770eb53a6d7cc5fcdf65eeb1a6eaa00d3ef2f82500c3976eac8d1ee5dc27002e2478d943eb6038212dfc0cd0ee9eedb960801c85bac0
7
+ data.tar.gz: e86e3e8188146d535b0cfd9a49a4d71fd9a52ef8dd44d2a468d49c8e93985195c0b824b8cea620699973fbf51982448e8403d862d1ac1256c6682f1cfd240647
@@ -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.
@@ -0,0 +1,94 @@
1
+ <a href="https://codeclimate.com/github/mzaccari/rake-migrations"><img src="https://codeclimate.com/github/mzaccari/rake-migrations/badges/gpa.svg" /></a>
2
+
3
+ # Rake::Migrations
4
+
5
+ Heavily based on the `seed_migration` gem [found here](https://github.com/harrystech/seed_migration).
6
+
7
+ 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 lets you run configured rake tasks `once` or `every` time the command is run.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'rake-migrations'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install rake-migrations
24
+
25
+ ## Usage
26
+
27
+ Given a set of rake tasks you might need to run on deployment:
28
+
29
+ ```ruby
30
+ namespace :users do
31
+ # Run this only once
32
+ task :migrate_names => :environment do
33
+ User.find_each do |user|
34
+ user.update_attributes(name: "#{user.first_name} #{user.last_name}")
35
+ end
36
+ end
37
+ end
38
+
39
+ namespace :deployment do
40
+ # Run this on each deployment
41
+ task :restart_jboss => :environment do
42
+ FileUtils.touch(Rails.root.join('tmp', 'restart.txt'))
43
+ end
44
+ end
45
+ ```
46
+
47
+ Create a file at `config/tasks.yml` with your rake migration configuration:
48
+
49
+ ```yml
50
+ tasks:
51
+ user_name_migration:
52
+ command: users:migrate_names
53
+ frequency: :once # default
54
+
55
+ restart_jboss:
56
+ command: deployment:restart_jboss
57
+ frequency: :every
58
+ ```
59
+
60
+ Then run the migration for your configured rake tasks:
61
+
62
+ ```
63
+ $ bundle exec rake tasks:migrate
64
+ == user_name_migration: migrating =============================================
65
+ == user_name_migration: migrated (0.0191s) ====================================
66
+ == restart_jboss: migrating ===================================================
67
+ == restart_jboss: migrated (0.0005s) ==========================================
68
+ ```
69
+
70
+ Notice that the user name task should only be run once. If we re-run the rake task migration it will not be included:
71
+ ```
72
+ $ bundle exec rake tasks:migrate
73
+ == restart_jboss: migrating ===================================================
74
+ == restart_jboss: migrated (0.0003s) ==========================================
75
+ ```
76
+
77
+
78
+
79
+
80
+ ## Development
81
+
82
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
83
+
84
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
85
+
86
+ ## Contributing
87
+
88
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mzaccari/rake-migrations.
89
+
90
+
91
+ ## License
92
+
93
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
94
+
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,13 @@
1
+ require "rake"
2
+ require "rake/migrations/version"
3
+ require "rake/migrations/engine"
4
+ require "yaml"
5
+
6
+ module Rake
7
+ module Migrations
8
+ autoload :Configuration, 'rake/migrations/configuration'
9
+ autoload :Manifest, 'rake/migrations/manifest'
10
+ autoload :Migrator, 'rake/migrations/migrator'
11
+ autoload :Task, 'rake/migrations/task'
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ class Rake::Migrations::Configuration
2
+
3
+ DEFAULT_FILE_PATH = Rails.root.join('config', 'tasks.yml')
4
+
5
+ def self.load(file_path = DEFAULT_FILE_PATH)
6
+ config = new(file_path)
7
+ config.load
8
+ config
9
+ end
10
+
11
+ attr_reader :file_path, :tasks
12
+
13
+ def initialize(file_path)
14
+ @file_path = file_path
15
+ @tasks = []
16
+ end
17
+
18
+ def load
19
+ config = YAML.load_file(file_path).with_indifferent_access
20
+ (config[:tasks] || []).each do |task|
21
+ add_task Rake::Migrations::Task.new(*task)
22
+ end
23
+ end
24
+
25
+ def add_task(task)
26
+ @tasks << task unless @tasks.include?(task)
27
+ end
28
+
29
+ end
@@ -0,0 +1,10 @@
1
+ require 'rails'
2
+
3
+ module Rake
4
+ module Migrations
5
+ class Engine < ::Rails::Engine
6
+ isolate_namespace Rake::Migrations
7
+
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,49 @@
1
+ class Rake::Migrations::Manifest
2
+
3
+ DEFAULT_FILE_PATH = Rails.root.join('.rake.migrations.yml')
4
+
5
+ def self.load(file_path = DEFAULT_FILE_PATH)
6
+ manifest = new(file_path)
7
+ manifest.load
8
+ manifest
9
+ end
10
+
11
+ attr_reader :file_path, :tasks
12
+
13
+ def initialize(file_path)
14
+ @file_path = file_path
15
+ @tasks = []
16
+ end
17
+
18
+ def add_task(task)
19
+ @tasks << task unless @tasks.include?(task)
20
+ end
21
+
22
+ def update(task)
23
+ add_task(task) && save
24
+ end
25
+
26
+ def load
27
+ manifest = YAML.load_file(file_path).with_indifferent_access
28
+ (manifest[:tasks] || []).each do |task|
29
+ add_task Rake::Migrations::Task.new(*task)
30
+ end
31
+ rescue Errno::ENOENT
32
+ end
33
+
34
+ def save
35
+ File.open(file_path, 'w') { |f| f.write to_yaml }
36
+ end
37
+
38
+ def to_h
39
+ {
40
+ tasks: tasks.map(&:name)
41
+ }
42
+ end
43
+ alias_method :to_hash, :to_h
44
+
45
+ def to_yaml
46
+ to_hash.to_yaml
47
+ end
48
+
49
+ end
@@ -0,0 +1,72 @@
1
+ class Rake::Migrations::Migrator
2
+
3
+ def self.run_task_migrations
4
+ get_task_migrations.each do |task|
5
+ invoke task
6
+ end
7
+ end
8
+
9
+ # tasks to run every time + tasks that haven't been run yet
10
+ def self.get_task_migrations
11
+ config.tasks.select do |task|
12
+ task.run_every_time? || manifest.tasks.exclude?(task)
13
+ end
14
+ end
15
+
16
+ def self.manifest
17
+ @manifest ||= Rake::Migrations::Manifest.load
18
+ end
19
+
20
+ def self.config
21
+ @config ||= Rake::Migrations::Configuration.load
22
+ end
23
+
24
+ def self.invoke(task)
25
+ new(task).invoke
26
+ end
27
+
28
+ attr_reader :task
29
+
30
+ def initialize(task)
31
+ @task = task
32
+ end
33
+
34
+ def invoke
35
+ with_handler do
36
+ Rake::Task[task.command].invoke
37
+ end
38
+ end
39
+
40
+ def manifest
41
+ self.class.manifest
42
+ end
43
+
44
+ private
45
+
46
+ def with_handler(&block)
47
+ announce "migrating"
48
+
49
+ time = Benchmark.measure do
50
+ block.call
51
+ end
52
+
53
+ announce "migrated (%.4fs)" % time.real;
54
+
55
+ manifest.update(task)
56
+ end
57
+
58
+ def write(text)
59
+ puts(text)
60
+ end
61
+
62
+ def say(message, subitem=false)
63
+ write "#{subitem ? " ->" : "--"} #{message}"
64
+ end
65
+
66
+ def announce(message)
67
+ text = "#{task.name}: #{message}"
68
+ length = [0, 75 - text.length].max
69
+ write "== %s %s" % [text, "=" * length]
70
+ end
71
+
72
+ end
@@ -0,0 +1,36 @@
1
+ class Rake::Migrations::Task
2
+
3
+ RUN_ONCE = :once
4
+ RUN_EVERY_TIME = :every
5
+
6
+ attr_reader :name, :command, :frequency
7
+
8
+ def initialize(name, attrs = {})
9
+ @name = name
10
+ @command = attrs[:command]
11
+ @frequency = attrs[:frequency] || RUN_ONCE
12
+ end
13
+
14
+ def run_every_time?
15
+ frequency == RUN_EVERY_TIME
16
+ end
17
+
18
+ def run_once?
19
+ frequency == RUN_ONCE
20
+ end
21
+
22
+ def to_h
23
+ {
24
+ name => {
25
+ command: command,
26
+ frequency: frequency
27
+ }
28
+ }
29
+ end
30
+
31
+ def ==(other)
32
+ name == other.name
33
+ end
34
+ alias_method :equal?, :==
35
+
36
+ end
@@ -0,0 +1,5 @@
1
+ module Rake
2
+ module Migrations
3
+ VERSION = '0.2.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'rake/migrations'
@@ -0,0 +1,7 @@
1
+ namespace :tasks do
2
+
3
+ task :migrate => :environment do
4
+ Rake::Migrations::Migrator.run_task_migrations
5
+ end
6
+
7
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rake-migrations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Zaccari
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-01-22 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
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ description: Rails gem for rake task migrations
28
+ email:
29
+ - michael.zaccari@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE.txt
35
+ - README.md
36
+ - Rakefile
37
+ - lib/rake/migrations.rb
38
+ - lib/rake/migrations/configuration.rb
39
+ - lib/rake/migrations/engine.rb
40
+ - lib/rake/migrations/manifest.rb
41
+ - lib/rake/migrations/migrator.rb
42
+ - lib/rake/migrations/task.rb
43
+ - lib/rake/migrations/version.rb
44
+ - lib/rake_migrations.rb
45
+ - lib/tasks/rake_migrations.rake
46
+ homepage: https://github.com/mzaccari/rake-migrations
47
+ licenses:
48
+ - MIT
49
+ metadata: {}
50
+ post_install_message:
51
+ rdoc_options: []
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 2.4.5.1
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Rake Task Migrations
70
+ test_files: []
71
+ has_rdoc: