/
create_table.cr
62 lines (56 loc) · 1.87 KB
/
create_table.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
module Marten
module DB
abstract class Migration
module Operation
class CreateTable < Base
getter name
getter columns
getter indexes
getter unique_constraints
def initialize(
@name : String,
@columns : Array(Management::Column::Base),
@unique_constraints : Array(Management::Constraint::Unique) = [] of Management::Constraint::Unique,
@indexes : Array(Management::Index) = [] of Management::Index
)
end
def describe : String
"Create #{@name} table"
end
def mutate_db_backward(
app_label : String,
schema_editor : Management::SchemaEditor::Base,
from_state : Management::ProjectState,
to_state : Management::ProjectState
) : Nil
table = from_state.get_table(app_label, @name)
schema_editor.delete_table(table)
end
def mutate_db_forward(
app_label : String,
schema_editor : Management::SchemaEditor::Base,
from_state : Management::ProjectState,
to_state : Management::ProjectState
) : Nil
table = to_state.get_table(app_label, @name)
schema_editor.create_table(table)
end
def mutate_state_forward(app_label : String, state : Management::ProjectState) : Nil
state.add_table(
Management::TableState.new(
app_label: app_label,
name: name,
columns: columns.dup,
unique_constraints: unique_constraints.dup,
indexes: indexes.dup
)
)
end
def serialize : String
ECR.render "#{__DIR__}/templates/create_table.ecr"
end
end
end
end
end
end