diff --git a/.travis.yml b/.travis.yml index c679f99..1fd4ac7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,7 @@ language: go go: - - 1.13.x - - 1.14.x + - 1.25.x env: GO111MODULE=on diff --git a/element/column.go b/element/column.go index 15f5e21..59e9e1a 100644 --- a/element/column.go +++ b/element/column.go @@ -13,7 +13,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/format" "github.com/pingcap/tidb/pkg/parser/types" sqlite "github.com/rqlite/sql" - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqltemplates" ) const ( @@ -146,7 +146,7 @@ func (c Column) migrationUp(tbName, after string, ident int) []string { } func (c Column) migrationCommentUp(tbName string) []string { - if c.CurrentAttr.Comment == "" || sql.GetDialect() != sql_templates.PostgresDialect { + if c.CurrentAttr.Comment == "" || sql.GetDialect() != sqltemplates.PostgresDialect { return nil } diff --git a/element/migration.go b/element/migration.go index ff7a0a9..94331d8 100644 --- a/element/migration.go +++ b/element/migration.go @@ -6,12 +6,12 @@ import ( "strings" "github.com/pingcap/tidb/pkg/parser/ast" - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqltemplates" "github.com/sunary/sqlize/utils" ) var ( - sql *sql_templates.Sql + sql *sqltemplates.Sql ignoreFieldOrder bool ) @@ -23,8 +23,8 @@ type Migration struct { } // NewMigration ... -func NewMigration(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) Migration { - sql = sql_templates.NewSql(dialect, lowercase) +func NewMigration(dialect sqltemplates.SqlDialect, lowercase, ignoreOrder bool) Migration { + sql = sqltemplates.NewSql(dialect, lowercase) ignoreFieldOrder = ignoreOrder return Migration{ diff --git a/export/avro/builder.go b/export/avro/builder.go index fce75fb..105220b 100644 --- a/export/avro/builder.go +++ b/export/avro/builder.go @@ -9,8 +9,8 @@ import ( "github.com/sunary/sqlize/element" ) -// NewArvoSchema ... -func NewArvoSchema(table element.Table) *RecordSchema { +// NewAvroSchema ... +func NewAvroSchema(table element.Table) *RecordSchema { fields := buildFieldsFromTable(table) record := newRecordSchema(table.Name, table.Name) record.Name = table.Name diff --git a/options.go b/options.go index 5394d42..7577f8c 100644 --- a/options.go +++ b/options.go @@ -1,8 +1,6 @@ package sqlize -import ( - sql_templates "github.com/sunary/sqlize/sql-templates" -) +import "github.com/sunary/sqlize/sqltemplates" type sqlizeOptions struct { migrationFolder string @@ -11,7 +9,7 @@ type sqlizeOptions struct { migrationTable string sqlTag string - dialect sql_templates.SqlDialect + dialect sqltemplates.SqlDialect lowercase bool pluralTableName bool generateComment bool @@ -69,28 +67,28 @@ func WithSqlTag(sqlTag string) SqlizeOption { // WithMysql default func WithMysql() SqlizeOption { return newFuncSqlizeOption(func(o *sqlizeOptions) { - o.dialect = sql_templates.MysqlDialect + o.dialect = sqltemplates.MysqlDialect }) } // WithPostgresql ... func WithPostgresql() SqlizeOption { return newFuncSqlizeOption(func(o *sqlizeOptions) { - o.dialect = sql_templates.PostgresDialect + o.dialect = sqltemplates.PostgresDialect }) } // WithSqlserver ... func WithSqlserver() SqlizeOption { return newFuncSqlizeOption(func(o *sqlizeOptions) { - o.dialect = sql_templates.SqlserverDialect + o.dialect = sqltemplates.SqlserverDialect }) } // WithSqlite ... func WithSqlite() SqlizeOption { return newFuncSqlizeOption(func(o *sqlizeOptions) { - o.dialect = sql_templates.SqliteDialect + o.dialect = sqltemplates.SqliteDialect }) } diff --git a/sql-builder/builder.go b/sqlbuilder/builder.go similarity index 97% rename from sql-builder/builder.go rename to sqlbuilder/builder.go index b9811c7..800f316 100644 --- a/sql-builder/builder.go +++ b/sqlbuilder/builder.go @@ -1,4 +1,4 @@ -package sql_builder +package sqlbuilder import ( "database/sql" @@ -9,7 +9,7 @@ import ( "strings" "time" - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqltemplates" "github.com/sunary/sqlize/utils" ) @@ -78,9 +78,9 @@ var ( // SqlBuilder ... type SqlBuilder struct { - sql *sql_templates.Sql + sql *sqltemplates.Sql sqlTag string - dialect sql_templates.SqlDialect + dialect sqltemplates.SqlDialect generateComment bool pluralTableName bool tables map[string]string @@ -89,7 +89,7 @@ type SqlBuilder struct { // NewSqlBuilder ... func NewSqlBuilder(opts ...SqlBuilderOption) *SqlBuilder { o := sqlBuilderOptions{ - dialect: sql_templates.MysqlDialect, + dialect: sqltemplates.MysqlDialect, lowercase: false, pluralTableName: false, sqlTag: SqlTagDefault, @@ -99,7 +99,7 @@ func NewSqlBuilder(opts ...SqlBuilderOption) *SqlBuilder { } return &SqlBuilder{ - sql: sql_templates.NewSql(o.dialect, o.lowercase), + sql: sqltemplates.NewSql(o.dialect, o.lowercase), dialect: o.dialect, sqlTag: o.sqlTag, pluralTableName: o.pluralTableName, @@ -130,7 +130,7 @@ func (s SqlBuilder) AddTable(obj interface{}) string { comments := []string{} if s.generateComment { switch s.dialect { - case sql_templates.PostgresDialect: + case sqltemplates.PostgresDialect: comments = append(comments, fmt.Sprintf(s.sql.TableComment(), s.sql.EscapeSqlName(tableName), tableName)) default: @@ -432,7 +432,7 @@ func (s SqlBuilder) parseStruct(tableName, prefix string, obj interface{}) ([]st if at.Comment != "" { switch s.dialect { - case sql_templates.PostgresDialect: + case sqltemplates.PostgresDialect: comments = append(comments, fmt.Sprintf(s.sql.ColumnComment(), s.sql.EscapeSqlName(tableName), s.sql.EscapeSqlName(at.Name), at.Comment)) diff --git a/sql-builder/options.go b/sqlbuilder/options.go similarity index 84% rename from sql-builder/options.go rename to sqlbuilder/options.go index 58605b9..0ff8efa 100644 --- a/sql-builder/options.go +++ b/sqlbuilder/options.go @@ -1,12 +1,12 @@ -package sql_builder +package sqlbuilder import ( - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqltemplates" ) type sqlBuilderOptions struct { sqlTag string - dialect sql_templates.SqlDialect + dialect sqltemplates.SqlDialect lowercase bool generateComment bool pluralTableName bool @@ -41,33 +41,33 @@ func WithSqlTag(sqlTag string) SqlBuilderOption { // WithMysql default func WithMysql() SqlBuilderOption { return newFuncSqlBuilderOption(func(o *sqlBuilderOptions) { - o.dialect = sql_templates.MysqlDialect + o.dialect = sqltemplates.MysqlDialect }) } // WithPostgresql ... func WithPostgresql() SqlBuilderOption { return newFuncSqlBuilderOption(func(o *sqlBuilderOptions) { - o.dialect = sql_templates.PostgresDialect + o.dialect = sqltemplates.PostgresDialect }) } // WithSqlserver ... func WithSqlserver() SqlBuilderOption { return newFuncSqlBuilderOption(func(o *sqlBuilderOptions) { - o.dialect = sql_templates.SqlserverDialect + o.dialect = sqltemplates.SqlserverDialect }) } // WithSqlite ... func WithSqlite() SqlBuilderOption { return newFuncSqlBuilderOption(func(o *sqlBuilderOptions) { - o.dialect = sql_templates.SqliteDialect + o.dialect = sqltemplates.SqliteDialect }) } // WithDialect ... -func WithDialect(dialect sql_templates.SqlDialect) SqlBuilderOption { +func WithDialect(dialect sqltemplates.SqlDialect) SqlBuilderOption { return newFuncSqlBuilderOption(func(o *sqlBuilderOptions) { o.dialect = dialect }) diff --git a/sqlize.go b/sqlize.go index c30d5f9..25fb7c1 100644 --- a/sqlize.go +++ b/sqlize.go @@ -10,9 +10,9 @@ import ( "github.com/sunary/sqlize/element" "github.com/sunary/sqlize/export/avro" "github.com/sunary/sqlize/export/mermaidjs" - sql_builder "github.com/sunary/sqlize/sql-builder" - sql_parser "github.com/sunary/sqlize/sql-parser" - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqlbuilder" + "github.com/sunary/sqlize/sqlparser" + "github.com/sunary/sqlize/sqltemplates" "github.com/sunary/sqlize/utils" ) @@ -27,11 +27,11 @@ type Sqlize struct { migrationUpSuffix string migrationDownSuffix string migrationTable string - dialect sql_templates.SqlDialect + dialect sqltemplates.SqlDialect lowercase bool pluralTableName bool - sqlBuilder *sql_builder.SqlBuilder - parser *sql_parser.Parser + sqlBuilder *sqlbuilder.SqlBuilder + parser *sqlparser.Parser } // NewSqlize ... @@ -42,9 +42,9 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize { migrationDownSuffix: utils.DefaultMigrationDownSuffix, migrationTable: utils.DefaultMigrationTable, - dialect: sql_templates.MysqlDialect, + dialect: sqltemplates.MysqlDialect, lowercase: false, - sqlTag: sql_builder.SqlTagDefault, + sqlTag: sqlbuilder.SqlTagDefault, pluralTableName: false, generateComment: false, ignoreFieldOrder: false, @@ -53,21 +53,21 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize { opts[i].apply(&o) } - opt := []sql_builder.SqlBuilderOption{sql_builder.WithSqlTag(o.sqlTag), sql_builder.WithDialect(o.dialect)} + opt := []sqlbuilder.SqlBuilderOption{sqlbuilder.WithSqlTag(o.sqlTag), sqlbuilder.WithDialect(o.dialect)} if o.lowercase { - opt = append(opt, sql_builder.WithSqlLowercase()) + opt = append(opt, sqlbuilder.WithSqlLowercase()) } if o.generateComment { - opt = append(opt, sql_builder.WithCommentGenerate()) + opt = append(opt, sqlbuilder.WithCommentGenerate()) } if o.pluralTableName { - opt = append(opt, sql_builder.WithPluralTableName()) + opt = append(opt, sqlbuilder.WithPluralTableName()) } - sb := sql_builder.NewSqlBuilder(opt...) + sb := sqlbuilder.NewSqlBuilder(opt...) return &Sqlize{ migrationFolder: o.migrationFolder, @@ -78,7 +78,7 @@ func NewSqlize(opts ...SqlizeOption) *Sqlize { lowercase: o.lowercase, pluralTableName: o.pluralTableName, sqlBuilder: sb, - parser: sql_parser.NewParser(o.dialect, o.lowercase, o.ignoreFieldOrder), + parser: sqlparser.NewParser(o.dialect, o.lowercase, o.ignoreFieldOrder), } } @@ -210,7 +210,7 @@ func (s Sqlize) WriteFilesWithVersion(name string, ver int64, dirty bool) error } func (s Sqlize) migrationUpVersion(ver int64, dirty bool) string { - tmp := sql_templates.NewSql(s.dialect, s.lowercase) + tmp := sqltemplates.NewSql(s.dialect, s.lowercase) if ver == 0 { return fmt.Sprintf(tmp.CreateTableMigration(), s.migrationTable) } @@ -219,7 +219,7 @@ func (s Sqlize) migrationUpVersion(ver int64, dirty bool) string { } func (s Sqlize) migrationDownVersion(ver int64) string { - tmp := sql_templates.NewSql(s.dialect, s.lowercase) + tmp := sqltemplates.NewSql(s.dialect, s.lowercase) if ver == 0 { return fmt.Sprintf(tmp.DropTableMigration(), s.migrationTable) } @@ -251,16 +251,16 @@ func (s Sqlize) MermaidJsLive(needTables ...string) string { return mm.Live() } -// ArvoSchema export arvo schema, support mysql only -func (s Sqlize) ArvoSchema(needTables ...string) []string { - if s.dialect != sql_templates.MysqlDialect { +// AvroSchema export avro schema, support mysql only +func (s Sqlize) AvroSchema(needTables ...string) []string { + if s.dialect != sqltemplates.MysqlDialect { return nil } tables := s.selectTable(needTables...) schemas := make([]string, 0, len(tables)) for i := range tables { - record := avro.NewArvoSchema(tables[i]) + record := avro.NewAvroSchema(tables[i]) jsonData, _ := json.Marshal(record) schemas = append(schemas, string(jsonData)) } diff --git a/sqlize_test.go b/sqlize_test.go index bb3dc4f..5e36f2f 100644 --- a/sqlize_test.go +++ b/sqlize_test.go @@ -282,11 +282,11 @@ DROP TABLE IF EXISTS orders_sqlite;` expectPersonCityMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBQRVJTT04gewogIGludCgxMSkgaWQgUEsgCiAgdmFyY2hhcig2NCkgbmFtZSAgCiAgaW50KDExKSBhZ2UgIAogIHRpbnlpbnQoMSkgaXNfZmVtYWxlICAKICBkYXRldGltZSBjcmVhdGVkX2F0ICAKIH0KIENJVFkgewogIGludCgxMSkgaWQgUEsgCiAgdGV4dCBuYW1lICAKICBlbnVtIHJlZ2lvbiAgCiB9Cg==` expectOrderUserMermaidJsLive = `https://mermaid.ink/img/ZXJEaWFncmFtCiBPUkRFUlMgewogIHZhcmNoYXIoMjU1KSBjbGllbnRfaWQgIAogIHZhcmNoYXIoMjU1KSBjb3VudHJ5ICAKICB2YXJjaGFyKDI1NSkgZW1haWwgRksgCiAgZGF0ZXRpbWUgY3JlYXRlZF9hdCAgCiAgZGF0ZXRpbWUgdXBkYXRlZF9hdCAgCiB9CiBVU0VSIHsKICB0ZXh0IGVtYWlsICAKIH0KIE9SREVSUyB9by0tfHwgVVNFUjogZW1haWw=` - expectPersonArvo = ` + expectPersonAvro = ` {"type":"record","name":"person","namespace":"person","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"age","type":"int"},{"name":"is_female","type":"bool"},{"name":"created_at","type":["null",{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}]}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"person"}` - expectHotelArvo = ` + expectHotelAvro = ` {"type":"record","name":"hotel","namespace":"hotel","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"grand_opening","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"created_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"updated_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"base_created_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}},{"name":"base_updated_at","type":{"connect.default":"1970-01-01T00:00:00Z","connect.name":"io.debezium.time.ZonedTimestamp","connect.version":1,"type":"string"}}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"hotel"}` - expectCityArvo = ` + expectCityAvro = ` {"type":"record","name":"city","namespace":"city","fields":[{"name":"before","type":["null",{"type":"record","name":"Value","namespace":"","fields":[{"name":"id","type":"int"},{"name":"name","type":"string"},{"name":"region","type":["null",{"connect.default":"init","connect.name":"io.debezium.data.Enum","connect.parameters":{"allowed":"northern,southern"},"connect.version":1,"type":"string"}]}],"connect.name":""}]},{"name":"after","type":["null","Value"]},{"name":"op","type":"string"},{"name":"ts_ms","type":["null","long"]},{"name":"transaction","type":["null",{"type":"record","name":"ConnectDefault","namespace":"io.confluent.connect.avro","fields":[{"name":"id","type":"string"},{"name":"total_order","type":"long"},{"name":"data_collection_order","type":"long"}],"connect.name":""}]}],"connect.name":"city"}` expectCreateMigrationTableUp = `CREATE TABLE IF NOT EXISTS schema_migrations ( @@ -944,7 +944,7 @@ func TestSqlize_Mermaidjs(t *testing.T) { } } -func TestSqlize_ArvoSchema(t *testing.T) { +func TestSqlize_AvroSchema(t *testing.T) { now := time.Now() type args struct { @@ -952,44 +952,44 @@ func TestSqlize_ArvoSchema(t *testing.T) { needTables []string } - arvoSchemaMysqlTestcases := []struct { + avroSchemaMysqlTestcases := []struct { name string args args want []string }{ { - name: "person arvo", + name: "person avro", args: args{ []interface{}{person{}}, []string{"person"}, }, - want: []string{expectPersonArvo}, + want: []string{expectPersonAvro}, }, { - name: "hotel arvo", + name: "hotel avro", args: args{ []interface{}{hotel{GrandOpening: &now}}, []string{"hotel"}, }, - want: []string{expectHotelArvo}, + want: []string{expectHotelAvro}, }, { - name: "city arvo", + name: "city avro", args: args{ []interface{}{city{}}, []string{"city"}, }, - want: []string{expectCityArvo}, + want: []string{expectCityAvro}, }, } - for _, tt := range arvoSchemaMysqlTestcases { + for _, tt := range avroSchemaMysqlTestcases { t.Run(tt.name, func(t *testing.T) { opts := []SqlizeOption{} s := NewSqlize(opts...) s.FromObjects(tt.args.models...) - if got := s.ArvoSchema(tt.args.needTables...); (got != nil || tt.want != nil) && !areEqualJSON(got[0], tt.want[0]) { - t.Errorf("ArvoSchema() mysql got = \n%v,\nexpected = \n%v", got, tt.want) + if got := s.AvroSchema(tt.args.needTables...); (got != nil || tt.want != nil) && !areEqualJSON(got[0], tt.want[0]) { + t.Errorf("AvroSchema() mysql got = \n%v,\nexpected = \n%v", got, tt.want) } }) } diff --git a/sql-parser/mysql.go b/sqlparser/mysql.go similarity index 99% rename from sql-parser/mysql.go rename to sqlparser/mysql.go index 2330152..bd8b4aa 100644 --- a/sql-parser/mysql.go +++ b/sqlparser/mysql.go @@ -1,4 +1,4 @@ -package sql_parser +package sqlparser import ( "github.com/pingcap/tidb/pkg/parser" diff --git a/sql-parser/parser.go b/sqlparser/parser.go similarity index 77% rename from sql-parser/parser.go rename to sqlparser/parser.go index 6d4120a..aa44fa6 100644 --- a/sql-parser/parser.go +++ b/sqlparser/parser.go @@ -1,19 +1,19 @@ -package sql_parser +package sqlparser import ( "github.com/sunary/sqlize/element" - sql_templates "github.com/sunary/sqlize/sql-templates" + "github.com/sunary/sqlize/sqltemplates" ) // Parser ... type Parser struct { - dialect sql_templates.SqlDialect + dialect sqltemplates.SqlDialect Migration element.Migration ignoreOrder bool } // NewParser ... -func NewParser(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) *Parser { +func NewParser(dialect sqltemplates.SqlDialect, lowercase, ignoreOrder bool) *Parser { return &Parser{ dialect: dialect, Migration: element.NewMigration(dialect, lowercase, ignoreOrder), @@ -23,10 +23,10 @@ func NewParser(dialect sql_templates.SqlDialect, lowercase, ignoreOrder bool) *P // Parser ... func (p *Parser) Parser(sql string) error { switch p.dialect { - case sql_templates.PostgresDialect: + case sqltemplates.PostgresDialect: return p.ParserPostgresql(sql) - case sql_templates.SqliteDialect: + case sqltemplates.SqliteDialect: return p.ParserSqlite(sql) default: diff --git a/sql-parser/postgresql.go b/sqlparser/postgresql.go similarity index 99% rename from sql-parser/postgresql.go rename to sqlparser/postgresql.go index 759d72b..73e2aca 100644 --- a/sql-parser/postgresql.go +++ b/sqlparser/postgresql.go @@ -1,4 +1,4 @@ -package sql_parser +package sqlparser import ( "strings" diff --git a/sql-parser/sqlite.go b/sqlparser/sqlite.go similarity index 99% rename from sql-parser/sqlite.go rename to sqlparser/sqlite.go index 95fc5ac..32820c5 100644 --- a/sql-parser/sqlite.go +++ b/sqlparser/sqlite.go @@ -1,4 +1,4 @@ -package sql_parser +package sqlparser import ( "strings" diff --git a/sql-templates/ddl.go b/sqltemplates/ddl.go similarity index 99% rename from sql-templates/ddl.go rename to sqltemplates/ddl.go index 087a7bb..5df523e 100644 --- a/sql-templates/ddl.go +++ b/sqltemplates/ddl.go @@ -1,4 +1,4 @@ -package sql_templates +package sqltemplates import ( "strings" diff --git a/sql-templates/option.go b/sqltemplates/option.go similarity index 98% rename from sql-templates/option.go rename to sqltemplates/option.go index 25d909b..42c2ee8 100644 --- a/sql-templates/option.go +++ b/sqltemplates/option.go @@ -1,4 +1,4 @@ -package sql_templates +package sqltemplates import ( "fmt" diff --git a/sql-templates/type.go b/sqltemplates/type.go similarity index 98% rename from sql-templates/type.go rename to sqltemplates/type.go index c2bd8f5..c766d9d 100644 --- a/sql-templates/type.go +++ b/sqltemplates/type.go @@ -1,4 +1,4 @@ -package sql_templates +package sqltemplates // BooleanType ... func (s Sql) BooleanType() string {