Class: Radical::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/radical/migration.rb

Class Method Summary collapse

Class Method Details

.change(&block) ⇒ Object



6
7
8
# File 'lib/radical/migration.rb', line 6

def change(&block)
  @change = block
end

.create_table(name, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/radical/migration.rb', line 18

def create_table(name, &block)
  return drop_table(name) if @change && @rollback

  table = Table.new(name)

  block.call(table)

  "create table #{name} ( id integer primary key, #{table.columns.join(',')} )"
end

.down(&block) ⇒ Object



14
15
16
# File 'lib/radical/migration.rb', line 14

def down(&block)
  @down = block
end

.drop_table(name) ⇒ Object



28
29
30
# File 'lib/radical/migration.rb', line 28

def drop_table(name)
  "drop table #{name}"
end

.migrate!(db:, version:) ⇒ Object



32
33
34
35
# File 'lib/radical/migration.rb', line 32

def migrate!(db:, version:)
  db.execute(@change&.call || @up&.call)
  db.execute 'insert into radical_migrations (version) values (?)', [version]
end

.rollback!(db:, version:) ⇒ Object



37
38
39
40
41
42
# File 'lib/radical/migration.rb', line 37

def rollback!(db:, version:)
  @rollback = true

  db.execute(@change&.call || @down&.call)
  db.execute 'delete from radical_migrations where version = ?', [version]
end

.up(&block) ⇒ Object



10
11
12
# File 'lib/radical/migration.rb', line 10

def up(&block)
  @up = block
end