-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema.go
172 lines (164 loc) · 5.21 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
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
package notebrew
import (
"bytes"
_ "embed"
"encoding/json"
"fmt"
"github.com/bokwoon95/sqddl/ddl"
)
type rawTable struct {
Table string
PrimaryKey []string
Columns []struct {
Dialect string
Column string
Type map[string]string
Generated struct {
Expression string
Stored bool
}
Index bool
PrimaryKey bool
Unique bool
NotNull bool
References struct {
Table string
Column string
}
}
Indexes []struct {
Dialect string
Type string
Unique bool
Columns []string
IncludeColumns []string
Predicate string
}
Constraints []struct {
Dialect string
Type string
Columns []string
ReferencesTable string
ReferencesColumns []string
}
}
//go:embed schema_files.json
var filesSchemaBytes []byte
// FilesCatalog returns a catalog representing the desired catalog schema for
// the files database.
func FilesCatalog(dialect string) (*ddl.Catalog, error) {
return UnmarshalCatalog(dialect, filesSchemaBytes)
}
//go:embed schema_database.json
var databaseSchemaBytes []byte
// DatabaseCatalog returns a catalog representing the desired catalog schema for
// the database.
func DatabaseCatalog(dialect string) (*ddl.Catalog, error) {
return UnmarshalCatalog(dialect, databaseSchemaBytes)
}
// UnmarshalCatalog unmarshals a JSON payload into a *ddl.Catalog.
func UnmarshalCatalog(dialect string, b []byte) (*ddl.Catalog, error) {
var rawTables []rawTable
decoder := json.NewDecoder(bytes.NewReader(b))
decoder.DisallowUnknownFields()
err := decoder.Decode(&rawTables)
if err != nil {
return nil, err
}
catalog := &ddl.Catalog{
Dialect: dialect,
}
cache := ddl.NewCatalogCache(catalog)
schema := cache.GetOrCreateSchema(catalog, "")
for _, rawTable := range rawTables {
table := cache.GetOrCreateTable(schema, rawTable.Table)
if len(rawTable.PrimaryKey) != 0 {
cache.AddOrUpdateConstraint(table, ddl.Constraint{
ConstraintName: ddl.GenerateName(ddl.PRIMARY_KEY, rawTable.Table, rawTable.PrimaryKey),
ConstraintType: ddl.PRIMARY_KEY,
Columns: rawTable.PrimaryKey,
})
}
for _, rawColumn := range rawTable.Columns {
columnType := rawColumn.Type[dialect]
if columnType == "" {
columnType = rawColumn.Type["default"]
}
if rawColumn.Dialect != "" && rawColumn.Dialect != dialect {
continue
}
cache.AddOrUpdateColumn(table, ddl.Column{
ColumnName: rawColumn.Column,
ColumnType: columnType,
IsPrimaryKey: rawColumn.PrimaryKey,
IsUnique: rawColumn.Unique,
IsNotNull: rawColumn.NotNull,
GeneratedExpr: rawColumn.Generated.Expression,
GeneratedExprStored: rawColumn.Generated.Stored,
})
if rawColumn.PrimaryKey {
cache.AddOrUpdateConstraint(table, ddl.Constraint{
ConstraintName: ddl.GenerateName(ddl.PRIMARY_KEY, rawTable.Table, []string{rawColumn.Column}),
ConstraintType: ddl.PRIMARY_KEY,
Columns: []string{rawColumn.Column},
})
}
if rawColumn.Unique {
cache.AddOrUpdateConstraint(table, ddl.Constraint{
ConstraintName: ddl.GenerateName(ddl.UNIQUE, rawTable.Table, []string{rawColumn.Column}),
ConstraintType: ddl.UNIQUE,
Columns: []string{rawColumn.Column},
})
}
if rawColumn.Index {
cache.AddOrUpdateIndex(table, ddl.Index{
IndexName: ddl.GenerateName(ddl.INDEX, rawTable.Table, []string{rawColumn.Column}),
Columns: []string{rawColumn.Column},
})
}
if rawColumn.References.Table != "" {
columnName := rawColumn.References.Column
if columnName == "" {
columnName = rawColumn.Column
}
cache.AddOrUpdateConstraint(table, ddl.Constraint{
ConstraintName: ddl.GenerateName(ddl.FOREIGN_KEY, rawTable.Table, []string{rawColumn.Column}),
ConstraintType: ddl.FOREIGN_KEY,
Columns: []string{rawColumn.Column},
ReferencesTable: rawColumn.References.Table,
ReferencesColumns: []string{columnName},
UpdateRule: ddl.CASCADE,
})
}
}
for _, rawIndex := range rawTable.Indexes {
if rawIndex.Dialect != "" && rawIndex.Dialect != dialect {
continue
}
cache.AddOrUpdateIndex(table, ddl.Index{
IndexName: ddl.GenerateName(ddl.INDEX, rawTable.Table, rawIndex.Columns),
IndexType: rawIndex.Type,
IsUnique: rawIndex.Unique,
Columns: rawIndex.Columns,
IncludeColumns: rawIndex.IncludeColumns,
Predicate: rawIndex.Predicate,
})
}
for _, rawConstraint := range rawTable.Constraints {
if rawConstraint.Dialect != "" && rawConstraint.Dialect != dialect {
continue
}
if rawConstraint.Type != ddl.PRIMARY_KEY && rawConstraint.Type != ddl.FOREIGN_KEY && rawConstraint.Type != ddl.UNIQUE {
return nil, fmt.Errorf("%s: invalid constraint type %q", rawTable.Table, rawConstraint.Type)
}
cache.AddOrUpdateConstraint(table, ddl.Constraint{
ConstraintName: ddl.GenerateName(rawConstraint.Type, rawTable.Table, rawConstraint.Columns),
ConstraintType: rawConstraint.Type,
Columns: rawConstraint.Columns,
ReferencesTable: rawConstraint.ReferencesTable,
ReferencesColumns: rawConstraint.ReferencesColumns,
})
}
}
return catalog, nil
}