plugin_migrator 0.0.1

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: 23833d371c1f5a346fe9241e40e503bce8ac96fb
4
+ data.tar.gz: fc21455c246281ef40a227ca28ed0041eccb1a19
5
+ SHA512:
6
+ metadata.gz: d3a8c9cf6ffc1752167544c88402bdb6b8baca47efbd1a0bf9fb6b567a38fec1abc9f23e4989a0841ff006712ce0c29a5923044ce5e55b6de456ffcdcdd9448e
7
+ data.tar.gz: ea521f41d397397ec7579984f1753eb7458464a6300920fff1f32d97c7526ea52760cb86173cc3a7d1a080151cb63fbde40cdf16492f91178d5288a4ba409653
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = PluginMigrator
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'PluginMigrator'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+ Bundler::GemHelper.install_tasks
21
+
@@ -0,0 +1,10 @@
1
+ require "string-cases"
2
+ load "#{File.dirname(__FILE__)}/tasks/plugin_migrator_tasks.rake" if ::Kernel.const_defined?(:Rake)
3
+
4
+ module PluginMigrator
5
+ def self.const_missing(name)
6
+ require_relative "plugin_migrator/#{::StringCases.camel_to_snake(name)}"
7
+ raise LoadError, "Still not loaded: '#{name}'." unless ::PluginMigrator.const_defined?(name)
8
+ return ::PluginMigrator.const_get(name)
9
+ end
10
+ end
@@ -0,0 +1,28 @@
1
+ require "string-cases"
2
+
3
+ class PluginMigrator::Migration
4
+ def initialize(args)
5
+ @args = args
6
+ @version = args[:version]
7
+ @version_helper = args[:version_helper]
8
+ end
9
+
10
+ def instance
11
+ require @args[:path]
12
+ @instance ||= @args[:class_name].constantize.new
13
+ end
14
+
15
+ def migrated?
16
+ @version_helper.version_exists?(@version)
17
+ end
18
+
19
+ def migrate!
20
+ instance.migrate(:up)
21
+ @version_helper.add_version(@version)
22
+ end
23
+
24
+ def rollback!
25
+ instance.migrate(:down)
26
+ @version_helper.remove_version(@version)
27
+ end
28
+ end
@@ -0,0 +1,29 @@
1
+ class PluginMigrator::MigrationsHelper
2
+ def migrations
3
+ version_helper = ::PluginMigrator::VersionHelper.new
4
+
5
+ Gem.loaded_specs.values.each do |gem_i|
6
+ gem_path = gem_i.gem_dir
7
+ plugin_migrator_path = "#{gem_path}/db/plugin_migrate"
8
+ next unless File.exists?(plugin_migrator_path)
9
+
10
+ Dir.foreach(plugin_migrator_path) do |file|
11
+ match = file.match(/^(\d+)_(.+)\.rb$/)
12
+ next unless match
13
+
14
+ migration_path = "#{plugin_migrator_path}/#{file}"
15
+ migration_class_name = StringCases.snake_to_camel(match[2])
16
+ migration_version = match[1].to_i
17
+
18
+ migration = ::PluginMigrator::Migration.new(
19
+ :path => migration_path,
20
+ :version => migration_version,
21
+ :class_name => migration_class_name,
22
+ :version_helper => version_helper
23
+ )
24
+
25
+ yield migration
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module PluginMigrator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ class PluginMigrator::VersionHelper
2
+ def version_exists?(version_number)
3
+ ActiveRecord::Base.connection.execute("SELECT * FROM schema_migrations WHERE version = '#{version_number}' LIMIT 1").any?
4
+ end
5
+
6
+ def add_version(version_number)
7
+ ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations (version) VALUES ('#{version_number}')")
8
+ end
9
+
10
+ def remove_version(version_number)
11
+ ActiveRecord::Base.connection.execute("DELETE FROM schema_migrations WHERE version = '#{version_number}'")
12
+ end
13
+ end
@@ -0,0 +1,19 @@
1
+ namespace :plugin_migrator do
2
+ task "migrate" => :environment do
3
+ ActiveRecord::Migration.verbose = true
4
+
5
+ PluginMigrator::MigrationsHelper.new.migrations do |migration|
6
+ migration.migrate! if !migration.migrated?
7
+ end
8
+
9
+ Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
10
+ end
11
+
12
+ task "rollback" => :environment do
13
+ ActiveRecord::Migration.verbose = true
14
+
15
+ PluginMigrator::MigrationsHelper.new.migrations do |migration|
16
+ migration.rollback! if migration.migrated?
17
+ end
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plugin_migrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Kasper Johansen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-05-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: 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: sqlite3
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Migration of plugins DB migrations easily.
42
+ email:
43
+ - k@spernj.org
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - lib/plugin_migrator/version_helper.rb
49
+ - lib/plugin_migrator/version.rb
50
+ - lib/plugin_migrator/migration.rb
51
+ - lib/plugin_migrator/migrations_helper.rb
52
+ - lib/plugin_migrator.rb
53
+ - lib/tasks/plugin_migrator_tasks.rake
54
+ - MIT-LICENSE
55
+ - Rakefile
56
+ - README.rdoc
57
+ homepage: https://www.github.com/kaspernj/plugin_migrator
58
+ licenses: []
59
+ metadata: {}
60
+ post_install_message:
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubyforge_project:
76
+ rubygems_version: 2.0.7
77
+ signing_key:
78
+ specification_version: 4
79
+ summary: Migrate plugin DB easily.
80
+ test_files: []