Sequel::Plugins::InlineMigrations::

ClassMethods module

Methods to extend Model classes with.

Constants

MIGRATION_NAME_PATTERN

A Regexp for matching valid migration names

Attributes

migrations R

The Hash of Sequel::SimpleMigration objects for this model, keyed by name

Public Instance Methods

after_create_table()

After table creation hook to register any existing migrations as being already applied, as the schema declared by set_schema should be the latest schema, and already incorporate the changes described by the migrations.

# File lib/sequel/plugins/inline_migrations.rb, line 190
def after_create_table
        super
        self.register_existing_migrations
end
after_migration()

Table-migration hook; called once after missing tables are installed and pending migrations are run.

# File lib/sequel/plugins/inline_migrations.rb, line 182
def after_migration
        return true
end
before_migration()

Table-migration hook; called once before missing tables are installed and pending migrations are run.

# File lib/sequel/plugins/inline_migrations.rb, line 175
def before_migration
        return true
end
migrate( target=nil )

Create any new tables and run any pending migrations. If the optional target is supplied, the migrations up to (and including) that one will be applied. If it has already been applied, any from the currently-applied one to it (inclusive) will be reversed. A target of nil is equivalent to the last one

# File lib/sequel/plugins/inline_migrations.rb, line 220
def migrate( target=nil )
        migrator = self.migrator( target )
        classes_to_install = self.uninstalled_tables
        self.db.log_info "Classes with tables that need to be installed: %p" % [ classes_to_install ]
        views_to_install = self.uninstalled_views
        self.db.log_info "Views to install: %p" % [ views_to_install.map(&:table_name) ]

        self.db.transaction do
                self.before_migration
                self.db.log_info "Creating tables that don't yet exist..."
                classes_to_install.each( &:create_table )

                self.db.log_info "Running any pending migrations..."
                migrator.run
                self.after_migration

                self.db.log_info "(Re)-creating any modeled views..."
                views_to_install.each( &:create_view )
        end
end
migration( name, description=nil, &block )

Add a migration with the specified name and optional description. See the docs for Sequel::Migration for usage, and Sequel::MigrationDSL for the allowed syntax in the block. The name of the migration should be in the form: <year><month><day><hour><minute><underbarred_desc>

# File lib/sequel/plugins/inline_migrations.rb, line 158
def migration( name, description=nil, &block )
        raise ScriptError, "invalid migration name %p" % [ name ] unless
                MIGRATION_NAME_PATTERN.match( name )

        @migrations ||= {}
        migration_obj = Sequel::MigrationDSL.create( &block )
        migration_obj.extend( MigrationIntrospection )
        migration_obj.name = name
        migration_obj.model_class = self
        migration_obj.description = description

        @migrations[ name ] = migration_obj
end
migrator( target=nil )

Return a configured inline migrator set with the given target migration.

# File lib/sequel/plugins/inline_migrations.rb, line 243
def migrator( target=nil )
        self.db.log_info "Creating the migrator..."
        Sequel::Plugins::InlineMigrations::Migrator.new( self, nil, target: target )
end
register_existing_migrations()

Register any migrations on the receiver as having already been run (as when creating the table initially).

# File lib/sequel/plugins/inline_migrations.rb, line 198
def register_existing_migrations
        # Register existing migrations as already being applied
        if self.migrations && !self.migrations.empty?
                migrator = self.migrator

                self.migrations.each_value do |migration|
                        migration_data = {
                                name: migration.name,
                                model_class: migration.model_class.name
                        }
                        next unless migrator.dataset.filter( migration_data ).empty?
                        self.db.log_info "  fast-forwarding migration #{migration.name}..."
                        migrator.dataset.insert( migration_data )
                end
        end
end