-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
obsservice: connect to the sink cluster and run migrations
This patch adds a --sink-pgurl flag to the obsservice for the connection string to the sink cluster. It uses it to run SQL schema migrations, of which there is a single dummy one. We're using https://github.com/pressly/goose for schema migrations; it can be used both as a cmdline binary, and as a library (we use it as a library), and it supports migrations written as either SQL files, or as a set of go functions (for the more involved ones, I guess). Release note: None
- Loading branch information
1 parent
9e47dc9
commit 726e978
Showing
13 changed files
with
701 additions
and
105 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "migrations", | ||
srcs = ["migrations.go"], | ||
embedsrcs = ["sqlmigrations/0001_init.sql"], | ||
importpath = "github.com/cockroachdb/cockroach/pkg/obsservice/obslib/migrations", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//pkg/util/log", | ||
"@com_github_jackc_pgx_v4//:pgx", | ||
"@com_github_jackc_pgx_v4//stdlib", | ||
"@com_github_pressly_goose_v3//:goose", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright 2022 The Cockroach Authors. | ||
// | ||
// 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 | ||
|
||
package migrations | ||
|
||
import ( | ||
"context" | ||
"embed" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/log" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/jackc/pgx/v4/stdlib" | ||
"github.com/pressly/goose/v3" | ||
) | ||
|
||
// sqlMigrations embeds all the .sql file containing migrations to be run by | ||
// Goose. | ||
//go:embed sqlmigrations/*.sql | ||
var sqlMigrations embed.FS | ||
|
||
// defaultSinkDBName is the name of the database to be used by default | ||
const defaultSinkDBName = "obsservice" | ||
|
||
// RunDBMigrations brings the SQL schema in the sink cluster up to date. | ||
// | ||
// sinkPGURL is the connection string for the sink cluster. If it includes a | ||
// database, that database will be used. If it doesn't, a default one will be | ||
// used. | ||
func RunDBMigrations(ctx context.Context, sinkPGURL string) error { | ||
connCfg, err := pgx.ParseConfig(sinkPGURL) | ||
if err != nil { | ||
return err | ||
} | ||
if connCfg.Database == "" { | ||
fmt.Printf("No database explicitly provided in --sink-pgurl. Using %q.\n", defaultSinkDBName) | ||
connCfg.Database = defaultSinkDBName | ||
} | ||
|
||
if log.V(2) { | ||
goose.SetVerbose(true) | ||
} | ||
goose.SetBaseFS(sqlMigrations) | ||
|
||
db := stdlib.OpenDB(*connCfg) | ||
// We need to create the database by hand; Goose expects the database to exist. | ||
if _, err := db.ExecContext(ctx, "CREATE DATABASE IF NOT EXISTS "+connCfg.Database); err != nil { | ||
return err | ||
} | ||
// goose will <db>.obs_admin.migrations to store the migration bookkeeping. | ||
if _, err := db.ExecContext(ctx, "CREATE schema IF NOT EXISTS obs_admin"); err != nil { | ||
return err | ||
} | ||
goose.SetTableName("obs_admin.migrations") | ||
if err := goose.SetDialect("postgres"); err != nil { | ||
return err | ||
} | ||
|
||
// Run the missing migrations, if any. | ||
if err := goose.Up(db, "sqlmigrations"); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
-- +goose Up | ||
CREATE TABLE events(); | ||
|
||
-- +goose Down | ||
DROP TABLE events; |
Submodule vendor
updated
78 files