Skip to content

Commit

Permalink
Additional tests for inline-comment
Browse files Browse the repository at this point in the history
  • Loading branch information
jtwatson committed Nov 29, 2024
1 parent be12225 commit c8e5bac
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
4 changes: 2 additions & 2 deletions database/spanner/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ func statementGroups(migr []byte) (groups []*statementGroup, err error) {
continue
}

if len(lex.Token.Comments) > 0 {
// preserve newline where comments are removed
if len(lex.Token.Comments) > 0 && strings.HasPrefix(lex.Token.Comments[0].Raw, "--") {
// standard comment Token consumes a \n, so we need to add it back
if _, err := stmt.WriteString("\n"); err != nil {
return nil, err
}
Expand Down
54 changes: 44 additions & 10 deletions database/spanner/spanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY (id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY (id)",
},
},
},
Expand All @@ -143,7 +143,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY (id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY (id)",
},
},
},
Expand All @@ -160,7 +160,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY(id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
Expand All @@ -178,7 +178,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY(id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
Expand All @@ -197,25 +197,59 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY(id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
},
},
{
name: "multi statement, no trailing semicolon, inline comment",
name: "multi statement, no trailing semicolon, end-of-line comment",
// From https://github.com/mattes/migrate/pull/281
multiStatement: `CREATE TABLE table_name (
id STRING(255) NOT NULL, -- inline comment
id STRING(255) NOT NULL, -- end-of-line comment
) PRIMARY KEY(id);
CREATE INDEX table_name_id_idx ON table_name (id)`,
expected: []*statementGroup{
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n ) PRIMARY KEY(id)",
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
},
},
{
name: "multi statement, inline comment",
multiStatement: `CREATE TABLE table_name (
id STRING(255) NOT NULL, /* inline comment */
) PRIMARY KEY(id);
CREATE INDEX table_name_id_idx ON table_name (id);`,
expected: []*statementGroup{
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
},
},
{
name: "multi statement, inline comment inside DML",
multiStatement: `CREATE TABLE table_name (
id STRING(255 /* inline comment */) NOT NULL,
) PRIMARY KEY(id);
CREATE INDEX table_name_id_idx ON table_name (id);`,
expected: []*statementGroup{
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n\t\t\tid STRING(255) NOT NULL,\n\t\t) PRIMARY KEY(id)",
"CREATE INDEX table_name_id_idx ON table_name (id)",
},
},
Expand All @@ -229,7 +263,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"ALTER TABLE users ALTER COLUMN created\n SET OPTIONS (allow_commit_timestamp=true)",
"ALTER TABLE users ALTER COLUMN created\n\t\t\tSET OPTIONS (allow_commit_timestamp=true)",
},
},
},
Expand All @@ -244,7 +278,7 @@ func Test_statementGroups(t *testing.T) {
{
typ: statementTypeDDL,
stmts: []string{
"CREATE TABLE table_name (\n id STRING(255) NOT NULL,\n sum NUMERIC,\n ) PRIMARY KEY (id)",
"CREATE TABLE table_name (\n\t\t\t\tid STRING(255) NOT NULL,\n\t\t\t\tsum NUMERIC,\n\t\t\t) PRIMARY KEY (id)",
},
},
},
Expand Down

0 comments on commit c8e5bac

Please sign in to comment.