Skip to content

Commit 7535e5c

Browse files
authored
ignore field order (#53)
1 parent a6deebe commit 7535e5c

File tree

8 files changed

+80
-42
lines changed

8 files changed

+80
-42
lines changed

element/migration.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import (
1111
)
1212

1313
var (
14-
sql *sql_templates.Sql
14+
sql *sql_templates.Sql
15+
ignoreFieldOrder bool
1516
)
1617

1718
// Migration ...
@@ -22,8 +23,10 @@ type Migration struct {
2223
}
2324

2425
// NewMigration ...
25-
func NewMigration(dialect sql_templates.SqlDialect, lowercase bool) Migration {
26+
func NewMigration(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) Migration {
2627
sql = sql_templates.NewSql(dialect, lowercase)
28+
ignoreFieldOrder = ignoreOrder
29+
2730
return Migration{
2831
Tables: []Table{},
2932
tableIndexes: map[string]int{},

element/table.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,10 @@ func (t Table) MigrationColumnUp() ([]string, map[string]struct{}) {
383383
dropCols[t.Columns[i].Name] = struct{}{}
384384
}
385385

386-
if after != "" {
387-
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, after, -1)...)
388-
} else {
386+
if ignoreFieldOrder || after == "" {
389387
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, "", -1)...)
388+
} else {
389+
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, after, -1)...)
390390
}
391391
}
392392
}
@@ -516,10 +516,10 @@ func (t Table) MigrationColumnDown() ([]string, map[string]struct{}) {
516516
dropCols[t.Columns[i].Name] = struct{}{}
517517
}
518518

519-
if after != "" {
520-
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, after)...)
521-
} else {
519+
if ignoreFieldOrder || after == "" {
522520
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, "")...)
521+
} else {
522+
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, after)...)
523523
}
524524
}
525525
}

options.go

+13-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ type sqlizeOptions struct {
1010
migrationDownSuffix string
1111
migrationTable string
1212

13-
sqlTag string
14-
dialect sql_templates.SqlDialect
15-
lowercase bool
16-
pluralTableName bool
17-
generateComment bool
13+
sqlTag string
14+
dialect sql_templates.SqlDialect
15+
lowercase bool
16+
pluralTableName bool
17+
generateComment bool
18+
ignoreFieldOrder bool
1819
}
1920

2021
type funcSqlizeOption struct {
@@ -120,3 +121,10 @@ func WithCommentGenerate() SqlizeOption {
120121
o.generateComment = true
121122
})
122123
}
124+
125+
// WithIgnoreFieldOrder ...
126+
func WithIgnoreFieldOrder() SqlizeOption {
127+
return newFuncSqlizeOption(func(o *sqlizeOptions) {
128+
o.ignoreFieldOrder = true
129+
})
130+
}

sql-builder/builder.go

+43-18
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,51 @@ import (
1212
"github.com/sunary/sqlize/utils"
1313
)
1414

15+
// Tag prefixes
1516
const (
16-
// SqlTagDefault ...
1717
SqlTagDefault = "sql"
18-
tagIsSquash = "squash"
19-
tagIsEmbedded = "embedded"
2018
prefixColumn = "column:" // set column name, eg: 'column:column_name'
2119
prefixEmbedded = "embedded_prefix:" // set embed prefix for flatten struct, eg: 'embedded_prefix:base_'
2220
prefixPreviousName = ",previous:" // mark previous name-field, eg: 'column:column_name,previous:old_name'
2321
prefixType = "type:" // set field type, eg: 'type:VARCHAR(64)'
2422
prefixDefault = "default:" // set default value, eg: 'default:0'
25-
tagEnum = "enum" // type:ENUM('open','close')
2623
prefixComment = "comment:" // comment field, eg: 'comment:sth you want to comment'
24+
)
25+
26+
// Special tags
27+
const (
28+
tagIsSquash = "squash"
29+
tagIsEmbedded = "embedded"
30+
tagEnum = "enum" // type:ENUM('open','close')
31+
)
2732

33+
// Null and key constraints
34+
const (
2835
tagIsNull = "null"
2936
tagIsNotNull = "not_null"
3037
tagIsAutoIncrement = "auto_increment"
3138
tagIsPrimaryKey = "primary_key" // this field is primary key, eg: 'primary_key'
32-
tagIsIndex = "index" // indexing this field, eg: 'index' (=> idx_column_name)
33-
tagIsUniqueIndex = "unique" // unique indexing this field, eg: 'unique' (=> idx_column_name)
39+
)
3440

