Skip to content

Commit

Permalink
sql: improved diff tool for any namespace
Browse files Browse the repository at this point in the history
Previously, diff tool worked only for pg_catalog
This was inadequate because it can be used for information_schema as well
To address this, this patch takes the namespace as parameter to compare a
different database

Release note: None

Fixes cockroachdb#58037

Release justification: non-production code changes
  • Loading branch information
MiguelNovelo committed Mar 1, 2021
1 parent 3fe1992 commit 6f89411
Show file tree
Hide file tree
Showing 6 changed files with 5,920 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")

go_library(
name = "generate-pg-catalog_lib",
name = "generate-postgres-metadata-tables_lib",
srcs = ["main.go"],
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/generate-pg-catalog",
importpath = "github.com/cockroachdb/cockroach/pkg/cmd/generate-postgres-metadata-tables",
visibility = ["//visibility:private"],
deps = [
"//pkg/sql",
Expand All @@ -12,7 +12,7 @@ go_library(
)

go_binary(
name = "generate-pg-catalog",
embed = [":generate-pg-catalog_lib"],
name = "generate-postgres-metadata-tables",
embed = [":generate-postgres-metadata-tables_lib"],
visibility = ["//visibility:public"],
)
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import (
const getServerVersion = `SELECT current_setting('server_version');`

var (
postgresAddr = flag.String("addr", "localhost:5432", "Postgres server address")
postgresUser = flag.String("user", "postgres", "Postgres user")
postgresAddr = flag.String("addr", "localhost:5432", "Postgres server address")
postgresUser = flag.String("user", "postgres", "Postgres user")
postgresSchema = flag.String("catalog", "pg_catalog", "Catalog or namespace, default: pg_catalog")
)

func main() {
Expand All @@ -62,7 +63,7 @@ func main() {
}

func describePgCatalog(conn *pgx.Conn) *pgx.Rows {
rows, err := conn.Query(sql.GetPGCatalogSQL)
rows, err := conn.Query(sql.GetPGCatalogSQL, *postgresSchema)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/pg_catalog_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const GetPGCatalogSQL = `
JOIN pg_attribute a ON a.attrelid = c.oid
JOIN pg_type t ON t.oid = a.atttypid
JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE n.nspname = 'pg_catalog'
WHERE n.nspname = $1
AND a.attnum > 0
ORDER BY 1, 2;
`
Expand Down
19 changes: 11 additions & 8 deletions pkg/sql/pg_catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ import (

// Test data files
const (
pgCatalogDump = "pg_catalog_tables.json" // PostgreSQL pg_catalog schema
expectedDiffs = "pg_catalog_test_expected_diffs.json" // Contains expected difference between postgres and cockroach
testdata = "testdata" // testdata directory
catalogDump = "%s_tables.json" // PostgreSQL pg_catalog schema
expectedDiffs = "%s_test_expected_diffs.json" // Contains expected difference between postgres and cockroach
testdata = "testdata" // testdata directory
)

// When running test with -rewrite-diffs test will pass and re-create pg_catalog_test-diffs.json
var rewriteFlag = flag.Bool("rewrite-diffs", false, "This will re-create the expected diffs file")
var (
rewriteFlag = flag.Bool("rewrite-diffs", false, "This will re-create the expected diffs file")
catalogName = flag.String("catalog", "pg_catalog", "Catalog or namespace, default: pg_catalog")
)

// summary will keep accountability for any unexpected difference and report it in the log
type summary struct {
Expand All @@ -79,7 +82,7 @@ func (sum *summary) report(t *testing.T) {
// loadTestData retrieves the pg_catalog from the dumpfile generated from Postgres
func loadTestData(t testing.TB) PGCatalogTables {
var pgCatalogFile PGCatalogFile
testdataFile := filepath.Join(testdata, pgCatalogDump)
testdataFile := filepath.Join(testdata, fmt.Sprintf(catalogDump, *catalogName))
f, err := os.Open(testdataFile)
if err != nil {
t.Fatal(err)
Expand All @@ -105,7 +108,7 @@ func loadCockroachPgCatalog(t testing.TB) PGCatalogTables {
s, db, _ := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
sqlRunner := sqlutils.MakeSQLRunner(db)
rows := sqlRunner.Query(t, GetPGCatalogSQL)
rows := sqlRunner.Query(t, GetPGCatalogSQL, *catalogName)
defer rows.Close()

for rows.Next() {
Expand All @@ -128,7 +131,7 @@ func loadExpectedDiffs(t *testing.T) (diffs PGCatalogTables) {
return
}

diffFile := filepath.Join(testdata, expectedDiffs)
diffFile := filepath.Join(testdata, fmt.Sprintf(expectedDiffs, *catalogName))
if _, err := os.Stat(diffFile); err != nil {
if oserror.IsNotExist(err) {
// File does not exists it means diffs are not expected
Expand Down Expand Up @@ -217,5 +220,5 @@ func TestPGCatalog(t *testing.T) {
}

sum.report(t)
rewriteDiffs(t, diffs, filepath.Join(testdata, expectedDiffs))
rewriteDiffs(t, diffs, filepath.Join(testdata, fmt.Sprintf(expectedDiffs, *catalogName)))
}
Loading

0 comments on commit 6f89411

Please sign in to comment.