mongrations 0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +2 -0
 - data/Gemfile +4 -0
 - data/MIT-LICENSE +20 -0
 - data/README.markdown +67 -0
 - data/Rakefile +25 -0
 - data/generators/mongration/mongration_generator.rb +15 -0
 - data/generators/mongration/templates/mongration.rb +7 -0
 - data/lib/mongrations.rb +6 -0
 - data/lib/mongrations/exceptions.rb +31 -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/schema_migration.rb +8 -0
 - data/lib/mongrations/tasks.rb +41 -0
 - data/lib/mongrations/version.rb +3 -0
 - data/mongrations.gemspec +26 -0
 - data/test/models/widget.rb +6 -0
 - data/test/mongrations/20101125020919_fix_groupable_keys.rb +15 -0
 - data/test/mongrations_test.rb +63 -0
 - data/test/test_helper.rb +22 -0
 - metadata +131 -0
 
    
        data/.gitignore
    ADDED
    
    
    
        data/Gemfile
    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,67 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
            Installation
         
     | 
| 
      
 17 
     | 
    
         
            +
            ============
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
            If you're using Rails 2:
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
            `gem install mongrations`
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
            In your environment.rb:
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
            `config.gem "mongrations"`
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
            If you're using Rails 3/Bundler, just add:
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
            `gem "mongrations"`
         
     | 
| 
      
 30 
     | 
    
         
            +
             
     | 
| 
      
 31 
     | 
    
         
            +
            to your Gemfile.
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
            In both cases, open your Rakefile, and add:
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
            `require 'mongrations/tasks'`
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
            Example
         
     | 
| 
      
 38 
     | 
    
         
            +
            =======
         
     | 
| 
      
 39 
     | 
    
         
            +
             
     | 
| 
      
 40 
     | 
    
         
            +
            To generate a mongration, just do:
         
     | 
| 
      
 41 
     | 
    
         
            +
             
     | 
| 
      
 42 
     | 
    
         
            +
            `script/generate mongration whatever_you_want_your_mongration_to_be`
         
     | 
| 
      
 43 
     | 
    
         
            +
             
     | 
| 
      
 44 
     | 
    
         
            +
            To run it, do
         
     | 
| 
      
 45 
     | 
    
         
            +
             
     | 
| 
      
 46 
     | 
    
         
            +
            `rake mongo:mongrate`
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
            Other rake tasks added have been `mongo:mongrate:redo`, `mongo:mongrate:up`, `mongo:mongrate:down`, `mongo:mongrate:rollback`.
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
             
     | 
| 
      
 51 
     | 
    
         
            +
            Dependencies
         
     | 
| 
      
 52 
     | 
    
         
            +
            ============
         
     | 
| 
      
 53 
     | 
    
         
            +
             
     | 
| 
      
 54 
     | 
    
         
            +
            You need Mongo and MongoMapper for this to be of any use. Shoulda and Matchy are required to run the tests.
         
     | 
| 
      
 55 
     | 
    
         
            +
             
     | 
| 
      
 56 
     | 
    
         
            +
            Credit
         
     | 
| 
      
 57 
     | 
    
         
            +
            ======
         
     | 
| 
      
 58 
     | 
    
         
            +
            Original code is from Rails 2.3.5 and ActiveRecord 2.3.5, now adapted to work
         
     | 
| 
      
 59 
     | 
    
         
            +
            with MongoMapper
         
     | 
| 
      
 60 
     | 
    
         
            +
             
     | 
| 
      
 61 
     | 
    
         
            +
            Original adaptation by terrbear.
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
            Fixed to work with recent versions of MongoMapper and released as a gem by Chris Heald.
         
     | 
| 
      
 64 
     | 
    
         
            +
             
     | 
| 
      
 65 
     | 
    
         
            +
            License
         
     | 
| 
      
 66 
     | 
    
         
            +
            =======
         
     | 
| 
      
 67 
     | 
    
         
            +
            Released under the MIT license
         
     | 
    
        data/Rakefile
    ADDED
    
    | 
         @@ -0,0 +1,25 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rake'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'rake/testtask'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'rake/rdoctask'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'bundler'
         
     | 
| 
      
 5 
     | 
    
         
            +
            Bundler::GemHelper.install_tasks
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
      
 7 
     | 
    
         
            +
            desc 'Default: run unit tests.'
         
     | 
| 
      
 8 
     | 
    
         
            +
            task :default => :test
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            desc 'Test the mongrations plugin.'
         
     | 
| 
      
 11 
     | 
    
         
            +
            Rake::TestTask.new(:test) do |t|
         
     | 
| 
      
 12 
     | 
    
         
            +
              t.libs << 'lib'
         
     | 
| 
      
 13 
     | 
    
         
            +
              t.libs << 'test'
         
     | 
| 
      
 14 
     | 
    
         
            +
              t.pattern = 'test/**/*_test.rb'
         
     | 
| 
      
 15 
     | 
    
         
            +
              t.verbose = true
         
     | 
| 
      
 16 
     | 
    
         
            +
            end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            desc 'Generate documentation for the mongrations plugin.'
         
     | 
| 
      
 19 
     | 
    
         
            +
            Rake::RDocTask.new(:rdoc) do |rdoc|
         
     | 
| 
      
 20 
     | 
    
         
            +
              rdoc.rdoc_dir = 'rdoc'
         
     | 
| 
      
 21 
     | 
    
         
            +
              rdoc.title    = 'Mongrations'
         
     | 
