sorted/mysql: create the db too if it doesn't exist.

Change-Id: I566d49052057848e9d4af0f01243a89db367e20a
This commit is contained in:
Brad Fitzpatrick 2014-08-06 17:21:42 -07:00
parent b6cff26139
commit ac5c2fd045
2 changed files with 8 additions and 3 deletions

View File

@ -28,12 +28,13 @@ func SchemaVersion() int {
// which is purely about bytes.
func SQLCreateTables() []string {
return []string{
`CREATE TABLE IF NOT EXISTS /*DB*/rows (
`CREATE DATABASE IF NOT EXISTS /*DB*/`,
`CREATE TABLE IF NOT EXISTS /*DB*/.rows (
k VARCHAR(255) NOT NULL PRIMARY KEY,
v VARCHAR(255))
DEFAULT CHARACTER SET binary`,
`CREATE TABLE IF NOT EXISTS /*DB*/meta (
`CREATE TABLE IF NOT EXISTS /*DB*/.meta (
metakey VARCHAR(255) NOT NULL PRIMARY KEY,
value VARCHAR(255) NOT NULL)
DEFAULT CHARACTER SET binary`,

View File

@ -47,6 +47,10 @@ func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
}
var err error
if host != "" {
host, err = maybeRemapCloudSQL(host)
if err != nil {
return nil, err
}
if !strings.Contains(host, ":") {
host += ":3306"
}
@ -65,7 +69,7 @@ func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
}
for _, tableSQL := range SQLCreateTables() {
tableSQL = strings.Replace(tableSQL, "/*DB*/", database+".", -1)
tableSQL = strings.Replace(tableSQL, "/*DB*/", database, -1)
if _, err := db.Exec(tableSQL); err != nil {
return nil, fmt.Errorf("error creating table with %q: %v", tableSQL, err)
}