/
sqlspec.go
314 lines (292 loc) · 10.8 KB
/
sqlspec.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package sqlite
import (
"fmt"
"strconv"
"strings"
"ariga.io/atlas/schemahcl"
"ariga.io/atlas/sql/internal/specutil"
"ariga.io/atlas/sql/internal/sqlx"
"ariga.io/atlas/sql/schema"
"ariga.io/atlas/sql/sqlspec"
"github.com/hashicorp/hcl/v2/hclparse"
"github.com/zclconf/go-cty/cty"
)
type doc struct {
Tables []*sqlspec.Table `spec:"table"`
Views []*sqlspec.View `spec:"view"`
Triggers []*sqlspec.Trigger `spec:"trigger"`
Schemas []*sqlspec.Schema `spec:"schema"`
}
// evalSpec evaluates an Atlas DDL document using an unmarshaler into v by using the input.
func evalSpec(p *hclparse.Parser, v any, input map[string]cty.Value) error {
switch v := v.(type) {
case *schema.Realm:
var d doc
if err := hclState.Eval(p, &d, input); err != nil {
return err
}
if err := specutil.Scan(v,
&specutil.ScanDoc{Schemas: d.Schemas, Tables: d.Tables, Views: d.Views, Triggers: d.Triggers},
scanFuncs,
); err != nil {
return fmt.Errorf("specutil: failed converting to *schema.Realm: %w", err)
}
case *schema.Schema:
var d doc
if err := hclState.Eval(p, &d, input); err != nil {
return err
}
if len(d.Schemas) != 1 {
return fmt.Errorf("specutil: expecting document to contain a single schema, got %d", len(d.Schemas))
}
r := &schema.Realm{}
if err := specutil.Scan(r,
&specutil.ScanDoc{Schemas: d.Schemas, Tables: d.Tables, Views: d.Views, Triggers: d.Triggers},
scanFuncs,
); err != nil {
return err
}
*v = *r.Schemas[0]
case schema.Schema, schema.Realm:
return fmt.Errorf("sqlite: Eval expects a pointer: received %[1]T, expected *%[1]T", v)
default:
return hclState.Eval(p, v, input)
}
return nil
}
// MarshalSpec marshals v into an Atlas DDL document using a schemahcl.Marshaler.
func MarshalSpec(v any, marshaler schemahcl.Marshaler) ([]byte, error) {
return specutil.Marshal(v, marshaler, specutil.RealmFuncs{
Schema: schemaSpec,
Triggers: triggersSpec,
})
}
// convertTable converts a sqlspec.Table to a schema.Table. Table conversion is done without converting
// ForeignKeySpecs into ForeignKeys, as the target tables do not necessarily exist in the schema
// at this point. Instead, the linking is done by the convertSchema function.
func convertTable(spec *sqlspec.Table, parent *schema.Schema) (*schema.Table, error) {
t, err := specutil.Table(spec, parent, convertColumn, specutil.PrimaryKey, convertIndex, specutil.Check)
if err != nil {
return nil, err
}
if attr, ok := spec.Attr("without_rowid"); ok {
b, err := attr.Bool()
if err != nil {
return nil, err
}
if b {
t.AddAttrs(&WithoutRowID{})
}
}
if attr, ok := spec.Attr("strict"); ok {
b, err := attr.Bool()
if err != nil {
return nil, err
}
if b {
t.AddAttrs(&Strict{})
}
}
return t, nil
}
// convertView converts a sqlspec.View to a schema.View.
func convertView(spec *sqlspec.View, parent *schema.Schema) (*schema.View, error) {
v, err := specutil.View(
spec, parent,
func(c *sqlspec.Column, _ *schema.View) (*schema.Column, error) {
return specutil.Column(c, convertColumnType)
},
func(i *sqlspec.Index, v *schema.View) (*schema.Index, error) {
return nil, fmt.Errorf("unexpected view index %s.%s", v.Name, i.Name)
},
)
if err != nil {
return nil, err
}
return v, nil
}
// convertIndex converts a sqlspec.Index into a schema.Index.
func convertIndex(spec *sqlspec.Index, t *schema.Table) (*schema.Index, error) {
idx, err := specutil.Index(spec, t)
if err != nil {
return nil, err
}
if attr, ok := spec.Attr("where"); ok {
p, err := attr.String()
if err != nil {
return nil, err
}
idx.Attrs = append(idx.Attrs, &IndexPredicate{P: p})
}
return idx, nil
}
// convertColumn converts a sqlspec.Column into a schema.Column.
func convertColumn(spec *sqlspec.Column, _ *schema.Table) (*schema.Column, error) {
c, err := specutil.Column(spec, convertColumnType)
if err != nil {
return nil, err
}
if attr, ok := spec.Attr("auto_increment"); ok {
b, err := attr.Bool()
if err != nil {
return nil, err
}
if b {
c.AddAttrs(&AutoIncrement{})
}
}
if err := specutil.ConvertGenExpr(spec.Remain(), c, storedOrVirtual); err != nil {
return nil, err
}
return c, nil
}
// convertColumnType converts a sqlspec.Column into a concrete SQLite schema.Type.
func convertColumnType(spec *sqlspec.Column) (schema.Type, error) {
return TypeRegistry.Type(spec.Type, spec.Extra.Attrs)
}
// schemaSpec converts from a concrete SQLite schema to Atlas specification.
func schemaSpec(s *schema.Schema) (*specutil.SchemaSpec, error) {
return specutil.FromSchema(s, &specutil.SchemaFuncs{
Table: tableSpec,
View: viewSpec,
})
}
// tableSpec converts from a concrete SQLite sqlspec.Table to a schema.Table.
func tableSpec(t *schema.Table) (*sqlspec.Table, error) {
spec, err := specutil.FromTable(
t,
columnSpec,
specutil.FromPrimaryKey,
indexSpec,
specutil.FromForeignKey,
specutil.FromCheck,
)
if err != nil {
return nil, err
}
options := &schemahcl.Resource{}
if sqlx.Has(t.Attrs, &WithoutRowID{}) {
options.SetAttr(schemahcl.BoolAttr("without_rowid", true))
}
if sqlx.Has(t.Attrs, &Strict{}) {
options.SetAttr(schemahcl.BoolAttr("strict", true))
}
if options != nil {
spec.Extra.Children = append(spec.Extra.Children, options)
}
return spec, nil
}
// viewSpec converts from a concrete SQLite schema.View to a sqlspec.View.
func viewSpec(view *schema.View) (*sqlspec.View, error) {
spec, err := specutil.FromView(
view,
func(c *schema.Column, _ *schema.View) (*sqlspec.Column, error) {
return specutil.FromColumn(c, columnTypeSpec)
},
indexSpec,
)
if err != nil {
return nil, err
}
return spec, nil
}
func indexSpec(idx *schema.Index) (*sqlspec.Index, error) {
spec, err := specutil.FromIndex(idx)
if err != nil {
return nil, err
}
if i := (IndexPredicate{}); sqlx.Has(idx.Attrs, &i) && i.P != "" {
spec.Extra.Attrs = append(spec.Extra.Attrs, specutil.VarAttr("where", strconv.Quote(i.P)))
}
return spec, nil
}
// columnSpec converts from a concrete SQLite schema.Column into a sqlspec.Column.
func columnSpec(c *schema.Column, _ *schema.Table) (*sqlspec.Column, error) {
s, err := specutil.FromColumn(c, columnTypeSpec)
if err != nil {
return nil, err
}
if sqlx.Has(c.Attrs, &AutoIncrement{}) {
s.Extra.Attrs = append(s.Extra.Attrs, schemahcl.BoolAttr("auto_increment", true))
}
if x := (schema.GeneratedExpr{}); sqlx.Has(c.Attrs, &x) {
s.Extra.Children = append(s.Extra.Children, specutil.FromGenExpr(x, storedOrVirtual))
}
return s, nil
}
// columnTypeSpec converts from a concrete MySQL schema.Type into sqlspec.Column Type.
func columnTypeSpec(t schema.Type) (*sqlspec.Column, error) {
st, err := TypeRegistry.Convert(t)
if err != nil {
return nil, err
}
return &sqlspec.Column{Type: st}, nil
}
// TypeRegistry contains the supported TypeSpecs for the sqlite driver.
var TypeRegistry = schemahcl.NewRegistry(
schemahcl.WithFormatter(FormatType),
schemahcl.WithParser(ParseType),
schemahcl.WithSpecs(
schemahcl.NewTypeSpec(TypeReal, schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
schemahcl.NewTypeSpec(TypeBlob, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec(TypeText, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec(TypeInteger, schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("int", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("tinyint", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("smallint", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("mediumint", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("bigint", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.AliasTypeSpec("unsigned_big_int", "unsigned big int", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("int2", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("int8", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("uint64", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("double", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.AliasTypeSpec("double_precision", "double precision", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("float", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("character", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("varchar", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.AliasTypeSpec("varying_character", "varying character", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("nchar", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.AliasTypeSpec("native_character", "native character", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("nvarchar", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("clob", schemahcl.WithAttributes(schemahcl.SizeTypeAttr(false))),
schemahcl.NewTypeSpec("numeric", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
schemahcl.NewTypeSpec("decimal", schemahcl.WithAttributes(schemahcl.PrecisionTypeAttr(), schemahcl.ScaleTypeAttr())),
schemahcl.NewTypeSpec("bool"),
schemahcl.NewTypeSpec("boolean"),
schemahcl.NewTypeSpec("date"),
schemahcl.NewTypeSpec("datetime"),
schemahcl.NewTypeSpec("json"),
schemahcl.NewTypeSpec("uuid"),
),
)
var (
hclState = schemahcl.New(append(
specOptions,
schemahcl.WithTypes("table.column.type", TypeRegistry.Specs()),
schemahcl.WithTypes("view.column.type", TypeRegistry.Specs()),
schemahcl.WithScopedEnums("table.column.as.type", stored, virtual),
schemahcl.WithScopedEnums("table.foreign_key.on_update", specutil.ReferenceVars...),
schemahcl.WithScopedEnums("table.foreign_key.on_delete", specutil.ReferenceVars...),
)...)
// MarshalHCL marshals v into an Atlas HCL DDL document.
MarshalHCL = schemahcl.MarshalerFunc(func(v any) ([]byte, error) {
return MarshalSpec(v, hclState)
})
// EvalHCL implements the schemahcl.Evaluator interface.
EvalHCL = schemahcl.EvalFunc(evalSpec)
// EvalHCLBytes is a helper that evaluates an HCL document from a byte slice instead
// of from an hclparse.Parser instance.
EvalHCLBytes = specutil.HCLBytesFunc(EvalHCL)
)
// storedOrVirtual returns a STORED or VIRTUAL
// generated type option based on the given string.
func storedOrVirtual(s string) string {
if s = strings.ToUpper(s); s == "" {
return virtual
}
return s
}