| 
      
 22 
     | 
    
         
            +
              rdoc.options << '--line-numbers' << '--inline-source'
         
     | 
| 
      
 23 
     | 
    
         
            +
              rdoc.rdoc_files.include('README')
         
     | 
| 
      
 24 
     | 
    
         
            +
              rdoc.rdoc_files.include('lib/**/*.rb')
         
     | 
| 
      
 25 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -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/lib/mongrations.rb
    ADDED
    
    
| 
         @@ -0,0 +1,31 @@ 
     | 
|
| 
      
 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
         
     | 
| 
      
 31 
     | 
    
         
            +
             
     | 
| 
         @@ -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
         
     | 
| 
      
 7 
     | 
    
         
            +
                      up(migrations_path, target_version)
         
     | 
| 
      
 8 
     | 
    
         
            +
                    when current_version > target_version then
         
     | 
| 
      
 9 
     | 
    
         
            +
                      down(migrations_path, target_version)
         
     | 
| 
      
 10 
     | 
    
         
            +
                    else
         
     | 
| 
      
 11 
     | 
    
         
            +
                      up(migrations_path, target_version)
         
     | 
| 
      
 12 
     | 
    
         
            +
                    end
         
     | 
| 
      
 13 
     | 
    
         
            +
                  end
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
                  def rollback(migrations_path, steps=1)
         
     | 
| 
      
 16 
     | 
    
         
            +
                    migrator    = self.new(:down, migrations_path)
         
     | 
| 
      
 17 
     | 
    
         
            +
                    start_index = migrator.migrations.index(migrator.current_migration)
         
     | 
| 
      
 18 
     | 
    
         
            +
             
     | 
| 
      
 19 
     | 
    
         
            +
                    return unless start_index
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                    finish = migrator.migrations[start_index + steps]
         
     | 
| 
      
 22 
     | 
    
         
            +
                    down(migrations_path, finish ? finish.version : 0)
         
     | 
| 
      
 23 
     | 
    
         
            +
                  end
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  def up(migrations_path, target_version = nil)
         
     | 
| 
      
 26 
     | 
    
         
            +
                    self.new(:up, migrations_path, target_version).migrate
         
     | 
| 
      
 27 
     | 
    
         
            +
                  end
         
     | 
| 
      
 28 
     | 
    
         
            +
             
     | 
| 
      
 29 
     | 
    
         
            +
                  def down(migrations_path, target_version = nil)
         
     | 
| 
      
 30 
     | 
    
         
            +
                    self.new(:down, migrations_path, target_version).migrate
         
     | 
| 
      
 31 
     | 
    
         
            +
                  end
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  def run(direction, migrations_path, target_version)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    self.new(direction, migrations_path, target_version).run
         
     | 
| 
      
 35 
     | 
    
         
            +
                  end
         
     | 
| 
      
 36 
     | 
    
         
            +
             
     | 
| 
      
 37 
     | 
    
         
            +
                  def get_all_versions
         
     | 
| 
      
 38 
     | 
    
         
            +
                    MongoMapper::SchemaMigration.all.map { |sm| sm.version.to_i }.sort
         
     | 
| 
      
 39 
     | 
    
         
            +
                  end
         
     | 
| 
      
 40 
     | 
    
         
            +
             
     | 
| 
      
 41 
     | 
    
         
            +
                  def current_version
         
     | 
| 
      
 42 
     | 
    
         
            +
                    get_all_versions.max || 0
         
     | 
| 
      
 43 
     | 
    
         
            +
                  end
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                  def proper_table_name(name)
         
     | 
| 
      
 46 
     | 
    
         
            +
                    name.table_name rescue name.to_s
         
     | 
| 
      
 47 
     | 
    
         
            +
                  end
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
             
     | 
| 
      
 50 
     | 
    
         
            +
                def initialize(direction, migrations_path, target_version = nil)
         
     | 
| 
      
 51 
     | 
    
         
            +
                  target_version = target_version.to_i unless target_version.nil?
         
     | 
| 
      
 52 
     | 
    
         
            +
                  @direction, @migrations_path, @target_version = direction, migrations_path, target_version
         
     | 
| 
      
 53 
     | 
    
         
            +
                end
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
                def current_version
         
     | 
| 
      
 56 
     | 
    
         
            +
                  migrated.last || 0
         
     | 
| 
      
 57 
     | 
    
         
            +
                end
         
     | 
| 
      
 58 
     | 
    
         
            +
             
     | 
| 
      
 59 
     | 
    
         
            +
                def current_migration
         
     | 
| 
      
 60 
     | 
    
         
            +
                  migrations.detect { |m| m.version == current_version }
         
     | 
| 
      
 61 
     | 
    
         
            +
                end
         
     | 
| 
      
 62 
     | 
    
         
            +
             
     | 
| 
      
 63 
     | 
    
         
            +
                def run
         
     | 
| 
      
 64 
     | 
    
         
            +
                  target = migrations.detect { |m| m.version == @target_version }
         
     | 
| 
      
 65 
     | 
    
         
            +
                  raise UnknownMigrationVersionError.new(@target_version) if target.nil?
         
     | 
| 
      
 66 
     | 
    
         
            +
                  unless (up? && migrated.include?(target.version.to_i)) || (down? && !migrated.include?(target.version.to_i))
         
     | 
| 
      
 67 
     | 
    
         
            +
                    target.migrate(@direction)
         
     | 
| 
      
 68 
     | 
    
         
            +
                    record_version_state_after_migrating(target.version)
         
     | 