41+
// Index related
42+
const (
43+
tagIsIndex = "index" // indexing this field, eg: 'index' (=> idx_column_name)
44+
tagIsUniqueIndex = "unique" // unique indexing this field, eg: 'unique' (=> idx_column_name)
3545
prefixIndex = "index:" // indexing with name, eg: 'index:idx_name'
3646
prefixUniqueIndex = "unique:" // unique indexing with name, eg: 'unique:idx_name'
3747
prefixIndexColumns = "index_columns:" // indexing these fields, eg: 'index_columns:col1,col2' (=> idx_col1_col2)
3848
prefixIndexType = "index_type:" // indexing with type, eg: 'index_type:btree' (default) or 'index_type:hash'
49+
)
3950

51+
// Foreign key related
52+
const (
4053
prefixForeignKey = "foreign_key:" // 'foreign_key:'
4154
prefixFkReferences = "references:" // 'references:'
4255
prefixFkConstraint = "constraint:" // 'constraint:'
56+
)
4357

58+
// Function names
59+
const (
4460
funcTableName = "TableName"
4561
)
4662

@@ -140,21 +156,30 @@ func (s SqlBuilder) AddTable(obj interface{}) string {
140156
}
141157

142158
type attrs struct {
143-
Name string
144-
Prefix string
145-
Type string
146-
Value string
147-
IsPk bool
148-
ForeignKey *fkAttrs
149-
IsUnique bool
159+
// Basic attributes
160+
Name string
161+
Prefix string
162+
Type string
163+
Value string
164+
Comment string
165+
166+
// Key and constraint attributes
167+
IsPk bool
168+
IsUnique bool
169+
IsNull bool
170+
IsNotNull bool
171+
IsAutoIncr bool
172+
173+
// Foreign key
174+
ForeignKey *fkAttrs
175+
176+
// Index attributes
150177
Index string
151178
IndexType string
152179
IndexColumns string
153-
IsNull bool
154-
IsNotNull bool
155-
IsAutoIncr bool
156-
Comment string
157-
IsEmbedded bool
180+
181+
// Special attribute
182+
IsEmbedded bool
158183
}
159184

160185
type fkAttrs struct {

sql-parser/mysql.go

-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ func (p *Parser) ParserMysql(sql string) error {
1919
switch n.(type) {
2020
case ast.DDLNode:
2121
n.Accept(p)
22-
break
2322
}
2423
}
2524

sql-parser/parser.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77

88
// Parser ...
99
type Parser struct {
10-
dialect sql_templates.SqlDialect
11-
Migration element.Migration
10+
dialect sql_templates.SqlDialect
11+
Migration element.Migration
12+
ignoreOrder bool
1213
}
1314

1415
// NewParser ...
15-
func NewParser(dialect sql_templates.SqlDialect, lowercase bool) *Parser {
16+
func NewParser(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) *Parser {
1617
return &Parser{
1718
dialect: dialect,
18-
Migration: element.NewMigration(dialect, lowercase),
19+
Migration: element.NewMigration(dialect, lowercase, ignoreOrder),
1920
}
2021
}
2122

sqlize.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize {
4040
migrationDownSuffix: utils.DefaultMigrationDownSuffix,
4141
migrationTable: utils.DefaultMigrationTable,
4242

43-
dialect: sql_templates.MysqlDialect,
44-
lowercase: false,
45-
sqlTag: sql_builder.SqlTagDefault,
46-
pluralTableName: false,
47-
generateComment: false,
43+
dialect: sql_templates.MysqlDialect,
44+
lowercase: false,
45+
sqlTag: sql_builder.SqlTagDefault,
46+
pluralTableName: false,
47+
generateComment: false,
48+
ignoreFieldOrder: false,
4849
}
4950
for i := range opts {
5051
opts[i].apply(&o)
@@ -75,7 +76,7 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize {
7576
lowercase: o.lowercase,
7677
pluralTableName: o.pluralTableName,
7778
sqlBuilder: sb,
78-
parser: sql_parser.NewParser(o.dialect, o.lowercase),
79+
parser: sql_parser.NewParser(o.dialect, o.lowercase, o.ignoreFieldOrder),
7980
}
8081
}
8182

sqlize_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,7 @@ func TestSqlize_MigrationVersion(t *testing.T) {
736736
WithMigrationFolder(""),
737737
WithMigrationTable(utils.DefaultMigrationTable),
738738
WithPostgresql(),
739+
WithIgnoreFieldOrder(),
739740
}
740741
s := NewSqlize(opts...)
741742

0 commit comments

Comments
 (0)