Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
c0422dea
Commit
c0422dea
authored
Oct 05, 2023
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: fix sqlite migrator
parent
7791fb10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
22 additions
and
13 deletions
+22
-13
db.go
store/db/db.go
+10
-10
migrator.go
store/db/sqlite/migrator.go
+12
-3
No files found.
store/db/db.go
View file @
c0422dea
...
...
@@ -15,17 +15,17 @@ func NewDBDriver(profile *profile.Profile) (store.Driver, error) {
var
err
error
// As mysql driver is not fully implemented, we use sqlite for now in prod mode.
if
profile
.
Mode
==
"prod"
{
if
profile
.
Mode
==
"prod"
&&
profile
.
Driver
!=
"sqlite"
{
return
nil
,
errors
.
New
(
"Only SQLite is supported in prod mode"
)
}
switch
profile
.
Driver
{
case
"sqlite"
:
driver
,
err
=
sqlite
.
NewDB
(
profile
)
}
else
{
switch
profile
.
Driver
{
case
"sqlite"
:
driver
,
err
=
sqlite
.
NewDB
(
profile
)
case
"mysql"
:
driver
,
err
=
mysql
.
NewDB
(
profile
)
default
:
return
nil
,
errors
.
New
(
"unknown db driver"
)
}
case
"mysql"
:
driver
,
err
=
mysql
.
NewDB
(
profile
)
default
:
return
nil
,
errors
.
New
(
"unknown db driver"
)
}
if
err
!=
nil
{
return
nil
,
errors
.
Wrap
(
err
,
"failed to create db driver"
)
...
...
store/db/sqlite/migrator.go
View file @
c0422dea
...
...
@@ -23,6 +23,7 @@ var seedFS embed.FS
// Migrate applies the latest schema to the database.
func
(
d
*
DB
)
Migrate
(
ctx
context
.
Context
)
error
{
currentVersion
:=
version
.
GetCurrentVersion
(
d
.
profile
.
Mode
)
if
d
.
profile
.
Mode
==
"prod"
{
_
,
err
:=
os
.
Stat
(
d
.
profile
.
DSN
)
if
err
!=
nil
{
...
...
@@ -31,17 +32,27 @@ func (d *DB) Migrate(ctx context.Context) error {
if
err
:=
d
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to apply latest schema"
)
}
// Upsert the newest version to migration_history.
if
_
,
err
:=
d
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
Version
:
currentVersion
,
});
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to upsert migration history"
)
}
}
else
{
return
errors
.
Wrap
(
err
,
"failed to get db file stat"
)
}
}
else
{
// If db file exists, we should check if we need to migrate the database.
currentVersion
:=
version
.
GetCurrentVersion
(
d
.
profile
.
Mode
)
migrationHistoryList
,
err
:=
d
.
FindMigrationHistoryList
(
ctx
,
&
MigrationHistoryFind
{})
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to find migration history"
)
}
// If no migration history, we should apply the latest version migration and upsert the migration history.
if
len
(
migrationHistoryList
)
==
0
{
minorVersion
:=
version
.
GetMinorVersion
(
currentVersion
)
if
err
:=
d
.
applyMigrationForMinorVersion
(
ctx
,
minorVersion
);
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to apply version %s migration"
,
minorVersion
)
}
_
,
err
:=
d
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
Version
:
currentVersion
,
})
...
...
@@ -60,7 +71,6 @@ func (d *DB) Migrate(ctx context.Context) error {
if
version
.
IsVersionGreaterThan
(
version
.
GetSchemaVersion
(
currentVersion
),
latestMigrationHistoryVersion
)
{
minorVersionList
:=
getMinorVersionList
()
// backup the raw database file before migration
rawBytes
,
err
:=
os
.
ReadFile
(
d
.
profile
.
DSN
)
if
err
!=
nil
{
...
...
@@ -71,7 +81,6 @@ func (d *DB) Migrate(ctx context.Context) error {
return
errors
.
Wrap
(
err
,
"failed to write raw database file"
)
}
println
(
"succeed to copy a backup database file"
)
println
(
"start migrate"
)
for
_
,
minorVersion
:=
range
minorVersionList
{
normalizedVersion
:=
minorVersion
+
".0"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment