2019-12-05 17:22:27 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package schema
|
|
|
|
|
|
|
|
import (
|
2020-01-13 13:57:47 +00:00
|
|
|
"context"
|
2019-12-05 20:42:12 +00:00
|
|
|
"fmt"
|
2019-12-05 17:22:27 +00:00
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
2020-01-19 14:41:23 +00:00
|
|
|
|
2020-06-28 04:56:29 +01:00
|
|
|
"storj.io/storj/private/dbutil/pgutil"
|
2020-01-19 14:41:23 +00:00
|
|
|
"storj.io/storj/private/tagsql"
|
2019-12-05 17:22:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// PrepareDB creates the pathdata tables if they don't already exist.
|
2020-01-19 14:41:23 +00:00
|
|
|
func PrepareDB(ctx context.Context, db tagsql.DB) (err error) {
|
2019-12-05 20:42:12 +00:00
|
|
|
var dbName string
|
2020-01-19 14:41:23 +00:00
|
|
|
if err := db.QueryRow(ctx, `SELECT current_database();`).Scan(&dbName); err != nil {
|
2019-12-05 20:42:12 +00:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-12-05 17:22:27 +00:00
|
|
|
// 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.
|
2020-01-19 14:41:23 +00:00
|
|
|
_, err = db.Exec(ctx, fmt.Sprintf(`
|
2019-12-05 20:42:12 +00:00
|
|
|
CREATE DATABASE IF NOT EXISTS %s;
|
2019-12-05 17:22:27 +00:00
|
|
|
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
|
|
|
|
);
|
2020-06-28 04:56:29 +01:00
|
|
|
`, pgutil.QuoteIdentifier(dbName)))
|
2019-12-05 17:22:27 +00:00
|
|
|
return errs.Wrap(err)
|
|
|
|
}
|