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
15e10374
Commit
15e10374
authored
Jul 22, 2022
by
boojack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: create backup when migration
parent
5da4c98f
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
31 additions
and
17 deletions
+31
-17
profile.go
server/profile/profile.go
+2
-2
db.go
store/db/db.go
+29
-15
No files found.
server/profile/profile.go
View file @
15e10374
...
...
@@ -38,8 +38,7 @@ func checkDSN(dataDir string) (string, error) {
dataDir
=
strings
.
TrimRight
(
dataDir
,
"/"
)
if
_
,
err
:=
os
.
Stat
(
dataDir
);
err
!=
nil
{
error
:=
fmt
.
Errorf
(
"unable to access -data %s, err %w"
,
dataDir
,
err
)
return
""
,
error
return
""
,
fmt
.
Errorf
(
"unable to access data folder %s, err %w"
,
dataDir
,
err
)
}
return
dataDir
,
nil
...
...
@@ -67,6 +66,7 @@ func GetProfile() *Profile {
os
.
Exit
(
1
)
}
profile
.
Data
=
dataDir
profile
.
DSN
=
fmt
.
Sprintf
(
"%s/memos_%s.db"
,
dataDir
,
profile
.
Mode
)
profile
.
Version
=
common
.
GetCurrentVersion
(
profile
.
Mode
)
...
...
store/db/db.go
View file @
15e10374
...
...
@@ -6,9 +6,11 @@ import (
"errors"
"fmt"
"io/fs"
"io/ioutil"
"os"
"regexp"
"sort"
"time"
"github.com/usememos/memos/common"
"github.com/usememos/memos/server/profile"
...
...
@@ -24,37 +26,33 @@ var seedFS embed.FS
type
DB
struct
{
// sqlite db connection instance
Db
*
sql
.
DB
// datasource name
DSN
string
// mode should be prod or dev
mode
string
Db
*
sql
.
DB
profile
*
profile
.
Profile
}
// NewDB returns a new instance of DB associated with the given datasource name.
func
NewDB
(
profile
*
profile
.
Profile
)
*
DB
{
db
:=
&
DB
{
DSN
:
profile
.
DSN
,
mode
:
profile
.
Mode
,
profile
:
profile
,
}
return
db
}
func
(
db
*
DB
)
Open
()
(
err
error
)
{
// Ensure a DSN is set before attempting to open the database.
if
db
.
DSN
==
""
{
if
db
.
profile
.
DSN
==
""
{
return
fmt
.
Errorf
(
"dsn required"
)
}
// Connect to the database.
sqlDB
,
err
:=
sql
.
Open
(
"sqlite3"
,
db
.
DSN
)
sqlDB
,
err
:=
sql
.
Open
(
"sqlite3"
,
db
.
profile
.
DSN
)
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to open db with dsn: %s, err: %w"
,
db
.
DSN
,
err
)
return
fmt
.
Errorf
(
"failed to open db with dsn: %s, err: %w"
,
db
.
profile
.
DSN
,
err
)
}
db
.
Db
=
sqlDB
// If mode is dev, we should migrate and seed the database.
if
db
.
m
ode
==
"dev"
{
if
db
.
profile
.
M
ode
==
"dev"
{
if
err
:=
db
.
applyLatestSchema
();
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
}
...
...
@@ -63,7 +61,7 @@ func (db *DB) Open() (err error) {
}
}
else
{
// If db file not exists, we should migrate the database.
if
_
,
err
:=
os
.
Stat
(
db
.
DSN
);
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
if
_
,
err
:=
os
.
Stat
(
db
.
profile
.
DSN
);
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
err
:=
db
.
applyLatestSchema
()
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
...
...
@@ -74,7 +72,7 @@ func (db *DB) Open() (err error) {
return
fmt
.
Errorf
(
"failed to create migration_history table: %w"
,
err
)
}
currentVersion
:=
common
.
GetCurrentVersion
(
db
.
m
ode
)
currentVersion
:=
common
.
GetCurrentVersion
(
db
.
profile
.
M
ode
)
migrationHistory
,
err
:=
findMigrationHistory
(
db
.
Db
,
&
MigrationHistoryFind
{})
if
err
!=
nil
{
return
err
...
...
@@ -90,18 +88,34 @@ func (db *DB) Open() (err error) {
if
common
.
IsVersionGreaterThan
(
currentVersion
,
migrationHistory
.
Version
)
{
minorVersionList
:=
getMinorVersionList
()
// backup the raw database file before migration
rawBytes
,
err
:=
ioutil
.
ReadFile
(
db
.
profile
.
DSN
)
if
err
!=
nil
{
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
())
if
err
:=
ioutil
.
WriteFile
(
backupDBFilePath
,
rawBytes
,
0644
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to write raw database file, err: %w"
,
err
)
}
println
(
"succeed to copy a backup database file"
)
println
(
"start migrate"
)
for
_
,
minorVersion
:=
range
minorVersionList
{
normalizedVersion
:=
minorVersion
+
".0"
if
common
.
IsVersionGreaterThan
(
normalizedVersion
,
migrationHistory
.
Version
)
&&
common
.
IsVersionGreaterOrEqualThan
(
currentVersion
,
normalizedVersion
)
{
println
(
"applying migration for"
,
normalizedVersion
)
err
:=
db
.
applyMigrationForMinorVersion
(
minorVersion
)
if
err
!=
nil
{
if
err
:=
db
.
applyMigrationForMinorVersion
(
minorVersion
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply minor version migration: %w"
,
err
)
}
}
}
println
(
"end migrate"
)
// remove the created backup db file after migrate succeed
if
err
:=
os
.
Remove
(
backupDBFilePath
);
err
!=
nil
{
println
(
fmt
.
Sprintf
(
"Failed to remove temp database file, err %v"
,
err
))
}
}
}
}
...
...
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