jonbell-mongrations 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +56 -0
- data/Rakefile +23 -0
- data/VERSION +1 -0
- data/generators/mongration/mongration_generator.rb +15 -0
- data/generators/mongration/templates/mongration.rb +7 -0
- data/init.rb +8 -0
- data/install.rb +1 -0
- data/lib/mongo_mapper/exceptions.rb +30 -0
- data/lib/mongo_mapper/migration_proxy.rb +22 -0
- data/lib/mongo_mapper/migrator.rb +169 -0
- data/lib/mongo_mapper/mongration.rb +96 -0
- data/lib/mongo_mapper/schema_migration.rb +8 -0
- data/lib/mongrations.rb +17 -0
- data/lib/mongrations/railtie.rb +10 -0
- data/lib/tasks/mongo.rake +39 -0
- data/tasks/mongo.rake +39 -0
- data/test/mongrations_test.rb +8 -0
- data/test/test_helper.rb +3 -0
- data/uninstall.rb +1 -0
- metadata +99 -0
data/.gitignore
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 [name of plugin creator]
|
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.markdown
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Mongrations
|
2
|
+
===========
|
3
|
+
|
4
|
+
Mongrations aims to be similar to ActiveRecord's data migrations, except
|
5
|
+
instead of worrying about schema changes, offering a way to change data when
|
6
|
+
necessary.
|
7
|
+
|
8
|
+
Example: You decide after a few weeks of running an application that you want
|
9
|
+
to add timestamps, and want to display the created_at time for an application.
|
10
|
+
|
11
|
+
You could put a default timestamp into the model (and in this case that might
|
12
|
+
be appropriate), but you might also just want to say, "everything needs to get
|
13
|
+
a new timestamp that doesn't already have one, let's say when the US beat
|
14
|
+
Canada in hockey in the Olympics."
|
15
|
+
|
16
|
+
|
17
|
+
Example
|
18
|
+
=======
|
19
|
+
|
20
|
+
To generate a mongration, just do:
|
21
|
+
|
22
|
+
`script/generate mongration whatever_you_want_your_mongration_to_be`
|
23
|
+
|
24
|
+
To run it, do
|
25
|
+
|
26
|
+
`rake db:mongrate`
|
27
|
+
|
28
|
+
Other rake tasks added have been `db:mongrate:redo`, `db:mongrate:up`, `db:mongrate:down`, `db:mongrate:rollback`.
|
29
|
+
|
30
|
+
|
31
|
+
Dependencies
|
32
|
+
============
|
33
|
+
|
34
|
+
You need Mongo and MongoMapper for this to be of any use.
|
35
|
+
|
36
|
+
Also, this has only been tested on Rails 3.2.3.
|
37
|
+
|
38
|
+
Disclaimer
|
39
|
+
==========
|
40
|
+
|
41
|
+
*This is not ready for production*
|
42
|
+
|
43
|
+
I just adapted this, at 10:30PM, half watching the Olympics. I'm not responsible
|
44
|
+
for any damage to your data, your mongodb, your bongos, your cat, your wife, or
|
45
|
+
your kids as a result of installing this plugin.
|
46
|
+
|
47
|
+
Give it a few days. Please report bugs.
|
48
|
+
|
49
|
+
Credit
|
50
|
+
======
|
51
|
+
Original code is from Rails 2.3.5 and ActiveRecord 2.3.5, now adapted to work
|
52
|
+
with MongoMapper.
|
53
|
+
|
54
|
+
License
|
55
|
+
=======
|
56
|
+
Released under the MIT license
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/rdoctask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |gem|
|
9
|
+
gem.name = "jonbell-mongrations"
|
10
|
+
gem.summary = %Q{Data migrating for MongoMapper}
|
11
|
+
gem.description = %Q{Mongrations aims to be similar to ActiveRecord's data migrations, except
|
12
|
+
instead of worrying about schema changes, offering a way to change data when
|
13
|
+
necessary}
|
14
|
+
gem.email = "jonbell@spamcop.net"
|
15
|
+
gem.homepage = "http://github.com/jonbell/mongrations"
|
16
|
+
gem.authors = ["jonbell"]
|
17
|
+
gem.add_dependency "mongo_mapper", ">= 0.11.0"
|
18
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
19
|
+
end
|
20
|
+
Jeweler::GemcutterTasks.new
|
21
|
+
rescue LoadError
|
22
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
23
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,15 @@
|
|
1
|
+
Rails::Generator::Commands::Base.class_eval do
|
2
|
+
def next_migration_string(padding = 3)
|
3
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
class MongrationGenerator < Rails::Generator::NamedBase
|
8
|
+
|
9
|
+
def manifest
|
10
|
+
record do |m|
|
11
|
+
m.directory File.join('db/mongrations')
|
12
|
+
m.migration_template 'mongration.rb', 'db/mongrations'
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
# Include hook code here
|
2
|
+
require "mongo_mapper"
|
3
|
+
|
4
|
+
require File.join(File.dirname(__FILE__), "lib", "mongration")
|
5
|
+
require File.join(File.dirname(__FILE__), "lib", "mongo_mapper", "mongration")
|
6
|
+
require File.join(File.dirname(__FILE__), "lib", "mongo_mapper", "migration_proxy")
|
7
|
+
require File.join(File.dirname(__FILE__), "lib", "mongo_mapper", "migrator")
|
8
|
+
require File.join(File.dirname(__FILE__), "lib", "mongo_mapper", "schema_migration")
|
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
class MongoMapperError < StandardError; end
|
3
|
+
|
4
|
+
class IrreversibleMigration < MongoMapperError#:nodoc:
|
5
|
+
end
|
6
|
+
|
7
|
+
class DuplicateMigrationVersionError < MongoMapperError#:nodoc:
|
8
|
+
def initialize(version)
|
9
|
+
super("Multiple migrations have the version number #{version}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class DuplicateMigrationNameError < MongoMapperError#:nodoc:
|
14
|
+
def initialize(name)
|
15
|
+
super("Multiple migrations have the name #{name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class UnknownMigrationVersionError < MongoMapperError #:nodoc:
|
20
|
+
def initialize(version)
|
21
|
+
super("No migration with version number #{version}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class IllegalMigrationNameError < MongoMapperError#:nodoc:
|
26
|
+
def initialize(name)
|
27
|
+
super("Illegal name for migration file: #{name}\n\t(only lower case letters, numbers, and '_' allowed)")
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# MigrationProxy is used to defer loading of the actual migration classes
|
2
|
+
# until they are needed
|
3
|
+
module MongoMapper
|
4
|
+
class MigrationProxy
|
5
|
+
|
6
|
+
attr_accessor :name, :version, :filename
|
7
|
+
|
8
|
+
delegate :migrate, :announce, :write, :to=>:migration
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def migration
|
13
|
+
@migration ||= load_migration
|
14
|
+
end
|
15
|
+
|
16
|
+
def load_migration
|
17
|
+
load(filename)
|
18
|
+
name.constantize
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,169 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
class Migrator#:nodoc:
|
3
|
+
class << self
|
4
|
+
def migrate(migrations_path, target_version = nil)
|
5
|
+
case
|
6
|
+
when target_version.nil? then up(migrations_path, target_version)
|
7
|
+
when current_version > target_version then down(migrations_path, target_version)
|
8
|
+
else up(migrations_path, target_version)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def rollback(migrations_path, steps=1)
|
13
|
+
migrator = self.new(:down, migrations_path)
|
14
|
+
start_index = migrator.migrations.index(migrator.current_migration)
|
15
|
+
|
16
|
+
return unless start_index
|
17
|
+
|
18
|
+
finish = migrator.migrations[start_index + steps]
|
19
|
+
down(migrations_path, finish ? finish.version : 0)
|
20
|
+
end
|
21
|
+
|
22
|
+
def up(migrations_path, target_version = nil)
|
23
|
+
self.new(:up, migrations_path, target_version).migrate
|
24
|
+
end
|
25
|
+
|
26
|
+
def down(migrations_path, target_version = nil)
|
27
|
+
self.new(:down, migrations_path, target_version).migrate
|
28
|
+
end
|
29
|
+
|
30
|
+
def run(direction, migrations_path, target_version)
|
31
|
+
self.new(direction, migrations_path, target_version).run
|
32
|
+
end
|
33
|
+
|
34
|
+
def get_all_versions
|
35
|
+
MongoMapper::SchemaMigration.all.map{|sm| sm.version.to_i}.sort
|
36
|
+
end
|
37
|
+
|
38
|
+
def current_version
|
39
|
+
get_all_versions.max || 0
|
40
|
+
end
|
41
|
+
|
42
|
+
def proper_table_name(name)
|
43
|
+
# Use the Active Record objects own table_name, or pre/suffix from MongoMapper::Base if name is a symbol/string
|
44
|
+
name.table_name rescue "#{MongoMapper::Base.table_name_prefix}#{name}#{MongoMapper::Base.table_name_suffix}"
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def initialize(direction, migrations_path, target_version = nil)
|
49
|
+
@direction, @migrations_path, @target_version = direction, migrations_path, target_version
|
50
|
+
end
|
51
|
+
|
52
|
+
def current_version
|
53
|
+
migrated.last || 0
|
54
|
+
end
|
55
|
+
|
56
|
+
def current_migration
|
57
|
+
migrations.detect { |m| m.version == current_version }
|
58
|
+
end
|
59
|
+
|
60
|
+
def run
|
61
|
+
target = migrations.detect { |m| m.version == @target_version }
|
62
|
+
raise UnknownMigrationVersionError.new(@target_version) if target.nil?
|
63
|
+
unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
|
64
|
+
target.migrate(@direction)
|
65
|
+
record_version_state_after_migrating(target.version)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def migrate
|
70
|
+
current = migrations.detect { |m| m.version == current_version }
|
71
|
+
target = migrations.detect { |m| m.version == @target_version }
|
72
|
+
|
73
|
+
if target.nil? && !@target_version.nil? && @target_version > 0
|
74
|
+
raise UnknownMigrationVersionError.new(@target_version)
|
75
|
+
end
|
76
|
+
|
77
|
+
start = up? ? 0 : (migrations.index(current) || 0)
|
78
|
+
finish = migrations.index(target) || migrations.size - 1
|
79
|
+
runnable = migrations[start..finish]
|
80
|
+
|
81
|
+
# skip the last migration if we're headed down, but not ALL the way down
|
82
|
+
runnable.pop if down? && !target.nil?
|
83
|
+
|
84
|
+
runnable.each do |migration|
|
85
|
+
# On our way up, we skip migrating the ones we've already migrated
|
86
|
+
next if up? && migrated.include?(migration.version.to_i)
|
87
|
+
|
88
|
+
# On our way down, we skip reverting the ones we've never migrated
|
89
|
+
if down? && !migrated.include?(migration.version.to_i)
|
90
|
+
migration.announce 'never migrated, skipping'; migration.write
|
91
|
+
next
|
92
|
+
end
|
93
|
+
|
94
|
+
migration.migrate(@direction)
|
95
|
+
record_version_state_after_migrating(migration.version)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def migrations
|
100
|
+
@migrations ||= begin
|
101
|
+
files = Dir["#{@migrations_path}/[0-9]*_*.rb"]
|
102
|
+
|
103
|
+
migrations = files.inject([]) do |klasses, file|
|
104
|
+
version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
|
105
|
+
|
106
|
+
raise IllegalMigrationNameError.new(file) unless version
|
107
|
+
version = version.to_i
|
108
|
+
|
109
|
+
if klasses.detect { |m| m.version == version }
|
110
|
+
raise DuplicateMigrationVersionError.new(version)
|
111
|
+
end
|
112
|
+
|
113
|
+
if klasses.detect { |m| m.name == name.camelize }
|
114
|
+
raise DuplicateMigrationNameError.new(name.camelize)
|
115
|
+
end
|
116
|
+
|
117
|
+
klasses << returning(MigrationProxy.new) do |migration|
|
118
|
+
migration.name = name.camelize
|
119
|
+
migration.version = version
|
120
|
+
migration.filename = file
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
migrations = migrations.sort_by(&:version)
|
125
|
+
down? ? migrations.reverse : migrations
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def pending_migrations
|
130
|
+
already_migrated = migrated
|
131
|
+
migrations.reject { |m| already_migrated.include?(m.version.to_i) }
|
132
|
+
end
|
133
|
+
|
134
|
+
def migrated
|
135
|
+
@migrated_versions ||= self.class.get_all_versions
|
136
|
+
end
|
137
|
+
|
138
|
+
private
|
139
|
+
def record_version_state_after_migrating(version)
|
140
|
+
@migrated_versions ||= []
|
141
|
+
if down?
|
142
|
+
@migrated_versions.delete(version.to_i)
|
143
|
+
sm = MongoMapper::SchemaMigration.find_by_version(version.to_s)
|
144
|
+
sm && sm.delete
|
145
|
+
else
|
146
|
+
@migrated_versions.push(version.to_i).sort!
|
147
|
+
MongoMapper::SchemaMigration.create(:version => version)
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def up?
|
152
|
+
@direction == :up
|
153
|
+
end
|
154
|
+
|
155
|
+
def down?
|
156
|
+
@direction == :down
|
157
|
+
end
|
158
|
+
|
159
|
+
# Wrap the migration in a transaction only if supported by the adapter.
|
160
|
+
def ddl_transaction(&block)
|
161
|
+
if Base.connection.supports_ddl_transactions?
|
162
|
+
Base.transaction { block.call }
|
163
|
+
else
|
164
|
+
block.call
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module MongoMapper
|
2
|
+
class Mongration
|
3
|
+
@@verbose = true
|
4
|
+
cattr_accessor :verbose
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def up_with_benchmarks #:nodoc:
|
8
|
+
migrate(:up)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Execute this migration in the named direction
|
12
|
+
def migrate(direction)
|
13
|
+
return unless respond_to?(direction)
|
14
|
+
|
15
|
+
case direction
|
16
|
+
when :up then announce "migrating"
|
17
|
+
when :down then announce "reverting"
|
18
|
+
end
|
19
|
+
|
20
|
+
result = nil
|
21
|
+
time = Benchmark.measure { result = send("#{direction}_without_benchmarks") }
|
22
|
+
|
23
|
+
case direction
|
24
|
+
when :up then announce "migrated (%.4fs)" % time.real; write
|
25
|
+
when :down then announce "reverted (%.4fs)" % time.real; write
|
26
|
+
end
|
27
|
+
|
28
|
+
result
|
29
|
+
end
|
30
|
+
|
31
|
+
# Because the method added may do an alias_method, it can be invoked
|
32
|
+
# recursively. We use @ignore_new_methods as a guard to indicate whether
|
33
|
+
# it is safe for the call to proceed.
|
34
|
+
def singleton_method_added(sym) #:nodoc:
|
35
|
+
return if defined?(@ignore_new_methods) && @ignore_new_methods
|
36
|
+
|
37
|
+
begin
|
38
|
+
@ignore_new_methods = true
|
39
|
+
|
40
|
+
case sym
|
41
|
+
when :up, :down
|
42
|
+
klass = (class << self; self; end)
|
43
|
+
klass.send(:alias_method_chain, sym, "benchmarks")
|
44
|
+
end
|
45
|
+
ensure
|
46
|
+
@ignore_new_methods = false
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def write(text="")
|
51
|
+
puts(text) if verbose
|
52
|
+
end
|
53
|
+
|
54
|
+
def announce(message)
|
55
|
+
text = "#{@version} #{name}: #{message}"
|
56
|
+
length = [0, 75 - text.length].max
|
57
|
+
write "== %s %s" % [text, "=" * length]
|
58
|
+
end
|
59
|
+
|
60
|
+
def say(message, subitem=false)
|
61
|
+
write "#{subitem ? " ->" : "--"} #{message}"
|
62
|
+
end
|
63
|
+
|
64
|
+
def say_with_time(message)
|
65
|
+
say(message)
|
66
|
+
result = nil
|
67
|
+
time = Benchmark.measure { result = yield }
|
68
|
+
say "%.4fs" % time.real, :subitem
|
69
|
+
say("#{result} rows", :subitem) if result.is_a?(Integer)
|
70
|
+
result
|
71
|
+
end
|
72
|
+
|
73
|
+
def suppress_messages
|
74
|
+
save, self.verbose = verbose, false
|
75
|
+
yield
|
76
|
+
ensure
|
77
|
+
self.verbose = save
|
78
|
+
end
|
79
|
+
|
80
|
+
def connection
|
81
|
+
MongoMapper.connection
|
82
|
+
end
|
83
|
+
|
84
|
+
def method_missing(method, *arguments, &block)
|
85
|
+
arg_list = arguments.map(&:inspect) * ', '
|
86
|
+
|
87
|
+
say_with_time "#{method}(#{arg_list})" do
|
88
|
+
unless arguments.empty? || method == :execute
|
89
|
+
arguments[0] = Migrator.proper_table_name(arguments.first)
|
90
|
+
end
|
91
|
+
connection.send(method, *arguments, &block)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/lib/mongrations.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
module MongoMapper
|
4
|
+
autoload :MongoMapperError, 'mongo_mapper/exceptions'
|
5
|
+
autoload :IrreversibleMigration, 'mongo_mapper/exceptions'
|
6
|
+
autoload :DuplicateMigrationVersionError, 'mongo_mapper/exceptions'
|
7
|
+
autoload :DuplicateMigrationNameError, 'mongo_mapper/exceptions'
|
8
|
+
autoload :UnknownMigrationVersionError, 'mongo_mapper/exceptions'
|
9
|
+
autoload :IllegalMigrationNameError, 'mongo_mapper/exceptions'
|
10
|
+
|
11
|
+
autoload :MigrationProxy, 'mongo_mapper/migration_proxy'
|
12
|
+
autoload :Migrator, 'mongo_mapper/migrator'
|
13
|
+
autoload :Mongration, 'mongo_mapper/mongration'
|
14
|
+
autoload :SchemaMigration, 'mongo_mapper/schema_migration'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'mongrations/railtie' if defined?(Rails)
|
@@ -0,0 +1,39 @@
|
|
1
|
+
namespace :mongo do
|
2
|
+
desc "Migrate the database through scripts in db/mongrations. Target specific version with VERSION=x."
|
3
|
+
task :mongrate => :environment do
|
4
|
+
MongoMapper::Migrator.migrate("db/mongrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
5
|
+
end
|
6
|
+
|
7
|
+
namespace :mongrate do
|
8
|
+
desc 'Rollbacks the database one mongration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
|
9
|
+
task :redo => :environment do
|
10
|
+
if ENV["VERSION"]
|
11
|
+
Rake::Task["mongo:mongrate:down"].invoke
|
12
|
+
Rake::Task["mongo:mongrate:up"].invoke
|
13
|
+
else
|
14
|
+
Rake::Task["mongo:mongo_rollback"].invoke
|
15
|
+
Rake::Task["mongo:mongrate"].invoke
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Runs the "up" for a given mongration VERSION.'
|
20
|
+
task :up => :environment do
|
21
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
22
|
+
raise "VERSION is required" unless version
|
23
|
+
MongoMapper::Migrator.run(:up, "db/mongrations/", version)
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Runs the "down" for a given mongration VERSION.'
|
27
|
+
task :down => :environment do
|
28
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
29
|
+
raise "VERSION is required" unless version
|
30
|
+
MongoMapper::Migrator.run(:down, "db/mongrations/", version)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
35
|
+
task :mongo_rollback => :environment do
|
36
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
37
|
+
MongoMapper::Migrator.rollback('db/mongrations/', step)
|
38
|
+
end
|
39
|
+
end
|
data/tasks/mongo.rake
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
namespace :mongo do
|
2
|
+
desc "Migrate the database through scripts in db/mongrations. Target specific version with VERSION=x."
|
3
|
+
task :mongrate => :environment do
|
4
|
+
MongoMapper::Migrator.migrate("db/mongrations/", ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
|
5
|
+
end
|
6
|
+
|
7
|
+
namespace :mongrate do
|
8
|
+
desc 'Rollbacks the database one mongration and re migrate up. If you want to rollback more than one step, define STEP=x. Target specific version with VERSION=x.'
|
9
|
+
task :redo => :environment do
|
10
|
+
if ENV["VERSION"]
|
11
|
+
Rake::Task["mongo:mongrate:down"].invoke
|
12
|
+
Rake::Task["mongo:mongrate:up"].invoke
|
13
|
+
else
|
14
|
+
Rake::Task["mongo:mongo_rollback"].invoke
|
15
|
+
Rake::Task["mongo:mongrate"].invoke
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
desc 'Runs the "up" for a given mongration VERSION.'
|
20
|
+
task :up => :environment do
|
21
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
22
|
+
raise "VERSION is required" unless version
|
23
|
+
MongoMapper::Migrator.run(:up, "db/mongrations/", version)
|
24
|
+
end
|
25
|
+
|
26
|
+
desc 'Runs the "down" for a given mongration VERSION.'
|
27
|
+
task :down => :environment do
|
28
|
+
version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
|
29
|
+
raise "VERSION is required" unless version
|
30
|
+
MongoMapper::Migrator.run(:down, "db/mongrations/", version)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
|
35
|
+
task :mongo_rollback => :environment do
|
36
|
+
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
|
37
|
+
MongoMapper::Migrator.rollback('db/mongrations/', step)
|
38
|
+
end
|
39
|
+
end
|
data/test/test_helper.rb
ADDED
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jonbell-mongrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- jonbell
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2012-04-05 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: mongo_mapper
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
29
|
+
- 11
|
30
|
+
- 0
|
31
|
+
version: 0.11.0
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
description: |-
|
35
|
+
Mongrations aims to be similar to ActiveRecord's data migrations, except
|
36
|
+
instead of worrying about schema changes, offering a way to change data when
|
37
|
+
necessary
|
38
|
+
email: jonbell@spamcop.net
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.markdown
|
45
|
+
files:
|
46
|
+
- .gitignore
|
47
|
+
- MIT-LICENSE
|
48
|
+
- README.markdown
|
49
|
+
- Rakefile
|
50
|
+
- VERSION
|
51
|
+
- generators/mongration/mongration_generator.rb
|
52
|
+
- generators/mongration/templates/mongration.rb
|
53
|
+
- init.rb
|
54
|
+
- install.rb
|
55
|
+
- lib/mongo_mapper/exceptions.rb
|
56
|
+
- lib/mongo_mapper/migration_proxy.rb
|
57
|
+
- lib/mongo_mapper/migrator.rb
|
58
|
+
- lib/mongo_mapper/mongration.rb
|
59
|
+
- lib/mongo_mapper/schema_migration.rb
|
60
|
+
- lib/mongrations.rb
|
61
|
+
- lib/mongrations/railtie.rb
|
62
|
+
- lib/tasks/mongo.rake
|
63
|
+
- tasks/mongo.rake
|
64
|
+
- test/mongrations_test.rb
|
65
|
+
- test/test_helper.rb
|
66
|
+
- uninstall.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/jonbell/mongrations
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options:
|
73
|
+
- --charset=UTF-8
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
segments:
|
81
|
+
- 0
|
82
|
+
version: "0"
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
segments:
|
88
|
+
- 0
|
89
|
+
version: "0"
|
90
|
+
requirements: []
|
91
|
+
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 1.3.6
|
94
|
+
signing_key:
|
95
|
+
specification_version: 3
|
96
|
+
summary: Data migrating for MongoMapper
|
97
|
+
test_files:
|
98
|
+
- test/mongrations_test.rb
|
99
|
+
- test/test_helper.rb
|