fix: backtick only works with mysql

This commit is contained in:
Reinaldy Rafli 2021-06-30 22:57:32 +07:00
parent 03e6bdb7fc
commit 8d6de642b2
2 changed files with 8 additions and 8 deletions

View File

@ -78,15 +78,15 @@ func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {
sql.WriteString("CREATE TABLE ") sql.WriteString("CREATE TABLE ")
if d.Schema != "" { if d.Schema != "" {
sql.WriteString("`" + d.Schema + "`.") sql.WriteString("\"" + d.Schema + "\".")
} }
sql.WriteString("`" + d.TableName + "`") sql.WriteString("\"" + d.TableName + "\"")
sql.WriteString(" ") sql.WriteString(" ")
var columnTypes []string var columnTypes []string
for i := 0; i < len(d.Columns); i++ { for i := 0; i < len(d.Columns); i++ {
columnTypes = append(columnTypes, "`"+d.Columns[i]+"` "+d.Types[i]) columnTypes = append(columnTypes, "\""+d.Columns[i]+"\" "+d.Types[i])
} }
sql.WriteString("(") sql.WriteString("(")
@ -99,7 +99,7 @@ func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {
return return
} }
sql.WriteString(" ") sql.WriteString(" ")
sql.WriteString("ALTER TABLE `" + d.TableName + "` ADD PRIMARY KEY (`" + d.Primary + "`);") sql.WriteString("ALTER TABLE \"" + d.TableName + "\" ADD PRIMARY KEY (\"" + d.Primary + "\");")
} }
if d.Unique != "" { if d.Unique != "" {
@ -108,7 +108,7 @@ func (d *createData) ToSQL() (sqlStr string, args []interface{}, err error) {
return return
} }
sql.WriteString(" ") sql.WriteString(" ")
sql.WriteString("ALTER TABLE `" + d.TableName + "` ADD UNIQUE (`" + d.Unique + "`);") sql.WriteString("ALTER TABLE \"" + d.TableName + "\" ADD UNIQUE (\"" + d.Unique + "\");")
} }
sqlStr = sql.String() sqlStr = sql.String()
return return

View File

@ -12,7 +12,7 @@ func TestCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE `users` (`name` varchar(255), `password` text, `date` date);" result := "CREATE TABLE \"users\" (\"name\" varchar(255), \"password\" text, \"date\" date);"
if sql != result { if sql != result {
t.Fatal("sql is not equal to result:", sql) t.Fatal("sql is not equal to result:", sql)
} }
@ -28,7 +28,7 @@ func TestCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE `users` (`id` uuid, `name` varchar(255), `email` varchar(255), `password` text, `date` date); ALTER TABLE `users` ADD PRIMARY KEY (`id`); ALTER TABLE `users` ADD UNIQUE (`email`);" result := "CREATE TABLE \"users\" (\"id\" uuid, \"name\" varchar(255), \"email\" varchar(255), \"password\" text, \"date\" date); ALTER TABLE \"users\" ADD PRIMARY KEY (\"id\"); ALTER TABLE \"users\" ADD UNIQUE (\"email\");"
if sql != result { if sql != result {
t.Fatal("sql is not equal to result:", sql) t.Fatal("sql is not equal to result:", sql)
} }
@ -39,7 +39,7 @@ func TestCreate(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err.Error()) t.Fatal(err.Error())
} }
result := "CREATE TABLE `private`.`users` (`name` varchar(255), `password` text, `date` date);" result := "CREATE TABLE \"private\".\"users\" (\"name\" varchar(255), \"password\" text, \"date\" date);"
if sql != result { if sql != result {
t.Fatal("sql is not equal to result:", sql) t.Fatal("sql is not equal to result:", sql)
} }