storj/storage/cockroachkv/schema/migrate.go
Egon Elbre b9740f0c0a storage/cockroachkv: add ctx argument
Change-Id: Ib6c29f44722b0354afcd499a0e567f04aef7eb28
2020-01-13 15:57:47 +02:00

39 lines
982 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package schema
import (
"context"
"database/sql"
"fmt"
"github.com/lib/pq"
"github.com/zeebo/errs"
)
// PrepareDB creates the pathdata tables if they don't already exist.
func PrepareDB(ctx context.Context, 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)
}