@@ -117,25 +117,25 @@ CREATE TABLE sample (
117117package main
118118
119119import (
120- " time"
121-
122- " github.com/sunary/sqlize"
120+ " time"
121+
122+ " github.com/sunary/sqlize"
123123)
124124
125125type user struct {
126- ID int32 ` sql:"primary_key;auto_increment"`
127- Alias string ` sql:"type:VARCHAR(64)"`
128- Name string ` sql:"type:VARCHAR(64);unique;index_columns:name,age"`
129- Age int
130- Bio string
131- IgnoreMe string ` sql:"-"`
132- AcceptTncAt *time.Time ` sql:"index:idx_accept_tnc_at"`
133- CreatedAt time.Time ` sql:"default:CURRENT_TIMESTAMP"`
134- UpdatedAt time.Time ` sql:"default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index:idx_updated_at"`
126+ ID int32 ` sql:"primary_key;auto_increment"`
127+ Alias string ` sql:"type:VARCHAR(64)"`
128+ Name string ` sql:"type:VARCHAR(64);unique;index_columns:name,age"`
129+ Age int
130+ Bio string
131+ IgnoreMe string ` sql:"-"`
132+ AcceptTncAt *time.Time ` sql:"index:idx_accept_tnc_at"`
133+ CreatedAt time.Time ` sql:"default:CURRENT_TIMESTAMP"`
134+ UpdatedAt time.Time ` sql:"default:CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP;index:idx_updated_at"`
135135}
136136
137137func (user ) TableName () string {
138- return " user"
138+ return " user"
139139}
140140
141141var createStm = `
@@ -153,48 +153,49 @@ CREATE UNIQUE INDEX idx_name_age ON user(name, age);
153153CREATE INDEX idx_updated_at ON user(updated_at);`
154154
155155func main () {
156- n := time.Now ()
157- newMigration := sqlize.NewSqlize (sqlize.WithSqlTag (" sql" ), sqlize.WithMigrationFolder (" " ))
158- _ = newMigration.FromObjects (user{AcceptTncAt: &n})
159-
160- println (newMigration.StringUp ())
161- // CREATE TABLE `user` (
162- // `id` int(11) AUTO_INCREMENT PRIMARY KEY,
163- // `alias` varchar(64),
164- // `name` varchar(64),
165- // `age` int(11),
166- // `bio` text,
167- // `accept_tnc_at` datetime NULL,
168- // `created_at` datetime DEFAULT CURRENT_TIMESTAMP(),
169- // `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()
170- // );
171- // CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);
172- // CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);
173- // CREATE INDEX `idx_updated_at` ON `user`(`updated_at`);
174-
175- println (newMigration.StringDown ())
176- // DROP TABLE IF EXISTS `user`;
177-
178- oldMigration := sqlize.NewSqlize (sqlize.WithMigrationFolder (" " ))
179- // _ = oldMigration.FromMigrationFolder()
180- _ = oldMigration.FromString (createStm)
181-
182- newMigration.Diff (*oldMigration)
183-
184- println (newMigration.StringUp ())
185- // ALTER TABLE `user` ADD COLUMN `alias` varchar(64) AFTER `id`;
186- // ALTER TABLE `user` DROP COLUMN `gender`;
187- // CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);
188-
189- println (newMigration.StringDown ())
190- // ALTER TABLE `user` DROP COLUMN `alias`;
191- // ALTER TABLE `user` ADD COLUMN `gender` tinyint(1) AFTER `age`;
192- // DROP INDEX `idx_accept_tnc_at` ON `user`;
193-
194- println (newMigration.ArvoSchema ())
195- // ...
196-
197- _ = newMigration.WriteFiles (" demo migration" )
156+ n := time.Now ()
157+ newMigration := sqlize.NewSqlize (sqlize.WithSqlTag (" sql" ), sqlize.WithMigrationFolder (" " ))
158+ _ = newMigration.FromObjects (user{AcceptTncAt: &n})
159+
160+ println (newMigration.StringUp ())
161+ // CREATE TABLE `user` (
162+ // `id` int(11) AUTO_INCREMENT PRIMARY KEY,
163+ // `alias` varchar(64),
164+ // `name` varchar(64),
165+ // `age` int(11),
166+ // `bio` text,
167+ // `accept_tnc_at` datetime NULL,
168+ // `created_at` datetime DEFAULT CURRENT_TIMESTAMP(),
169+ // `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP()
170+ // );
171+ // CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);
172+ // CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);
173+ // CREATE INDEX `idx_updated_at` ON `user`(`updated_at`);
174+
175+ println (newMigration.StringDown ())
176+ // DROP TABLE IF EXISTS `user`;
177+
178+ oldMigration := sqlize.NewSqlize (sqlize.WithMigrationFolder (" " ))
179+ // _ = oldMigration.FromMigrationFolder()
180+ _ = oldMigration.FromString (createStm)
181+
182+ newMigration.Diff (*oldMigration)
183+
184+ println (newMigration.StringUp ())
185+ // ALTER TABLE `user` ADD COLUMN `alias` varchar(64) AFTER `id`;
186+ // ALTER TABLE `user` DROP COLUMN `gender`;
187+ // CREATE INDEX `idx_accept_tnc_at` ON `user`(`accept_tnc_at`);
188+
189+ println (newMigration.StringDown ())
190+ // ALTER TABLE `user` DROP COLUMN `alias`;
191+ // ALTER TABLE `user` ADD COLUMN `gender` tinyint(1) AFTER `age`;
192+ // DROP INDEX `idx_accept_tnc_at` ON `user`;
193+
194+ println (newMigration.MermaidJsLive ())
195+ println (newMigration.ArvoSchema ())
196+ // ...
197+
198+ _ = newMigration.WriteFiles (" demo migration" )
198199}
199200```
200201
0 commit comments