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