2011-03-05 22:14:00 +00:00
/ *
Copyright 2011 Google Inc .
Licensed under the Apache License , Version 2.0 ( the "License" ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an "AS IS" BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
* /
2012-03-27 16:37:32 +00:00
package mysql
2011-03-05 22:14:00 +00:00
import (
2012-03-25 02:45:23 +00:00
"database/sql"
2011-04-03 15:07:40 +00:00
"fmt"
2011-03-05 22:14:00 +00:00
"os"
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/blobserver"
2012-03-25 02:45:23 +00:00
"camlistore.org/pkg/index"
2012-04-09 16:53:49 +00:00
"camlistore.org/pkg/index/sqlindex"
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
"camlistore.org/pkg/jsonconfig"
2012-03-25 02:45:23 +00:00
_ "camlistore.org/third_party/github.com/ziutek/mymysql/godrv"
2011-03-05 22:14:00 +00:00
)
2012-03-25 02:45:23 +00:00
type myIndexStorage struct {
2012-04-09 16:53:49 +00:00
* sqlindex . Storage
2012-03-25 02:45:23 +00:00
2012-04-09 16:53:49 +00:00
host , user , password , database string
db * sql . DB
2012-03-25 02:45:23 +00:00
}
2013-01-14 04:19:36 +00:00
var _ index . Storage = ( * myIndexStorage ) ( nil )
2011-04-03 15:07:40 +00:00
2013-01-14 04:19:36 +00:00
// NewStorage returns an index.Storage implementation of the described MySQL database.
2012-03-29 23:12:12 +00:00
// This exists mostly for testing and does not initialize the schema.
2013-01-14 04:19:36 +00:00
func NewStorage ( host , user , password , dbname string ) ( index . Storage , error ) {
2012-04-09 16:53:49 +00:00
// TODO(bradfitz): host is ignored; how to plumb it through with mymysql?
2013-04-03 15:50:46 +00:00
dsn := dbname + "/" + user + "/" + password
2012-12-01 01:43:55 +00:00
db , err := sql . Open ( "mymysql" , dsn )
2012-04-09 16:53:49 +00:00
if err != nil {
return nil , err
}
// TODO(bradfitz): ping db, check that it's reachable.
return & myIndexStorage {
db : db ,
Storage : & sqlindex . Storage {
DB : db ,
} ,
2012-03-29 23:12:12 +00:00
host : host ,
user : user ,
password : password ,
database : dbname ,
2012-04-09 16:53:49 +00:00
} , nil
2012-03-29 23:12:12 +00:00
}
2013-04-03 15:50:46 +00:00
const fixSchema20to21 = ` Character set in tables changed to binary , you can fix your tables with :
ALTER TABLE rows CONVERT TO CHARACTER SET binary ;
ALTER TABLE meta CONVERT TO CHARACTER SET binary ;
UPDATE meta SET value = 21 WHERE metakey = ' version ' AND value = 20 ;
`
Update from r60 to [almost] Go 1.
A lot is still broken, but most stuff at least compiles now.
The directory tree has been rearranged now too. Go libraries are now
under "pkg". Fully qualified, they are e.g. "camlistore.org/pkg/jsonsign".
The go tool cannot yet fetch from arbitrary domains, but discussion is
happening now on which mechanism to use to allow that.
For now, put the camlistore root under $GOPATH/src. Typically $GOPATH
is $HOME, so Camlistore should be at $HOME/src/camlistore.org.
Then you can:
$ go build ./server/camlistored
... etc
The build.pl script is currently disabled. It'll be resurrected at
some point, but with a very different role (helping create a fake
GOPATH and running the go build command, if things are installed at
the wrong place, and/or running fileembed generators).
Many things are certainly broken.
Many things are disabled. (MySQL, all indexing, etc).
Many things need to be moved into
camlistore.org/third_party/{code.google.com,github.com} and updated
from their r60 to Go 1 versions, where applicable.
The GoMySQL stuff should be updated to use database/sql and the ziutek
library implementing database/sql/driver.
Help wanted.
Change-Id: If71217dc5c8f0e70dbe46e9504ca5131c6eeacde
2012-02-19 05:53:06 +00:00
func newFromConfig ( ld blobserver . Loader , config jsonconfig . Obj ) ( blobserver . Storage , error ) {
2012-04-09 16:53:49 +00:00
var (
blobPrefix = config . RequiredString ( "blobSource" )
host = config . OptionalString ( "host" , "localhost" )
user = config . RequiredString ( "user" )
password = config . OptionalString ( "password" , "" )
database = config . RequiredString ( "database" )
)
2011-04-03 15:07:40 +00:00
if err := config . Validate ( ) ; err != nil {
return nil , err
}
2012-03-26 20:57:53 +00:00
sto , err := ld . GetStorage ( blobPrefix )
if err != nil {
return nil , err
}
2012-04-09 16:53:49 +00:00
isto , err := NewStorage ( host , user , password , database )
2012-03-25 06:53:35 +00:00
if err != nil {
return nil , err
}
2012-04-09 16:53:49 +00:00
is := isto . ( * myIndexStorage )
2012-03-25 06:53:35 +00:00
if err := is . ping ( ) ; err != nil {
return nil , err
}
2011-06-23 01:08:07 +00:00
2012-03-25 02:45:23 +00:00
version , err := is . SchemaVersion ( )
2011-06-19 21:36:46 +00:00
if err != nil {
return nil , fmt . Errorf ( "error getting schema version (need to init database?): %v" , err )
}
if version != requiredSchemaVersion {
2013-04-03 15:50:46 +00:00
if version == 20 && requiredSchemaVersion == 21 {
fmt . Fprintf ( os . Stderr , fixSchema20to21 )
}
2011-06-19 21:36:46 +00:00
if os . Getenv ( "CAMLI_ADVERTISED_PASSWORD" ) != "" {
// Good signal that we're using the dev-server script, so help out
// the user with a more useful tip:
return nil , fmt . Errorf ( "database schema version is %d; expect %d (run \"./dev-server --wipe\" to wipe both your blobs and re-populate the database schema)" , version , requiredSchemaVersion )
}
return nil , fmt . Errorf ( "database schema version is %d; expect %d (need to re-init/upgrade database?)" ,
version , requiredSchemaVersion )
}
2012-03-26 20:57:53 +00:00
ix := index . New ( is )
ix . BlobSource = sto
// Good enough, for now:
ix . KeyFetcher = ix . BlobSource
return ix , nil
2011-04-03 15:07:40 +00:00
}
func init ( ) {
blobserver . RegisterStorageConstructor ( "mysqlindexer" , blobserver . StorageConstructor ( newFromConfig ) )
}
2012-03-25 06:53:35 +00:00
func ( mi * myIndexStorage ) ping ( ) error {
2012-03-25 02:45:23 +00:00
// TODO(bradfitz): something more efficient here?
2012-03-25 06:53:35 +00:00
_ , err := mi . SchemaVersion ( )
return err
2011-03-05 23:09:36 +00:00
}
2012-03-25 02:45:23 +00:00
func ( mi * myIndexStorage ) SchemaVersion ( ) ( version int , err error ) {
err = mi . db . QueryRow ( "SELECT value FROM meta WHERE metakey='version'" ) . Scan ( & version )
return
2011-03-05 22:14:00 +00:00
}