Skip to content

Commit

Permalink
ignore field order (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunary authored Oct 7, 2024
1 parent a6deebe commit 7535e5c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 42 deletions.
7 changes: 5 additions & 2 deletions element/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
)

var (
sql *sql_templates.Sql
sql *sql_templates.Sql
ignoreFieldOrder bool
)

// Migration ...
Expand All @@ -22,8 +23,10 @@ type Migration struct {
}

// NewMigration ...
func NewMigration(dialect sql_templates.SqlDialect, lowercase bool) Migration {
func NewMigration(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) Migration {
sql = sql_templates.NewSql(dialect, lowercase)
ignoreFieldOrder = ignoreOrder

return Migration{
Tables: []Table{},
tableIndexes: map[string]int{},
Expand Down
12 changes: 6 additions & 6 deletions element/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,10 @@ func (t Table) MigrationColumnUp() ([]string, map[string]struct{}) {
dropCols[t.Columns[i].Name] = struct{}{}
}

if after != "" {
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, after, -1)...)
} else {
if ignoreFieldOrder || after == "" {
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, "", -1)...)
} else {
strSqls = append(strSqls, t.Columns[i].migrationUp(t.Name, after, -1)...)
}
}
}
Expand Down Expand Up @@ -516,10 +516,10 @@ func (t Table) MigrationColumnDown() ([]string, map[string]struct{}) {
dropCols[t.Columns[i].Name] = struct{}{}
}

if after != "" {
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, after)...)
} else {
if ignoreFieldOrder || after == "" {
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, "")...)
} else {
strSqls = append(strSqls, t.Columns[i].migrationDown(t.Name, after)...)
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ type sqlizeOptions struct {
migrationDownSuffix string
migrationTable string

sqlTag string
dialect sql_templates.SqlDialect
lowercase bool
pluralTableName bool
generateComment bool
sqlTag string
dialect sql_templates.SqlDialect
lowercase bool
pluralTableName bool
generateComment bool
ignoreFieldOrder bool
}

type funcSqlizeOption struct {
Expand Down Expand Up @@ -120,3 +121,10 @@ func WithCommentGenerate() SqlizeOption {
o.generateComment = true
})
}

// WithIgnoreFieldOrder ...
func WithIgnoreFieldOrder() SqlizeOption {
return newFuncSqlizeOption(func(o *sqlizeOptions) {
o.ignoreFieldOrder = true
})
}
61 changes: 43 additions & 18 deletions sql-builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,51 @@ import (
"github.com/sunary/sqlize/utils"
)

// Tag prefixes
const (
// SqlTagDefault ...
SqlTagDefault = "sql"
tagIsSquash = "squash"
tagIsEmbedded = "embedded"
prefixColumn = "column:" // set column name, eg: 'column:column_name'
prefixEmbedded = "embedded_prefix:" // set embed prefix for flatten struct, eg: 'embedded_prefix:base_'
prefixPreviousName = ",previous:" // mark previous name-field, eg: 'column:column_name,previous:old_name'
prefixType = "type:" // set field type, eg: 'type:VARCHAR(64)'
prefixDefault = "default:" // set default value, eg: 'default:0'
tagEnum = "enum" // type:ENUM('open','close')
prefixComment = "comment:" // comment field, eg: 'comment:sth you want to comment'
)

// Special tags
const (
tagIsSquash = "squash"
tagIsEmbedded = "embedded"
tagEnum = "enum" // type:ENUM('open','close')
)

// Null and key constraints
const (
tagIsNull = "null"
tagIsNotNull = "not_null"
tagIsAutoIncrement = "auto_increment"
tagIsPrimaryKey = "primary_key" // this field is primary key, eg: 'primary_key'
tagIsIndex = "index" // indexing this field, eg: 'index' (=> idx_column_name)
tagIsUniqueIndex = "unique" // unique indexing this field, eg: 'unique' (=> idx_column_name)
)

