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
fbdfaf85
Commit
fbdfaf85
authored
Aug 26, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update migrator
parent
ccd3fdcd
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
71 additions
and
6 deletions
+71
-6
version_test.go
server/version/version_test.go
+10
-0
migrator.go
store/migrator.go
+61
-6
No files found.
server/version/version_test.go
View file @
fbdfaf85
...
@@ -53,6 +53,11 @@ func TestIsVersionGreaterThan(t *testing.T) {
...
@@ -53,6 +53,11 @@ func TestIsVersionGreaterThan(t *testing.T) {
target
:
"0.8.0"
,
target
:
"0.8.0"
,
want
:
true
,
want
:
true
,
},
},
{
version
:
"0.23"
,
target
:
"0.22"
,
want
:
true
,
},
{
{
version
:
"0.8.0"
,
version
:
"0.8.0"
,
target
:
"0.10.0"
,
target
:
"0.10.0"
,
...
@@ -63,6 +68,11 @@ func TestIsVersionGreaterThan(t *testing.T) {
...
@@ -63,6 +68,11 @@ func TestIsVersionGreaterThan(t *testing.T) {
target
:
"0.9.1"
,
target
:
"0.9.1"
,
want
:
false
,
want
:
false
,
},
},
{
version
:
"0.22"
,
target
:
"0.22"
,
want
:
false
,
},
}
}
for
_
,
test
:=
range
tests
{
for
_
,
test
:=
range
tests
{
result
:=
IsVersionGreaterThan
(
test
.
version
,
test
.
target
)
result
:=
IsVersionGreaterThan
(
test
.
version
,
test
.
target
)
...
...
store/migrator.go
View file @
fbdfaf85
...
@@ -17,6 +17,12 @@ import (
...
@@ -17,6 +17,12 @@ import (
"github.com/usememos/memos/server/version"
"github.com/usememos/memos/server/version"
)
)
//go:embed migration
var
migrationFS
embed
.
FS
//go:embed seed
var
seedFS
embed
.
FS
const
(
const
(
// MigrateFileNameSplit is the split character between the patch version and the description in the migration file name.
// MigrateFileNameSplit is the split character between the patch version and the description in the migration file name.
// For example, "1__create_table.sql".
// For example, "1__create_table.sql".
...
@@ -26,12 +32,6 @@ const (
...
@@ -26,12 +32,6 @@ const (
LatestSchemaFileName
=
"LATEST_SCHEMA.sql"
LatestSchemaFileName
=
"LATEST_SCHEMA.sql"
)
)
//go:embed migration
var
migrationFS
embed
.
FS
//go:embed seed
var
seedFS
embed
.
FS
// Migrate applies the latest schema to the database.
// Migrate applies the latest schema to the database.
func
(
s
*
Store
)
Migrate
(
ctx
context
.
Context
)
error
{
func
(
s
*
Store
)
Migrate
(
ctx
context
.
Context
)
error
{
if
err
:=
s
.
preMigrate
(
ctx
);
err
!=
nil
{
if
err
:=
s
.
preMigrate
(
ctx
);
err
!=
nil
{
...
@@ -147,6 +147,9 @@ func (s *Store) preMigrate(ctx context.Context) error {
...
@@ -147,6 +147,9 @@ func (s *Store) preMigrate(ctx context.Context) error {
return
errors
.
Wrap
(
err
,
"failed to upsert migration history"
)
return
errors
.
Wrap
(
err
,
"failed to upsert migration history"
)
}
}
}
}
if
err
:=
s
.
normalizedMigrationHistoryList
(
ctx
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to normalize migration history list"
)
}
return
nil
return
nil
}
}
...
@@ -237,3 +240,55 @@ func (*Store) execute(ctx context.Context, tx *sql.Tx, stmt string) error {
...
@@ -237,3 +240,55 @@ func (*Store) execute(ctx context.Context, tx *sql.Tx, stmt string) error {
}
}
return
nil
return
nil
}
}
func
(
s
*
Store
)
normalizedMigrationHistoryList
(
ctx
context
.
Context
)
error
{
migrationHistoryList
,
err
:=
s
.
driver
.
FindMigrationHistoryList
(
ctx
,
&
FindMigrationHistory
{})
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to find migration history"
)
}
versions
:=
[]
string
{}
for
_
,
migrationHistory
:=
range
migrationHistoryList
{
versions
=
append
(
versions
,
migrationHistory
.
Version
)
}
sort
.
Sort
(
version
.
SortVersion
(
versions
))
latestVersion
:=
versions
[
len
(
versions
)
-
1
]
latestMinorVersion
:=
version
.
GetMinorVersion
(
latestVersion
)
// If the latest version is greater than 0.22, return.
// As of 0.22, the migration history is already normalized.
if
version
.
IsVersionGreaterThan
(
latestMinorVersion
,
"0.22"
)
{
return
nil
}
schemaVersionMap
:=
map
[
string
]
string
{}
filePaths
,
err
:=
fs
.
Glob
(
migrationFS
,
fmt
.
Sprintf
(
"%s/*/*.sql"
,
s
.
getMigrationBasePath
()))
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to read migration files"
)
}
sort
.
Strings
(
filePaths
)
for
_
,
filePath
:=
range
filePaths
{
fileSchemaVersion
,
err
:=
s
.
getSchemaVersionOfMigrateScript
(
filePath
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get schema version of migrate script"
)
}
schemaVersionMap
[
version
.
GetMinorVersion
((
fileSchemaVersion
))]
=
fileSchemaVersion
}
latestSchemaVersion
:=
schemaVersionMap
[
latestMinorVersion
]
if
latestSchemaVersion
==
""
{
return
errors
.
Errorf
(
"latest schema version not found"
)
}
if
version
.
IsVersionGreaterOrEqualThan
(
latestVersion
,
latestSchemaVersion
)
{
return
nil
}
// Start a transaction to insert the latest schema version to migration_history.
tx
,
err
:=
s
.
driver
.
GetDB
()
.
Begin
()
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to start transaction"
)
}
defer
tx
.
Rollback
()
if
err
:=
s
.
execute
(
ctx
,
tx
,
fmt
.
Sprintf
(
"INSERT INTO migration_history (version) VALUES ('%s')"
,
latestSchemaVersion
));
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to insert migration history"
)
}
return
tx
.
Commit
()
}
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