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
07e82c3f
Unverified
Commit
07e82c3f
authored
Jun 20, 2023
by
boojack
Committed by
GitHub
Jun 20, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: schema migrate (#1846)
* fix: schema migrate * chore: update
parent
b34aded3
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
56 additions
and
50 deletions
+56
-50
db.go
store/db/db.go
+55
-49
migration_history.go
store/db/migration_history.go
+1
-1
No files found.
store/db/db.go
View file @
07e82c3f
...
@@ -50,64 +50,70 @@ func (db *DB) Open(ctx context.Context) (err error) {
...
@@ -50,64 +50,70 @@ func (db *DB) Open(ctx context.Context) (err error) {
db
.
DBInstance
=
sqliteDB
db
.
DBInstance
=
sqliteDB
if
db
.
profile
.
Mode
==
"prod"
{
if
db
.
profile
.
Mode
==
"prod"
{
// If db file not exists, we should migrate the database.
_
,
err
:=
os
.
Stat
(
db
.
profile
.
DSN
)
if
_
,
err
:=
os
.
Stat
(
db
.
profile
.
DSN
);
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
}
}
currentVersion
:=
version
.
GetCurrentVersion
(
db
.
profile
.
Mode
)
migrationHistoryList
,
err
:=
db
.
FindMigrationHistoryList
(
ctx
,
&
MigrationHistoryFind
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to find migration history, err: %w"
,
err
)
// If db file not exists, we should create a new one with latest schema.
}
if
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
if
len
(
migrationHistoryList
)
==
0
{
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
_
,
err
:=
db
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
return
fmt
.
Errorf
(
"failed to apply latest schema, err: %w"
,
err
)
Version
:
currentVersion
,
}
})
}
else
{
return
fmt
.
Errorf
(
"failed to get db file stat, err: %w"
,
err
)
}
}
else
{
// If db file exists, we should check if we need to migrate the database.
currentVersion
:=
version
.
GetCurrentVersion
(
db
.
profile
.
Mode
)
migrationHistoryList
,
err
:=
db
.
FindMigrationHistoryList
(
ctx
,
&
MigrationHistoryFind
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to upsert migration history, err: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to find migration history, err: %w"
,
err
)
}
if
len
(
migrationHistoryList
)
==
0
{
_
,
err
:=
db
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
Version
:
currentVersion
,
})
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to upsert migration history, err: %w"
,
err
)
}
return
nil
}
}
return
nil
}
migrationHistoryVersionList
:=
[]
string
{}
migrationHistoryVersionList
:=
[]
string
{}
for
_
,
migrationHistory
:=
range
migrationHistoryList
{
for
_
,
migrationHistory
:=
range
migrationHistoryList
{
migrationHistoryVersionList
=
append
(
migrationHistoryVersionList
,
migrationHistory
.
Version
)
migrationHistoryVersionList
=
append
(
migrationHistoryVersionList
,
migrationHistory
.
Version
)
}
}
sort
.
Sort
(
version
.
SortVersion
(
migrationHistoryVersionList
))
sort
.
Sort
(
version
.
SortVersion
(
migrationHistoryVersionList
))
latestMigrationHistoryVersion
:=
migrationHistoryVersionList
[
len
(
migrationHistoryVersionList
)
-
1
]
latestMigrationHistoryVersion
:=
migrationHistoryVersionList
[
len
(
migrationHistoryVersionList
)
-
1
]
if
version
.
IsVersionGreaterThan
(
version
.
GetSchemaVersion
(
currentVersion
),
latestMigrationHistoryVersion
)
{
if
version
.
IsVersionGreaterThan
(
version
.
GetSchemaVersion
(
currentVersion
),
latestMigrationHistoryVersion
)
{
minorVersionList
:=
getMinorVersionList
()
minorVersionList
:=
getMinorVersionList
()
// backup the raw database file before migration
// backup the raw database file before migration
rawBytes
,
err
:=
os
.
ReadFile
(
db
.
profile
.
DSN
)
rawBytes
,
err
:=
os
.
ReadFile
(
db
.
profile
.
DSN
)
if
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to read raw database file, err: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to read raw database file, err: %w"
,
err
)
}
}
backupDBFilePath
:=
fmt
.
Sprintf
(
"%s/memos_%s_%d_backup.db"
,
db
.
profile
.
Data
,
db
.
profile
.
Version
,
time
.
Now
()
.
Unix
())
backupDBFilePath
:=
fmt
.
Sprintf
(
"%s/memos_%s_%d_backup.db"
,
db
.
profile
.
Data
,
db
.
profile
.
Version
,
time
.
Now
()
.
Unix
())
if
err
:=
os
.
WriteFile
(
backupDBFilePath
,
rawBytes
,
0644
);
err
!=
nil
{
if
err
:=
os
.
WriteFile
(
backupDBFilePath
,
rawBytes
,
0644
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to write raw database file, err: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to write raw database file, err: %w"
,
err
)
}
}
println
(
"succeed to copy a backup database file"
)
println
(
"succeed to copy a backup database file"
)
println
(
"start migrate"
)
println
(
"start migrate"
)
for
_
,
minorVersion
:=
range
minorVersionList
{
for
_
,
minorVersion
:=
range
minorVersionList
{
normalizedVersion
:=
minorVersion
+
".0"
normalizedVersion
:=
minorVersion
+
".0"
if
version
.
IsVersionGreaterThan
(
normalizedVersion
,
latestMigrationHistoryVersion
)
&&
version
.
IsVersionGreaterOrEqualThan
(
currentVersion
,
normalizedVersion
)
{
if
version
.
IsVersionGreaterThan
(
normalizedVersion
,
latestMigrationHistoryVersion
)
&&
version
.
IsVersionGreaterOrEqualThan
(
currentVersion
,
normalizedVersion
)
{
println
(
"applying migration for"
,
normalizedVersion
)
println
(
"applying migration for"
,
normalizedVersion
)
if
err
:=
db
.
applyMigrationForMinorVersion
(
ctx
,
minorVersion
);
err
!=
nil
{
if
err
:=
db
.
applyMigrationForMinorVersion
(
ctx
,
minorVersion
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply minor version migration: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to apply minor version migration: %w"
,
err
)
}
}
}
}
}
}
println
(
"end migrate"
)
println
(
"end migrate"
)
// remove the created backup db file after migrate succeed
// remove the created backup db file after migrate succeed
if
err
:=
os
.
Remove
(
backupDBFilePath
);
err
!=
nil
{
if
err
:=
os
.
Remove
(
backupDBFilePath
);
err
!=
nil
{
println
(
fmt
.
Sprintf
(
"Failed to remove temp database file, err %v"
,
err
))
println
(
fmt
.
Sprintf
(
"Failed to remove temp database file, err %v"
,
err
))
}
}
}
}
}
}
else
{
}
else
{
...
...
store/db/migration_history.go
View file @
07e82c3f
...
@@ -67,7 +67,7 @@ func findMigrationHistoryList(ctx context.Context, tx *sql.Tx, find *MigrationHi
...
@@ -67,7 +67,7 @@ func findMigrationHistoryList(ctx context.Context, tx *sql.Tx, find *MigrationHi
FROM
FROM
migration_history
migration_history
WHERE `
+
strings
.
Join
(
where
,
" AND "
)
+
`
WHERE `
+
strings
.
Join
(
where
,
" AND "
)
+
`
ORDER BY
version
DESC
ORDER BY
created_ts
DESC
`
`
rows
,
err
:=
tx
.
QueryContext
(
ctx
,
query
,
args
...
)
rows
,
err
:=
tx
.
QueryContext
(
ctx
,
query
,
args
...
)
if
err
!=
nil
{
if
err
!=
nil
{
...
...
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