Skip to content

Commit 5d78e5b

Browse files
committed
ptr lib
migration fixes log print bug
1 parent 97dc442 commit 5d78e5b

File tree

3 files changed

+95
-10
lines changed

3 files changed

+95
-10
lines changed

lib/db/schema/ddl/ddl.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ func FromStatement(stmt *gorm.Statement) Table {
151151

152152
// fix gorm problem with primaryKey
153153
if (column.Type == "bigint(20)" || column.Type == "bigint" || column.Type == "int") && field.FieldType.Kind() == reflect.String {
154+
fmt.Println("===>", column.Name, field.FieldType.Kind())
154155
column.Type = "varchar"
155156
}
156157
if column.Type == "varchar" {
@@ -203,7 +204,7 @@ func FromStatement(stmt *gorm.Statement) Table {
203204
}
204205
}
205206

206-
if column.Type == "TIMESTAMP" && column.Default == "" && !column.Nullable {
207+
if (column.Type == "TIMESTAMP" || column.Type == "timestamp") && column.Default == "" && !column.Nullable {
207208
column.Default = "0000-00-00 00:00:00"
208209
}
209210

@@ -410,22 +411,22 @@ func (local Table) GetDiff(remote table.Table) []string {
410411
}
411412

412413
if fieldType(strings.ToLower(field.Type)) != fieldType(strings.ToLower(r.ColumnType)) {
413-
queries = append(queries, fmt.Sprintf("-- type does not match. new:%s old:%s", fieldType(field.Type), strings.ToLower(r.ColumnType)))
414+
queries = append(queries, fmt.Sprintf("-- column %s type does not match. new:%s old:%s", field.Name, fieldType(field.Type), strings.ToLower(r.ColumnType)))
414415
diff = true
415416
}
416417
if len(field.Collate) > 0 && strings.ToLower(field.Collate) != strings.ToLower(r.Collation) {
417-
queries = append(queries, fmt.Sprintf("-- collation does not match. new:%s old:%s", field.Collate, r.Collation))
418+
queries = append(queries, fmt.Sprintf("-- column %s collation does not match. new:%s old:%s", field.Name, field.Collate, r.Collation))
418419
diff = true
419420
}
420421
if len(field.Charset) > 0 && strings.ToLower(field.Charset) != strings.ToLower(r.CharacterSet) {
421-
queries = append(queries, fmt.Sprintf("-- charset does not match. new:%s old:%s", field.Charset, r.CharacterSet))
422+
queries = append(queries, fmt.Sprintf("-- column %s charset does not match. new:%s old:%s", field.Name, field.Charset, r.CharacterSet))
422423
diff = true
423424
}
424425
if field.Comment != r.Comment {
425-
queries = append(queries, fmt.Sprintf("-- comment does not match. new:%s old:%s", field.Comment, r.Comment))
426+
queries = append(queries, fmt.Sprintf("-- column %s comment does not match. new:%s old:%s", field.Name, field.Comment, r.Comment))
426427
diff = true
427428
}
428-
if field.Default != fieldType(getString(r.ColumnDefault)) {
429+
if field.Default != getString(r.ColumnDefault) {
429430
var skip = false
430431
for _, row := range InternalFunctions {
431432
if slices.Contains(row, field.Default) && slices.Contains(row, getString(r.ColumnDefault)) {
@@ -437,17 +438,17 @@ func (local Table) GetDiff(remote table.Table) []string {
437438
}
438439

439440
if !skip && !(field.Default == "NULL" && r.ColumnDefault == nil) {
440-
queries = append(queries, fmt.Sprintf("-- default value does not match. new:%s old:%s", field.Default, getString(r.ColumnDefault)))
441+
queries = append(queries, fmt.Sprintf("-- field %s default value does not match. new:%s old:%s", field.Name, field.Default, getString(r.ColumnDefault)))
441442
diff = true
442443
}
443444
}
444445
if field.Nullable != (r.Nullable == "YES") {
445-
queries = append(queries, fmt.Sprintf("-- nullable does not match. new:%t old:%t", field.Nullable, r.Nullable == "YES"))
446+
queries = append(queries, fmt.Sprintf("-- column %s nullable does not match. new:%t old:%t", field.Name, field.Nullable, r.Nullable == "YES"))
446447
diff = true
447448
}
448449
var needPK = false
449450
if field.AutoIncrement && strings.ToLower(r.Extra) != "auto_increment" {
450-
afterPK = append(afterPK, fmt.Sprintf("-- auto_increment does not match. new:%t old:%t", field.AutoIncrement, !field.AutoIncrement))
451+
afterPK = append(afterPK, fmt.Sprintf("-- field %s auto_increment does not match. new:%t old:%t", field.Name, field.AutoIncrement, !field.AutoIncrement))
451452
diff = true
452453
needPK = true
453454
}

lib/log/log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,11 @@ func msg(message any, level Level, params ...any) {
113113
return
114114
}
115115
_, file, line, _ := runtime.Caller(2 + stackTraceLevel)
116+
116117
entry := Entry{
117118
Level: levels[level],
118119
Date: time.Now(),
119-
File: file[len(wd)+1:],
120+
File: file,
120121
Line: line,
121122
Message: fmt.Sprintf(fmt.Sprint(message), params...),
122123
}

lib/ptr/ptr.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package ptr
2+
3+
import "time"
4+
5+
// Int returns a pointer to the given int value.
6+
func Int(v int) *int {
7+
return &v
8+
}
9+
10+
// Int8 returns a pointer to the given int8 value.
11+
func Int8(v int8) *int8 {
12+
return &v
13+
}
14+
15+
// Int16 returns a pointer to the given int16 value.
16+
func Int16(v int16) *int16 {
17+
return &v
18+
}
19+
20+
// Int32 returns a pointer to the given int32 value.
21+
func Int32(v int32) *int32 {
22+
return &v
23+
}
24+
25+
// Int64 returns a pointer to the given int64 value.
26+
func Int64(v int64) *int64 {
27+
return &v
28+
}
29+
30+
// Uint returns a pointer to the given uint value.
31+
func Uint(v uint) *uint {
32+
return &v
33+
}
34+
35+
// Uint8 returns a pointer to the given uint8 value.
36+
func Uint8(v uint8) *uint8 {
37+
return &v
38+
}
39+
40+
// Uint16 returns a pointer to the given uint16 value.
41+
func Uint16(v uint16) *uint16 {
42+
return &v
43+
}
44+
45+
// Uint32 returns a pointer to the given uint32 value.
46+
func Uint32(v uint32) *uint32 {
47+
return &v
48+
}
49+
50+
// Uint64 returns a pointer to the given uint64 value.
51+
func Uint64(v uint64) *uint64 {
52+
return &v
53+
}
54+
55+
// Float32 returns a pointer to the given float32 value.
56+
func Float32(v float32) *float32 {
57+
return &v
58+
}
59+
60+
// Float64 returns a pointer to the given float64 value.
61+
func Float64(v float64) *float64 {
62+
return &v
63+
}
64+
65+
// String returns a pointer to the given string value.
66+
func String(v string) *string {
67+
return &v
68+
}
69+
70+
// Bool returns a pointer to the given bool value.
71+
func Bool(v bool) *bool {
72+
return &v
73+
}
74+
75+
// Time returns a pointer to the given time.Time value.
76+
func Time(v time.Time) *time.Time {
77+
return &v
78+
}
79+
80+
// Interface returns a pointer to the given interface{} value.
81+
func Interface(v interface{}) *interface{} {
82+
return &v
83+
}

0 commit comments

Comments
 (0)