Skip to content

Commit

Permalink
Expose COLUMN_TYPE column for MySQL
Browse files Browse the repository at this point in the history
resolves gnormal#86

In MySQL, the DATA_TYPE column contains only the basic type of the
column. The COLUMN_TYPE column provides a more detailed explanation of
the column's type, including "unsigned" for numeric types.
  • Loading branch information
Erik Pettis committed Dec 19, 2017
1 parent 61fec0c commit 8bba71b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions database/drivers/mysql/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func toDBColumn(c *columns.Row, log *log.Logger) (*database.Column, *database.En
Nullable: c.IsNullable == "YES",
HasDefault: c.ColumnDefault.String != "",
Type: c.DataType,
ColumnType: c.ColumnType,
Orig: *c,
IsPrimaryKey: strings.Contains(c.ColumnKey, "PRI"),
}
Expand Down
1 change: 1 addition & 0 deletions database/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type ForeignKey struct {
type Column struct {
Name string // the original name of the column in the DB
Type string // the original type of the column in the DB
ColumnType string // [mysql] a detailed description of the type of the column in the DB
IsArray bool // true if the column type is an array
Length int // non-zero if the type has a length (e.g. varchar[16])
UserDefined bool // true if the type is user-defined
Expand Down
1 change: 1 addition & 0 deletions run/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func makeData(log *log.Logger, info *database.Info, cfg *Config) (*data.DBData,
Table: table,
DBName: c.Name,
DBType: c.Type,
ColumnType: c.ColumnType,
IsArray: c.IsArray,
Length: c.Length,
UserDefined: c.UserDefined,
Expand Down
1 change: 1 addition & 0 deletions run/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type Column struct {
DBName string // the original name of the column in the DB
Type string // the converted name of the type
DBType string // the original type of the column in the DB
ColumnType string // [mysql] a detail description of the type of the column in the DB
IsArray bool // true if the column type is an array
Length int // non-zero if the type has a length (e.g. varchar[16])
UserDefined bool // true if the type is user-defined
Expand Down
2 changes: 1 addition & 1 deletion run/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Enum: {{.Name}}({{$schema}}.{{.DBName}})
{{end -}}
{{range .Tables}}
Table: {{.Name}}({{$schema}}.{{.DBName}})
{{makeTable .Columns "{{.Name}}|{{.DBName}}|{{.Type}}|{{.DBType}}|{{.IsArray}}|{{.IsPrimaryKey}}|{{.IsFK}}|{{.HasFKRef}}|{{.Length}}|{{.UserDefined}}|{{.Nullable}}|{{.HasDefault}}" "Name" "DBName" "Type" "DBType" "IsArray" "IsPrimaryKey" "IsFK" "HasFKRef" "Length" "UserDefined" "Nullable" "HasDefault" -}}
{{makeTable .Columns "{{.Name}}|{{.DBName}}|{{.Type}}|{{.DBType}}|{{.ColumnType}}|{{.IsArray}}|{{.IsPrimaryKey}}|{{.IsFK}}|{{.HasFKRef}}|{{.Length}}|{{.UserDefined}}|{{.Nullable}}|{{.HasDefault}}" "Name" "DBName" "Type" "DBType" "ColumnType" "IsArray" "IsPrimaryKey" "IsFK" "HasFKRef" "Length" "UserDefined" "Nullable" "HasDefault" -}}
Indexes:
{{makeTable .Indexes "{{.Name}}|{{.DBName}}|{{join .Columns.Names \", \"}}" "Name" "DBName" "Columns"}}
{{end -}}
Expand Down
48 changes: 34 additions & 14 deletions run/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (dummyDriver) Parse(log *log.Logger, conn string, schemaNames []string, fil
Columns: []*database.Column{{
Name: "col1",
Type: "int",
ColumnType: "int(10)",
IsPrimaryKey: true,
}, {
Name: "col2",
Expand All @@ -73,6 +74,7 @@ func (dummyDriver) Parse(log *log.Logger, conn string, schemaNames []string, fil
Columns: []*database.Column{{
Name: "col1",
Type: "int",
ColumnType: "int(10)",
IsPrimaryKey: true,
}},
}},
Expand Down Expand Up @@ -117,6 +119,7 @@ const expectYaml = `schemas:
dbname: col1
type: INTEGER
dbtype: int
columntype: int(10)
isarray: false
length: 0
userdefined: false
Expand All @@ -134,6 +137,7 @@ const expectYaml = `schemas:
dbname: col2
type: '*INTEGER'
dbtype: '*int'
columntype: ""
isarray: false
length: 0
userdefined: false
Expand All @@ -148,6 +152,7 @@ const expectYaml = `schemas:
dbname: col3
type: ""
dbtype: string
columntype: ""
isarray: false
length: 0
userdefined: false
Expand All @@ -162,6 +167,7 @@ const expectYaml = `schemas:
dbname: col4
type: ""
dbtype: '*string'
columntype: ""
isarray: false
length: 0
userdefined: false
Expand All @@ -177,6 +183,7 @@ const expectYaml = `schemas:
dbname: col1
type: INTEGER
dbtype: int
columntype: int(10)
isarray: false
length: 0
userdefined: false
Expand All @@ -198,6 +205,7 @@ const expectYaml = `schemas:
dbname: col1
type: INTEGER
dbtype: int
columntype: int(10)
isarray: false
length: 0
userdefined: false
Expand Down Expand Up @@ -228,6 +236,7 @@ const expectYaml = `schemas:
dbname: col1
type: INTEGER
dbtype: int
columntype: ""
isarray: false
length: 0
userdefined: false
Expand All @@ -242,6 +251,7 @@ const expectYaml = `schemas:
dbname: col2
type: INTEGER
dbtype: int
columntype: ""
isarray: false
length: 0
userdefined: false
Expand All @@ -260,6 +270,7 @@ const expectYaml = `schemas:
dbname: col1
type: INTEGER
dbtype: int
columntype: ""
isarray: false
length: 0
userdefined: false
Expand Down Expand Up @@ -301,14 +312,14 @@ Enum: abc enum(schema.enum)
Table: abc table(schema.table)
+----------+--------+----------+---------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| Name | DBName | Type | DBType | IsArray | IsPrimaryKey | IsFK | HasFKRef | Length | UserDefined | Nullable | HasDefault |
+----------+--------+----------+---------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| abc col1 | col1 | INTEGER | int | false | true | false | true | 0 | false | false | false |
| abc col2 | col2 | *INTEGER | *int | false | false | false | false | 0 | false | true | false |
| abc col3 | col3 | | string | false | false | false | false | 0 | false | false | false |
| abc col4 | col4 | | *string | false | false | false | false | 0 | false | true | false |
+----------+--------+----------+---------+---------+--------------+-------+----------+--------+-------------+----------+------------+
+----------+--------+----------+---------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| Name | DBName | Type | DBType | ColumnType | IsArray | IsPrimaryKey | IsFK | HasFKRef | Length | UserDefined | Nullable | HasDefault |
+----------+--------+----------+---------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| abc col1 | col1 | INTEGER | int | int(10) | false | true | false | true | 0 | false | false | false |
| abc col2 | col2 | *INTEGER | *int | | false | false | false | false | 0 | false | true | false |
| abc col3 | col3 | | string | | false | false | false | false | 0 | false | false | false |
| abc col4 | col4 | | *string | | false | false | false | false | 0 | false | true | false |
+----------+--------+----------+---------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
Indexes:
+---------------+-----------+----------+
| Name | DBName | Columns |
Expand All @@ -318,12 +329,12 @@ Indexes:
Table: abc tb2(schema.tb2)
+----------+--------+---------+--------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| Name | DBName | Type | DBType | IsArray | IsPrimaryKey | IsFK | HasFKRef | Length | UserDefined | Nullable | HasDefault |
+----------+--------+---------+--------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| abc col1 | col1 | INTEGER | int | false | true | false | false | 0 | false | false | false |
| abc col2 | col2 | INTEGER | int | false | false | true | false | 0 | false | false | false |
+----------+--------+---------+--------+---------+--------------+-------+----------+--------+-------------+----------+------------+
+----------+--------+---------+--------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| Name | DBName | Type | DBType | ColumnType | IsArray | IsPrimaryKey | IsFK | HasFKRef | Length | UserDefined | Nullable | HasDefault |
+----------+--------+---------+--------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
| abc col1 | col1 | INTEGER | int | | false | true | false | false | 0 | false | false | false |
| abc col2 | col2 | INTEGER | int | | false | false | true | false | 0 | false | false | false |
+----------+--------+---------+--------+------------+---------+--------------+-------+----------+--------+-------------+----------+------------+
Indexes:
+------+--------+---------+
| Name | DBName | Columns |
Expand Down Expand Up @@ -378,6 +389,7 @@ var expectJSON = `
"DBName": "col1",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "int(10)",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -400,6 +412,7 @@ var expectJSON = `
"DBName": "col2",
"Type": "*INTEGER",
"DBType": "*int",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -416,6 +429,7 @@ var expectJSON = `
"DBName": "col3",
"Type": "",
"DBType": "string",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -432,6 +446,7 @@ var expectJSON = `
"DBName": "col4",
"Type": "",
"DBType": "*string",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -450,6 +465,7 @@ var expectJSON = `
"DBName": "col1",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "int(10)",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand Down Expand Up @@ -478,6 +494,7 @@ var expectJSON = `
"DBName": "col1",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "int(10)",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand Down Expand Up @@ -524,6 +541,7 @@ var expectJSON = `
"DBName": "col1",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -540,6 +558,7 @@ var expectJSON = `
"DBName": "col2",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand All @@ -562,6 +581,7 @@ var expectJSON = `
"DBName": "col1",
"Type": "INTEGER",
"DBType": "int",
"ColumnType": "",
"IsArray": false,
"Length": 0,
"UserDefined": false,
Expand Down
1 change: 1 addition & 0 deletions site/content/templates/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Column is the data about a DB column of a table.
| DBName | string | the original name of the column in the DB
| Type |string | the converted name of the type
| DBType | string | the original type name of the column in the DB
| ColumnType | string | [mysql] a detailed description of the type of the column in the DB
| IsArray | boolean | true if the column type is an array
| Length | integer | non-zero if the type has a length (e.g. varchar[16])
| UserDefined | boolean | true if the type is user-defined
Expand Down

0 comments on commit 8bba71b

Please sign in to comment.