| 
      
 69 
     | 
    
         
            +
                  end
         
     | 
| 
      
 70 
     | 
    
         
            +
                end
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
                def migrate
         
     | 
| 
      
 73 
     | 
    
         
            +
                  current = migrations.detect { |m| m.version == current_version }
         
     | 
| 
      
 74 
     | 
    
         
            +
                  target  = migrations.detect { |m| m.version == @target_version }
         
     | 
| 
      
 75 
     | 
    
         
            +
             
     | 
| 
      
 76 
     | 
    
         
            +
                  if target.nil? && !@target_version.nil? && @target_version > 0
         
     | 
| 
      
 77 
     | 
    
         
            +
                    raise UnknownMigrationVersionError.new(@target_version)
         
     | 
| 
      
 78 
     | 
    
         
            +
                  end
         
     | 
| 
      
 79 
     | 
    
         
            +
             
     | 
| 
      
 80 
     | 
    
         
            +
                  start    = up? ? 0 : (migrations.index(current) || 0)
         
     | 
| 
      
 81 
     | 
    
         
            +
                  finish   = migrations.index(target) || migrations.size - 1
         
     | 
| 
      
 82 
     | 
    
         
            +
                  runnable = migrations[start..finish]
         
     | 
| 
      
 83 
     | 
    
         
            +
             
     | 
| 
      
 84 
     | 
    
         
            +
                  # skip the last migration if we're headed down, but not ALL the way down
         
     | 
| 
      
 85 
     | 
    
         
            +
                  runnable.pop if down? && !target.nil?
         
     | 
| 
      
 86 
     | 
    
         
            +
             
     | 
| 
      
 87 
     | 
    
         
            +
                  runnable.each do |migration|
         
     | 
| 
      
 88 
     | 
    
         
            +
                    # On our way up, we skip migrating the ones we've already migrated
         
     | 
| 
      
 89 
     | 
    
         
            +
                    next if up? && migrated.include?(migration.version.to_i)
         
     | 
| 
      
 90 
     | 
    
         
            +
             
     | 
| 
      
 91 
     | 
    
         
            +
                    # On our way down, we skip reverting the ones we've never migrated
         
     | 
| 
      
 92 
     | 
    
         
            +
                    if down? && !migrated.include?(migration.version.to_i)
         
     | 
| 
      
 93 
     | 
    
         
            +
                      migration.announce 'never migrated, skipping'; migration.write
         
     | 
| 
      
 94 
     | 
    
         
            +
                      next
         
     | 
| 
      
 95 
     | 
    
         
            +
                    end
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
                    migration.migrate(@direction)
         
     | 
| 
      
 98 
     | 
    
         
            +
                    record_version_state_after_migrating(migration.version)
         
     | 
| 
      
 99 
     | 
    
         
            +
                  end
         
     | 
| 
      
 100 
     | 
    
         
            +
                end
         
     | 
| 
      
 101 
     | 
    
         
            +
             
     | 
| 
      
 102 
     | 
    
         
            +
                def migrations
         
     | 
| 
      
 103 
     | 
    
         
            +
                  @migrations ||= begin
         
     | 
| 
      
 104 
     | 
    
         
            +
                    files      = Dir["#{@migrations_path}/[0-9]*_*.rb"]
         
     | 
| 
      
 105 
     | 
    
         
            +
             
     | 
| 
      
 106 
     | 
    
         
            +
                    migrations = files.inject([]) do |klasses, file|
         
     | 
| 
      
 107 
     | 
    
         
            +
                      version, name = file.scan(/([0-9]+)_([_a-z0-9]*).rb/).first
         
     | 
| 
      
 108 
     | 
    
         
            +
             
     | 
| 
      
 109 
     | 
    
         
            +
                      raise IllegalMigrationNameError.new(file) unless version
         
     | 
| 
      
 110 
     | 
    
         
            +
                      version = version.to_i
         
     | 
| 
      
 111 
     | 
    
         
            +
             
     | 
| 
      
 112 
     | 
    
         
            +
                      if klasses.detect { |m| m.version == version }
         
     | 
| 
      
 113 
     | 
    
         
            +
                        raise DuplicateMigrationVersionError.new(version)
         
     | 
| 
      
 114 
     | 
    
         
            +
                      end
         
     | 
| 
      
 115 
     | 
    
         
            +
             
     | 
| 
      
 116 
     | 
    
         
            +
                      if klasses.detect { |m| m.name == name.camelize }
         
     | 
| 
      
 117 
     | 
    
         
            +
                        raise DuplicateMigrationNameError.new(name.camelize)
         
     | 
| 
      
 118 
     | 
    
         
            +
                      end
         
     | 
| 
      
 119 
     | 
    
         
            +
             
     | 
| 
      
 120 
     | 
    
         
            +
                      klasses << MigrationProxy.new.tap do |migration|
         
     | 
| 
      
 121 
     | 
    
         
            +
                        migration.name     = name.camelize
         
     | 
| 
      
 122 
     | 
    
         
            +
                        migration.version  = version
         
     | 
| 
      
 123 
     | 
    
         
            +
                        migration.filename = file
         
     | 
| 
      
 124 
     | 
    
         
            +
                      end
         
     | 
| 
      
 125 
     | 
    
         
            +
                    end
         
     | 
| 
      
 126 
     | 
    
         
            +
             
     | 
| 
      
 127 
     | 
    
         
            +
                    migrations = migrations.sort_by(&:version)
         
     | 