// Index related
const (
tagIsIndex = "index" // indexing this field, eg: 'index' (=> idx_column_name)
tagIsUniqueIndex = "unique" // unique indexing this field, eg: 'unique' (=> idx_column_name)
prefixIndex = "index:" // indexing with name, eg: 'index:idx_name'
prefixUniqueIndex = "unique:" // unique indexing with name, eg: 'unique:idx_name'
prefixIndexColumns = "index_columns:" // indexing these fields, eg: 'index_columns:col1,col2' (=> idx_col1_col2)
prefixIndexType = "index_type:" // indexing with type, eg: 'index_type:btree' (default) or 'index_type:hash'
)

// Foreign key related
const (
prefixForeignKey = "foreign_key:" // 'foreign_key:'
prefixFkReferences = "references:" // 'references:'
prefixFkConstraint = "constraint:" // 'constraint:'
)

// Function names
const (
funcTableName = "TableName"
)

Expand Down Expand Up @@ -140,21 +156,30 @@ func (s SqlBuilder) AddTable(obj interface{}) string {
}

type attrs struct {
Name string
Prefix string
Type string
Value string
IsPk bool
ForeignKey *fkAttrs
IsUnique bool
// Basic attributes
Name string
Prefix string
Type string
Value string
Comment string

// Key and constraint attributes
IsPk bool
IsUnique bool
IsNull bool
IsNotNull bool
IsAutoIncr bool

// Foreign key
ForeignKey *fkAttrs

// Index attributes
Index string
IndexType string
IndexColumns string
IsNull bool
IsNotNull bool
IsAutoIncr bool
Comment string
IsEmbedded bool

// Special attribute
IsEmbedded bool
}

type fkAttrs struct {
Expand Down
1 change: 0 additions & 1 deletion sql-parser/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ func (p *Parser) ParserMysql(sql string) error {
switch n.(type) {
case ast.DDLNode:
n.Accept(p)
break
}
}

Expand Down
9 changes: 5 additions & 4 deletions sql-parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (

// Parser ...
type Parser struct {
dialect sql_templates.SqlDialect
Migration element.Migration
dialect sql_templates.SqlDialect
Migration element.Migration
ignoreOrder bool
}

// NewParser ...
func NewParser(dialect sql_templates.SqlDialect, lowercase bool) *Parser {
func NewParser(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) *Parser {
return &Parser{
dialect: dialect,
Migration: element.NewMigration(dialect, lowercase),
Migration: element.NewMigration(dialect, lowercase, ignoreOrder),
}
}

Expand Down
13 changes: 7 additions & 6 deletions sqlize.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,12 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize {
migrationDownSuffix: utils.DefaultMigrationDownSuffix,
migrationTable: utils.DefaultMigrationTable,

dialect: sql_templates.MysqlDialect,
lowercase: false,
sqlTag: sql_builder.SqlTagDefault,
pluralTableName: false,
generateComment: false,
dialect: sql_templates.MysqlDialect,
lowercase: false,
sqlTag: sql_builder.SqlTagDefault,
pluralTableName: false,
generateComment: false,
ignoreFieldOrder: false,
}
for i := range opts {
opts[i].apply(&o)
Expand Down Expand Up @@ -75,7 +76,7 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize {
lowercase: o.lowercase,
pluralTableName: o.pluralTableName,
sqlBuilder: sb,
parser: sql_parser.NewParser(o.dialect, o.lowercase),
parser: sql_parser.NewParser(o.dialect, o.lowercase, o.ignoreFieldOrder),
}
}

Expand Down
1 change: 1 addition & 0 deletions sqlize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,7 @@ func TestSqlize_MigrationVersion(t *testing.T) {
WithMigrationFolder(""),
WithMigrationTable(utils.DefaultMigrationTable),
WithPostgresql(),
WithIgnoreFieldOrder(),
}
s := NewSqlize(opts...)

Expand Down

0 comments on commit 7535e5c

Please sign in to comment.