MCPcopy Index your code
hub / github.com/apache/answer / Migrate

Function Migrate

internal/migrations/migrations.go:144–198  ·  view source on GitHub ↗

Migrate database to current version

(debug bool, dbConf *data.Database, cacheConf *data.CacheConf, upgradeToSpecificVersion string)

Source from the content-addressed store, hash-verified

142
143// Migrate database to current version
144func Migrate(debug bool, dbConf *data.Database, cacheConf *data.CacheConf, upgradeToSpecificVersion string) error {
145 cache, cacheCleanup, err := data.NewCache(cacheConf)
146 if err != nil {
147 fmt.Println("new cache failed:", err.Error())
148 }
149 engine, err := data.NewDB(debug, dbConf)
150 if err != nil {
151 fmt.Println("new database failed: ", err.Error())
152 return err
153 }
154 defer func() {
155 _ = engine.Close()
156 }()
157
158 currentDBVersion, err := GetCurrentDBVersion(engine)
159 if err != nil {
160 return err
161 }
162 expectedVersion := ExpectedVersion()
163 if len(upgradeToSpecificVersion) > 0 {
164 fmt.Printf("[migrate] user set upgrade to version: %s\n", upgradeToSpecificVersion)
165 for i, m := range migrations {
166 if m.Version() == upgradeToSpecificVersion {
167 currentDBVersion = int64(i)
168 break
169 }
170 }
171 }
172
173 for currentDBVersion < expectedVersion {
174 fmt.Printf("[migrate] current db version is %d, try to migrate version %d, latest version is %d\n",
175 currentDBVersion, currentDBVersion+1, expectedVersion)
176 migrationFunc := migrations[currentDBVersion]
177 fmt.Printf("[migrate] try to migrate Answer version %s, description: %s\n", migrationFunc.Version(), migrationFunc.Description())
178 if err := migrationFunc.Migrate(context.Background(), engine); err != nil {
179 fmt.Printf("[migrate] migrate to db version %d failed: %s\n", currentDBVersion+1, err.Error())
180 return err
181 }
182 if migrationFunc.ShouldCleanCache() {
183 if err := cache.Flush(context.Background()); err != nil {
184 fmt.Printf("[migrate] flush cache failed: %s\n", err.Error())
185 }
186 }
187 fmt.Printf("[migrate] migrate to db version %d success\n", currentDBVersion+1)
188 if _, err := engine.Update(&entity.Version{ID: 1, VersionNumber: currentDBVersion + 1}); err != nil {
189 fmt.Printf("[migrate] migrate to db version %d, update failed: %s", currentDBVersion+1, err.Error())
190 return err
191 }
192 currentDBVersion++
193 }
194 if cache != nil {
195 cacheCleanup()
196 }
197 return nil
198}

Callers 1

command.goFile · 0.92

Calls 11

FlushMethod · 0.95
NewCacheFunction · 0.92
NewDBFunction · 0.92
GetCurrentDBVersionFunction · 0.85
ExpectedVersionFunction · 0.85
UpdateMethod · 0.80
CloseMethod · 0.65
VersionMethod · 0.65
DescriptionMethod · 0.65
MigrateMethod · 0.65
ShouldCleanCacheMethod · 0.65

Tested by

no test coverage detected