leifcr-mongrations 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +69 -0
- data/MIT-LICENSE +20 -0
- data/README.markdown +56 -0
- data/Rakefile +20 -0
- data/VERSION +1 -0
- data/generators/mongrations/templates/mongration.rb +7 -0
- data/generators/mongrations_generator.rb +15 -0
- data/init.rb +8 -0
- data/install.rb +1 -0
- data/leifcr-mongrations.gemspec +66 -0
- data/lib/mongrations/exceptions.rb +30 -0
- data/lib/mongrations/migration_proxy.rb +22 -0
- data/lib/mongrations/migrator.rb +169 -0
- data/lib/mongrations/mongration.rb +100 -0
- data/lib/mongrations/railtie.rb +14 -0
- data/lib/mongrations/schema_migration.rb +9 -0
- data/lib/mongrations.rb +17 -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 +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5a4fa8cb3eb9e92370130c33f6bdddf8aa86b1d9
|
4
|
+
data.tar.gz: d642e0451b8b3eb30a26b9e34c2da68de89e2a88
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fb5a97d2bc8e07b825ae18f53505dc87f620d11df432810fc1d4b1e6abba83886481b9cf0fbc0986b8e2e7a13144fb3278be9bea142cec2b267bf09ab931fd63
|
7
|
+
data.tar.gz: 846b26b38da1d03427cd373a7868693dc4514dd413f9643fba4969eb6afc0d3288302ace59718925faeb57e94a8083c1c986fd3c710498e3bc972856f321646a
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.2.15)
|
5
|
+
activesupport (= 3.2.15)
|
6
|
+
builder (~> 3.0.0)
|
7
|
+
activesupport (3.2.15)
|
8
|
+
i18n (~> 0.6, >= 0.6.4)
|
9
|
+
multi_json (~> 1.0)
|
10
|
+
addressable (2.3.5)
|
11
|
+
bson (1.9.2)
|
12
|
+
builder (3.0.4)
|
13
|
+
faraday (0.8.8)
|
14
|
+
multipart-post (~> 1.2.0)
|
15
|
+
git (1.2.6)
|
16
|
+
github_api (0.10.1)
|
17
|
+
addressable
|
18
|
+
faraday (~> 0.8.1)
|
19
|
+
hashie (>= 1.2)
|
20
|
+
multi_json (~> 1.4)
|
21
|
+
nokogiri (~> 1.5.2)
|
22
|
+
oauth2
|
23
|
+
hashie (2.0.5)
|
24
|
+
highline (1.6.20)
|
25
|
+
httpauth (0.2.0)
|
26
|
+
i18n (0.6.5)
|
27
|
+
jeweler (1.8.8)
|
28
|
+
builder
|
29
|
+
bundler (~> 1.0)
|
30
|
+
git (>= 1.2.5)
|
31
|
+
github_api (= 0.10.1)
|
32
|
+
highline (>= 1.6.15)
|
33
|
+
nokogiri (= 1.5.10)
|
34
|
+
rake
|
35
|
+
rdoc
|
36
|
+
json (1.8.1)
|
37
|
+
jwt (0.1.8)
|
38
|
+
multi_json (>= 1.5)
|
39
|
+
mongo (1.9.2)
|
40
|
+
bson (~> 1.9.2)
|
41
|
+
mongo_mapper (0.12.0)
|
42
|
+
activemodel (~> 3.0)
|
43
|
+
activesupport (~> 3.0)
|
44
|
+
plucky (~> 0.5.2)
|
45
|
+
multi_json (1.8.2)
|
46
|
+
multi_xml (0.5.5)
|
47
|
+
multipart-post (1.2.0)
|
48
|
+
nokogiri (1.5.10)
|
49
|
+
oauth2 (0.9.2)
|
50
|
+
faraday (~> 0.8)
|
51
|
+
httpauth (~> 0.2)
|
52
|
+
jwt (~> 0.1.4)
|
53
|
+
multi_json (~> 1.0)
|
54
|
+
multi_xml (~> 0.5)
|
55
|
+
rack (~> 1.2)
|
56
|
+
plucky (0.5.2)
|
57
|
+
mongo (~> 1.5)
|
58
|
+
rack (1.5.2)
|
59
|
+
rake (10.1.0)
|
60
|
+
rdoc (4.0.1)
|
61
|
+
json (~> 1.4)
|
62
|
+
|
63
|
+
PLATFORMS
|
64
|
+
ruby
|
65
|
+
|
66
|
+
DEPENDENCIES
|
67
|
+
bundler (>= 1.3.5)
|
68
|
+
jeweler (~> 1.8.8)
|
69
|
+
mongo_mapper (>= 0.12.0)
|
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 mongo:mongrate`
|
27
|
+
|
28
|
+
Other rake tasks added have been `mongo:mongrate:redo`, `mongo:mongrate:up`, `mongo:mongrate:down`, `mongo: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,20 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'rake/testtask'
|
4
|
+
|
5
|
+
begin
|
6
|
+
require 'jeweler'
|
7
|
+
Jeweler::Tasks.new do |gem|
|
8
|
+
gem.name = "leifcr-mongrations"
|
9
|
+
gem.summary = %Q{Data migrating for MongoMapper}
|
10
|
+
gem.description = %Q{Mongrations aims to be similar to ActiveRecord's data migrations, except
|
11
|
+
instead of worrying about schema changes, offering a way to change data when
|
12
|
+
necessary}
|
13
|
+
gem.email = "leifcr@gmail.com"
|
14
|
+
gem.homepage = "http://github.com/leifcr/mongrations"
|
15
|
+
gem.authors = ["jonbell", "leifcr"]
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.6
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rails/generators/named_base'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
class MongrationGenerator < Rails::Generators::NamedBase
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
source_root File.join(File.dirname(__FILE__), 'mongrations', 'templates')
|
7
|
+
|
8
|
+
def self.next_migration_number(dirname)
|
9
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_mongration
|
13
|
+
migration_template('mongration.rb', "db/mongrations/#{file_name}")
|
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,66 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
# stub: leifcr-mongrations 0.1.6 ruby lib
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = "leifcr-mongrations"
|
9
|
+
s.version = "0.1.6"
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["jonbell", "leifcr"]
|
13
|
+
s.date = "2013-10-28"
|
14
|
+
s.description = "Mongrations aims to be similar to ActiveRecord's data migrations, except\ninstead of worrying about schema changes, offering a way to change data when\nnecessary"
|
15
|
+
s.email = "leifcr@gmail.com"
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"README.markdown"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
"Gemfile",
|
21
|
+
"Gemfile.lock",
|
22
|
+
"MIT-LICENSE",
|
23
|
+
"README.markdown",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"generators/mongrations/templates/mongration.rb",
|
27
|
+
"generators/mongrations_generator.rb",
|
28
|
+
"init.rb",
|
29
|
+
"install.rb",
|
30
|
+
"leifcr-mongrations.gemspec",
|
31
|
+
"lib/mongrations.rb",
|
32
|
+
"lib/mongrations/exceptions.rb",
|
33
|
+
"lib/mongrations/migration_proxy.rb",
|
34
|
+
"lib/mongrations/migrator.rb",
|
35
|
+
"lib/mongrations/mongration.rb",
|
36
|
+
"lib/mongrations/railtie.rb",
|
37
|
+
"lib/mongrations/schema_migration.rb",
|
38
|
+
"tasks/mongo.rake",
|
39
|
+
"test/mongrations_test.rb",
|
40
|
+
"test/test_helper.rb",
|
41
|
+
"uninstall.rb"
|
42
|
+
]
|
43
|
+
s.homepage = "http://github.com/leifcr/mongrations"
|
44
|
+
s.require_paths = ["lib"]
|
45
|
+
s.rubygems_version = "2.1.4"
|
46
|
+
s.summary = "Data migrating for MongoMapper"
|
47
|
+
|
48
|
+
if s.respond_to? :specification_version then
|
49
|
+
s.specification_version = 4
|
50
|
+
|
51
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
+
s.add_runtime_dependency(%q<mongo_mapper>, [">= 0.12.0"])
|
53
|
+
s.add_development_dependency(%q<bundler>, [">= 1.3.5"])
|
54
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.8.8"])
|
55
|
+
else
|
56
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.12.0"])
|
57
|
+
s.add_dependency(%q<bundler>, [">= 1.3.5"])
|
58
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.8"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<mongo_mapper>, [">= 0.12.0"])
|
62
|
+
s.add_dependency(%q<bundler>, [">= 1.3.5"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.8.8"])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Mongrations
|
2
|
+
class MongrationError < ::MongoMapper::Error; end
|
3
|
+
|
4
|
+
class IrreversibleMigration < MongrationError#:nodoc:
|
5
|
+
end
|
6
|
+
|
7
|
+
class DuplicateMigrationVersionError < MongrationError#:nodoc:
|
8
|
+
def initialize(version)
|
9
|
+
super("Multiple migrations have the version number #{version}")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class DuplicateMigrationNameError < MongrationError#:nodoc:
|
14
|
+
def initialize(name)
|
15
|
+
super("Multiple migrations have the name #{name}")
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class UnknownMigrationVersionError < MongrationError #:nodoc:
|
20
|
+
def initialize(version)
|
21
|
+
super("No migration with version number #{version}")
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class IllegalMigrationNameError < MongrationError#: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 Mongrations
|
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 Mongrations
|
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
|
+
Mongrations::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
|
+
migration = MigrationProxy.new
|
118
|
+
migration.name = name.camelize
|
119
|
+
migration.version = version
|
120
|
+
migration.filename = file
|
121
|
+
klasses << migration
|
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 = Mongrations::SchemaMigration.find_by_version(version.to_s)
|
144
|
+
sm && sm.delete
|
145
|
+
else
|
146
|
+
@migrated_versions.push(version.to_i).sort!
|
147
|
+
Mongrations::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,100 @@
|
|
1
|
+
module Mongrations
|
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
|
+
def down_with_benchmarks #:nodoc:
|
12
|
+
migrate(:up)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Execute this migration in the named direction
|
16
|
+
def migrate(direction)
|
17
|
+
return unless respond_to?(direction)
|
18
|
+
|
19
|
+
case direction
|
20
|
+
when :up then announce "migrating"
|
21
|
+
when :down then announce "reverting"
|
22
|
+
end
|
23
|
+
|
24
|
+
result = nil
|
25
|
+
time = Benchmark.measure { result = send("#{direction}_without_benchmarks") }
|
26
|
+
|
27
|
+
case direction
|
28
|
+
when :up then announce "migrated (%.4fs)" % time.real; write
|
29
|
+
when :down then announce "reverted (%.4fs)" % time.real; write
|
30
|
+
end
|
31
|
+
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
35
|
+
# Because the method added may do an alias_method, it can be invoked
|
36
|
+
# recursively. We use @ignore_new_methods as a guard to indicate whether
|
37
|
+
# it is safe for the call to proceed.
|
38
|
+
def singleton_method_added(sym) #:nodoc:
|
39
|
+
return if defined?(@ignore_new_methods) && @ignore_new_methods
|
40
|
+
|
41
|
+
begin
|
42
|
+
@ignore_new_methods = true
|
43
|
+
|
44
|
+
case sym
|
45
|
+
when :up, :down
|
46
|
+
klass = (class << self; self; end)
|
47
|
+
klass.send(:alias_method_chain, sym, "benchmarks")
|
48
|
+
end
|
49
|
+
ensure
|
50
|
+
@ignore_new_methods = false
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def write(text="")
|
55
|
+
puts(text) if verbose
|
56
|
+
end
|
57
|
+
|
58
|
+
def announce(message)
|
59
|
+
text = "#{@version} #{name}: #{message}"
|
60
|
+
length = [0, 75 - text.length].max
|
61
|
+
write "== %s %s" % [text, "=" * length]
|
62
|
+
end
|
63
|
+
|
64
|
+
def say(message, subitem=false)
|
65
|
+
write "#{subitem ? " ->" : "--"} #{message}"
|
66
|
+
end
|
67
|
+
|
68
|
+
def say_with_time(message)
|
69
|
+
say(message)
|
70
|
+
result = nil
|
71
|
+
time = Benchmark.measure { result = yield }
|
72
|
+
say "%.4fs" % time.real, :subitem
|
73
|
+
say("#{result} rows", :subitem) if result.is_a?(Integer)
|
74
|
+
result
|
75
|
+
end
|
76
|
+
|
77
|
+
def suppress_messages
|
78
|
+
save, self.verbose = verbose, false
|
79
|
+
yield
|
80
|
+
ensure
|
81
|
+
self.verbose = save
|
82
|
+
end
|
83
|
+
|
84
|
+
def connection
|
85
|
+
MongoMapper.connection
|
86
|
+
end
|
87
|
+
|
88
|
+
def method_missing(method, *arguments, &block)
|
89
|
+
arg_list = arguments.map(&:inspect) * ', '
|
90
|
+
|
91
|
+
say_with_time "#{method}(#{arg_list})" do
|
92
|
+
unless arguments.empty? || method == :execute
|
93
|
+
arguments[0] = Migrator.proper_table_name(arguments.first)
|
94
|
+
end
|
95
|
+
connection.send(method, *arguments, &block)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rails'
|
2
|
+
require 'mongrations'
|
3
|
+
|
4
|
+
module Mongrations
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
rake_tasks do
|
7
|
+
load File.join(File.dirname(__FILE__), '..', '..', 'tasks', 'mongo.rake')
|
8
|
+
end
|
9
|
+
|
10
|
+
generators do
|
11
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'generators', 'mongrations_generator.rb')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/lib/mongrations.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'mongo_mapper'
|
2
|
+
|
3
|
+
module Mongrations
|
4
|
+
autoload :MongoMapperError, 'mongrations/exceptions'
|
5
|
+
autoload :IrreversibleMigration, 'mongrations/exceptions'
|
6
|
+
autoload :DuplicateMigrationVersionError, 'mongrations/exceptions'
|
7
|
+
autoload :DuplicateMigrationNameError, 'mongrations/exceptions'
|
8
|
+
autoload :UnknownMigrationVersionError, 'mongrations/exceptions'
|
9
|
+
autoload :IllegalMigrationNameError, 'mongrations/exceptions'
|
10
|
+
|
11
|
+
autoload :MigrationProxy, 'mongrations/migration_proxy'
|
12
|
+
autoload :Migrator, 'mongrations/migrator'
|
13
|
+
autoload :Mongration, 'mongrations/mongration'
|
14
|
+
autoload :SchemaMigration, 'mongrations/schema_migration'
|
15
|
+
end
|
16
|
+
|
17
|
+
require 'mongrations/railtie' if defined?(Rails)
|
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
|
+
Mongrations::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
|
+
Mongrations::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
|
+
Mongrations::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
|
+
Mongrations::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,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: leifcr-mongrations
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- jonbell
|
8
|
+
- leifcr
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-10-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: mongo_mapper
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - '>='
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 0.12.0
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - '>='
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: 0.12.0
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: bundler
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - '>='
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 1.3.5
|
35
|
+
type: :development
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: 1.3.5
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: jeweler
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ~>
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: 1.8.8
|
49
|
+
type: :development
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ~>
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: 1.8.8
|
56
|
+
description: |-
|
57
|
+
Mongrations aims to be similar to ActiveRecord's data migrations, except
|
58
|
+
instead of worrying about schema changes, offering a way to change data when
|
59
|
+
necessary
|
60
|
+
email: leifcr@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files:
|
64
|
+
- README.markdown
|
65
|
+
files:
|
66
|
+
- Gemfile
|
67
|
+
- Gemfile.lock
|
68
|
+
- MIT-LICENSE
|
69
|
+
- README.markdown
|
70
|
+
- Rakefile
|
71
|
+
- VERSION
|
72
|
+
- generators/mongrations/templates/mongration.rb
|
73
|
+
- generators/mongrations_generator.rb
|
74
|
+
- init.rb
|
75
|
+
- install.rb
|
76
|
+
- leifcr-mongrations.gemspec
|
77
|
+
- lib/mongrations.rb
|
78
|
+
- lib/mongrations/exceptions.rb
|
79
|
+
- lib/mongrations/migration_proxy.rb
|
80
|
+
- lib/mongrations/migrator.rb
|
81
|
+
- lib/mongrations/mongration.rb
|
82
|
+
- lib/mongrations/railtie.rb
|
83
|
+
- lib/mongrations/schema_migration.rb
|
84
|
+
- tasks/mongo.rake
|
85
|
+
- test/mongrations_test.rb
|
86
|
+
- test/test_helper.rb
|
87
|
+
- uninstall.rb
|
88
|
+
homepage: http://github.com/leifcr/mongrations
|
89
|
+
licenses: []
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.1.4
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: Data migrating for MongoMapper
|
111
|
+
test_files: []
|