Class: SlonikMigration::Extension

Inherits:
Object
  • Object
show all
Defined in:
lib/slonik_migration/extension.rb

Class Method Summary collapse

Class Method Details

.overwriteObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/slonik_migration/extension.rb', line 8

def overwrite
  ActiveRecord::Migrator.class_eval do
    private
    alias_method :orig_use_transaction?, :use_transaction?
    def use_transaction?(migration)
      false
    end
  end

  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
    alias_method :orig_execute, :execute
    def execute(sql, name = nil)
      if sql =~ /^create (table|sequence|view) "([^"]+)"/i
        ret = SlonikMigration::Command.new.execute(sql, target: $1, name: $2)
        raise "failed to execute slonik command." unless ret
      elsif sql =~ /^(create|alter|drop)/i
        ret = SlonikMigration::Command.new.execute(sql)
        raise "failed to execute slonik command." unless ret
      else
        orig_execute sql, name
      end
    end
  end
end

.restoreObject



33
34
35
36
37
38
39
40
41
# File 'lib/slonik_migration/extension.rb', line 33

def restore
  ActiveRecord::Migrator.class_eval do
    alias_method :use_transaction?, :orig_use_transaction?
  end

  ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.class_eval do
    alias_method :execute, :orig_execute
  end
end