diff options
| author | Jonathan Amsterdam <jba@google.com> | 2020-11-01 06:28:53 -0500 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2020-11-03 18:25:21 +0000 |
| commit | 00097bcc4757c2db30878dbf613532d257c0ebb8 (patch) | |
| tree | ad6900934c6d9c35d00a7f29743890b886f4061b /internal/postgres/insert_module.go | |
| parent | fa9ffc5361016571af0189203003f4be630f2ca2 (diff) | |
| download | go-x-pkgsite-00097bcc4757c2db30878dbf613532d257c0ebb8.tar.xz | |
internal/postgres: determine path/unit_id column dynamically
Three tables still have a path_id column that is a foreign key for the
"units" (previously "paths") table. It is not possible to rename those
columns while keeping the code working: we would have to create a new
column, change the code, then delete the old column, all the while
keeping the columns in sync.
This CL suggests a simpler approach: the code figures out the name of
the column on its own. Then we can simply do one migration to rename
the all the path_id to unit_id columns, and the code will continue to
work.
The code becomes uglier, but it is temporary: after we're done, we'll
put it back the way it was, with literal column names.
Change-Id: Ia4f621e1c0112d378cf63811311a430982c1cb5f
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/267238
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'internal/postgres/insert_module.go')
| -rw-r--r-- | internal/postgres/insert_module.go | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go index fcb38933..7b4953fb 100644 --- a/internal/postgres/insert_module.go +++ b/internal/postgres/insert_module.go @@ -90,7 +90,7 @@ func (db *DB) saveModule(ctx context.Context, m *internal.Module) (err error) { } logMemory(ctx, "after insertLicenses") - if err := insertUnits(ctx, tx, m, moduleID); err != nil { + if err := db.insertUnits(ctx, tx, m, moduleID); err != nil { return err } logMemory(ctx, "after insertUnits") @@ -259,7 +259,7 @@ func insertImportsUnique(ctx context.Context, tx *database.DB, m *internal.Modul return tx.BulkUpsert(ctx, "imports_unique", cols, values, cols) } -func insertUnits(ctx context.Context, db *database.DB, m *internal.Module, moduleID int) (err error) { +func (pdb *DB) insertUnits(ctx context.Context, db *database.DB, m *internal.Module, moduleID int) (err error) { defer derrors.Wrap(&err, "insertUnits(ctx, tx, %q, %q)", m.ModulePath, m.Version) ctx, span := trace.StartSpan(ctx, "insertUnits") defer span.End() @@ -378,8 +378,9 @@ func insertUnits(ctx context.Context, db *database.DB, m *internal.Module, modul id := pathToID[path] readmeValues = append(readmeValues, id, readme.Filepath, readmeContents) } - readmeCols := []string{"path_id", "file_path", "contents"} - if err := db.BulkUpsert(ctx, "readmes", readmeCols, readmeValues, []string{"path_id"}); err != nil { + rcol := pdb.unitIDColumn(ctx, "readmes") + readmeCols := []string{rcol, "file_path", "contents"} + if err := db.BulkUpsert(ctx, "readmes", readmeCols, readmeValues, []string{rcol}); err != nil { return err } } @@ -395,7 +396,7 @@ func insertUnits(ctx context.Context, db *database.DB, m *internal.Module, modul id := pathToID[path] docValues = append(docValues, id, doc.GOOS, doc.GOARCH, doc.Synopsis, makeValidUnicode(doc.HTML.String()), doc.Source) } - uniqueCols := []string{"path_id", "goos", "goarch"} + uniqueCols := []string{pdb.unitIDColumn(ctx, "documentation"), "goos", "goarch"} docCols := append(uniqueCols, "synopsis", "html", "source") if err := db.BulkUpsert(ctx, "documentation", docCols, docValues, uniqueCols); err != nil { return err @@ -414,7 +415,7 @@ func insertUnits(ctx context.Context, db *database.DB, m *internal.Module, modul importValues = append(importValues, id, toPath) } } - importCols := []string{"path_id", "to_path"} + importCols := []string{pdb.unitIDColumn(ctx, "package_imports"), "to_path"} return db.BulkUpsert(ctx, "package_imports", importCols, importValues, importCols) } |
