storj/storage/cockroachkv/schema/migrate.go
Egon Elbre 1279eeae39 private/tagsql,storage: fixes to context cancellation
Replace all the remaining uses of sql.DB with tagsql.DB to
fix issues with context cancellation.

Introduce tagsql.Open which helps to get rid of all tagsql.Wrap-s.
Use tagsql in cockroachkv and postgreskv.

Change-Id: I8946d203341cb85a25976896fc7881e1f704e779
2020-01-20 15:44:39 +02:00

40 lines
1012 B
Go

// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package schema
import (
"context"
"fmt"
"github.com/lib/pq"
"github.com/zeebo/errs"
"storj.io/storj/private/tagsql"
)
// PrepareDB creates the pathdata tables if they don't already exist.
func PrepareDB(ctx context.Context, db tagsql.DB) (err error) {
var dbName string
if err := db.QueryRow(ctx, `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(ctx, 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)
}