-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
217 lines (180 loc) · 4.44 KB
/
migration.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package activerecord
import (
"context"
"errors"
"fmt"
"strings"
"time"
. "github.com/activegraph/activegraph/activesupport"
)
const (
SchemaMigrationsName = "schema_migrations"
)
type Table struct {
name string
primaryKey string
foreignKeys []string
columns map[string]Type
}
func (tb *Table) Name() string {
return tb.name
}
func (tb *Table) ForeignKeys() []string {
return tb.foreignKeys
}
func (tb *Table) Columns() (columns []ColumnDefinition) {
for columnName, columnType := range tb.columns {
columns = append(columns, ColumnDefinition{
Name: columnName,
Type: columnType,
IsPrimaryKey: columnName == tb.primaryKey,
})
}
if _, ok := tb.columns[tb.primaryKey]; !ok {
columns = append(columns, ColumnDefinition{
Name: "id",
Type: new(Int64),
IsPrimaryKey: true,
NotNull: true,
})
}
return columns
}
func (tb *Table) PrimaryKey(primaryKey string) {
tb.primaryKey = primaryKey
}
func (tb *Table) DefineColumn(name string, t Type) {
tb.columns[name] = t
}
func (tb *Table) Int64(name string) {
tb.DefineColumn(name, new(Int64))
}
func (tb *Table) String(name string) {
tb.DefineColumn(name, new(String))
}
func (tb *Table) DateTime(name string) {
tb.DefineColumn(name, new(DateTime))
}
func (tb *Table) ForeignKey(target string) {
tb.foreignKeys = append(tb.foreignKeys, target)
}
type References struct {
ForeignKey bool
}
func (tb *Table) References(target string, init ...References) {
switch len(init) {
case 0:
case 1:
if init[0].ForeignKey {
tb.ForeignKey(target)
}
default:
panic(ErrMultipleVariadicArguments{Name: "init"})
}
ref := fmt.Sprintf("%s_id", strings.TrimSuffix(target, "s"))
tb.DefineColumn(ref, new(Int64))
}
type M struct {
tables map[string]Table
references map[string]string
connections *connectionHandler
}
func (m *M) TableExists(tableName string) Result[bool] {
return FutureOk(true).AndThen(
func(bool) Result[bool] {
conn, err := m.connections.RetrieveConnection(primaryConnectionName)
if err != nil {
return Err[bool](err)
}
_, err = conn.ColumnDefinitions(context.TODO(), tableName)
if errors.Is(err, ErrTableNotExist{tableName}) {
return Ok(false)
}
if err != nil {
return Err[bool](err)
}
return Ok(true)
},
)
}
func (m *M) CreateTable(name string, init func(*Table)) {
table := Table{
name: name,
columns: make(map[string]Type),
}
init(&table)
m.tables[name] = table
}
func (m *M) AddForeignKey(owner, target string) {
if table, ok := m.tables[owner]; ok {
// If it's a new table, add a foreign key directly into the table definition.
table.ForeignKey(target)
m.tables[owner] = table
} else {
m.references[owner] = target
}
}
func Migrate(id string, init func(m *M)) {
m := M{
tables: make(map[string]Table),
references: make(map[string]string),
connections: globalConnectionHandler,
}
init(&m)
// Before applying further migrations, ensure that table exists.
// This operation should be unwrapped within a migration transaction.
schema := m.TableExists(SchemaMigrationsName).AndThen(
func(exists bool) Result[bool] {
if !exists {
m.CreateTable(SchemaMigrationsName, func(t *Table) {
t.PrimaryKey("version")
t.String("version")
t.DateTime("created_at")
})
}
return Ok(true)
},
)
// TODO: Use the specified connection name, instead of the default.
err := m.connections.Transaction(context.TODO(), func() error {
if schema.IsErr() {
return schema.Err()
}
conn, err := m.connections.RetrieveConnection(primaryConnectionName)
if err != nil {
return err
}
schemaTable, ok := m.tables[SchemaMigrationsName]
if ok {
delete(m.tables, SchemaMigrationsName)
err := conn.CreateTable(context.TODO(), &schemaTable)
if err != nil {
return err
}
}
SchemaMigration := New("schema_migration")
migration := SchemaMigration.Create(Hash{"version": id, "created_at": time.Now()})
if errors.Is(migration.Err(), new(ErrRecordNotUnique)) {
// Commit the transaction since it's already applied.
return nil
} else if migration.IsErr() {
return migration.Err()
}
for _, table := range m.tables {
err = conn.CreateTable(context.TODO(), &table)
if err != nil {
return err
}
}
for owner := range m.references {
err = conn.AddForeignKey(context.TODO(), owner, m.references[owner])
if err != nil {
return err
}
}
return nil
})
if err != nil {
panic(err)
}
}