storj/storage/cockroachkv/schema/migrate.go
Jeff Wendling 48da8baab5 storj-sim: work with cockroach:// urls for satellite databases
for storj-sim to work, we need to avoid schemas in cockroach urls
so we have storj-sim create namespaced databases instead of schemas
and we have the migrate command create the database in the same way
that it would create a schema for postgres. then it works!

a follow up commit will move the creation of the database/schemas
into storj-sim's setup step so that we can avoid doing these icky
creations during normal migration calls. it will also make the
pointerdb have an explicit call to migrate instead of just doing
it every time it's opened.

Change-Id: If69ef5cb96b6866b0438c761bd445afb3597ae5f
2019-12-09 23:44:00 +00:00

38 lines
950 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package schema
import (
"database/sql"
"fmt"
"github.com/lib/pq"
"github.com/zeebo/errs"
)
// PrepareDB creates the pathdata tables if they don't already exist.
func PrepareDB(db *sql.DB) (err error) {
var dbName string
if err := db.QueryRow(`SELECT current_database();`).Scan(&dbName); err != nil {
return errs.Wrap(err)
}
// Note: the buckets table is unused. It exists here to ease importing
// backups from postgres. Similarly, the bucket column in pathdata is
// also unused and exists to ease imports.
_, err = db.Exec(fmt.Sprintf(`
CREATE DATABASE IF NOT EXISTS %s;
CREATE TABLE IF NOT EXISTS buckets (
bucketname BYTES PRIMARY KEY,
delim INT8 NOT NULL
);
CREATE TABLE IF NOT EXISTS pathdata (
fullpath BYTEA PRIMARY KEY,
metadata BYTEA NOT NULL,
bucket BYTEA
);
`, pq.QuoteIdentifier(dbName)))
return errs.Wrap(err)
}