| 
      
 128 
     | 
    
         
            +
                    down? ? migrations.reverse : migrations
         
     | 
| 
      
 129 
     | 
    
         
            +
                  end
         
     | 
| 
      
 130 
     | 
    
         
            +
                end
         
     | 
| 
      
 131 
     | 
    
         
            +
             
     | 
| 
      
 132 
     | 
    
         
            +
                def pending_migrations
         
     | 
| 
      
 133 
     | 
    
         
            +
                  already_migrated = migrated
         
     | 
| 
      
 134 
     | 
    
         
            +
                  migrations.reject { |m| already_migrated.include?(m.version.to_i) }
         
     | 
| 
      
 135 
     | 
    
         
            +
                end
         
     | 
| 
      
 136 
     | 
    
         
            +
             
     | 
| 
      
 137 
     | 
    
         
            +
                def migrated
         
     | 
| 
      
 138 
     | 
    
         
            +
                  @migrated_versions ||= self.class.get_all_versions
         
     | 
| 
      
 139 
     | 
    
         
            +
                end
         
     | 
| 
      
 140 
     | 
    
         
            +
             
     | 
| 
      
 141 
     | 
    
         
            +
                private
         
     | 
| 
      
 142 
     | 
    
         
            +
             
     | 
| 
      
 143 
     | 
    
         
            +
                def record_version_state_after_migrating(version)
         
     | 
| 
      
 144 
     | 
    
         
            +
                  @migrated_versions ||= []
         
     | 
| 
      
 145 
     | 
    
         
            +
                  if down?
         
     | 
| 
      
 146 
     | 
    
         
            +
                    @migrated_versions.delete(version.to_i)
         
     | 
| 
      
 147 
     | 
    
         
            +
                    sm = MongoMapper::SchemaMigration.find_by_version(version.to_s)
         
     | 
| 
      
 148 
     | 
    
         
            +
                    sm && sm.delete
         
     | 
| 
      
 149 
     | 
    
         
            +
                  else
         
     | 
| 
      
 150 
     | 
    
         
            +
                    @migrated_versions.push(version.to_i).sort!
         
     | 
| 
      
 151 
     | 
    
         
            +
                    MongoMapper::SchemaMigration.create(:version => version)
         
     | 
| 
      
 152 
     | 
    
         
            +
                  end
         
     | 
| 
      
 153 
     | 
    
         
            +
                end
         
     | 
| 
      
 154 
     | 
    
         
            +
             
     | 
| 
      
 155 
     | 
    
         
            +
                def up?
         
     | 
| 
      
 156 
     | 
    
         
            +
                  @direction == :up
         
     | 
| 
      
 157 
     | 
    
         
            +
                end
         
     | 
| 
      
 158 
     | 
    
         
            +
             
     | 
| 
      
 159 
     | 
    
         
            +
                def down?
         
     | 
| 
      
 160 
     | 
    
         
            +
                  @direction == :down
         
     | 
| 
      
 161 
     | 
    
         
            +
                end
         
     | 
| 
      
 162 
     | 
    
         
            +
             
     | 
| 
      
 163 
     | 
    
         
            +
                # Wrap the migration in a transaction only if supported by the adapter.
         
     | 
| 
      
 164 
     | 
    
         
            +
                # (It's not.)
         
     | 
| 
      
 165 
     | 
    
         
            +
                def ddl_transaction(&block)
         
     | 
| 
      
 166 
     | 
    
         
            +
                  block.call
         
     | 
| 
      
 167 
     | 
    
         
            +
                end
         
     | 
| 
      
 168 
     | 
    
         
            +
              end
         
     | 
| 
      
 169 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,100 @@ 
     | 
|
| 
      
 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 
     | 
    
         
            +
                  def down_with_benchmarks #:nodoc:
         
     | 
| 
      
 12 
     | 
    
         
            +
                    migrate(:down)
         
     | 
| 
      
 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,41 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            namespace :mongo do
         
     | 
| 
      
 2 
     | 
    
         
            +
              @path = File.join(Rails.root, "db", "mongrations")
         
     | 
| 
      
 3 
     | 
    
         
            +
             
     | 
| 
      
 4 
     | 
    
         
            +
              desc "Migrate the database through scripts in db/mongrations. Target specific version with VERSION=x."
         
     | 
| 
      
 5 
     | 
    
         
            +
              task :mongrate => :environment do
         
     | 
