Class: Reins::Migration
- Inherits:
-
Object
show all
- Defined in:
- lib/reins/migration.rb
Defined Under Namespace
Classes: NotSupported, TableDefinition
Constant Summary
collapse
- SUPPORTED_TYPES =
{
string: "VARCHAR(255)",
text: "TEXT",
integer: "INTEGER",
float: "FLOAT",
boolean: "BOOLEAN",
datetime: "DATETIME"
}.freeze
Instance Method Summary
collapse
-
#add_column(table, name, type) ⇒ Object
-
#add_index(table, columns, unique: false, name: nil) ⇒ Object
-
#change_column(_table, _name, _type) ⇒ Object
-
#create_table(name, &block) ⇒ Object
-
#drop_table(name) ⇒ Object
-
#remove_column(table, name) ⇒ Object
-
#remove_index(table, columns_or_name) ⇒ Object
-
#rename_column(table, old_name, new_name) ⇒ Object
-
#run_down ⇒ Object
-
#run_up ⇒ Object
Instance Method Details
#add_column(table, name, type) ⇒ Object
55
56
57
58
59
|
# File 'lib/reins/migration.rb', line 55
def add_column(table, name, type, **)
return record_op(:add_column, table, name) if @recording_mode
execute("ALTER TABLE #{table} ADD COLUMN #{name} #{sql_type_for(type)}")
end
|
#add_index(table, columns, unique: false, name: nil) ⇒ Object
67
68
69
70
71
72
73
|
# File 'lib/reins/migration.rb', line 67
def add_index(table, columns, unique: false, name: nil)
return record_op(:add_index, table, columns) if @recording_mode
cols = Array(columns)
index_name = name || "index_#{table}_on_#{cols.join('_')}"
execute("CREATE #{'UNIQUE ' if unique}INDEX #{index_name} ON #{table} (#{cols.join(', ')})")
end
|
#change_column(_table, _name, _type) ⇒ Object
92
93
94
95
|
# File 'lib/reins/migration.rb', line 92
def change_column(_table, _name, _type, **)
raise NotSupported,
"change_column is not supported in SQLite — define explicit up/down with table copy"
end
|
#create_table(name, &block) ⇒ Object
40
41
42
43
44
45
46
47
|
# File 'lib/reins/migration.rb', line 40
def create_table(name, &block)
return record_op(:create_table, name) if @recording_mode
td = TableDefinition.new(name)
block&.call(td)
execute(td.to_sql)
td.indexes.each { |args| add_index(*args) }
end
|
#drop_table(name) ⇒ Object
49
50
51
52
53
|
# File 'lib/reins/migration.rb', line 49
def drop_table(name)
return record_op(:drop_table, name) if @recording_mode
execute("DROP TABLE #{name}")
end
|
#remove_column(table, name) ⇒ Object
61
62
63
64
65
|
# File 'lib/reins/migration.rb', line 61
def remove_column(table, name)
return record_op(:remove_column, table, name) if @recording_mode
execute("ALTER TABLE #{table} DROP COLUMN #{name}")
end
|
#remove_index(table, columns_or_name) ⇒ Object
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/reins/migration.rb', line 75
def remove_index(table, columns_or_name)
return record_op(:remove_index, table, columns_or_name) if @recording_mode
index_name = if columns_or_name.is_a?(String)
columns_or_name
else
"index_#{table}_on_#{Array(columns_or_name).join('_')}"
end
execute("DROP INDEX #{index_name}")
end
|
#rename_column(table, old_name, new_name) ⇒ Object
86
87
88
89
90
|
# File 'lib/reins/migration.rb', line 86
def rename_column(table, old_name, new_name)
return record_op(:rename_column, table, old_name, new_name) if @recording_mode
execute("ALTER TABLE #{table} RENAME COLUMN #{old_name} TO #{new_name}")
end
|
#run_down ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/reins/migration.rb', line 26
def run_down
if respond_to?(:down)
down
elsif respond_to?(:change)
@recording_mode = true
@recorded_ops = []
change
@recording_mode = false
invert_recorded_ops
else
raise Reins::IrreversibleMigration, "Migration #{self.class.name} cannot be rolled back"
end
end
|
#run_up ⇒ Object
16
17
18
19
20
21
22
23
24
|
# File 'lib/reins/migration.rb', line 16
def run_up
if respond_to?(:up)
up
elsif respond_to?(:change)
change
else
raise "Migration #{self.class.name} must define `up` or `change`"
end
end
|