Module: Ronin::Database::Migrations

Extended by:
DataMapper::Property::Lookup
Defined in:
lib/ronin/database/migrations/1.0.0.rb,
lib/ronin/database/migrations/1.1.0.rb,
lib/ronin/database/migrations/graph.rb,
lib/ronin/database/migrations/migration.rb,
lib/ronin/database/migrations/migrations.rb,
lib/ronin/database/migrations/exceptions/unknown_migration.rb,
lib/ronin/database/migrations/exceptions/duplicate_migration.rb

Defined Under Namespace

Classes: DuplicateMigration, Graph, Migration, UnknownMigration

Class Method Summary collapse

Class Method Details

.migrate_down!(position_or_name = nil) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Migrates the database downwards to a certain migration position or name.

Parameters:

  • position_or_name (Symbol, Integer, nil) (defaults to: nil)

    The migration position or name to migrate the database down to.

Returns:

  • (true)

    The database was successfully migrated down.

Raises:

Since:

  • 1.0.1


176
177
178
179
180
181
182
# File 'lib/ronin/database/migrations/migrations.rb', line 176

def self.migrate_down!(position_or_name=nil)
  self.migrations.downto(position_or_name) do |migration|
    migration.perform_down
  end

  return true
end

.migrate_up!(position_or_name = nil) ⇒ true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Migrates the database upward to a given migration position or name.

Parameters:

  • position_or_name (Symbol, Integer, nil) (defaults to: nil)

    The migration position or name to migrate the database to.

Returns:

  • (true)

    The database was successfully migrated up.

Raises:

Since:

  • 1.0.1


151
152
153
154
155
156
157
# File 'lib/ronin/database/migrations/migrations.rb', line 151

def self.migrate_up!(position_or_name=nil)
  self.migrations.upto(position_or_name) do |migration|
    migration.perform_up
  end

  return true
end

.migration(*arguments) { ... } ⇒ Migration

Note:

Its recommended that you stick with raw SQL for migrations that manipulate data. If you write a migration using a model, then later change the model, there's a possibility the migration will no longer work. Using SQL will always work.

Defines a new migration.

Examples:

Defining a migration at a position

migration(1, :create_people_table) do
  up do
    create_table :people do
      column :id,   Integer, :serial => true
      column :name, String, :size => 50
      column :age,  Integer
    end
  end

  down do
    drop_table :people
  end
end

Defining a migration with a name

migration(:create_people_table) do
  up do
    create_table :people do
      column :id,   Integer, :serial => true
      column :name, String, :size => 50
      column :age,  Integer
    end
  end

  down do
    drop_table :people
  end
end

Defining a migration with dependencies

migration(:add_salary_column, :needs => :create_people_table) do
  up do
    modify_table :people do
      add_column :salary, Integer
    end
  end

  down do
    modify_table :people do
      drop_column :salary
    end
  end
end

Parameters:

  • arguments (Array)

    Additional arguments.

Yields:

  • [] The given block will define the migration.

Returns:

  • (Migration)

    The newly defined migration.

Raises:

  • (ArgumentError)

    The first argument was not a Symbol, String or Integer.

  • (DuplicateMigration)

    Another migration was previously defined with the same name or position.

Since:

  • 1.0.1


117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/ronin/database/migrations/migrations.rb', line 117

def self.migration(*arguments,&block)
  case arguments[0]
  when Integer
    position = arguments[0]
    name = arguments[1]
    options = (arguments[2] || {})

    self.migrations.migration_at(position,name,options,&block)
  when Symbol, String
    name = arguments[0]
    options = (arguments[1] || {})

    self.migrations.migration_named(name,options,&block)
  else
    raise(ArgumentError,"first argument must be an Integer, Symbol or a String",caller)
  end
end

.migrationsGraph

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The defined migrations.

Returns:

  • (Graph)

    The defined migrations.

Since:

  • 1.0.1


39
40
41
# File 'lib/ronin/database/migrations/migrations.rb', line 39

def self.migrations
  @migrations ||= Graph.new
end