class Mystic::Migration

Constants

Error
IrreversibleError

Public Class Methods

new() click to toggle source
# File lib/mystic/migration.rb, line 8
def initialize
        @irreversible = false
        @sql = ""
end

Public Instance Methods

alter_table(name) { |table| ... } click to toggle source
# File lib/mystic/migration.rb, line 67
def alter_table name
                    raise ArgumentError, "No block provided, a block is required to alter a table." unless block_given?
  table = Mystic::SQL::Table.alter :name => name
  yield table
  execute table
end
create_ext(extname) click to toggle source
# File lib/mystic/migration.rb, line 83
def create_ext extname
  execute "CREATE EXTENSION \"#{extname.to_s}\""
end
create_table(name) { |table| ... } click to toggle source
# File lib/mystic/migration.rb, line 60
def create_table name
  raise ArgumentError, "No block provided, a block is required to create a table." unless block_given?
  table = Mystic::SQL::Table.create :name => name
  yield table
  execute table
end
create_view(name, sql) click to toggle source
# File lib/mystic/migration.rb, line 91
def create_view name, sql
  execute "CREATE VIEW #{name} AS #{sql}"
end
drop_ext(extname) click to toggle source
# File lib/mystic/migration.rb, line 87
def drop_ext extname
  execute "DROP EXTENSION \"#{extname.to_s}\""
end
drop_index(idx_name) click to toggle source
# File lib/mystic/migration.rb, line 79
          def drop_index idx_name
execute "DROP INDEX #{idx_name}"
          end
drop_table(name, opts={}) click to toggle source
# File lib/mystic/migration.rb, line 74
def drop_table name, opts={}
                    irreversible!
  execute "DROP TABLE #{name} #{opts[:cascade] ? "CASCADE" : "RESTRICT" }"
end
drop_view(name) click to toggle source
# File lib/mystic/migration.rb, line 95
def drop_view name
  execute "DROP VIEW #{name}"
end
exec_migration(direction) click to toggle source
# File lib/mystic/migration.rb, line 37
          def exec_migration direction
sql = to_sql direction
                  Mystic.postgres.execute sql
          end
execute(obj) click to toggle source

All migration SQL goes through here

# File lib/mystic/migration.rb, line 48
def execute obj
                    @sql << obj.to_s.sql_terminate # to_sql isn't defined for strings, to_sql is aliased to to_s
end
irreversible!() click to toggle source
# File lib/mystic/migration.rb, line 52
def irreversible!
        @irreversible = true
end
irreversible?() click to toggle source
# File lib/mystic/migration.rb, line 56
def irreversible?
        @irreversible
end
migrate() click to toggle source
# File lib/mystic/migration.rb, line 13
def migrate
        exec_migration :up
end
rollback() click to toggle source
# File lib/mystic/migration.rb, line 17
def rollback
        exec_migration :down
end
to_sql(direction) click to toggle source
# File lib/mystic/migration.rb, line 21
def to_sql direction
  @sql = ""
                    _direction = direction.to_sym
                    
                    raise ArgumentError, "Direction must be either :up or :down." unless [:up, :down].include? _direction
                    raise IrreversibleError, "Impossible to roll back an irreversible migration." if _direction == :down && irreversible?
                    
  execute "BEGIN"
                    method(_direction).call
  execute "COMMIT"
  
  res = @sql.dup
  @sql = ""
  res
end