github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/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  
    26  	"camlistore.org/pkg/jsonconfig"
    27  	"camlistore.org/pkg/sorted"
    28  	"camlistore.org/pkg/sorted/sqlkv"
    29  
    30  	_ "camlistore.org/third_party/github.com/ziutek/mymysql/godrv"
    31  )
    32  
    33  func init() {
    34  	sorted.RegisterKeyValue("mysql", newKeyValueFromJSONConfig)
    35  }
    36  
    37  // Config holds the parameters used to connect to the MySQL db.
    38  type Config struct {
    39  	Host     string // Optional. Defaults to "localhost" in ConfigFromJSON.
    40  	Database string // Required.
    41  	User     string // Required.
    42  	Password string // Optional.
    43  }
    44  
    45  // ConfigFromJSON populates Config from config, and validates
    46  // config. It returns an error if config fails to validate.
    47  func ConfigFromJSON(config jsonconfig.Obj) (Config, error) {
    48  	conf := Config{
    49  		Host:     config.OptionalString("host", "localhost"),
    50  		User:     config.RequiredString("user"),
    51  		Password: config.OptionalString("password", ""),
    52  		Database: config.RequiredString("database"),
    53  	}
    54  	if err := config.Validate(); err != nil {
    55  		return Config{}, err
    56  	}
    57  	return conf, nil
    58  }
    59  
    60  func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
    61  	conf, err := ConfigFromJSON(cfg)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return NewKeyValue(conf)
    66  }
    67  
    68  // NewKeyValue returns a sorted.KeyValue implementation of the described MySQL database.
    69  func NewKeyValue(cfg Config) (sorted.KeyValue, error) {
    70  	// TODO(bradfitz,mpl): host is ignored for now. I think we can connect to host with:
    71  	// tcp:ADDR*DBNAME/USER/PASSWD (http://godoc.org/github.com/ziutek/mymysql/godrv#Driver.Open)
    72  	// I suppose we'll have to do a lookup first.
    73  	dsn := cfg.Database + "/" + cfg.User + "/" + cfg.Password
    74  	db, err := sql.Open("mymysql", dsn)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	kv := &keyValue{
    79  		db: db,
    80  		KeyValue: &sqlkv.KeyValue{
    81  			DB: db,
    82  		},
    83  		conf: cfg,
    84  	}
    85  	if err := kv.ping(); err != nil {
    86  		return nil, fmt.Errorf("MySQL db unreachable: %v", err)
    87  	}
    88  	version, err := kv.SchemaVersion()
    89  	if err != nil {
    90  		return nil, fmt.Errorf("error getting schema version (need to init database?): %v", err)
    91  	}
    92  	if version != requiredSchemaVersion {
    93  		if version == 20 && requiredSchemaVersion == 21 {
    94  			fmt.Fprintf(os.Stderr, fixSchema20to21)
    95  		}
    96  		if os.Getenv("CAMLI_DEV_CAMLI_ROOT") != "" {
    97  			// Good signal that we're using the devcam server, so help out
    98  			// the user with a more useful tip:
    99  			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)
   100  		}
   101  		return nil, fmt.Errorf("database schema version is %d; expect %d (need to re-init/upgrade database?)",
   102  			version, requiredSchemaVersion)
   103  	}
   104  
   105  	return kv, nil
   106  }
   107  
   108  type keyValue struct {
   109  	*sqlkv.KeyValue
   110  
   111  	conf Config
   112  	db   *sql.DB
   113  }
   114  
   115  func (kv *keyValue) ping() error {
   116  	// TODO(bradfitz): something more efficient here?
   117  	_, err := kv.SchemaVersion()
   118  	return err
   119  }
   120  
   121  func (kv *keyValue) SchemaVersion() (version int, err error) {
   122  	err = kv.db.QueryRow("SELECT value FROM meta WHERE metakey='version'").Scan(&version)
   123  	return
   124  }
   125  
   126  const fixSchema20to21 = `Character set in tables changed to binary, you can fix your tables with:
   127  ALTER TABLE rows CONVERT TO CHARACTER SET binary;
   128  ALTER TABLE meta CONVERT TO CHARACTER SET binary;
   129  UPDATE meta SET value=21 WHERE metakey='version' AND value=20;
   130  `