2011-05-02 01:21:22 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-03-25 02:45:23 +00:00
|
|
|
"database/sql"
|
2013-03-20 16:10:39 +00:00
|
|
|
"errors"
|
2011-05-02 01:21:22 +00:00
|
|
|
"flag"
|
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
|
|
|
"fmt"
|
2011-05-02 01:21:22 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
"camlistore.org/pkg/cmdmain"
|
2013-12-13 15:07:45 +00:00
|
|
|
"camlistore.org/pkg/sorted/mongo"
|
2013-12-18 18:01:13 +00:00
|
|
|
"camlistore.org/pkg/sorted/mysql"
|
2013-12-23 15:30:28 +00:00
|
|
|
"camlistore.org/pkg/sorted/postgres"
|
2013-12-13 21:14:14 +00:00
|
|
|
"camlistore.org/pkg/sorted/sqlite"
|
2011-06-19 21:36:46 +00:00
|
|
|
|
2014-05-01 15:42:45 +00:00
|
|
|
_ "camlistore.org/third_party/github.com/go-sql-driver/mysql"
|
2013-06-25 23:13:22 +00:00
|
|
|
_ "camlistore.org/third_party/github.com/lib/pq"
|
2013-12-13 15:07:45 +00:00
|
|
|
"camlistore.org/third_party/labix.org/v2/mgo"
|
2011-05-02 01:21:22 +00:00
|
|
|
)
|
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
type dbinitCmd struct {
|
|
|
|
user string
|
|
|
|
password string
|
|
|
|
host string
|
|
|
|
dbName string
|
|
|
|
dbType string
|
2013-06-25 04:27:23 +00:00
|
|
|
sslMode string // Postgres SSL mode configuration
|
2011-05-02 01:21:22 +00:00
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
wipe bool
|
|
|
|
keep bool
|
2013-03-22 23:34:08 +00:00
|
|
|
wal bool // Write-Ahead Logging for SQLite
|
2013-02-26 21:42:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmdmain.RegisterCommand("dbinit", func(flags *flag.FlagSet) cmdmain.CommandRunner {
|
|
|
|
cmd := new(dbinitCmd)
|
|
|
|
flags.StringVar(&cmd.user, "user", "root", "Admin user.")
|
|
|
|
flags.StringVar(&cmd.password, "password", "", "Admin password.")
|
|
|
|
flags.StringVar(&cmd.host, "host", "localhost", "host[:port]")
|
2013-03-20 16:10:39 +00:00
|
|
|
flags.StringVar(&cmd.dbName, "dbname", "", "Database to wipe or create. For sqlite, this is the db filename.")
|
2013-12-13 15:07:45 +00:00
|
|
|
flags.StringVar(&cmd.dbType, "dbtype", "mysql", "Which RDMS to use; possible values: mysql, postgres, sqlite, mongo.")
|
2013-06-25 04:27:23 +00:00
|
|
|
flags.StringVar(&cmd.sslMode, "sslmode", "require", "Configure SSL mode for postgres. Possible values: require, verify-full, disable.")
|
2011-05-02 01:21:22 +00:00
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
flags.BoolVar(&cmd.wipe, "wipe", false, "Wipe the database and re-create it?")
|
|
|
|
flags.BoolVar(&cmd.keep, "ignoreexists", false, "Do nothing if database already exists.")
|
2013-03-22 23:34:08 +00:00
|
|
|
// Defaults to true, because it fixes http://camlistore.org/issues/114
|
|
|
|
flags.BoolVar(&cmd.wal, "wal", true, "Enable Write-Ahead Logging with SQLite, for better concurrency. Requires SQLite >= 3.7.0.")
|
2013-02-26 21:42:38 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbinitCmd) Describe() string {
|
|
|
|
return "Set up the database for the indexer."
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbinitCmd) Usage() {
|
|
|
|
fmt.Fprintf(os.Stderr, "Usage: camtool [globalopts] dbinit [dbinitopts] \n")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbinitCmd) Examples() []string {
|
|
|
|
return []string{
|
|
|
|
"-user root -password root -host localhost -dbname camliprod -wipe",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *dbinitCmd) RunCommand(args []string) error {
|
|
|
|
if c.dbName == "" {
|
|
|
|
return cmdmain.UsageError("--dbname flag required")
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 15:07:45 +00:00
|
|
|
if c.dbType != "mysql" && c.dbType != "postgres" && c.dbType != "mongo" {
|
2013-03-20 16:10:39 +00:00
|
|
|
if c.dbType == "sqlite" {
|
|
|
|
if !WithSQLite {
|
|
|
|
return ErrNoSQLite
|
|
|
|
}
|
2013-03-22 23:34:08 +00:00
|
|
|
c.wal = c.wal && sqlite.IsWALCapable()
|
|
|
|
if !c.wal {
|
|
|
|
fmt.Print("WARNING: An SQLite indexer without Write Ahead Logging will most likely fail. See http://camlistore.org/issues/114\n")
|
|
|
|
}
|
2013-03-20 16:10:39 +00:00
|
|
|
} else {
|
2013-12-13 15:07:45 +00:00
|
|
|
return cmdmain.UsageError(fmt.Sprintf("--dbtype flag: got %v, want %v", c.dbType, `"mysql" or "postgres" or "sqlite", or "mongo"`))
|
2013-03-20 16:10:39 +00:00
|
|
|
}
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
2013-03-20 16:10:39 +00:00
|
|
|
|
2012-11-03 18:58:50 +00:00
|
|
|
var rootdb *sql.DB
|
|
|
|
var err error
|
2013-02-26 21:42:38 +00:00
|
|
|
switch c.dbType {
|
2012-11-03 18:58:50 +00:00
|
|
|
case "postgres":
|
2013-06-25 04:27:23 +00:00
|
|
|
conninfo := fmt.Sprintf("user=%s dbname=%s host=%s password=%s sslmode=%s", c.user, "postgres", c.host, c.password, c.sslMode)
|
2012-11-03 18:58:50 +00:00
|
|
|
rootdb, err = sql.Open("postgres", conninfo)
|
|
|
|
case "mysql":
|
2014-05-01 15:42:45 +00:00
|
|
|
rootdb, err = sql.Open("mysql", c.user+":"+c.password+"@/mysql")
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
2011-05-02 01:21:22 +00:00
|
|
|
if err != nil {
|
2013-02-26 21:42:38 +00:00
|
|
|
exitf("Error connecting to the root %s database: %v", c.dbType, err)
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
dbname := c.dbName
|
2013-12-13 15:07:45 +00:00
|
|
|
exists := c.dbExists(rootdb)
|
2011-05-02 01:21:22 +00:00
|
|
|
if exists {
|
2013-02-26 21:42:38 +00:00
|
|
|
if c.keep {
|
|
|
|
return nil
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
2013-02-26 21:42:38 +00:00
|
|
|
if !c.wipe {
|
|
|
|
return cmdmain.UsageError(fmt.Sprintf("Database %q already exists, but --wipe not given. Stopping.", dbname))
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
2013-12-13 15:07:45 +00:00
|
|
|
if c.dbType == "mongo" {
|
|
|
|
return c.wipeMongo()
|
|
|
|
}
|
2013-03-20 16:10:39 +00:00
|
|
|
if c.dbType != "sqlite" {
|
|
|
|
do(rootdb, "DROP DATABASE "+dbname)
|
|
|
|
}
|
|
|
|
}
|
2013-12-13 15:07:45 +00:00
|
|
|
switch c.dbType {
|
|
|
|
case "sqlite":
|
2013-03-20 16:10:39 +00:00
|
|
|
_, err := os.Create(dbname)
|
|
|
|
if err != nil {
|
|
|
|
exitf("Error creating file %v for sqlite db: %v", dbname, err)
|
|
|
|
}
|
2013-12-13 15:07:45 +00:00
|
|
|
case "mongo":
|
|
|
|
return nil
|
2013-12-23 15:30:28 +00:00
|
|
|
case "postgres":
|
|
|
|
// because we want string comparison to work as on MySQL and SQLite.
|
|
|
|
// in particular we want: 'foo|bar' < 'foo}' (which is not the case with an utf8 collation apparently).
|
|
|
|
do(rootdb, "CREATE DATABASE "+dbname+" LC_COLLATE = 'C' TEMPLATE = template0")
|
2013-12-13 15:07:45 +00:00
|
|
|
default:
|
2013-03-20 16:10:39 +00:00
|
|
|
do(rootdb, "CREATE DATABASE "+dbname)
|
2012-03-26 20:57:53 +00:00
|
|
|
}
|
|
|
|
|
2012-11-03 18:58:50 +00:00
|
|
|
var db *sql.DB
|
2013-02-26 21:42:38 +00:00
|
|
|
switch c.dbType {
|
2012-11-03 18:58:50 +00:00
|
|
|
case "postgres":
|
2013-06-25 04:27:23 +00:00
|
|
|
conninfo := fmt.Sprintf("user=%s dbname=%s host=%s password=%s sslmode=%s", c.user, dbname, c.host, c.password, c.sslMode)
|
2012-11-03 18:58:50 +00:00
|
|
|
db, err = sql.Open("postgres", conninfo)
|
2013-03-20 16:10:39 +00:00
|
|
|
case "sqlite":
|
|
|
|
db, err = sql.Open("sqlite3", dbname)
|
2012-11-03 18:58:50 +00:00
|
|
|
default:
|
2014-05-01 15:42:45 +00:00
|
|
|
db, err = sql.Open("mysql", c.user+":"+c.password+"@/"+dbname)
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
2012-03-26 20:57:53 +00:00
|
|
|
if err != nil {
|
2013-03-20 16:10:39 +00:00
|
|
|
return fmt.Errorf("Connecting to the %s %s database: %v", dbname, c.dbType, err)
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
2011-06-19 21:36:46 +00:00
|
|
|
|
2013-02-26 21:42:38 +00:00
|
|
|
switch c.dbType {
|
2012-11-03 18:58:50 +00:00
|
|
|
case "postgres":
|
|
|
|
for _, tableSql := range postgres.SQLCreateTables() {
|
|
|
|
do(db, tableSql)
|
|
|
|
}
|
|
|
|
for _, statement := range postgres.SQLDefineReplace() {
|
|
|
|
do(db, statement)
|
|
|
|
}
|
|
|
|
doQuery(db, fmt.Sprintf(`SELECT replaceintometa('version', '%d')`, postgres.SchemaVersion()))
|
|
|
|
case "mysql":
|
|
|
|
for _, tableSql := range mysql.SQLCreateTables() {
|
|
|
|
do(db, tableSql)
|
|
|
|
}
|
|
|
|
do(db, fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, mysql.SchemaVersion()))
|
2013-03-20 16:10:39 +00:00
|
|
|
case "sqlite":
|
|
|
|
for _, tableSql := range sqlite.SQLCreateTables() {
|
|
|
|
do(db, tableSql)
|
|
|
|
}
|
2013-03-22 23:34:08 +00:00
|
|
|
if c.wal {
|
|
|
|
do(db, sqlite.EnableWAL())
|
|
|
|
}
|
2013-03-20 16:10:39 +00:00
|
|
|
do(db, fmt.Sprintf(`REPLACE INTO meta VALUES ('version', '%d')`, sqlite.SchemaVersion()))
|
2011-06-19 21:36:46 +00:00
|
|
|
}
|
2013-02-26 21:42:38 +00:00
|
|
|
return nil
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
|
|
|
|
2012-03-25 02:45:23 +00:00
|
|
|
func do(db *sql.DB, sql string) {
|
|
|
|
_, err := db.Exec(sql)
|
2013-02-26 21:42:38 +00:00
|
|
|
if err != nil {
|
|
|
|
exitf("Error %v running SQL: %s", err, sql)
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-03 18:58:50 +00:00
|
|
|
func doQuery(db *sql.DB, sql string) {
|
|
|
|
r, err := db.Query(sql)
|
|
|
|
if err == nil {
|
|
|
|
r.Close()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
exitf("Error %v running SQL: %s", err, sql)
|
|
|
|
}
|
|
|
|
|
2013-12-13 15:07:45 +00:00
|
|
|
func (c *dbinitCmd) dbExists(db *sql.DB) bool {
|
2012-11-03 18:58:50 +00:00
|
|
|
query := "SHOW DATABASES"
|
2013-12-13 15:07:45 +00:00
|
|
|
switch c.dbType {
|
2012-11-03 18:58:50 +00:00
|
|
|
case "postgres":
|
|
|
|
query = "SELECT datname FROM pg_database"
|
|
|
|
case "mysql":
|
|
|
|
query = "SHOW DATABASES"
|
2013-03-20 16:10:39 +00:00
|
|
|
case "sqlite":
|
|
|
|
// There is no point in using sql.Open because it apparently does
|
|
|
|
// not return an error when the file does not exist.
|
2013-12-13 15:07:45 +00:00
|
|
|
fi, err := os.Stat(c.dbName)
|
2013-06-23 00:51:09 +00:00
|
|
|
return err == nil && fi.Size() > 0
|
2013-12-13 15:07:45 +00:00
|
|
|
case "mongo":
|
|
|
|
session, err := c.mongoSession()
|
|
|
|
if err != nil {
|
|
|
|
exitf("%v", err)
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
n, err := session.DB(c.dbName).C(mongo.CollectionName).Find(nil).Limit(1).Count()
|
|
|
|
if err != nil {
|
|
|
|
exitf("%v", err)
|
|
|
|
}
|
|
|
|
return n != 0
|
2012-11-03 18:58:50 +00:00
|
|
|
}
|
|
|
|
rows, err := db.Query(query)
|
2011-05-02 01:21:22 +00:00
|
|
|
check(err)
|
2012-03-25 02:45:23 +00:00
|
|
|
defer rows.Close()
|
|
|
|
for rows.Next() {
|
|
|
|
var db string
|
|
|
|
check(rows.Scan(&db))
|
2013-12-13 15:07:45 +00:00
|
|
|
if db == c.dbName {
|
2011-05-02 01:21:22 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
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 check(err error) {
|
2011-05-02 01:21:22 +00:00
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2013-02-26 21:42:38 +00:00
|
|
|
exitf("SQL error: %v", err)
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func exitf(format string, args ...interface{}) {
|
|
|
|
if !strings.HasSuffix(format, "\n") {
|
|
|
|
format = format + "\n"
|
|
|
|
}
|
2013-02-26 21:42:38 +00:00
|
|
|
cmdmain.Errorf(format, args)
|
|
|
|
cmdmain.Exit(1)
|
2011-05-02 01:21:22 +00:00
|
|
|
}
|
2013-03-20 16:10:39 +00:00
|
|
|
|
|
|
|
var WithSQLite = false
|
|
|
|
|
2013-09-19 09:17:57 +00:00
|
|
|
var ErrNoSQLite = errors.New("the command was not built with SQLite support. See https://code.google.com/p/camlistore/wiki/SQLite" + compileHint())
|
2013-03-20 16:10:39 +00:00
|
|
|
|
|
|
|
func compileHint() string {
|
|
|
|
if _, err := os.Stat("/etc/apt"); err == nil {
|
|
|
|
return " (Required: apt-get install libsqlite3-dev)"
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2013-12-13 15:07:45 +00:00
|
|
|
|
|
|
|
// mongoSession returns an *mgo.Session or nil if c.dbtype is
|
|
|
|
// not "mongo" or if there was an error.
|
|
|
|
func (c *dbinitCmd) mongoSession() (*mgo.Session, error) {
|
|
|
|
if c.dbType != "mongo" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
url := ""
|
|
|
|
if c.user == "" || c.password == "" {
|
|
|
|
url = c.host
|
|
|
|
} else {
|
|
|
|
url = c.user + ":" + c.password + "@" + c.host + "/" + c.dbName
|
|
|
|
}
|
|
|
|
return mgo.Dial(url)
|
|
|
|
}
|
|
|
|
|
|
|
|
// wipeMongo erases all documents from the mongo collection
|
|
|
|
// if c.dbType is "mongo".
|
|
|
|
func (c *dbinitCmd) wipeMongo() error {
|
|
|
|
if c.dbType != "mongo" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
session, err := c.mongoSession()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer session.Close()
|
|
|
|
if _, err := session.DB(c.dbName).C(mongo.CollectionName).RemoveAll(nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|