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
  • Loading branch information
MiguelNovelo committed Feb 9, 2021
1 parent e82b7eb commit b27e131
Show file tree
Hide file tree
Showing 5 changed files with 5,916 additions and 12 deletions.
7 changes: 4 additions & 3 deletions pkg/cmd/generate-pg-catalog/main.go
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")
postgresCatalog = 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, *postgresCatalog)
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
pgCatalogDump = "%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")
postgresCatalog = 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(pgCatalogDump, *postgresCatalog))
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, *postgresCatalog)
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, *postgresCatalog))
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, *postgresCatalog)))
}
Loading

0 comments on commit b27e131

Please sign in to comment.