internal/postgres: stop writing to units.path

In a future CL, we will be deprecating units.path.

Change-Id: I17f8a3b1499b906f36567271c652a65b3bebfc57
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/282116
Trust: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go
index 03c9b55..e26c251 100644
--- a/internal/postgres/insert_module.go
+++ b/internal/postgres/insert_module.go
@@ -281,11 +281,12 @@
 	}
 
 	var (
-		unitValues    []interface{}
 		paths         []string
+		unitValues    []interface{}
 		pathToReadme  = map[string]*internal.Readme{}
 		pathToDoc     = map[string]*internal.Documentation{}
 		pathToImports = map[string][]string{}
+		pathIDToPath  = map[int]string{}
 	)
 	for _, u := range m.Units {
 		var licenseTypes, licensePaths []string
@@ -305,9 +306,10 @@
 			}
 		}
 		v1path := internal.V1Path(u.Path, m.ModulePath)
+		pathID := pathToID[u.Path]
+		pathIDToPath[pathID] = u.Path
 		unitValues = append(unitValues,
-			u.Path,
-			pathToID[u.Path],
+			pathID,
 			moduleID,
 			pathToID[v1path],
 			v1path,
@@ -328,10 +330,14 @@
 		}
 		paths = append(paths, u.Path)
 	}
-	pathToUnitID, err := insertUnits(ctx, db, unitValues)
+	pathIDToUnitID, err := insertUnits(ctx, db, unitValues)
 	if err != nil {
 		return err
 	}
+	pathToUnitID := map[string]int{}
+	for pid, uid := range pathIDToUnitID {
+		pathToUnitID[pathIDToPath[pid]] = uid
+	}
 	if err := insertReadmes(ctx, db, paths, pathToUnitID, pathToReadme); err != nil {
 		return err
 	}
@@ -393,12 +399,11 @@
 	return pathToID, nil
 }
 
-func insertUnits(ctx context.Context, db *database.DB, unitValues []interface{}) (pathToUnitID map[string]int, err error) {
+func insertUnits(ctx context.Context, db *database.DB, unitValues []interface{}) (pathIDToUnitID map[int]int, err error) {
 	defer derrors.Wrap(&err, "insertUnits")
 
 	// Insert data into the units table.
 	unitCols := []string{
-		"path",
 		"path_id",
 		"module_id",
 		"v1path_id",
@@ -408,25 +413,22 @@
 		"license_paths",
 		"redistributable",
 	}
-	uniqueUnitCols := []string{"path", "module_id"}
-	returningUnitCols := []string{"id", "path"}
+	uniqueUnitCols := []string{"path_id", "module_id"}
+	returningUnitCols := []string{"id", "path_id"}
 
-	pathToUnitID = map[string]int{}
+	pathIDToUnitID = map[int]int{}
 	if err := db.BulkUpsertReturning(ctx, "units", unitCols, unitValues,
 		uniqueUnitCols, returningUnitCols, func(rows *sql.Rows) error {
-			var (
-				unitID int
-				path   string
-			)
-			if err := rows.Scan(&unitID, &path); err != nil {
+			var pathID, unitID int
+			if err := rows.Scan(&unitID, &pathID); err != nil {
 				return err
 			}
-			pathToUnitID[path] = unitID
+			pathIDToUnitID[pathID] = unitID
 			return nil
 		}); err != nil {
 		return nil, err
 	}
-	return pathToUnitID, nil
+	return pathIDToUnitID, nil
 }
 
 func insertDoc(ctx context.Context, db *database.DB,