Skip to content

strict comparison attributes #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ CREATE TABLE sample (
*/
```

3. Complete example:
3. Comparing SQL schema with Go struct:

```go
package main
Expand Down Expand Up @@ -197,3 +197,46 @@ func main() {
_ = newMigration.WriteFiles("demo migration")
}
```

4. Comparing Two SQL Schemas:

```go
package main

import (
"github.com/sunary/sqlize"
)

func main() {
sql1 := sqlize.NewSqlize()
sql1.FromString(`
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(64),
age INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX idx_name_age ON user(name, age);
`)

sql2 := sqlize.NewSqlize()
sql2.FromString(`
CREATE TABLE user (
id INT,
name VARCHAR(64),
age INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME
);`)

sql1.Diff(*sql2)
println(sql1.StringUp())
//ALTER TABLE `user` MODIFY COLUMN `id` int(11) AUTO_INCREMENT PRIMARY KEY;
//ALTER TABLE `user` MODIFY COLUMN `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP();
//CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);

println(sql1.StringDown())
//DROP INDEX `idx_name_age` ON `user`;
}
```
45 changes: 44 additions & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ CREATE TABLE sample (
*/
```

3. 完整示例
3. 比较 SQL 架构与 Go 结构体

```go
package main
Expand Down Expand Up @@ -196,4 +196,47 @@ func main() {

_ = newMigration.WriteFiles("demo migration")
}
```

4. 比较两个 SQL 架构:

```go
package main

import (
"github.com/sunary/sqlize"
)

func main() {
sql1 := sqlize.NewSqlize()
sql1.FromString(`
CREATE TABLE user (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(64),
age INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE UNIQUE INDEX idx_name_age ON user(name, age);
`)

sql2 := sqlize.NewSqlize()
sql2.FromString(`
CREATE TABLE user (
id INT,
name VARCHAR(64),
age INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME
);`)

sql1.Diff(*sql2)
println(sql1.StringUp())
// ALTER TABLE `user` MODIFY COLUMN `id` int(11) AUTO_INCREMENT PRIMARY KEY;
// ALTER TABLE `user` MODIFY COLUMN `updated_at` datetime DEFAULT CURRENT_TIMESTAMP() ON UPDATE CURRENT_TIMESTAMP();
// CREATE UNIQUE INDEX `idx_name_age` ON `user`(`name`, `age`);

println(sql1.StringDown())
// DROP INDEX `idx_name_age` ON `user`;
}
```
43 changes: 39 additions & 4 deletions element/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (
"sort"
"strings"

ptypes "github.com/auxten/postgresql-parser/pkg/sql/types"
"github.com/pingcap/parser/ast"
"github.com/pingcap/parser/types"
"github.com/sunary/sqlize/utils"
)

Expand Down Expand Up @@ -252,16 +254,49 @@ func (t Table) getIndexForeignKey(fkName string) int {
return -1
}

func hasChangedMysqlOptions(new, old []*ast.ColumnOption) bool {
if len(new) != len(old) {
return true
}

mNew := map[ast.ColumnOptionType]int{}
for i := range old {
mNew[old[i].Tp] += 1
}

mOld := map[ast.ColumnOptionType]int{}
for i := range old {
mOld[old[i].Tp] += 1
}

for k, v := range mOld {
if mNew[k] != v {
return true
}
}

return false
}

func hasChangedMysqlType(new, old *types.FieldType) bool {
return new != nil && new.String() != old.String()
}

func hasChangePostgresType(new, old *ptypes.T) bool {
return new != nil && new.SQLString() != old.SQLString()
}

// Diff differ between 2 migrations
func (t *Table) Diff(old Table) {
for i := range t.Columns {
if j := old.getIndexColumn(t.Columns[i].Name); t.Columns[i].Action == MigrateAddAction &&
j >= 0 && old.Columns[j].Action != MigrateNoAction {
if (t.Columns[i].MysqlType != nil && t.Columns[i].MysqlType.String() == old.Columns[j].MysqlType.String()) ||
(t.Columns[i].PgType != nil && t.Columns[i].PgType.SQLString() == old.Columns[j].PgType.SQLString()) {
t.Columns[i].Action = MigrateNoAction
} else {
if hasChangedMysqlOptions(t.Columns[i].Options, old.Columns[j].Options) ||
hasChangedMysqlType(t.Columns[i].MysqlType, old.Columns[j].MysqlType) ||
hasChangePostgresType(t.Columns[i].PgType, old.Columns[j].PgType) {
t.Columns[i].Action = MigrateModifyAction
} else {
t.Columns[i].Action = MigrateNoAction
}
}
}
Expand Down
Loading