Best Testkube code snippet using migrator.Version
tables.go
Source:tables.go
...5)6// AddMigration defines database migrations.7func AddTablesMigrations(mg *migrator.Migrator) {8 AddAlertDefinitionMigrations(mg, 60)9 AddAlertDefinitionVersionMigrations(mg)10 // Create alert_instance table11 AlertInstanceMigration(mg)12 // Create alert_rule13 AddAlertRuleMigrations(mg, 60)14 AddAlertRuleVersionMigrations(mg)15 // Create Alertmanager configurations16 AddAlertmanagerConfigMigrations(mg)17 // Create Admin Configuration18 AddAlertAdminConfigMigrations(mg)19}20// AddAlertDefinitionMigrations should not be modified.21func AddAlertDefinitionMigrations(mg *migrator.Migrator, defaultIntervalSeconds int64) {22 mg.AddMigration("delete alert_definition table", migrator.NewDropTableMigration("alert_definition"))23 alertDefinition := migrator.Table{24 Name: "alert_definition",25 Columns: []*migrator.Column{26 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},27 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},28 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},29 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},30 {Name: "data", Type: migrator.DB_Text, Nullable: false},31 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},32 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false, Default: fmt.Sprintf("%d", defaultIntervalSeconds)},33 {Name: "version", Type: migrator.DB_Int, Nullable: false, Default: "0"},34 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},35 },36 Indices: []*migrator.Index{37 {Cols: []string{"org_id", "title"}, Type: migrator.IndexType},38 {Cols: []string{"org_id", "uid"}, Type: migrator.IndexType},39 },40 }41 // create table42 mg.AddMigration("recreate alert_definition table", migrator.NewAddTableMigration(alertDefinition))43 // create indices44 mg.AddMigration("add index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[0]))45 mg.AddMigration("add index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, alertDefinition.Indices[1]))46 mg.AddMigration("alter alert_definition table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").47 Mysql("ALTER TABLE alert_definition MODIFY data MEDIUMTEXT;"))48 mg.AddMigration("drop index in alert_definition on org_id and title columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[0]))49 mg.AddMigration("drop index in alert_definition on org_id and uid columns", migrator.NewDropIndexMigration(alertDefinition, alertDefinition.Indices[1]))50 uniqueIndices := []*migrator.Index{51 {Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex},52 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},53 }54 mg.AddMigration("add unique index in alert_definition on org_id and title columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[0]))55 mg.AddMigration("add unique index in alert_definition on org_id and uid columns", migrator.NewAddIndexMigration(alertDefinition, uniqueIndices[1]))56 mg.AddMigration("Add column paused in alert_definition", migrator.NewAddColumnMigration(alertDefinition, &migrator.Column{57 Name: "paused", Type: migrator.DB_Bool, Nullable: false, Default: "0",58 }))59 mg.AddMigration("drop alert_definition table", migrator.NewDropTableMigration("alert_definition"))60}61// AddAlertDefinitionMigrations should not be modified.62func AddAlertDefinitionVersionMigrations(mg *migrator.Migrator) {63 mg.AddMigration("delete alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))64 alertDefinitionVersion := migrator.Table{65 Name: "alert_definition_version",66 Columns: []*migrator.Column{67 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},68 {Name: "alert_definition_id", Type: migrator.DB_BigInt},69 {Name: "alert_definition_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},70 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},71 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},72 {Name: "version", Type: migrator.DB_Int, Nullable: false},73 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},74 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},75 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},76 {Name: "data", Type: migrator.DB_Text, Nullable: false},77 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},78 },79 Indices: []*migrator.Index{80 {Cols: []string{"alert_definition_id", "version"}, Type: migrator.UniqueIndex},81 {Cols: []string{"alert_definition_uid", "version"}, Type: migrator.UniqueIndex},82 },83 }84 mg.AddMigration("recreate alert_definition_version table", migrator.NewAddTableMigration(alertDefinitionVersion))85 mg.AddMigration("add index in alert_definition_version table on alert_definition_id and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[0]))86 mg.AddMigration("add index in alert_definition_version table on alert_definition_uid and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[1]))87 mg.AddMigration("alter alert_definition_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").88 Mysql("ALTER TABLE alert_definition_version MODIFY data MEDIUMTEXT;"))89 mg.AddMigration("drop alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))90}91func AlertInstanceMigration(mg *migrator.Migrator) {92 alertInstance := migrator.Table{93 Name: "alert_instance",94 Columns: []*migrator.Column{95 {Name: "def_org_id", Type: migrator.DB_BigInt, Nullable: false},96 {Name: "def_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},97 {Name: "labels", Type: migrator.DB_Text, Nullable: false},98 {Name: "labels_hash", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},99 {Name: "current_state", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},100 {Name: "current_state_since", Type: migrator.DB_BigInt, Nullable: false},101 {Name: "last_eval_time", Type: migrator.DB_BigInt, Nullable: false},102 },103 PrimaryKeys: []string{"def_org_id", "def_uid", "labels_hash"},104 Indices: []*migrator.Index{105 {Cols: []string{"def_org_id", "def_uid", "current_state"}, Type: migrator.IndexType},106 {Cols: []string{"def_org_id", "current_state"}, Type: migrator.IndexType},107 },108 }109 // create table110 mg.AddMigration("create alert_instance table", migrator.NewAddTableMigration(alertInstance))111 mg.AddMigration("add index in alert_instance table on def_org_id, def_uid and current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[0]))112 mg.AddMigration("add index in alert_instance table on def_org_id, current_state columns", migrator.NewAddIndexMigration(alertInstance, alertInstance.Indices[1]))113 mg.AddMigration("add column current_state_end to alert_instance", migrator.NewAddColumnMigration(alertInstance, &migrator.Column{114 Name: "current_state_end", Type: migrator.DB_BigInt, Nullable: false, Default: "0",115 }))116 mg.AddMigration("remove index def_org_id, def_uid, current_state on alert_instance", migrator.NewDropIndexMigration(alertInstance, alertInstance.Indices[0]))117 mg.AddMigration("remove index def_org_id, current_state on alert_instance", migrator.NewDropIndexMigration(alertInstance, alertInstance.Indices[1]))118 mg.AddMigration("rename def_org_id to rule_org_id in alert_instance", migrator.NewRawSQLMigration("").119 Default("ALTER TABLE alert_instance RENAME COLUMN def_org_id TO rule_org_id;").120 Mysql("ALTER TABLE alert_instance CHANGE def_org_id rule_org_id BIGINT;"))121 mg.AddMigration("rename def_uid to rule_uid in alert_instance", migrator.NewRawSQLMigration("").122 Default("ALTER TABLE alert_instance RENAME COLUMN def_uid TO rule_uid;").123 Mysql("ALTER TABLE alert_instance CHANGE def_uid rule_uid VARCHAR(40);"))124 mg.AddMigration("add index rule_org_id, rule_uid, current_state on alert_instance", migrator.NewAddIndexMigration(alertInstance, &migrator.Index{125 Cols: []string{"rule_org_id", "rule_uid", "current_state"}, Type: migrator.IndexType,126 }))127 mg.AddMigration("add index rule_org_id, current_state on alert_instance", migrator.NewAddIndexMigration(alertInstance, &migrator.Index{128 Cols: []string{"rule_org_id", "current_state"}, Type: migrator.IndexType,129 }))130}131func AddAlertRuleMigrations(mg *migrator.Migrator, defaultIntervalSeconds int64) {132 alertRule := migrator.Table{133 Name: "alert_rule",134 Columns: []*migrator.Column{135 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},136 {Name: "org_id", Type: migrator.DB_BigInt, Nullable: false},137 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},138 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},139 {Name: "data", Type: migrator.DB_Text, Nullable: false},140 {Name: "updated", Type: migrator.DB_DateTime, Nullable: false},141 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false, Default: fmt.Sprintf("%d", defaultIntervalSeconds)},142 {Name: "version", Type: migrator.DB_Int, Nullable: false, Default: "0"},143 {Name: "uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},144 // the following fields will correspond to a dashboard (or folder) UIID145 {Name: "namespace_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},146 {Name: "rule_group", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},147 {Name: "no_data_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'NoData'"},148 {Name: "exec_err_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'Alerting'"},149 },150 Indices: []*migrator.Index{151 {Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex},152 {Cols: []string{"org_id", "uid"}, Type: migrator.UniqueIndex},153 {Cols: []string{"org_id", "namespace_uid", "rule_group"}, Type: migrator.IndexType},154 },155 }156 // create table157 mg.AddMigration("create alert_rule table", migrator.NewAddTableMigration(alertRule))158 // create indices159 mg.AddMigration("add index in alert_rule on org_id and title columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[0]))160 mg.AddMigration("add index in alert_rule on org_id and uid columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[1]))161 mg.AddMigration("add index in alert_rule on org_id, namespace_uid, group_uid columns", migrator.NewAddIndexMigration(alertRule, alertRule.Indices[2]))162 mg.AddMigration("alter alert_rule table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").163 Mysql("ALTER TABLE alert_rule MODIFY data MEDIUMTEXT;"))164 // add for column165 mg.AddMigration("add column for to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))166 // add annotations column167 mg.AddMigration("add column annotations to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))168 // add labels column169 mg.AddMigration("add column labels to alert_rule", migrator.NewAddColumnMigration(alertRule, &migrator.Column{Name: "labels", Type: migrator.DB_Text, Nullable: true}))170 mg.AddMigration("remove unique index from alert_rule on org_id, title columns", migrator.NewDropIndexMigration(alertRule, &migrator.Index{171 Cols: []string{"org_id", "title"}, Type: migrator.UniqueIndex,172 }))173 mg.AddMigration("add index in alert_rule on org_id, namespase_uid and title columns", migrator.NewAddIndexMigration(alertRule, &migrator.Index{174 Cols: []string{"org_id", "namespace_uid", "title"}, Type: migrator.UniqueIndex,175 }))176 mg.AddMigration("add dashboard_uid column to alert_rule", migrator.NewAddColumnMigration(177 migrator.Table{Name: "alert_rule"},178 &migrator.Column{179 Name: "dashboard_uid",180 Type: migrator.DB_NVarchar,181 Length: 40,182 Nullable: true,183 },184 ))185 mg.AddMigration("add panel_id column to alert_rule", migrator.NewAddColumnMigration(186 migrator.Table{Name: "alert_rule"},187 &migrator.Column{188 Name: "panel_id",189 Type: migrator.DB_BigInt,190 Nullable: true,191 },192 ))193 mg.AddMigration("add index in alert_rule on org_id, dashboard_uid and panel_id columns", migrator.NewAddIndexMigration(194 migrator.Table{Name: "alert_rule"},195 &migrator.Index{196 Name: "IDX_alert_rule_org_id_dashboard_uid_panel_id",197 Cols: []string{"org_id", "dashboard_uid", "panel_id"},198 },199 ))200}201func AddAlertRuleVersionMigrations(mg *migrator.Migrator) {202 alertRuleVersion := migrator.Table{203 Name: "alert_rule_version",204 Columns: []*migrator.Column{205 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},206 {Name: "rule_org_id", Type: migrator.DB_BigInt},207 {Name: "rule_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},208 // the following fields will correspond to a dashboard (or folder) UID209 {Name: "rule_namespace_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false},210 {Name: "rule_group", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},211 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},212 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},213 {Name: "version", Type: migrator.DB_Int, Nullable: false},214 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},215 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},216 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},217 {Name: "data", Type: migrator.DB_Text, Nullable: false},218 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},219 {Name: "no_data_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'NoData'"},220 {Name: "exec_err_state", Type: migrator.DB_NVarchar, Length: 15, Nullable: false, Default: "'Alerting'"},221 },222 Indices: []*migrator.Index{223 {Cols: []string{"rule_org_id", "rule_uid", "version"}, Type: migrator.UniqueIndex},224 {Cols: []string{"rule_org_id", "rule_namespace_uid", "rule_group"}, Type: migrator.IndexType},225 },226 }227 mg.AddMigration("create alert_rule_version table", migrator.NewAddTableMigration(alertRuleVersion))228 mg.AddMigration("add index in alert_rule_version table on rule_org_id, rule_uid and version columns", migrator.NewAddIndexMigration(alertRuleVersion, alertRuleVersion.Indices[0]))229 mg.AddMigration("add index in alert_rule_version table on rule_org_id, rule_namespace_uid and rule_group columns", migrator.NewAddIndexMigration(alertRuleVersion, alertRuleVersion.Indices[1]))230 mg.AddMigration("alter alert_rule_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").231 Mysql("ALTER TABLE alert_rule_version MODIFY data MEDIUMTEXT;"))232 // add for column233 mg.AddMigration("add column for to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "for", Type: migrator.DB_BigInt, Nullable: false, Default: "0"}))234 // add annotations column235 mg.AddMigration("add column annotations to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "annotations", Type: migrator.DB_Text, Nullable: true}))236 // add labels column237 mg.AddMigration("add column labels to alert_rule_version", migrator.NewAddColumnMigration(alertRuleVersion, &migrator.Column{Name: "labels", Type: migrator.DB_Text, Nullable: true}))238}239func AddAlertmanagerConfigMigrations(mg *migrator.Migrator) {240 alertConfiguration := migrator.Table{241 Name: "alert_configuration",242 Columns: []*migrator.Column{243 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},244 {Name: "alertmanager_configuration", Type: migrator.DB_Text, Nullable: false},245 {Name: "configuration_version", Type: migrator.DB_NVarchar, Length: 3}, // In a format of vXX e.g. v1, v2, v10, etc246 {Name: "created_at", Type: migrator.DB_Int, Nullable: false},247 },248 }249 mg.AddMigration("create_alert_configuration_table", migrator.NewAddTableMigration(alertConfiguration))250 mg.AddMigration("Add column default in alert_configuration", migrator.NewAddColumnMigration(alertConfiguration, &migrator.Column{251 Name: "default", Type: migrator.DB_Bool, Nullable: false, Default: "0",...
database_mig.go
Source:database_mig.go
...41 mg.AddMigration("Add column paused in alert_definition", migrator.NewAddColumnMigration(alertDefinition, &migrator.Column{42 Name: "paused", Type: migrator.DB_Bool, Nullable: false, Default: "0",43 }))44}45func addAlertDefinitionVersionMigrations(mg *migrator.Migrator) {46 mg.AddMigration("delete alert_definition_version table", migrator.NewDropTableMigration("alert_definition_version"))47 alertDefinitionVersion := migrator.Table{48 Name: "alert_definition_version",49 Columns: []*migrator.Column{50 {Name: "id", Type: migrator.DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},51 {Name: "alert_definition_id", Type: migrator.DB_BigInt},52 {Name: "alert_definition_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},53 {Name: "parent_version", Type: migrator.DB_Int, Nullable: false},54 {Name: "restored_from", Type: migrator.DB_Int, Nullable: false},55 {Name: "version", Type: migrator.DB_Int, Nullable: false},56 {Name: "created", Type: migrator.DB_DateTime, Nullable: false},57 {Name: "title", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},58 {Name: "condition", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},59 {Name: "data", Type: migrator.DB_Text, Nullable: false},60 {Name: "interval_seconds", Type: migrator.DB_BigInt, Nullable: false},61 },62 Indices: []*migrator.Index{63 {Cols: []string{"alert_definition_id", "version"}, Type: migrator.UniqueIndex},64 {Cols: []string{"alert_definition_uid", "version"}, Type: migrator.UniqueIndex},65 },66 }67 mg.AddMigration("recreate alert_definition_version table", migrator.NewAddTableMigration(alertDefinitionVersion))68 mg.AddMigration("add index in alert_definition_version table on alert_definition_id and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[0]))69 mg.AddMigration("add index in alert_definition_version table on alert_definition_uid and version columns", migrator.NewAddIndexMigration(alertDefinitionVersion, alertDefinitionVersion.Indices[1]))70 mg.AddMigration("alter alert_definition_version table data column to mediumtext in mysql", migrator.NewRawSQLMigration("").71 Mysql("ALTER TABLE alert_definition_version MODIFY data MEDIUMTEXT;"))72}73func alertInstanceMigration(mg *migrator.Migrator) {74 alertInstance := migrator.Table{75 Name: "alert_instance",76 Columns: []*migrator.Column{77 {Name: "def_org_id", Type: migrator.DB_BigInt, Nullable: false},78 {Name: "def_uid", Type: migrator.DB_NVarchar, Length: 40, Nullable: false, Default: "0"},79 {Name: "labels", Type: migrator.DB_Text, Nullable: false},80 {Name: "labels_hash", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},81 {Name: "current_state", Type: migrator.DB_NVarchar, Length: 190, Nullable: false},82 {Name: "current_state_since", Type: migrator.DB_BigInt, Nullable: false},83 {Name: "last_eval_time", Type: migrator.DB_BigInt, Nullable: false},...
manager.go
Source:manager.go
1// Copyright Project Harbor Authors2//3// Licensed under the Apache License, Version 2.0 (the "License");4// you may not use this file except in compliance with the License.5// You may obtain a copy of the License at6//7// http://www.apache.org/licenses/LICENSE-2.08//9// Unless required by applicable law or agreed to in writing, software10// distributed under the License is distributed on an "AS IS" BASIS,11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12// See the License for the specific language governing permissions and13// limitations under the License.14package migration15import (16 "github.com/Masterminds/semver"17 "reflect"18 "github.com/gomodule/redigo/redis"19 "github.com/goharbor/harbor/src/jobservice/logger"20 "github.com/pkg/errors"21)22// Manager for managing the related migrators23type Manager interface {24 // Register the specified migrator to the execution chain25 Register(migratorFactory MigratorFactory)26 // Migrate data27 Migrate() error28}29// MigratorChainNode is a wrapper to append the migrator to the chain with a next reference30type MigratorChainNode struct {31 // Migrator implementation32 migrator RDBMigrator33 // Refer the next migration of the chain if existing34 next *MigratorChainNode35}36// BasicManager is the default implementation of manager interface37type BasicManager struct {38 // The head of migrator chain39 head *MigratorChainNode40 // Pool for connecting to redis41 pool *redis.Pool42 // RDB namespace43 namespace string44}45// New a basic manager46func New(pool *redis.Pool, ns string) Manager {47 return &BasicManager{48 pool: pool,49 namespace: ns,50 }51}52// Register the migrator to the chain53func (bm *BasicManager) Register(migratorFactory MigratorFactory) {54 if migratorFactory == nil {55 return // ignore, do nothing56 }57 migrator, err := migratorFactory(bm.pool, bm.namespace)58 if err != nil {59 logger.Errorf("migrator register error: %s", err)60 return61 }62 newNode := &MigratorChainNode{63 migrator: migrator,64 next: nil,65 }66 if bm.head == nil {67 bm.head = newNode68 return69 }70 bm.head.next = newNode71}72// Migrate data73func (bm *BasicManager) Migrate() error {74 conn := bm.pool.Get()75 defer func() {76 _ = conn.Close()77 }()78 // Read schema version first79 v, err := redis.String(conn.Do("GET", VersionKey(bm.namespace)))80 if err != nil && err != redis.ErrNil {81 return errors.Wrap(err, "read schema version failed")82 }83 if len(v) > 0 {84 current, err := semver.NewVersion(v)85 if err != nil {86 return errors.Wrap(err, "malformed schema version")87 }88 nowV, _ := semver.NewVersion(SchemaVersion)89 diff := nowV.Compare(current)90 if diff < 0 {91 return errors.Errorf("the schema version of migrator is smaller that the one in the rdb: %s<%s", nowV.String(), current.String())92 } else if diff == 0 {93 logger.Info("No migration needed")94 return nil95 }96 }97 if bm.head == nil {98 logger.Warning("No migrator registered, passed migration")99 return nil100 }101 logger.Info("Process for migrating data is started")102 h := bm.head103 for h != nil {104 meta := h.migrator.Metadata()105 if meta == nil {106 // Make metadata required107 return errors.Errorf("no metadata provided for the migrator %s", reflect.TypeOf(h.migrator).String())108 }109 logger.Infof("Migrate %s from %s to %s", meta.ObjectRef, meta.FromVersion, meta.ToVersion)110 if err := h.migrator.Migrate(); err != nil {111 return errors.Wrap(err, "migration chain calling failed")112 }113 // Next one if existing114 h = h.next115 }116 // Set schema version117 if _, err = conn.Do("SET", VersionKey(bm.namespace), SchemaVersion); err != nil {118 return errors.Wrap(err, "write schema version failed")119 }120 logger.Infof("Data schema version upgraded to %s", SchemaVersion)121 return nil122}...
Version
Using AI Code Generation
1migrator := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{2 {3 Migrate: func(tx *gorm.DB) error {4 type User struct {5 }6 return tx.AutoMigrate(&User{}).Error7 },8 Rollback: func(tx *gorm.DB) error {9 type User struct {10 }11 return tx.DropTable(&User{}).Error12 },13 },14})15migrator := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{16 {17 Migrate: func(tx *gorm.DB) error {18 type User struct {19 }20 return tx.AutoMigrate(&User{}).Error21 },22 Rollback: func(tx *gorm.DB) error {23 type User struct {24 }25 return tx.DropTable(&User{}).Error26 },27 },28})29migrator := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{30 {31 Migrate: func(tx *gorm.DB) error {
Version
Using AI Code Generation
1import (2func main() {3 migrator := migrations.NewMigrator(db, migrations.Table("schema_migrations"))4 fmt.Println(migrator.Version())5}6import (7func main() {8 migrator := migrations.NewMigrator(db, migrations.Table("schema_migrations"))9 err := migrator.Up(0)10 if err != nil {11 log.Fatal(err)12 }13 fmt.Println(migrator.Version())14}15import (16func main() {17 migrator := migrations.NewMigrator(db, migrations.Table("schema_migrations"))18 err := migrator.Up(0, func(version int64, name string) {19 fmt.Printf("running migration %s20 })21 if err != nil {22 log.Fatal(err)23 }24 fmt.Println(migrator.Version())25}
Version
Using AI Code Generation
1import (2func main() {3 if err != nil {4 fmt.Println("Error in connecting to database")5 }6 migrator := goose.NewMigrator(db, goose.PostgresDialect{})7 version, err := migrator.Version()8 if err != nil {9 fmt.Println("Error in getting version")10 }11 fmt.Println(version)12}13import (14func main() {15 err := goose.Run("create", nil, "sql")16 if err != nil {17 fmt.Println("Error in creating migration file")18 }19}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!