mirror of
https://github.com/goharbor/harbor.git
synced 2024-11-25 11:46:43 +01:00
Merge pull request #12832 from ywk253100/200820_data
Add id column to data_migration table
This commit is contained in:
commit
c0602b5fb3
@ -125,7 +125,10 @@ ALTER TABLE notification_policy DROP CONSTRAINT notification_policy_name_key;
|
|||||||
ALTER TABLE notification_policy ADD UNIQUE(name,project_id);
|
ALTER TABLE notification_policy ADD UNIQUE(name,project_id);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS data_migrations (
|
CREATE TABLE IF NOT EXISTS data_migrations (
|
||||||
version int
|
id SERIAL PRIMARY KEY NOT NULL,
|
||||||
|
version int,
|
||||||
|
creation_time timestamp default CURRENT_TIMESTAMP,
|
||||||
|
update_time timestamp default CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
INSERT INTO data_migrations (version) VALUES (
|
INSERT INTO data_migrations (version) VALUES (
|
||||||
CASE
|
CASE
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/lib/log"
|
"github.com/goharbor/harbor/src/lib/log"
|
||||||
"github.com/goharbor/harbor/src/lib/orm"
|
"github.com/goharbor/harbor/src/lib/orm"
|
||||||
"github.com/golang-migrate/migrate/v4"
|
"github.com/golang-migrate/migrate/v4"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -66,7 +67,11 @@ func AbstractArtifactData() error {
|
|||||||
log.Info("No need to abstract artifact data. Skip")
|
log.Info("No need to abstract artifact data. Skip")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return abstractArtData(ctx)
|
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
|
// Migrate the database schema and abstract artifact data
|
||||||
@ -80,16 +85,30 @@ func Migrate(database *models.Database) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type dataVersion struct {
|
||||||
|
ID int64
|
||||||
|
Version int
|
||||||
|
CreationTime time.Time
|
||||||
|
UpdateTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
func getDataVersion(ctx context.Context) (int, error) {
|
func getDataVersion(ctx context.Context) (int, error) {
|
||||||
ormer, err := orm.FromContext(ctx)
|
ormer, err := orm.FromContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
var version int
|
versions := []*dataVersion{}
|
||||||
if err = ormer.Raw("select version from data_migrations").QueryRow(&version); err != nil {
|
if _, err = ormer.Raw("select * from data_migrations order by id").QueryRows(&versions); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
return version, nil
|
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 {
|
func setDataVersion(ctx context.Context, version int) error {
|
||||||
@ -97,6 +116,6 @@ func setDataVersion(ctx context.Context, version int) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = ormer.Raw("update data_migrations set version=?", version).Exec()
|
_, err = ormer.Raw("update data_migrations set version=?, update_time=?", version, time.Now()).Exec()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user