Class: Mystic::Migration
- Inherits:
-
Object
- Object
- Mystic::Migration
- Defined in:
- lib/mystic/migration.rb
Constant Summary collapse
- Error =
Class.new StandardError
- IrreversibleError =
Class.new StandardError
Instance Method Summary collapse
- #alter_table(name) {|table| ... } ⇒ Object
- #create_ext(extname) ⇒ Object
- #create_table(name) {|table| ... } ⇒ Object
- #create_view(name, sql) ⇒ Object
- #drop_ext(extname) ⇒ Object
- #drop_index(idx_name) ⇒ Object
- #drop_table(name, opts = {}) ⇒ Object
- #drop_view(name) ⇒ Object
- #exec_migration(direction) ⇒ Object
-
#execute(obj) ⇒ Object
All migration SQL goes through here.
-
#initialize ⇒ Migration
constructor
A new instance of Migration.
- #irreversible! ⇒ Object
- #irreversible? ⇒ Boolean
- #migrate ⇒ Object
- #rollback ⇒ Object
- #to_sql(direction) ⇒ Object
Constructor Details
#initialize ⇒ Migration
Returns a new instance of Migration.
8 9 10 11 |
# File 'lib/mystic/migration.rb', line 8 def initialize @irreversible = false @sql = "" end |
Instance Method Details
#alter_table(name) {|table| ... } ⇒ Object
67 68 69 70 71 72 |
# 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) ⇒ Object
83 84 85 |
# File 'lib/mystic/migration.rb', line 83 def create_ext extname execute "CREATE EXTENSION \"#{extname.to_s}\"" end |
#create_table(name) {|table| ... } ⇒ Object
60 61 62 63 64 65 |
# 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) ⇒ Object
91 92 93 |
# File 'lib/mystic/migration.rb', line 91 def create_view name, sql execute "CREATE VIEW #{name} AS #{sql}" end |
#drop_ext(extname) ⇒ Object
87 88 89 |
# File 'lib/mystic/migration.rb', line 87 def drop_ext extname execute "DROP EXTENSION \"#{extname.to_s}\"" end |
#drop_index(idx_name) ⇒ Object
79 80 81 |
# File 'lib/mystic/migration.rb', line 79 def drop_index idx_name execute "DROP INDEX #{idx_name}" end |
#drop_table(name, opts = {}) ⇒ Object
74 75 76 77 |
# 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) ⇒ Object
95 96 97 |
# File 'lib/mystic/migration.rb', line 95 def drop_view name execute "DROP VIEW #{name}" end |
#exec_migration(direction) ⇒ Object
37 38 39 40 |
# File 'lib/mystic/migration.rb', line 37 def exec_migration direction sql = to_sql direction Mystic.postgres.execute sql end |
#execute(obj) ⇒ Object
All migration SQL goes through here
48 49 50 |
# 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! ⇒ Object
52 53 54 |
# File 'lib/mystic/migration.rb', line 52 def irreversible! @irreversible = true end |
#irreversible? ⇒ Boolean
56 57 58 |
# File 'lib/mystic/migration.rb', line 56 def irreversible? @irreversible end |
#migrate ⇒ Object
13 14 15 |
# File 'lib/mystic/migration.rb', line 13 def migrate exec_migration :up end |
#rollback ⇒ Object
17 18 19 |
# File 'lib/mystic/migration.rb', line 17 def rollback exec_migration :down end |
#to_sql(direction) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# 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 |