Skip to content

Commit

Permalink
Added more unit test for testing pg parser Deparsing API
Browse files Browse the repository at this point in the history
  • Loading branch information
sanyamsinghal committed Mar 4, 2025
1 parent 7e0bfa4 commit 6823d83
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions yb-voyager/src/query/sqltransformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (t *Transformer) MergeConstraints(stmts []*pg_query.RawStmt) ([]*pg_query.R
Otherwise, add it to the result slice
*/
alterTableCmdType := alterTableCmd.GetSubtype()
log.Infof("alterTableCmdType: %v", *alterTableCmdType.Enum())
if *alterTableCmdType.Enum() != pg_query.AlterTableType_AT_AddConstraint {
// If the ALTER TABLE stmt is not an ADD CONSTRAINT stmt, then need to append it to the result slice
result = append(result, stmt)
Expand All @@ -146,6 +147,7 @@ func (t *Transformer) MergeConstraints(stmts []*pg_query.RawStmt) ([]*pg_query.R
if !ok {
return nil, fmt.Errorf("CREATE TABLE stmt not found for table %v", objectName)
}
log.Infof("merging constraint %v into CREATE TABLE for object %v", constrType, objectName)
createStmt.Stmt.GetCreateStmt().TableElts = append(createStmt.Stmt.GetCreateStmt().TableElts, alterTableCmd.GetDef())
}

Expand Down
46 changes: 42 additions & 4 deletions yb-voyager/src/query/sqltransformer/transformer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ limitations under the License.
package sqltransformer

import (
"os"
"testing"

log "github.com/sirupsen/logrus"
"github.com/yugabyte/yb-voyager/yb-voyager/src/query/queryparser"
testutils "github.com/yugabyte/yb-voyager/yb-voyager/test/utils"
)
Expand All @@ -35,6 +37,14 @@ import (
9. [Extra] Exclude constraint (omission of USING btree by parser)
*/

func TestMain(m *testing.M) {
// set log level to warn
log.SetLevel(log.WarnLevel)

exitCode := m.Run()
os.Exit(exitCode)
}

func TestMergeConstraints_Basic(t *testing.T) {
sqlFileContent := `
CREATE TABLE test_table1 (
Expand All @@ -49,14 +59,18 @@ func TestMergeConstraints_Basic(t *testing.T) {
);
ALTER TABLE test_table1 ADD CONSTRAINT test_table_pk PRIMARY KEY (id);
ALTER TABLE test_table2 ADD CONSTRAINT test_table_fk FOREIGN KEY (id) REFERENCES test_table1 (id);
ALTER TABLE test_table2 ADD CONSTRAINT test_table_uk UNIQUE (email);
-- Skip NOT VALID merging constraint
ALTER TABLE test_table1 ADD CONSTRAINT check_name CHECK (name <> '') NOT VALID;
ALTER TABLE test_table2 ADD CONSTRAINT test_table2_fk FOREIGN KEY (id) REFERENCES test_table1 (id);
ALTER TABLE test_table2 ADD CONSTRAINT test_table2_uk UNIQUE (email);
`

expectedSqls := []string{
`CREATE TABLE test_table1 (id int, name varchar(255), CONSTRAINT test_table_pk PRIMARY KEY (id));`,
`CREATE TABLE test_table2 (id int, name varchar(255), email varchar(255), CONSTRAINT test_table_uk UNIQUE (email));`,
`ALTER TABLE test_table2 ADD CONSTRAINT test_table_fk FOREIGN KEY (id) REFERENCES test_table1 (id);`,
`ALTER TABLE test_table1 ADD CONSTRAINT check_name CHECK (name <> '') NOT VALID;`,
`CREATE TABLE test_table2 (id int, name varchar(255), email varchar(255), CONSTRAINT test_table2_uk UNIQUE (email));`,
`ALTER TABLE test_table2 ADD CONSTRAINT test_table2_fk FOREIGN KEY (id) REFERENCES test_table1 (id);`,
}

tempFilePath, err := testutils.CreateTempFile("/tmp", sqlFileContent, "sql")
Expand Down Expand Up @@ -458,3 +472,27 @@ func Test_RemovalOfDefaultValuesByParser(t *testing.T) {

testutils.AssertEqualStringSlices(t, expectedSqls, finalSqlStmts)
}

// Tests cases where deparse() API deviates from expected SQL or a corner cases which is good to test.
func Test_DeparsingAPI(t *testing.T) {
sqlFileContent := `
CREATE TABLE my_table (created_at TIMESTAMPTZ NOT NULL DEFAULT (CURRENT_TIMESTAMP AT TIME ZONE 'UTC'));
`

expectedSqls := []string{
// expected: CREATE TABLE my_table (created_at timestamptz NOT NULL DEFAULT current_timestamp AT TIME ZONE 'UTC');
// but below is what parser actual returns due to Parser bug: https://github.com/pganalyze/pg_query_go/issues/126
`CREATE TABLE my_table (created_at timestamptz NOT NULL DEFAULT current_timestamp AT TIME ZONE 'UTC');`,
}

tempFilePath, err := testutils.CreateTempFile("/tmp", sqlFileContent, "sql")
testutils.FatalIfError(t, err)

stmts, err := queryparser.ParseSqlFile(tempFilePath)
testutils.FatalIfError(t, err)

finalSqlStmts, err := queryparser.DeparseRawStmts(stmts)
testutils.FatalIfError(t, err)

testutils.AssertEqualStringSlices(t, expectedSqls, finalSqlStmts)
}

0 comments on commit 6823d83

Please sign in to comment.