Gisle Aune
7 years ago
3 changed files with 99 additions and 0 deletions
@ -0,0 +1,54 @@ |
|||
package main |
|||
|
|||
import ( |
|||
"database/sql" |
|||
"flag" |
|||
"fmt" |
|||
"io/ioutil" |
|||
"log" |
|||
"os" |
|||
"strings" |
|||
|
|||
_ "github.com/go-sql-driver/mysql" |
|||
) |
|||
|
|||
var flagUser = flag.String("user", "logbot4", "MySQL user") |
|||
var flagPassword = flag.String("password", "logbot4", "MySQL password") |
|||
var flagDatabase = flag.String("database", "logbot4", "MySQL database") |
|||
|
|||
func main() { |
|||
flag.Parse() |
|||
|
|||
connString := fmt.Sprintf("%s:%s@/%s", *flagUser, *flagPassword, *flagDatabase) |
|||
db, err := sql.Open("mysql", connString) |
|||
if err != nil { |
|||
log.Fatalf("Could not open SQL connection: %s", err) |
|||
} |
|||
|
|||
file, err := os.Open("./data/setup.sql") |
|||
if err != nil { |
|||
log.Fatalf("Could not open ./data/setup.sql: %s", err) |
|||
} |
|||
|
|||
data, err := ioutil.ReadAll(file) |
|||
if err != nil { |
|||
log.Fatalf("Could not read setup.sql: %s", err) |
|||
} |
|||
statements := strings.Split(string(data), ";") |
|||
|
|||
for _, statement := range statements { |
|||
statement := strings.Trim(statement, "\n\t ") |
|||
|
|||
if len(statement) <= 1 { |
|||
continue |
|||
} |
|||
|
|||
result, err := db.Exec(statement) |
|||
if err != nil { |
|||
log.Fatalf("Could not execute statement: %s", err) |
|||
} |
|||
|
|||
affected, _ := result.RowsAffected() |
|||
log.Printf("%d rows affected", affected) |
|||
} |
|||
} |
@ -0,0 +1,40 @@ |
|||
CREATE TABLE IF NOT EXISTS `logheader` ( |
|||
`id` CHAR(64) NOT NULL PRIMARY KEY, |
|||
`channel` CHAR(32) NOT NULL, |
|||
`status` CHAR(8) NOT NULL, |
|||
`status_time` DATETIME NOT NULL, |
|||
`time` DATETIME NOT NULL, |
|||
`title` VARCHAR(255), |
|||
`tag` VARCHAR(255), |
|||
`location` VARCHAR(255) |
|||
) ENGINE=innodb CHARSET=utf8; |
|||
|
|||
CREATE TABLE IF NOT EXISTS `logpost` ( |
|||
`id` CHAR(24) NOT NULL PRIMARY KEY, |
|||
`nick` CHAR(30) NOT NULL, |
|||
`time` DATETIME NOT NULL, |
|||
`messages` TEXT NOT NULL, |
|||
`text` TEXT NOT NULL |
|||
) ENGINE=innodb CHARSET=utf8; |
|||
|
|||
CREATE TABLE IF NOT EXISTS `lognicks` ( |
|||
`log_id` CHAR(64) NOT NULL, |
|||
`nick` CHAR(32) NOT NULL, |
|||
|
|||
PRIMARY KEY (`log_id`, `nick`) |
|||
) ENGINE=innodb CHARSET=utf8; |
|||
|
|||
CREATE TABLE IF NOT EXISTS `rpchar` ( |
|||
`nick` CHAR(32) NOT NULL PRIMARY KEY, |
|||
`name` VARCHAR(255) NOT NULL, |
|||
`short_name` VARCHAR(255) NOT NULL, |
|||
`bio_url` VARCHAR(255), |
|||
`about` TEXT |
|||
) ENGINE=innodb CHARSET=utf8; |
|||
|
|||
CREATE TABLE IF NOT EXISTS `altnick` ( |
|||
`nick` CHAR(32) NOT NULL PRIMARY KEY, |
|||
`main_nick` CHAR(32) NOT NULL, |
|||
|
|||
FOREIGN KEY (`main_nick`) REFERENCES rpchar(nick) ON DELETE CASCADE |
|||
) ENGINE=innodb CHARSET=utf8; |
Write
Preview
Loading…
Cancel
Save
Reference in new issue