-
Notifications
You must be signed in to change notification settings - Fork 4
/
schema.go
77 lines (70 loc) · 1.92 KB
/
schema.go
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package migrator
// Schema allows adding commands on the schema.
// It should be used within migration to add migration commands.
type Schema struct {
pool []command
}
// CreateTable allows creating the table in the schema.
//
// Example:
// var s migrator.Schema
// t := migrator.Table{Name: "test"}
//
// s.CreateTable(t)
func (s *Schema) CreateTable(t Table) {
s.pool = append(s.pool, createTableCommand{t})
}
// DropTable removes a table from the schema.
// Warning ⚠️ BC incompatible!
//
// Example:
// var s migrator.Schema
// s.DropTable("test", false, "")
//
// Soft delete (drop if exists)
// s.DropTable("test", true, "")
func (s *Schema) DropTable(name string, soft bool, option string) {
s.pool = append(s.pool, dropTableCommand{name, soft, option})
}
// DropTableIfExists removes table if exists from the schema.
// Warning ⚠️ BC incompatible!
//
// Example:
// var s migrator.Schema
// s.DropTableIfExists("test")
func (s *Schema) DropTableIfExists(name string) {
s.pool = append(s.pool, dropTableCommand{name, true, ""})
}
// RenameTable executes a command to rename the table.
// Warning ⚠️ BC incompatible!
//
// Example:
// var s migrator.Schema
// s.RenameTable("old", "new")
func (s *Schema) RenameTable(old string, new string) {
s.pool = append(s.pool, renameTableCommand{old: old, new: new})
}
// AlterTable makes changes on the table level.
//
// Example:
// var s migrator.Schema
// var c TableCommands
// s.AlterTable("test", c)
func (s *Schema) AlterTable(name string, c TableCommands) {
s.pool = append(s.pool, alterTableCommand{name, c})
}
// CustomCommand allows adding the custom command to the Schema.
//
// Example:
// type customCommand string
//
// func (c customCommand) toSQL() string {
// return string(c)
// }
//
// c := customCommand("DROP PROCEDURE abc")
// var s migrator.Schema
// s.CustomCommand(c)
func (s *Schema) CustomCommand(c command) {
s.pool = append(s.pool, c)
}