| 
      
 6 
     | 
    
         
            +
                MongoMapper::Migrator.migrate(@path, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
         
     | 
| 
      
 7 
     | 
    
         
            +
              end
         
     | 
| 
      
 8 
     | 
    
         
            +
             
     | 
| 
      
 9 
     | 
    
         
            +
              namespace :mongrate do
         
     | 
| 
      
 10 
     | 
    
         
            +
                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.'
         
     | 
| 
      
 11 
     | 
    
         
            +
                task :redo => :environment do
         
     | 
| 
      
 12 
     | 
    
         
            +
                  if ENV["VERSION"]
         
     | 
| 
      
 13 
     | 
    
         
            +
                    Rake::Task["mongo:mongrate:down"].invoke
         
     | 
| 
      
 14 
     | 
    
         
            +
                    Rake::Task["mongo:mongrate:up"].invoke
         
     | 
| 
      
 15 
     | 
    
         
            +
                  else
         
     | 
| 
      
 16 
     | 
    
         
            +
                    Rake::Task["mongo:mongo_rollback"].invoke
         
     | 
| 
      
 17 
     | 
    
         
            +
                    Rake::Task["mongo:mongrate"].invoke
         
     | 
| 
      
 18 
     | 
    
         
            +
                  end
         
     | 
| 
      
 19 
     | 
    
         
            +
                end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                desc 'Runs the "up" for a given mongration VERSION.'
         
     | 
| 
      
 22 
     | 
    
         
            +
                task :up => :environment do
         
     | 
| 
      
 23 
     | 
    
         
            +
                  version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
         
     | 
| 
      
 24 
     | 
    
         
            +
                  raise "VERSION is required" unless version
         
     | 
| 
      
 25 
     | 
    
         
            +
                  MongoMapper::Migrator.run(:up, @path, version)
         
     | 
| 
      
 26 
     | 
    
         
            +
                end
         
     | 
| 
      
 27 
     | 
    
         
            +
             
     | 
| 
      
 28 
     | 
    
         
            +
                desc 'Runs the "down" for a given mongration VERSION.'
         
     | 
| 
      
 29 
     | 
    
         
            +
                task :down => :environment do
         
     | 
| 
      
 30 
     | 
    
         
            +
                  version = ENV["VERSION"] ? ENV["VERSION"].to_i : nil
         
     | 
| 
      
 31 
     | 
    
         
            +
                  raise "VERSION is required" unless version
         
     | 
| 
      
 32 
     | 
    
         
            +
                  MongoMapper::Migrator.run(:down, @path, version)
         
     | 
| 
      
 33 
     | 
    
         
            +
                end
         
     | 
| 
      
 34 
     | 
    
         
            +
             
     | 
| 
      
 35 
     | 
    
         
            +
                desc 'Rolls the schema back to the previous version. Specify the number of steps with STEP=n'
         
     | 
| 
      
 36 
     | 
    
         
            +
                task :rollback => :environment do
         
     | 
| 
      
 37 
     | 
    
         
            +
                  step = ENV['STEP'] ? ENV['STEP'].to_i : 1
         
     | 
| 
      
 38 
     | 
    
         
            +
                  MongoMapper::Migrator.rollback(@path, step)
         
     | 
| 
      
 39 
     | 
    
         
            +
                end    
         
     | 
| 
      
 40 
     | 
    
         
            +
              end
         
     | 
| 
      
 41 
     | 
    
         
            +
            end
         
     | 
    
        data/mongrations.gemspec
    ADDED
    
    | 
         @@ -0,0 +1,26 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            # -*- encoding: utf-8 -*-
         
     | 
| 
      
 2 
     | 
    
         
            +
            $:.push File.expand_path("../lib", __FILE__)
         
     | 
| 
      
 3 
     | 
    
         
            +
            require "mongrations/version"
         
     | 
| 
      
 4 
     | 
    
         
            +
             
     | 
| 
      
 5 
     | 
    
         
            +
            Gem::Specification.new do |s|
         
     | 
| 
      
 6 
     | 
    
         
            +
              s.name        = "mongrations"
         
     | 
| 
      
 7 
     | 
    
         
            +
              s.version     = Mongrations::VERSION
         
     | 
| 
      
 8 
     | 
    
         
            +
              s.platform    = Gem::Platform::RUBY
         
     | 
| 
      
 9 
     | 
    
         
            +
              s.authors     = ["Chris Heald", "terrbear"]
         
     | 
| 
      
 10 
     | 
    
         
            +
              s.email       = ["cheald@gmail.com"]
         
     | 
| 
      
 11 
     | 
    
         
            +
              s.homepage    = "https://github.com/cheald/mongrations"
         
     | 
| 
      
 12 
     | 
    
         
            +
              s.summary     = %q{Rails-style migrations for MongoMapper}
         
     | 
| 
      
 13 
     | 
    
         
            +
              s.description = %q{Rails-style migrations for MongoMapper}
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
      
 15 
     | 
    
         
            +
              s.rubyforge_project = "mongrations"
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
              s.files         = `git ls-files`.split("\n")
         
     | 
| 
      
 18 
     | 
    
         
            +
              s.test_files    = `git ls-files -- {test,spec,features}/*`.split("\n")
         
     | 
| 
      
 19 
     | 
    
         
            +
              s.executables   = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
         
     | 
| 
      
 20 
     | 
    
         
            +
              s.require_paths = ["lib"]
         
     | 
| 
      
 21 
     | 
    
         
            +
             
     | 
| 
      
 22 
     | 
    
         
            +
              s.add_development_dependency "shoulda"
         
     | 
| 
      
 23 
     | 
    
         
            +
              s.add_development_dependency "matchy"
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
              s.add_dependency "mongo_mapper"
         
     | 
| 
      
 26 
     | 
    
         
            +
            end
         
     | 
| 
         @@ -0,0 +1,63 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'test_helper'
         
     | 
| 
      
 2 
     | 
    
         
            +
             
     | 
| 
      
 3 
     | 
    
         
            +
            class MongrationsTest < ActiveSupport::TestCase
         
     | 
| 
      
 4 
     | 
    
         
            +
              context "A valid MongoMapper connection" do
         
     | 
| 
      
 5 
     | 
    
         
            +
                setup do
         
     | 
| 
      
 6 
     | 
    
         
            +
                  MongoMapper.connection = Mongo::Connection.new
         
     | 
| 
      
 7 
     | 
    
         
            +
                  MongoMapper.database = "mongrations_test"
         
     | 
| 
      
 8 
     | 
    
         
            +
                  @path = File.join("test", "mongrations")
         
     | 
| 
      
 9 
     | 
    
         
            +
                  @version = "20101125020919"
         
     | 
| 
      
 10 
     | 
    
         
            +
                end
         
     | 
| 
      
 11 
     | 
    
         
            +
             
     | 
| 
      
 12 
     | 
    
         
            +
                context "with a valid database" do
         
     | 
| 
      
 13 
     | 
    
         
            +
                  context "with a pending migration" do
         
     | 
| 
      
 14 
     | 
    
         
            +
                    should "migrate up to a given version" do
         
     | 
| 
      
 15 
     | 
    
         
            +
                      Widget.create(:name => "Sparkling Wizzles", :price => 100)
         
     | 
| 
      
 16 
     | 
    
         
            +
                      Widget.first.price.should == 100
         
     | 
| 
      
 17 
     | 
    
         
            +
                      MongoMapper::Migrator.run(:up, @path, @version)
         
     | 
| 
      
 18 
     | 
    
         
            +
                      Widget.first.price.should == 200
         
     | 
| 
      
 19 
     | 
    
         
            +
                    end
         
     | 
| 
      
 20 
     | 
    
         
            +
             
     | 
| 
      
 21 
     | 
    
         
            +
                    should "run all pending migrations" do
         
     | 
| 
      
 22 
     | 
    
         
            +
             
     | 
| 
      
 23 
     | 
    
         
            +
                      Widget.create(:name => "Sparkling Wizzles", :price => 100)
         
     | 
| 
      
 24 
     | 
    
         
            +
                      Widget.first.price.should == 100
         
     | 
| 
      
 25 
     | 
    
         
            +
                      MongoMapper::Migrator.migrate(@path, nil)
         
     | 
| 
      
 26 
     | 
    
         
            +
                      Widget.first.price.should == 200
         
     | 
| 
      
 27 
     | 
    
         
            +
                    end
         
     | 
| 
      
 28 
     | 
    
         
            +
                  end
         
     | 
| 
      
 29 
     | 
    
         
            +
             
     | 
| 
      
 30 
     | 
    
         
            +
                  should "run a migration:down" do
         
     | 
| 
      
 31 
     | 
    
         
            +
                    Widget.create(:name => "Sparkling Wizzles", :price => 100)
         
     | 
| 
      
 32 
     | 
    
         
            +
                    Widget.first.price.should == 100
         
     | 
| 
      
 33 
     | 
    
         
            +
                    MongoMapper::Migrator.migrate(@path, nil)
         
     | 
| 
      
 34 
     | 
    
         
            +
                    Widget.first.price.should == 200
         
     | 
| 
      
 35 
     | 
    
         
            +
                    MongoMapper::Migrator.run(:down, @path, @version)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    Widget.first.price.should == 100
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  should "run a migration rollback" do
         
     | 
| 
      
 40 
     | 
    
         
            +
                    Widget.create(:name => "Sparkling Wizzles", :price => 100)
         
     | 
| 
      
 41 
     | 
    
         
            +
                    Widget.first.price.should == 100
         
     | 
| 
      
 42 
     | 
    
         
            +
                    MongoMapper::Migrator.migrate(@path, nil)
         
     | 
| 
      
 43 
     | 
    
         
            +
                    Widget.first.price.should == 200
         
     | 
| 
      
 44 
     | 
    
         
            +
                    MongoMapper::Migrator.rollback(@path, 1)
         
     | 
| 
      
 45 
     | 
    
         
            +
                    Widget.first.price.should == 100
         
     | 
| 
      
 46 
     | 
    
         
            +
                  end
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                  should "not run a migration more than once" do
         
     | 
| 
      
 49 
     | 
    
         
            +
                    Widget.create(:name => "Sparkling Wizzles", :price => 100)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    Widget.first.price.should == 100
         
     | 
| 
      
 51 
     | 
    
         
            +
                    MongoMapper::Migrator.migrate(@path, nil)
         
     | 
| 
      
 52 
     | 
    
         
            +
                    Widget.first.price.should == 200
         
     | 
| 
      
 53 
     | 
    
         
            +
                    MongoMapper::Migrator.migrate(@path, nil)
         
     | 
| 
      
 54 
     | 
    
         
            +
                    Widget.first.price.should == 200
         
     | 
| 
      
 55 
     | 
    
         
            +
                  end
         
     | 
| 
      
 56 
     | 
    
         
            +
                end
         
     | 
| 
      
 57 
     | 
    
         
            +
             
     | 
| 
      
 58 
     | 
    
         
            +
                teardown do
         
     | 
| 
      
 59 
     | 
    
         
            +
                  # Not gonna remove it. For now.
         
     | 
| 
      
 60 
     | 
    
         
            +
                  # MongoMapper.connection.drop_database "mongrations_test"
         
     | 
| 
      
 61 
     | 
    
         
            +
                end
         
     | 
| 
      
 62 
     | 
    
         
            +
              end
         
     | 
| 
      
 63 
     | 
    
         
            +
            end
         
     | 
    
        data/test/test_helper.rb
    ADDED
    
    | 
         @@ -0,0 +1,22 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            require 'rubygems'
         
     | 
| 
      
 2 
     | 
    
         
            +
            require 'active_support'
         
     | 
| 
      
 3 
     | 
    
         
            +
            require 'active_support/test_case'
         
     | 
| 
      
 4 
     | 
    
         
            +
            require 'shoulda'
         
     | 
| 
      
 5 
     | 
    
         
            +
            require 'matchy'
         
     | 
| 
      
 6 
     | 
    
         
            +
            require 'mongo_mapper'
         
     | 
| 
      
 7 
     | 
    
         
            +
            require 'models/widget'
         
     | 
| 
      
 8 
     | 
    
         
            +
            require 'lib/mongrations'
         
     | 
| 
      
 9 
     | 
    
         
            +
             
     | 
| 
      
 10 
     | 
    
         
            +
            class ActiveSupport::TestCase
         
     | 
| 
      
 11 
     | 
    
         
            +
              def teardown
         
     | 
| 
      
 12 
     | 
    
         
            +
                MongoMapper.database.collections.each do |coll|
         
     | 
| 
      
 13 
     | 
    
         
            +
                  coll.remove
         
     | 
| 
      
 14 
     | 
    
         
            +
                end
         
     | 
| 
      
 15 
     | 
    
         
            +
              end
         
     | 
| 
      
 16 
     | 
    
         
            +
             
     | 
| 
      
 17 
     | 
    
         
            +
              def inherited(base)
         
     | 
| 
      
 18 
     | 
    
         
            +
                base.define_method teardown do
         
     | 
| 
      
 19 
     | 
    
         
            +
                  super
         
     | 
| 
      
 20 
     | 
    
         
            +
                end
         
     | 
| 
      
 21 
     | 
    
         
            +
              end
         
     | 
| 
      
 22 
     | 
    
         
            +
            end
         
     | 
    
        metadata
    ADDED
    
    | 
         @@ -0,0 +1,131 @@ 
     | 
|
| 
      
 1 
     | 
    
         
            +
            --- !ruby/object:Gem::Specification 
         
     | 
| 
      
 2 
     | 
    
         
            +
            name: mongrations
         
     | 
| 
      
 3 
     | 
    
         
            +
            version: !ruby/object:Gem::Version 
         
     | 
| 
      
 4 
     | 
    
         
            +
              hash: 15
         
     | 
| 
      
 5 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 6 
     | 
    
         
            +
              segments: 
         
     | 
| 
      
 7 
     | 
    
         
            +
              - 0
         
     | 
| 
      
 8 
     | 
    
         
            +
              - 2
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: "0.2"
         
     | 
| 
      
 10 
     | 
    
         
            +
            platform: ruby
         
     | 
| 
      
 11 
     | 
    
         
            +
            authors: 
         
     | 
| 
      
 12 
     | 
    
         
            +
            - Chris Heald
         
     | 
| 
      
 13 
     | 
    
         
            +
            - terrbear
         
     | 
| 
      
 14 
     | 
    
         
            +
            autorequire: 
         
     | 
| 
      
 15 
     | 
    
         
            +
            bindir: bin
         
     | 
| 
      
 16 
     | 
    
         
            +
            cert_chain: []
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
            date: 2011-01-31 00:00:00 -07:00
         
     | 
| 
      
 19 
     | 
    
         
            +
            default_executable: 
         
     | 
| 
      
 20 
     | 
    
         
            +
            dependencies: 
         
     | 
| 
      
 21 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 22 
     | 
    
         
            +
              name: shoulda
         
     | 
| 
      
 23 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 24 
     | 
    
         
            +
              requirement: &id001 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 25 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 26 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 27 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 28 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 29 
     | 
    
         
            +
                    hash: 3
         
     | 
| 
      
 30 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 31 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 32 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 33 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 34 
     | 
    
         
            +
              version_requirements: *id001
         
     | 
| 
      
 35 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 36 
     | 
    
         
            +
              name: matchy
         
     | 
| 
      
 37 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 38 
     | 
    
         
            +
              requirement: &id002 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 39 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 40 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 41 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 42 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 43 
     | 
    
         
            +
                    hash: 3
         
     | 
| 
      
 44 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 45 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 46 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 47 
     | 
    
         
            +
              type: :development
         
     | 
| 
      
 48 
     | 
    
         
            +
              version_requirements: *id002
         
     | 
| 
      
 49 
     | 
    
         
            +
            - !ruby/object:Gem::Dependency 
         
     | 
| 
      
 50 
     | 
    
         
            +
              name: mongo_mapper
         
     | 
| 
      
 51 
     | 
    
         
            +
              prerelease: false
         
     | 
| 
      
 52 
     | 
    
         
            +
              requirement: &id003 !ruby/object:Gem::Requirement 
         
     | 
| 
      
 53 
     | 
    
         
            +
                none: false
         
     | 
| 
      
 54 
     | 
    
         
            +
                requirements: 
         
     | 
| 
      
 55 
     | 
    
         
            +
                - - ">="
         
     | 
| 
      
 56 
     | 
    
         
            +
                  - !ruby/object:Gem::Version 
         
     | 
| 
      
 57 
     | 
    
         
            +
                    hash: 3
         
     | 
| 
      
 58 
     | 
    
         
            +
                    segments: 
         
     | 
| 
      
 59 
     | 
    
         
            +
                    - 0
         
     | 
| 
      
 60 
     | 
    
         
            +
                    version: "0"
         
     | 
| 
      
 61 
     | 
    
         
            +
              type: :runtime
         
     | 
| 
      
 62 
     | 
    
         
            +
              version_requirements: *id003
         
     | 
| 
      
 63 
     | 
    
         
            +
            description: Rails-style migrations for MongoMapper
         
     | 
| 
      
 64 
     | 
    
         
            +
            email: 
         
     | 
| 
      
 65 
     | 
    
         
            +
            - cheald@gmail.com
         
     | 
| 
      
 66 
     | 
    
         
            +
            executables: []
         
     | 
| 
      
 67 
     | 
    
         
            +
             
     | 
| 
      
 68 
     | 
    
         
            +
            extensions: []
         
     | 
| 
      
 69 
     | 
    
         
            +
             
     | 
| 
      
 70 
     | 
    
         
            +
            extra_rdoc_files: []
         
     | 
| 
      
 71 
     | 
    
         
            +
             
     | 
| 
      
 72 
     | 
    
         
            +
            files: 
         
     | 
| 
      
 73 
     | 
    
         
            +
            - .gitignore
         
     | 
| 
      
 74 
     | 
    
         
            +
            - Gemfile
         
     | 
| 
      
 75 
     | 
    
         
            +
            - MIT-LICENSE
         
     | 
| 
      
 76 
     | 
    
         
            +
            - README.markdown
         
     | 
| 
      
 77 
     | 
    
         
            +
            - Rakefile
         
     | 
| 
      
 78 
     | 
    
         
            +
            - generators/mongration/mongration_generator.rb
         
     | 
| 
      
 79 
     | 
    
         
            +
            - generators/mongration/templates/mongration.rb
         
     | 
| 
      
 80 
     | 
    
         
            +
            - lib/mongrations.rb
         
     | 
| 
      
 81 
     | 
    
         
            +
            - lib/mongrations/exceptions.rb
         
     | 
| 
      
 82 
     | 
    
         
            +
            - lib/mongrations/migration_proxy.rb
         
     | 
| 
      
 83 
     | 
    
         
            +
            - lib/mongrations/migrator.rb
         
     | 
| 
      
 84 
     | 
    
         
            +
            - lib/mongrations/mongration.rb
         
     | 
| 
      
 85 
     | 
    
         
            +
            - lib/mongrations/schema_migration.rb
         
     | 
| 
      
 86 
     | 
    
         
            +
            - lib/mongrations/tasks.rb
         
     | 
| 
      
 87 
     | 
    
         
            +
            - lib/mongrations/version.rb
         
     | 
| 
      
 88 
     | 
    
         
            +
            - mongrations.gemspec
         
     | 
| 
      
 89 
     | 
    
         
            +
            - test/models/widget.rb
         
     | 
| 
      
 90 
     | 
    
         
            +
            - test/mongrations/20101125020919_fix_groupable_keys.rb
         
     | 
| 
      
 91 
     | 
    
         
            +
            - test/mongrations_test.rb
         
     | 
| 
      
 92 
     | 
    
         
            +
            - test/test_helper.rb
         
     | 
| 
      
 93 
     | 
    
         
            +
            has_rdoc: true
         
     | 
| 
      
 94 
     | 
    
         
            +
            homepage: https://github.com/cheald/mongrations
         
     | 
| 
      
 95 
     | 
    
         
            +
            licenses: []
         
     | 
| 
      
 96 
     | 
    
         
            +
             
     | 
| 
      
 97 
     | 
    
         
            +
            post_install_message: 
         
     | 
| 
      
 98 
     | 
    
         
            +
            rdoc_options: []
         
     | 
| 
      
 99 
     | 
    
         
            +
             
     | 
| 
      
 100 
     | 
    
         
            +
            require_paths: 
         
     | 
| 
      
 101 
     | 
    
         
            +
            - lib
         
     | 
| 
      
 102 
     | 
    
         
            +
            required_ruby_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 103 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 104 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 105 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 106 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 107 
     | 
    
         
            +
                  hash: 3
         
     | 
| 
      
 108 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 109 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 110 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 111 
     | 
    
         
            +
            required_rubygems_version: !ruby/object:Gem::Requirement 
         
     | 
| 
      
 112 
     | 
    
         
            +
              none: false
         
     | 
| 
      
 113 
     | 
    
         
            +
              requirements: 
         
     | 
| 
      
 114 
     | 
    
         
            +
              - - ">="
         
     | 
| 
      
 115 
     | 
    
         
            +
                - !ruby/object:Gem::Version 
         
     | 
| 
      
 116 
     | 
    
         
            +
                  hash: 3
         
     | 
| 
      
 117 
     | 
    
         
            +
                  segments: 
         
     | 
| 
      
 118 
     | 
    
         
            +
                  - 0
         
     | 
| 
      
 119 
     | 
    
         
            +
                  version: "0"
         
     | 
| 
      
 120 
     | 
    
         
            +
            requirements: []
         
     | 
| 
      
 121 
     | 
    
         
            +
             
     | 
| 
      
 122 
     | 
    
         
            +
            rubyforge_project: mongrations
         
     | 
| 
      
 123 
     | 
    
         
            +
            rubygems_version: 1.3.7
         
     | 
| 
      
 124 
     | 
    
         
            +
            signing_key: 
         
     | 
| 
      
 125 
     | 
    
         
            +
            specification_version: 3
         
     | 
| 
      
 126 
     | 
    
         
            +
            summary: Rails-style migrations for MongoMapper
         
     | 
| 
      
 127 
     | 
    
         
            +
            test_files: 
         
     | 
| 
      
 128 
     | 
    
         
            +
            - test/models/widget.rb
         
     | 
| 
      
 129 
     | 
    
         
            +
            - test/mongrations/20101125020919_fix_groupable_keys.rb
         
     | 
| 
      
 130 
     | 
    
         
            +
            - test/mongrations_test.rb
         
     | 
| 
      
 131 
     | 
    
         
            +
            - test/test_helper.rb
         
     |