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

Release justification: non-production code changes
  • Loading branch information
MiguelNovelo committed Mar 10, 2021
1 parent 7a8e7fb commit 25aeb98
Show file tree
Hide file tree
Showing 4 changed files with 499 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,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
30 changes: 29 additions & 1 deletion pkg/sql/pg_metadata_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"
)

// GetPGMetadataSQL is a query uses udt_name::regtype instead of data_type column because
Expand Down Expand Up @@ -187,7 +190,8 @@ func (p PGMetadataTables) rewriteDiffs(diffFile string) error {
return nil
}

// Save have the purpose of storing all the data retrieved from postgres and useful information as postgres version
// Save have the purpose of storing all the data retrieved from postgres and
// useful information as postgres version.
func (f *PGMetadataFile) Save(writer io.Writer) {
byteArray, err := json.MarshalIndent(f, "", " ")
if err != nil {
Expand All @@ -198,3 +202,27 @@ func (f *PGMetadataFile) Save(writer io.Writer) {
panic(err)
}
}

//getUnimplementedTables retrieves the tables that are not yet part of CRDB.
func (p PGMetadataTables) getUnimplementedTables(source PGMetadataTables) PGMetadataTables {
notImplemented := make(PGMetadataTables)
for tableName := range p {
if len(p[tableName]) == 0 && len(source[tableName].getUnimplementedTypes()) == 0 {
notImplemented[tableName] = source[tableName]
}
}
return notImplemented
}

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

return unimplemented
}
Loading

0 comments on commit 25aeb98

Please sign in to comment.