Sequel::Plugins::InlineMigrations::
ClassMethods
module
Methods to extend Model classes with.
- MIGRATION_NAME_PATTERN
A Regexp for matching valid migration names
- migrations R
The Hash of Sequel::SimpleMigration objects for this model, keyed by name
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.
def after_create_table
super
self.register_existing_migrations
end
Table-migration hook; called once after missing tables are installed and pending migrations are run.
def after_migration
return true
end
Table-migration hook; called once before missing tables are installed and pending migrations are run.
def before_migration
return true
end
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
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>
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
Return a configured inline migrator set with the given target
migration.
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).
def register_existing_migrations
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