Skip to content

Commit 5c0101b

Browse files
Fix backfill on tables with case sensitive names (#831)
Ensure that the table name is correctly quoted when retrieving table rows counts as part of the backfill operation. Fixes #826
1 parent db2a61f commit 5c0101b

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

pkg/backfill/backfill.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func getRowCount(ctx context.Context, conn db.DB, tableName string) (int64, erro
122122
}
123123

124124
// If the estimate is zero, fall back to full count
125-
rows, err = conn.QueryContext(ctx, fmt.Sprintf(`SELECT count(*) from %s`, tableName))
125+
rows, err = conn.QueryContext(ctx, fmt.Sprintf(`SELECT count(*) from %s`, pq.QuoteIdentifier(tableName)))
126126
if err != nil {
127127
return 0, fmt.Errorf("getting row count for %q: %w", tableName, err)
128128
}

pkg/migrations/op_change_type_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,54 @@ func TestChangeColumnType(t *testing.T) {
649649
ColumnMustHaveComment(t, db, schema, "users", "username", "the name of the user")
650650
},
651651
},
652+
{
653+
name: "can change the type of a column on a table with a case-sensitive name",
654+
migrations: []migrations.Migration{
655+
{
656+
Name: "01_add_table",
657+
Operations: migrations.Operations{
658+
&migrations.OpCreateTable{
659+
Name: "Users",
660+
Columns: []migrations.Column{
661+
{
662+
Name: "id",
663+
Type: "serial",
664+
Pk: true,
665+
},
666+
{
667+
Name: "name",
668+
Type: "varchar(255)",
669+
},
670+
},
671+
},
672+
},
673+
},
674+
{
675+
Name: "02_change_type",
676+
Operations: migrations.Operations{
677+
&migrations.OpAlterColumn{
678+
Table: "Users",
679+
Column: "name",
680+
Type: ptr("text"),
681+
Up: "name",
682+
Down: "name",
683+
},
684+
},
685+
},
686+
},
687+
afterStart: func(t *testing.T, db *sql.DB, schema string) {
688+
// The new column has the expected type
689+
ColumnMustHaveType(t, db, schema, "Users", migrations.TemporaryName("name"), "text")
690+
},
691+
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
692+
// The table has been cleaned up
693+
TableMustBeCleanedUp(t, db, schema, "Users", "name")
694+
},
695+
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
696+
// The new column has the expected type
697+
ColumnMustHaveType(t, db, schema, "Users", "name", "text")
698+
},
699+
},
652700
})
653701
}
654702

pkg/migrations/op_common_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ func triggerExists(t *testing.T, db *sql.DB, schema, table, trigger string) bool
575575
WHERE tgrelid = $1::regclass
576576
AND tgname = $2
577577
)`,
578-
fmt.Sprintf("%s.%s", schema, table), trigger).Scan(&exists)
578+
fmt.Sprintf("%s.%s", pq.QuoteIdentifier(schema), pq.QuoteIdentifier(table)), trigger).Scan(&exists)
579579
if err != nil {
580580
t.Fatal(err)
581581
}

0 commit comments

Comments
 (0)