Skip to content

Commit 3e2b816

Browse files
committed
strict comparison attributes
1 parent 3688ad8 commit 3e2b816

File tree

3 files changed

+127
-6
lines changed

3 files changed

+127
-6
lines changed

README.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ CREATE TABLE sample (
111111
*/
112112
```
113113

114-
3. Complete example:
114+
3. Comparing SQL schema with Go struct:
115115

116116
```go
117117
package main
@@ -197,3 +197,46 @@ func main() {
197197
_ = newMigration.WriteFiles("demo migration")
198198
}
199199
```
200+
201+
4. Comparing Two SQL Schemas:
202+
203+
```go
204+
package main
205+
206+
import (
207+
"github.com/sunary/sqlize"
208+
)
209+
210+
func main() {
211+
sql1 := sqlize.NewSqlize()
212+
sql1.FromString(`
213+
CREATE TABLE user (
214+
id INT AUTO_INCREMENT PRIMARY KEY,
215+
name VARCHAR(64),
216+
age INT,
217+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
218+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
219+
);
220+
CREATE UNIQUE INDEX idx_name_age ON user(name, age);
221+
`)
222+
223+
sql2 := sqlize.NewSqlize()
224+
sql2.FromString(`
225+
CREATE TABLE user (
226+
id INT,
227+
name VARCHAR(64),
228+
age INT,
229+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
230+
updated_at DATETIME
231+
);`)
232+
233+
sql1.Diff(*sql2)
234+
println(sql1.StringUp())
235+
//ALTER TABLE `user` MODIFY COLUMN `id` int(11) AUTO_INCREMENT PRIMARY KEY;
236+
//ALTER TABLE `user` MODIFY COLUMN `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP();
237+
//CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);
238+
239+
println(sql1.StringDown())
240+
//DROP INDEX `idx_name_age` ON `user`;
241+
}
242+
```

README_zh.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ CREATE TABLE sample (
111111
*/
112112
```
113113

114-
3. 完整示例
114+
3. 比较 SQL 架构与 Go 结构体
115115

116116
```go
117117
package main
@@ -196,4 +196,47 @@ func main() {
196196

197197
_ = newMigration.WriteFiles("demo migration")
198198
}
199+
```
200+
201+
4. 比较两个 SQL 架构:
202+
203+
```go
204+
package main
205+
206+
import (
207+
"github.com/sunary/sqlize"
208+
)
209+
210+
func main() {
211+
sql1 := sqlize.NewSqlize()
212+
sql1.FromString(`
213+
CREATE TABLE user (
214+
id INT AUTO_INCREMENT PRIMARY KEY,
215+
name VARCHAR(64),
216+
age INT,
217+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
218+
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
219+
);
220+
CREATE UNIQUE INDEX idx_name_age ON user(name, age);
221+
`)
222+
223+
sql2 := sqlize.NewSqlize()
224+
sql2.FromString(`
225+
CREATE TABLE user (
226+
id INT,
227+
name VARCHAR(64),
228+
age INT,
229+
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
230+
updated_at DATETIME
231+
);`)
232+
233+
sql1.Diff(*sql2)
234+
println(sql1.StringUp())
235+
// ALTER TABLE `user` MODIFY COLUMN `id` int(11) AUTO_INCREMENT PRIMARY KEY;
236+
// ALTER TABLE `user` MODIFY COLUMN `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP();
237+
// CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);
238+
239+
println(sql1.StringDown())
240+
// DROP INDEX `idx_name_age` ON `user`;
241+
}
199242
```

element/table.go

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"sort"
88
"strings"
99

10+
ptypes "github.com/auxten/postgresql-parser/pkg/sql/types"
1011
"github.com/pingcap/parser/ast"
12+
"github.com/pingcap/parser/types"
1113
"github.com/sunary/sqlize/utils"
1214
)
1315

@@ -252,16 +254,49 @@ func (t Table) getIndexForeignKey(fkName string) int {
252254
return -1
253255
}
254256

257+
func hasChangedMysqlOptions(new, old []*ast.ColumnOption) bool {
258+
if len(new) != len(old) {
259+
return true
260+
}
261+
262+
mNew := map[ast.ColumnOptionType]int{}
263+
for i := range old {
264+
mNew[old[i].Tp] += 1
265+
}
266+
267+
mOld := map[ast.ColumnOptionType]int{}
268+
for i := range old {
269+
mOld[old[i].Tp] += 1
270+
}
271+
272+
for k, v := range mOld {
273+
if mNew[k] != v {
274+
return true
275+
}
276+
}
277+
278+
return false
279+
}
280+
281+
func hasChangedMysqlType(new, old *types.FieldType) bool {
282+
return new != nil && new.String() != old.String()
283+
}
284+
285+
func hasChangePostgresType(new, old *ptypes.T) bool {
286+
return new != nil && new.SQLString() != old.SQLString()
287+
}
288+
255289
// Diff differ between 2 migrations
256290
func (t *Table) Diff(old Table) {
257291
for i := range t.Columns {
258292
if j := old.getIndexColumn(t.Columns[i].Name); t.Columns[i].Action == MigrateAddAction &&
259293
j >= 0 && old.Columns[j].Action != MigrateNoAction {
260-
if (t.Columns[i].MysqlType != nil && t.Columns[i].MysqlType.String() == old.Columns[j].MysqlType.String()) ||
261-
(t.Columns[i].PgType != nil && t.Columns[i].PgType.SQLString() == old.Columns[j].PgType.SQLString()) {
262-
t.Columns[i].Action = MigrateNoAction
263-
} else {
294+
if hasChangedMysqlOptions(t.Columns[i].Options, old.Columns[j].Options) ||
295+
hasChangedMysqlType(t.Columns[i].MysqlType, old.Columns[j].MysqlType) ||
296+
hasChangePostgresType(t.Columns[i].PgType, old.Columns[j].PgType) {
264297
t.Columns[i].Action = MigrateModifyAction
298+
} else {
299+
t.Columns[i].Action = MigrateNoAction
265300
}
266301
}
267302
}

0 commit comments

Comments
 (0)