remove the legacy migration (#18754)

Delete the legacy migration and artifact abstration code for v2.0

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2023-05-30 15:17:24 +08:00 committed by GitHub
parent b7b0e43a21
commit 5c42bc10ec
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 3 additions and 174 deletions

View File

@ -57,7 +57,7 @@ func main() {
if err := dao.InitDatabase(db); err != nil {
log.Fatalf("failed to initialize database: %v", err)
}
if err := migration.MigrateDB(db); err != nil {
if err := migration.Migrate(db); err != nil {
log.Fatalf("failed to migrate DB: %v", err)
}
log.Info("Migration done. The data schema in DB is now update to date.")

View File

@ -1,87 +0,0 @@
// Copyright Project Harbor Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package migration
import (
"context"
art "github.com/goharbor/harbor/src/controller/artifact"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/q"
"github.com/goharbor/harbor/src/pkg"
"github.com/goharbor/harbor/src/pkg/artifact"
)
func abstractArtData(ctx context.Context) error {
abstractor := art.NewAbstractor()
pros, err := pkg.ProjectMgr.List(ctx, nil)
if err != nil {
return err
}
for _, pro := range pros {
repos, err := pkg.RepositoryMgr.List(ctx, &q.Query{
Keywords: map[string]interface{}{
"ProjectID": pro.ProjectID,
},
})
if err != nil {
log.Errorf("failed to list repositories under the project %s: %v, skip", pro.Name, err)
continue
}
for _, repo := range repos {
log.Infof("abstracting artifact metadata under repository %s ....", repo.Name)
arts, err := pkg.ArtifactMgr.List(ctx, &q.Query{
Keywords: map[string]interface{}{
"RepositoryID": repo.RepositoryID,
},
})
if err != nil {
log.Errorf("failed to list artifacts under the repository %s: %v, skip", repo.Name, err)
continue
}
for _, a := range arts {
if err = abstract(ctx, abstractor, a); err != nil {
log.Errorf("failed to abstract the artifact %s@%s: %v, skip", a.RepositoryName, a.Digest, err)
continue
}
if err = pkg.ArtifactMgr.Update(ctx, a); err != nil {
log.Errorf("failed to update the artifact %s@%s: %v, skip", repo.Name, a.Digest, err)
continue
}
}
log.Infof("artifact metadata under repository %s abstracted", repo.Name)
}
}
// update data version
return setDataVersion(ctx, dataversionV2_0_0)
}
func abstract(ctx context.Context, abstractor art.Abstractor, art *artifact.Artifact) error {
// abstract the children
for _, reference := range art.References {
child, err := pkg.ArtifactMgr.Get(ctx, reference.ChildID)
if err != nil {
log.Errorf("failed to get the artifact %d: %v, skip", reference.ChildID, err)
continue
}
if err = abstract(ctx, abstractor, child); err != nil {
log.Errorf("failed to abstract the artifact %s@%s: %v, skip", child.RepositoryName, child.Digest, err)
continue
}
}
// abstract the parent
return abstractor.AbstractMetadata(ctx, art)
}

View File

@ -15,27 +15,15 @@
package migration
import (
"context"
"fmt"
"time"
beegorm "github.com/beego/beego/v2/client/orm"
"github.com/golang-migrate/migrate/v4"
"github.com/goharbor/harbor/src/common/dao"
"github.com/goharbor/harbor/src/common/models"
"github.com/goharbor/harbor/src/lib/log"
"github.com/goharbor/harbor/src/lib/orm"
)
const (
schemaVersionV1_10_0 = 15
// data version for tracking the data integrity in the DB, it can be different from schema version
dataversionV2_0_0 = 30
)
// MigrateDB upgrades DB schema and do necessary transformation of the data in DB
func MigrateDB(database *models.Database) error {
// Migrate upgrades DB schema and do necessary transformation of the data in DB
func Migrate(database *models.Database) error {
// check the database schema version
migrator, err := dao.NewMigrator(database.PostGreSQL)
if err != nil {
@ -47,77 +35,5 @@ func MigrateDB(database *models.Database) error {
return err
}
log.Debugf("current database schema version: %v", schemaVersion)
// prior to 1.9, version = 0 means fresh install
if schemaVersion > 0 && schemaVersion < schemaVersionV1_10_0 {
return fmt.Errorf("please upgrade to version 1.10 first")
}
// update database schema
return dao.UpgradeSchema(database)
}
// AbstractArtifactData accesses the registry to
func AbstractArtifactData() error {
log.Info("Abstracting artifact data to DB...")
ctx := orm.NewContext(context.Background(), beegorm.NewOrm())
dataVersion, err := getDataVersion(ctx)
if err != nil {
return err
}
log.Debugf("current data version: %v", dataVersion)
// the abstract logic already done before, skip
if dataVersion >= dataversionV2_0_0 {
log.Info("No need to abstract artifact data. Skip")
return nil
}
if err = abstractArtData(ctx); err != nil {
return err
}
log.Info("Abstract artifact data to DB done")
return nil
}
// Migrate the database schema and abstract artifact data
func Migrate(database *models.Database) error {
if err := MigrateDB(database); err != nil {
return err
}
if err := AbstractArtifactData(); err != nil {
return err
}
return nil
}
type dataVersion struct {
ID int64
Version int
CreationTime time.Time
UpdateTime time.Time
}
func getDataVersion(ctx context.Context) (int, error) {
ormer, err := orm.FromContext(ctx)
if err != nil {
return 0, err
}
versions := []*dataVersion{}
if _, err = ormer.Raw("select * from data_migrations order by id").QueryRows(&versions); err != nil {
return 0, err
}
n := len(versions)
if n == 0 {
return 0, nil
}
if n > 1 {
return 0, fmt.Errorf("there should be only one record in the table data_migrations, but found %d records", n)
}
return versions[0].Version, nil
}
func setDataVersion(ctx context.Context, version int) error {
ormer, err := orm.FromContext(ctx)
if err != nil {
return err
}
_, err = ormer.Raw("update data_migrations set version=?, update_time=?", version, time.Now()).Exec()
return err
}