-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathschema_test.go
38 lines (32 loc) · 939 Bytes
/
schema_test.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
package mysql_test
import (
"../mysql"
)
type MyTable struct {
Id int64 `json:"id" column_schema:"INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT"`
Key string `json:"json_key" column:"key" column_schema:"VARCHAR(128) NOT NULL" comment:"key"`
Value string `json:"value" column_schema:"TEXT" comment:"value"`
}
// This example showing how to generation
// a table creating sql from model define
func Example_schema() {
schema := mysql.NewSchema()
schema.Add(
MyTable{},
).Append(
"ALTER TABLE `my_table` ADD UNIQUE(`key`);",
).SetCharset(
"utf8mb4",
)
// Create files into filepath
// if filepath is empty, output to stdout
schema.ToFiles("")
// Output:
// DROP TABLE IF EXISTS `my_table`;
// CREATE TABLE `my_table` (
// `id` INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
// `key` VARCHAR(128) NOT NULL,
// `value` TEXT
// ) DEFAULT CHARSET=utf8mb4;
// ALTER TABLE `my_table` ADD UNIQUE(`key`);
}