Skip to content

Commit

Permalink
sql: code generation for missing pg catalog tables
Browse files Browse the repository at this point in the history
Previously, programmer had to add missing table manually,
This was inadequate because postgress added a lot of tables and
manual process can lead to coding mistakes or gaps
To address this, diff tool now have the hability to generate the
missing code

Release note: None

Fixes #58001
  • Loading branch information
MiguelNovelo committed Feb 5, 2021
1 parent 8297ccd commit 962b06c
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 4 deletions.
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,7 @@ go_test(
"@com_github_jackc_pgx//pgtype",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_lib_pq//:pq",
"@com_github_lib_pq//oid",
"@com_github_pmezard_go_difflib//difflib",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
Expand Down
27 changes: 27 additions & 0 deletions pkg/sql/pg_catalog_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"encoding/json"
"io"
"os"

"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/lib/pq/oid"
)

// GetPGCatalogSQL is a query uses udt_name::regtype instead of data_type column because
Expand Down Expand Up @@ -185,6 +188,30 @@ func (p PGCatalogTables) rewriteDiffs(diffFile string) error {
return nil
}

// getNotImplementedTables retrieves tables that are not yet part of crdb
func (p PGCatalogTables) getNotImplementedTables(source PGCatalogTables) PGCatalogTables {
notImplemented := make(PGCatalogTables)
for tableName := range p {
if len(p[tableName]) == 0 && len(source[tableName].getNotImplementedTypes()) == 0 {
notImplemented[tableName] = source[tableName]
}
}
return notImplemented
}

//AreAllTypesImplemented verifies that all the types are implemented in cockroach db
func (c PGCatalogColumns) getNotImplementedTypes() map[oid.Oid]string {
notImplemented := make(map[oid.Oid]string)
for _, column := range c {
typeOid := oid.Oid(column.Oid)
if _, ok := types.OidToType[typeOid]; !ok || typeOid == oid.T_anyarray {
notImplemented[typeOid] = column.DataType
}
}

return notImplemented
}

// Save have the purpose of storing all the data retrieved from postgres and useful information as postgres version
func (f *PGCatalogFile) Save(writer io.Writer) {
byteArray, err := json.MarshalIndent(f, "", " ")
Expand Down
Loading

0 comments on commit 962b06c

Please sign in to comment.