/
mysql.go
130 lines (120 loc) · 3.13 KB
/
mysql.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
package importschema
import (
"context"
"encoding/json"
"fmt"
"ariga.io/atlas/sql/mysql"
"ariga.io/atlas/sql/schema"
"entgo.io/contrib/schemast"
"entgo.io/ent"
"entgo.io/ent/schema/field"
)
const (
mTinyInt = "tinyint" // MYSQL_TYPE_TINY
mSmallInt = "smallint" // MYSQL_TYPE_SHORT
mInt = "int" // MYSQL_TYPE_LONG
mMediumInt = "mediumint" // MYSQL_TYPE_INT24
mBigInt = "bigint" // MYSQL_TYPE_LONGLONG
)
// MySQL holds the schema import options and an Atlas inspector instance
type MySQL struct {
*ImportOptions
}
// NewMySQL - create aמ import structure for MySQL.
func NewMySQL(i *ImportOptions) (*MySQL, error) {
return &MySQL{
ImportOptions: i,
}, nil
}
// SchemaMutations implements SchemaImporter.
func (m *MySQL) SchemaMutations(ctx context.Context) ([]schemast.Mutator, error) {
inspectOptions := &schema.InspectOptions{
Tables: m.tables,
}
s, err := m.driver.InspectSchema(ctx, m.driver.SchemaName, inspectOptions)
if err != nil {
return nil, err
}
tables := s.Tables
if m.excludedTables != nil {
tables = nil
excludedTableNames := make(map[string]bool)
for _, t := range m.excludedTables {
excludedTableNames[t] = true
}
// filter out tables that are in excludedTables:
for _, t := range s.Tables {
if !excludedTableNames[t.Name] {
tables = append(tables, t)
}
}
}
return schemaMutations(m.field, tables)
}
func (m *MySQL) field(column *schema.Column) (f ent.Field, err error) {
name := column.Name
switch typ := column.Type.Type.(type) {
case *schema.BinaryType:
f = field.Bytes(name)
case *schema.BoolType:
f = field.Bool(name)
case *schema.DecimalType:
f = field.Float(name)
case *schema.EnumType:
f = field.Enum(name).Values(typ.Values...)
case *schema.FloatType:
f = m.convertFloat(typ, name)
case *schema.IntegerType:
f = m.convertInteger(typ, name)
case *schema.JSONType:
f = field.JSON(name, json.RawMessage{})
case *schema.StringType:
f = field.String(name)
case *schema.TimeType:
f = field.Time(name)
default:
return nil, fmt.Errorf("unsupported type %q for column %v", typ, column.Name)
}
applyColumnAttributes(f, column)
return f, err
}
func (m *MySQL) convertFloat(typ *schema.FloatType, name string) (f ent.Field) {
// A precision from 0 to 23 results in a 4-byte single-precision FLOAT column.
// A precision from 24 to 53 results in an 8-byte double-precision DOUBLE column:
// https://dev.mysql.com/doc/refman/8.0/en/floating-point-types.html
if typ.T == mysql.TypeDouble {
return field.Float(name)
}
return field.Float32(name)
}
func (m *MySQL) convertInteger(typ *schema.IntegerType, name string) (f ent.Field) {
if typ.Unsigned {
switch typ.T {
case mTinyInt:
f = field.Uint8(name)
case mSmallInt:
f = field.Uint16(name)
case mMediumInt:
f = field.Uint32(name)
case mInt:
f = field.Uint32(name)
case mBigInt:
f = field.Uint64(name)
}
return f
}
switch typ.T {
case mTinyInt:
f = field.Int8(name)
case mSmallInt:
f = field.Int16(name)
case mMediumInt:
f = field.Int32(name)
case mInt:
f = field.Int32(name)
case mBigInt:
// Int64 is not used on purpose.
f = field.Int(name)
}
return f
}