sql cleanup

This commit is contained in:
shoopea 2019-05-06 11:03:12 +08:00
parent 76a1911d63
commit 47161d7167

69
sql.go
View File

@ -14,40 +14,28 @@ func initDB() {
defer tx.Rollback()
_, err = tx.Exec("set foreign_key_checks = 0")
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : set foreign_key_checks = 0")
var name string
rows, err := db.Query("show tables")
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : show tables")
for rows.Next() {
err = rows.Scan(&name)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : show tables listing")
_, err = tx.Exec("drop table " + name)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : drop table "+name)
}
err = rows.Err()
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : show tables listing end")
rows.Close()
_, err = tx.Exec("set foreign_key_checks = 1")
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : set foreign_key_checks = 1")
err = tx.Commit()
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : commit cleanup")
log.Println("Database cleaned up")
@ -57,9 +45,7 @@ func initDB() {
,name VARCHAR(80) NOT NULL
,PRIMARY KEY (id)
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table code_obj_type")
_, err = db.Exec(`CREATE TABLE code_obj_sub_type (
id SMALLINT(5) UNSIGNED NOT NULL
@ -69,22 +55,18 @@ func initDB() {
,PRIMARY KEY (id)
,FOREIGN KEY (obj_type_id) REFERENCES code_obj_type(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table code_obj_sub_type")
_, err = db.Exec(`CREATE TABLE obj (
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
,obj_type_id SMALLINT UNSIGNED NOT NULL
,PRIMARY KEY (id)
) ENGINE = InnoDB AUTO_INCREMENT = 0 CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table obj")
_, err = db.Exec(`CREATE TABLE obj_user (
obj_id BIGINT UNSIGNED NOT NULL
,tg_id BIGINT UNSIGNED NOT NULL
,telegram_id BIGINT UNSIGNED NOT NULL
,user_id VARCHAR(32) NOT NULL
,name VARCHAR(80) NOT NULL
,guild_id BIGINT UNSIGNED
@ -93,9 +75,7 @@ func initDB() {
,role ENUM('commander', 'bartender', 'squire', 'none')
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table obj_user")
_, err = db.Exec(`CREATE TABLE obj_guild (
obj_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT
@ -105,9 +85,7 @@ func initDB() {
,deposit_chat_id BIGINT NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table obj_guild")
_, err = db.Exec(`CREATE TABLE obj_msg (
obj_id BIGINT UNSIGNED NOT NULL
@ -119,9 +97,7 @@ func initDB() {
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (msg_id, chat_id, sender_user_id)
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table obj_msg")
_, err = db.Exec(`CREATE TABLE obj_msg_report (
obj_id BIGINT UNSIGNED NOT NULL
@ -134,19 +110,14 @@ func initDB() {
,stamina BOOLEAN NOT NULL
,crit BOOLEAN NOT NULL
,FOREIGN KEY (obj_id) REFERENCES obj(id) ON DELETE CASCADE
,UNIQUE KEY (msg_id, chat_id, sender_user_id)
) ENGINE = InnoDB CHARSET=utf8 COLLATE utf8_unicode_ci;`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : create table obj_msg_report")
_, err = db.Exec(`INSERT INTO code_obj_type (id, intl_id, name)
VALUES (1, "user", "User")
,(2, "guild", "Guild")
,(3, "msg", "Message");`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : populate table code_obj_type")
_, err = db.Exec(`INSERT INTO code_obj_sub_type (id, intl_id, name, obj_type_id)
VALUES (1, "unprocessed", "Unprocessed", 3)
@ -158,9 +129,7 @@ func initDB() {
,(7, "hero", "Hero summary", 3)
,(8, "me", "Hero short summary", 3)
,(9, "inv", "Inventory", 3);`)
if err != nil {
log.Fatal(err)
}
failOnError(err, "initDB : populate table code_obj_sub_type")
log.Println("Database set up")
}