reduce table creation boilerplate in the patcher

prep for the tables table
This commit is contained in:
Azareal 2020-03-21 18:55:15 +10:00
parent ebe93df622
commit 5419af9c1c
2 changed files with 64 additions and 43 deletions

View File

@ -9,12 +9,19 @@ type tblColumn = qgen.DBTableColumn
type tC = tblColumn type tC = tblColumn
type tblKey = qgen.DBTableKey type tblKey = qgen.DBTableKey
func createTables(adapter qgen.Adapter) (err error) { func createTables(a qgen.Adapter) error {
createTable := func(table string, charset string, collation string, columns []tC, keys []tblKey) { f := func(table, charset, collation string, cols []tC, keys []tblKey) error {
return qgen.Install.CreateTable(table, charset, collation, cols, keys)
}
return createTables2(a, f)
}
func createTables2(a qgen.Adapter, f func(table, charset, collation string, columns []tC, keys []tblKey) error) (err error) {
createTable := func(table, charset, collation string, cols []tC, keys []tblKey) {
if err != nil { if err != nil {
return return
} }
err = qgen.Install.CreateTable(table, charset, collation, columns, keys) err = f(table, charset, collation, cols, keys)
} }
createTable("users", mysqlPre, mysqlCol, createTable("users", mysqlPre, mysqlCol,
[]tC{ []tC{
@ -762,5 +769,15 @@ func createTables(adapter qgen.Adapter) (err error) {
}, nil, }, nil,
) )
/*createTable("tables", mysqlPre, mysqlCol,
[]tC{
tC{"id", "int", 0, false, true, ""},
tC{"name", "varchar", 200, false, false, ""},
},
[]tblKey{
tblKey{"id", "primary", "", false},
},
)*/
return err return err
} }

View File

@ -63,19 +63,19 @@ func patch0(scanner *bufio.Scanner) (err error) {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("menus", "", "", err = createTable("menus", "", "",
[]tC{ []tC{
tC{"mid", "int", 0, false, true, ""}, tC{"mid", "int", 0, false, true, ""},
}, },
[]tK{ []tK{
tK{"mid", "primary", "", false}, tK{"mid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("menu_items", "", "", err = createTable("menu_items", "", "",
[]tC{ []tC{
tC{"miid", "int", 0, false, true, ""}, tC{"miid", "int", 0, false, true, ""},
tC{"mid", "int", 0, false, false, ""}, tC{"mid", "int", 0, false, false, ""},
@ -97,7 +97,7 @@ func patch0(scanner *bufio.Scanner) (err error) {
[]tK{ []tK{
tK{"miid", "primary", "", false}, tK{"miid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
@ -191,7 +191,7 @@ func patch2(scanner *bufio.Scanner) error {
} }
func patch3(scanner *bufio.Scanner) error { func patch3(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("registration_logs", "", "", return createTable("registration_logs", "", "",
[]tC{ []tC{
tC{"rlid", "int", 0, false, true, ""}, tC{"rlid", "int", 0, false, true, ""},
tC{"username", "varchar", 100, false, false, ""}, tC{"username", "varchar", 100, false, false, ""},
@ -204,7 +204,7 @@ func patch3(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"rlid", "primary", "", false}, tK{"rlid", "primary", "", false},
}, },
)) )
} }
func patch4(scanner *bufio.Scanner) error { func patch4(scanner *bufio.Scanner) error {
@ -255,7 +255,7 @@ func patch4(scanner *bufio.Scanner) error {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("pages", "utf8mb4", "utf8mb4_general_ci", err = createTable("pages", "utf8mb4", "utf8mb4_general_ci",
[]tC{ []tC{
tC{"pid", "int", 0, false, true, ""}, tC{"pid", "int", 0, false, true, ""},
tC{"name", "varchar", 200, false, false, ""}, tC{"name", "varchar", 200, false, false, ""},
@ -267,7 +267,7 @@ func patch4(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"pid", "primary", "", false}, tK{"pid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
@ -293,7 +293,7 @@ func patch5(scanner *bufio.Scanner) error {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("users_2fa_keys", "utf8mb4", "utf8mb4_general_ci", err = createTable("users_2fa_keys", "utf8mb4", "utf8mb4_general_ci",
[]tC{ []tC{
tC{"uid", "int", 0, false, false, ""}, tC{"uid", "int", 0, false, false, ""},
tC{"secret", "varchar", 100, false, false, ""}, tC{"secret", "varchar", 100, false, false, ""},
@ -310,7 +310,7 @@ func patch5(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"uid", "primary", "", false}, tK{"uid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
@ -323,14 +323,14 @@ func patch6(scanner *bufio.Scanner) error {
} }
func patch7(scanner *bufio.Scanner) error { func patch7(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("users_avatar_queue", "", "", return createTable("users_avatar_queue", "", "",
[]tC{ []tC{
tC{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key tC{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
}, },
[]tK{ []tK{
tK{"uid", "primary", "", false}, tK{"uid", "primary", "", false},
}, },
)) )
} }
func renameRoutes(routes map[string]string) error { func renameRoutes(routes map[string]string) error {
@ -386,11 +386,11 @@ func patch8(scanner *bufio.Scanner) error {
if err != nil { if err != nil {
return err return err
} }
return execStmt(qgen.Builder.CreateTable("updates", "", "", return createTable("updates", "", "",
[]tC{ []tC{
tC{"dbVersion", "int", 0, false, false, "0"}, tC{"dbVersion", "int", 0, false, false, "0"},
}, nil, }, nil,
)) )
} }
func patch9(scanner *bufio.Scanner) error { func patch9(scanner *bufio.Scanner) error {
@ -400,7 +400,7 @@ func patch9(scanner *bufio.Scanner) error {
return err return err
} }
return execStmt(qgen.Builder.CreateTable("login_logs", "", "", return createTable("login_logs", "", "",
[]tC{ []tC{
tC{"lid", "int", 0, false, true, ""}, tC{"lid", "int", 0, false, true, ""},
tC{"uid", "int", 0, false, false, ""}, tC{"uid", "int", 0, false, false, ""},
@ -411,7 +411,7 @@ func patch9(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"lid", "primary", "", false}, tK{"lid", "primary", "", false},
}, },
)) )
} }
var acc = qgen.NewAcc var acc = qgen.NewAcc
@ -552,7 +552,7 @@ func patch15(scanner *bufio.Scanner) error {
} }
func patch16(scanner *bufio.Scanner) error { func patch16(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("password_resets", "", "", return createTable("password_resets", "", "",
[]tC{ []tC{
tC{"email", "varchar", 200, false, false, ""}, tC{"email", "varchar", 200, false, false, ""},
tC{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key tC{"uid", "int", 0, false, false, ""}, // TODO: Make this a foreign key
@ -560,7 +560,7 @@ func patch16(scanner *bufio.Scanner) error {
tC{"token", "varchar", 200, false, false, ""}, tC{"token", "varchar", 200, false, false, ""},
tC{"createdAt", "createdAt", 0, false, false, ""}, tC{"createdAt", "createdAt", 0, false, false, ""},
}, nil, }, nil,
)) )
} }
func patch17(scanner *bufio.Scanner) error { func patch17(scanner *bufio.Scanner) error {
@ -602,12 +602,12 @@ func patch18(scanner *bufio.Scanner) error {
} }
func patch19(scanner *bufio.Scanner) error { func patch19(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("memchunks", "", "", return createTable("memchunks", "", "",
[]tC{ []tC{
tC{"count", "int", 0, false, false, "0"}, tC{"count", "int", 0, false, false, "0"},
tC{"createdAt", "datetime", 0, false, false, ""}, tC{"createdAt", "datetime", 0, false, false, ""},
}, nil, }, nil,
)) )
} }
func patch20(scanner *bufio.Scanner) error { func patch20(scanner *bufio.Scanner) error {
@ -642,12 +642,12 @@ func patch21(scanner *bufio.Scanner) error {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("meta", "", "", err = createTable("meta", "", "",
[]tC{ []tC{
tC{"name", "varchar", 200, false, false, ""}, tC{"name", "varchar", 200, false, false, ""},
tC{"value", "varchar", 200, false, false, ""}, tC{"value", "varchar", 200, false, false, ""},
}, nil, }, nil,
)) )
if err != nil { if err != nil {
return err return err
} }
@ -664,7 +664,7 @@ func patch23(scanner *bufio.Scanner) error {
if err != nil { if err != nil {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("conversations", "", "", err = createTable("conversations", "", "",
[]tC{ []tC{
tC{"cid", "int", 0, false, true, ""}, tC{"cid", "int", 0, false, true, ""},
tC{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key tC{"createdBy", "int", 0, false, false, ""}, // TODO: Make this a foreign key
@ -675,7 +675,7 @@ func patch23(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"cid", "primary", "", false}, tK{"cid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
@ -684,7 +684,7 @@ func patch23(scanner *bufio.Scanner) error {
if err != nil { if err != nil {
return err return err
} }
err = execStmt(qgen.Builder.CreateTable("conversations_posts", "", "", err = createTable("conversations_posts", "", "",
[]tC{ []tC{
tC{"pid", "int", 0, false, true, ""}, tC{"pid", "int", 0, false, true, ""},
tC{"cid", "int", 0, false, false, ""}, tC{"cid", "int", 0, false, false, ""},
@ -695,7 +695,7 @@ func patch23(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"pid", "primary", "", false}, tK{"pid", "primary", "", false},
}, },
)) )
if err != nil { if err != nil {
return err return err
} }
@ -704,12 +704,12 @@ func patch23(scanner *bufio.Scanner) error {
if err != nil { if err != nil {
return err return err
} }
return execStmt(qgen.Builder.CreateTable("conversations_participants", "", "", return createTable("conversations_participants", "", "",
[]tC{ []tC{
tC{"uid", "int", 0, false, false, ""}, tC{"uid", "int", 0, false, false, ""},
tC{"cid", "int", 0, false, false, ""}, tC{"cid", "int", 0, false, false, ""},
}, nil, }, nil,
)) )
} }
func patch24(scanner *bufio.Scanner) error { func patch24(scanner *bufio.Scanner) error {
@ -717,7 +717,7 @@ func patch24(scanner *bufio.Scanner) error {
if err != nil { if err != nil {
return err return err
} }
return execStmt(qgen.Builder.CreateTable("users_groups_promotions", "", "", return createTable("users_groups_promotions", "", "",
[]tC{ []tC{
tC{"pid", "int", 0, false, true, ""}, tC{"pid", "int", 0, false, true, ""},
tC{"from_gid", "int", 0, false, false, ""}, tC{"from_gid", "int", 0, false, false, ""},
@ -731,7 +731,7 @@ func patch24(scanner *bufio.Scanner) error {
[]tK{ []tK{
tK{"pid", "primary", "", false}, tK{"pid", "primary", "", false},
}, },
)) )
} }
func patch25(scanner *bufio.Scanner) error { func patch25(scanner *bufio.Scanner) error {
@ -739,12 +739,12 @@ func patch25(scanner *bufio.Scanner) error {
} }
func patch26(scanner *bufio.Scanner) error { func patch26(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("users_blocks", "", "", return createTable("users_blocks", "", "",
[]tC{ []tC{
tC{"blocker", "int", 0, false, false, ""}, tC{"blocker", "int", 0, false, false, ""},
tC{"blockedUser", "int", 0, false, false, ""}, tC{"blockedUser", "int", 0, false, false, ""},
}, nil, }, nil,
)) )
} }
func patch27(scanner *bufio.Scanner) error { func patch27(scanner *bufio.Scanner) error {
@ -895,15 +895,19 @@ func patch31(scanner *bufio.Scanner) error {
return nil return nil
} }
func createTable(table, charset, collation string, cols []tC, keys []tK) error {
return execStmt(qgen.Builder.CreateTable(table, charset, collation, cols, keys))
}
func patch32(scanner *bufio.Scanner) error { func patch32(scanner *bufio.Scanner) error {
return execStmt(qgen.Builder.CreateTable("perfchunks", "", "", return createTable("perfchunks", "", "",
[]tC{ []tC{
tC{"low", "int", 0, false, false, "0"}, tC{"low", "int", 0, false, false, "0"},
tC{"high", "int", 0, false, false, "0"}, tC{"high", "int", 0, false, false, "0"},
tC{"avg", "int", 0, false, false, "0"}, tC{"avg", "int", 0, false, false, "0"},
tC{"createdAt", "datetime", 0, false, false, ""}, tC{"createdAt", "datetime", 0, false, false, ""},
}, nil, }, nil,
)) )
} }
func patch33(scanner *bufio.Scanner) error { func patch33(scanner *bufio.Scanner) error {