internal/fetch, internal/postgres: ingest package suffix

This CL parses the package path in order to fill the 'suffix'
column in the packages table. There is currently no need to
get the value back from the database, it will be used to
fetch all the versions of a particular package in a series.

Fixes b/129546260

Change-Id: I403220f0d3ad90be4c30b563f1fe38d86a2f31df
Reviewed-on: https://team-review.git.corp.google.com/c/golang/discovery/+/442297
Reviewed-by: Julie Qiu <julieqiu@google.com>
This commit is contained in:
Channing Kimble-Brown 2019-03-29 14:46:53 -04:00 коммит произвёл Julie Qiu
Родитель df4cb83df4
Коммит 58c05abaa7
3 изменённых файлов: 7 добавлений и 5 удалений

Просмотреть файл

@ -46,6 +46,7 @@ type Package struct {
Path string
Name string
Synopsis string
Suffix string // if my.module/v2/A/B is the path, A/B is the package suffix
Version *Version
}

Просмотреть файл

@ -67,9 +67,9 @@ func parseVersion(version string) (internal.VersionType, error) {
prerelease = prerelease[1:] // remove starting dash
// if prerelease looks like a commit then return VersionTypePseudo
matched, err := regexp.MatchString(`[0-9]{14}-[0-9a-z]{12}`, prerelease)
matched, err := regexp.MatchString(`^[0-9]{14}-[0-9a-z]{12}$`, prerelease)
if err != nil {
return "", fmt.Errorf("regexp.MatchString(`[0-9]{14}-[0-9a-z]{12}`, %v): %v", prerelease, err)
return "", fmt.Errorf("regexp.MatchString(`^[0-9]{14}-[0-9a-z]{12}$`, %v): %v", prerelease, err)
}
if matched {
@ -229,6 +229,7 @@ func extractPackagesFromZip(module, version string, r *zip.Reader) ([]*internal.
Name: p.Name,
Path: p.PkgPath,
Synopsis: doc.Synopsis(d.Doc),
Suffix: strings.TrimPrefix(strings.TrimPrefix(p.PkgPath, module), "/"),
})
}
return packages, nil

Просмотреть файл

@ -496,15 +496,15 @@ func (db *DB) InsertVersion(version *internal.Version) error {
}
stmt, err := tx.Prepare(
`INSERT INTO packages (path, synopsis, name, version, module_path, version_type)
VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT DO NOTHING`)
`INSERT INTO packages (path, synopsis, name, version, module_path, version_type, suffix)
VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT DO NOTHING`)
if err != nil {
return fmt.Errorf("error preparing package stmt: %v", err)
}
defer stmt.Close()
for _, p := range version.Packages {
if _, err = stmt.Exec(p.Path, p.Synopsis, p.Name, version.Version, version.Module.Path, version.VersionType.String()); err != nil {
if _, err = stmt.Exec(p.Path, p.Synopsis, p.Name, version.Version, version.Module.Path, version.VersionType.String(), p.Suffix); err != nil {
return fmt.Errorf("error inserting package: %v", err)
}
}