-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
172 lines (142 loc) · 3.3 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 schema
import (
"encoding/json"
"io/ioutil"
"github.com/macinnir/dvc/core/lib"
)
const (
SchemaTypeMySQL = "mysql"
SchemaTypeSQLite = "sqlite"
SchemaTypePostgreSQL = "postgresql"
SchemaTypeSQLServer = "sqlserver"
)
// loadDatabase loads a database from configuration
func LoadLocalSchemas() (*SchemaList, error) {
fileBytes, e := ioutil.ReadFile(lib.SchemasFilePath)
if e != nil {
return nil, e
}
schemaList := &SchemaList{
Schemas: []*Schema{},
}
e = json.Unmarshal(fileBytes, schemaList)
if e != nil {
return nil, e
}
return schemaList, nil
}
// DataTypeToGoTypeString converts a database type to its equivalent golang datatype
func IsValidSQLType(str string) bool {
switch str {
case "int":
return true
case "varchar":
return true
case "enum":
return true
case "text":
return true
case "date":
return true
case "datetime":
return true
case "bigint":
return true
case "tinyint":
return true
case "char":
return true
case "decimal":
return true
}
return false
}
// DataTypeToFormatString converts a database type to its equivalent golang datatype
func GoTypeFormatString(goType string) (fieldType string) {
fieldType = "%s"
switch goType {
case "int", "int64":
fieldType = "%d"
case "string":
fieldType = "%s"
case "float", "float64":
fieldType = "%f"
}
return
}
// DataTypeToFormatString converts a database type to its equivalent golang datatype
// TODO move to mysql specific
// TODO make column types constants in mysql
func DataTypeToFormatString(column *Column) (fieldType string) {
fieldType = "%s"
switch column.DataType {
case "int", "bigint", "tinyint":
fieldType = "%d"
case "varchar", "enum", "text", "date", "datetime", "char":
fieldType = "%s"
case "decimal":
fieldType = "%f"
}
return
}
// DataTypeToGoTypeString converts a database type to its equivalent golang datatype
func DataTypeToGoTypeString(column *Column) (fieldType string) {
fieldType = "int64"
switch column.DataType {
case "int", "bigint":
fieldType = "int64"
case "tinyint":
fieldType = "int"
case "tinytext", "text", "longtext", "varchar", "date", "datetime", "char", "enum":
fieldType = "string"
case "decimal":
fieldType = "float64"
}
if column.IsNullable {
switch fieldType {
case "string":
// fieldType = "sql.NullString"
fieldType = "null.String"
case "int64":
// fieldType = "sql.NullInt64"
fieldType = "null.Int"
case "float64":
// fieldType = "sql.NullFloat64"
fieldType = "null.Float"
}
}
return
}
// TODO move to mysql specific
// TODO make column types constants in mysql
func DataTypeToTypescriptString(column *Column) (fieldType string) {
fieldType = "number"
switch column.DataType {
case "varchar", "enum", "text", "date", "datetime", "char", "longtext":
fieldType = "string"
}
return
}
// TODO move to mysql specific
// TODO make column types constants in mysql
func IsString(column *Column) bool {
switch column.DataType {
case "varchar", "enum", "text", "longtext", "tinytext", "date", "datetime", "char":
return true
default:
return false
}
}
type SchemaComparison struct {
Database string
DatabaseKey string
Additions int
Alterations int
Deletions int
Changes []*SchemaChange
}
type SchemaChange struct {
Type string
SQL string
IsDestructive bool
}