github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/sorted/mysql/mysqlkv.go (about) 1 /* 2 Copyright 2011 The Camlistore Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 // Package mysql provides an implementation of sorted.KeyValue 18 // on top of MySQL. 19 package mysql 20 21 import ( 22 "database/sql" 23 "fmt" 24 "os" 25 "strings" 26 27 "camlistore.org/pkg/jsonconfig" 28 "camlistore.org/pkg/sorted" 29 "camlistore.org/pkg/sorted/sqlkv" 30 31 _ "camlistore.org/third_party/github.com/go-sql-driver/mysql" 32 ) 33 34 func init() { 35 sorted.RegisterKeyValue("mysql", newKeyValueFromJSONConfig) 36 } 37 38 func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) { 39 host := cfg.OptionalString("host", "") 40 dsn := fmt.Sprintf("%s:%s@/%s", 41 cfg.RequiredString("user"), 42 cfg.OptionalString("password", ""), 43 cfg.RequiredString("database"), 44 ) 45 if err := cfg.Validate(); err != nil { 46 return nil, err 47 } 48 if host != "" { 49 // TODO(mpl): document that somewhere 50 if !strings.Contains(host, ":") { 51 host = host + ":3306" 52 } 53 dsn = strings.Replace(dsn, "@", fmt.Sprintf("@tcp(%v)", host), 1) 54 } 55 56 db, err := sql.Open("mysql", dsn) 57 if err != nil { 58 return nil, err 59 } 60 for _, tableSql := range SQLCreateTables() { 61 if _, err := db.Exec(tableSql); err != nil { 62 return nil, fmt.Errorf("error creating table with %q: %v", tableSql, err) 63 } 64 } 65 if _, err := db.Exec(fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, SchemaVersion())); err != nil { 66 return nil, fmt.Errorf("error setting schema version: %v", err) 67 } 68 69 kv := &keyValue{ 70 db: db, 71 KeyValue: &sqlkv.KeyValue{ 72 DB: db, 73 }, 74 } 75 if err := kv.ping(); err != nil { 76 return nil, fmt.Errorf("MySQL db unreachable: %v", err) 77 } 78 version, err := kv.SchemaVersion() 79 if err != nil { 80 return nil, fmt.Errorf("error getting schema version (need to init database?): %v", err) 81 } 82 if version != requiredSchemaVersion { 83 if version == 20 && requiredSchemaVersion == 21 { 84 fmt.Fprintf(os.Stderr, fixSchema20to21) 85 } 86 if os.Getenv("CAMLI_DEV_CAMLI_ROOT") != "" { 87 // Good signal that we're using the devcam server, so help out 88 // the user with a more useful tip: 89 return nil, fmt.Errorf("database schema version is %d; expect %d (run \"devcam server --wipe\" to wipe both your blobs and re-populate the database schema)", version, requiredSchemaVersion) 90 } 91 return nil, fmt.Errorf("database schema version is %d; expect %d (need to re-init/upgrade database?)", 92 version, requiredSchemaVersion) 93 } 94 95 return kv, nil 96 } 97 98 type keyValue struct { 99 *sqlkv.KeyValue 100 101 db *sql.DB 102 } 103 104 func (kv *keyValue) ping() error { 105 // TODO(bradfitz): something more efficient here? 106 _, err := kv.SchemaVersion() 107 return err 108 } 109 110 func (kv *keyValue) SchemaVersion() (version int, err error) { 111 err = kv.db.QueryRow("SELECT value FROM meta WHERE metakey='version'").Scan(&version) 112 return 113 } 114 115 const fixSchema20to21 = `Character set in tables changed to binary, you can fix your tables with: 116 ALTER TABLE rows CONVERT TO CHARACTER SET binary; 117 ALTER TABLE meta CONVERT TO CHARACTER SET binary; 118 UPDATE meta SET value=21 WHERE metakey='version' AND value=20; 119 `