Skip to content

Commit e63ee61

Browse files
committed
db: fix export for import into fresh database
Move the cover table before series in the creation and export order since series.cover_letter_id references cover.id. Clear the seeded state and tag rows before inserting exported data so that "pw db import" does not hit duplicate key violations on a freshly synced database. Fix bool formatting: use SQL true/false literals instead of integer 1/0. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 064059c commit e63ee61

3 files changed

Lines changed: 15 additions & 7 deletions

File tree

pkg/db/export.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ var exportTables = []exportTable{
3636
{dstName: "delegation_rule", srcName: "patchwork_delegationrule"},
3737
{dstName: "person", srcName: "patchwork_person"},
3838
{dstName: "patch_relation", srcName: "patchwork_patchrelation"},
39+
{dstName: "cover", srcName: "patchwork_cover"},
3940
{dstName: "series", srcName: "patchwork_series"},
4041
{dstName: "series_reference", srcName: "patchwork_seriesreference"},
4142
{dstName: "series_metadata", srcName: "patchwork_seriesmetadata", optional: true},
4243
{dstName: "series_dependencies", srcName: "patchwork_series_dependencies", optional: true},
43-
{dstName: "cover", srcName: "patchwork_cover"},
4444
{dstName: "patch", srcName: "patchwork_patch"},
4545
{dstName: "patch_tag", srcName: "patchwork_patchtag"},
4646
{dstName: "patch_comment", srcName: "patchwork_patchcomment"},
@@ -66,6 +66,11 @@ func Export(ctx context.Context, database *bun.DB, w io.Writer) error {
6666

6767
fmt.Fprint(w, "BEGIN;\n")
6868

69+
// Clear seeded data so the imported rows don't conflict with
70+
// defaults inserted by "pw db sync".
71+
fmt.Fprint(w, "DELETE FROM tag;\n")
72+
fmt.Fprint(w, "DELETE FROM state;\n")
73+
6974
for _, t := range exportTables {
7075
if t.optional && !tableExists(ctx, database, t.srcName) {
7176
continue
@@ -327,9 +332,9 @@ func formatSQLValue(v any) string {
327332
switch val := v.(type) {
328333
case bool:
329334
if val {
330-
return "1"
335+
return "true"
331336
}
332-
return "0"
337+
return "false"
333338
case int64:
334339
return fmt.Sprintf("%d", val)
335340
case float64:
@@ -360,9 +365,9 @@ func formatSQLValue(v any) string {
360365
return "NULL"
361366
}
362367
if val.Bool {
363-
return "1"
368+
return "true"
364369
}
365-
return "0"
370+
return "false"
366371
case sql.NullTime:
367372
if !val.Valid {
368373
return "NULL"

pkg/db/migrations/0001_initial_schema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,11 +305,11 @@ var tables0001 = []any{
305305
(*delegationRule0001)(nil),
306306
(*person0001)(nil),
307307
(*patchRelation0001)(nil),
308+
(*cover0001)(nil),
308309
(*series0001)(nil),
309310
(*seriesReference0001)(nil),
310311
(*seriesMetadata0001)(nil),
311312
(*seriesDependencies0001)(nil),
312-
(*cover0001)(nil),
313313
(*patch0001)(nil),
314314
(*patchTag0001)(nil),
315315
(*patchComment0001)(nil),

pkg/db/schema.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"strings"
1313

1414
"github.com/uptrace/bun"
15+
16+
"github.com/getpatchwork/patchwork/pkg/log"
1517
)
1618

1719
// Schema is declared from struct tags:
@@ -161,11 +163,11 @@ func CreateSchema(ctx context.Context, database bun.IDB) error {
161163
(*DelegationRule)(nil),
162164
(*Person)(nil),
163165
(*PatchRelation)(nil),
166+
(*Cover)(nil),
164167
(*Series)(nil),
165168
(*SeriesReference)(nil),
166169
(*SeriesMetadata)(nil),
167170
(*SeriesDependencies)(nil),
168-
(*Cover)(nil),
169171
(*Patch)(nil),
170172
(*PatchTag)(nil),
171173
(*PatchComment)(nil),
@@ -188,6 +190,7 @@ func CreateSchemaFrom(ctx context.Context, database bun.IDB, schema []any) error
188190
for _, fk := range fks {
189191
fk.add(q)
190192
}
193+
log.Noticef("creating table %q", q.GetTableName())
191194
if _, err := q.Exec(ctx); err != nil {
192195
return fmt.Errorf("create table %s: %w", q.GetTableName(), err)
193196
}

0 commit comments

Comments
 (0)