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
1da07529
Commit
1da07529
authored
Sep 14, 2024
by
johnnyjoy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: add schema version to workspace setting
parent
b787d1c7
Changes
4
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
221 additions
and
318 deletions
+221
-318
apidocs.swagger.yaml
docs/apidocs.swagger.yaml
+64
-201
workspace_setting.pb.go
proto/gen/store/workspace_setting.pb.go
+129
-117
workspace_setting.proto
proto/store/workspace_setting.proto
+3
-0
migrator.go
store/migrator.go
+25
-0
No files found.
docs/apidocs.swagger.yaml
View file @
1da07529
This diff is collapsed.
Click to expand it.
proto/gen/store/workspace_setting.pb.go
View file @
1da07529
This diff is collapsed.
Click to expand it.
proto/store/workspace_setting.proto
View file @
1da07529
...
@@ -27,7 +27,10 @@ message WorkspaceSetting {
...
@@ -27,7 +27,10 @@ message WorkspaceSetting {
}
}
message
WorkspaceBasicSetting
{
message
WorkspaceBasicSetting
{
// The secret key for workspace. Mainly used for session management.
string
secret_key
=
1
;
string
secret_key
=
1
;
// The current schema version of database.
string
schema_version
=
2
;
}
}
message
WorkspaceGeneralSetting
{
message
WorkspaceGeneralSetting
{
...
...
store/migrator.go
View file @
1da07529
...
@@ -14,6 +14,7 @@ import (
...
@@ -14,6 +14,7 @@ import (
"github.com/pkg/errors"
"github.com/pkg/errors"
storepb
"github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/version"
"github.com/usememos/memos/server/version"
)
)
...
@@ -96,11 +97,15 @@ func (s *Store) Migrate(ctx context.Context) error {
...
@@ -96,11 +97,15 @@ func (s *Store) Migrate(ctx context.Context) error {
slog
.
Info
(
"end migrate"
)
slog
.
Info
(
"end migrate"
)
// Upsert the current schema version to migration_history.
// Upsert the current schema version to migration_history.
// TODO: retire using migration history later.
if
_
,
err
=
s
.
driver
.
UpsertMigrationHistory
(
ctx
,
&
UpsertMigrationHistory
{
if
_
,
err
=
s
.
driver
.
UpsertMigrationHistory
(
ctx
,
&
UpsertMigrationHistory
{
Version
:
schemaVersion
,
Version
:
schemaVersion
,
});
err
!=
nil
{
});
err
!=
nil
{
return
errors
.
Wrapf
(
err
,
"failed to upsert migration history with version: %s"
,
schemaVersion
)
return
errors
.
Wrapf
(
err
,
"failed to upsert migration history with version: %s"
,
schemaVersion
)
}
}
if
err
:=
s
.
updateCurrentSchemaVersion
(
ctx
,
schemaVersion
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to update current schema version"
)
}
}
}
}
else
if
s
.
Profile
.
Mode
==
"demo"
{
}
else
if
s
.
Profile
.
Mode
==
"demo"
{
// In demo mode, we should seed the database.
// In demo mode, we should seed the database.
...
@@ -112,6 +117,7 @@ func (s *Store) Migrate(ctx context.Context) error {
...
@@ -112,6 +117,7 @@ func (s *Store) Migrate(ctx context.Context) error {
}
}
func
(
s
*
Store
)
preMigrate
(
ctx
context
.
Context
)
error
{
func
(
s
*
Store
)
preMigrate
(
ctx
context
.
Context
)
error
{
// TODO: using schema version in basic setting instead of migration history.
migrationHistoryList
,
err
:=
s
.
driver
.
FindMigrationHistoryList
(
ctx
,
&
FindMigrationHistory
{})
migrationHistoryList
,
err
:=
s
.
driver
.
FindMigrationHistoryList
(
ctx
,
&
FindMigrationHistory
{})
// If any error occurs or no migration history found, apply the latest schema.
// If any error occurs or no migration history found, apply the latest schema.
if
err
!=
nil
||
len
(
migrationHistoryList
)
==
0
{
if
err
!=
nil
||
len
(
migrationHistoryList
)
==
0
{
...
@@ -141,11 +147,15 @@ func (s *Store) preMigrate(ctx context.Context) error {
...
@@ -141,11 +147,15 @@ func (s *Store) preMigrate(ctx context.Context) error {
return
errors
.
Wrap
(
err
,
"failed to commit transaction"
)
return
errors
.
Wrap
(
err
,
"failed to commit transaction"
)
}
}
// TODO: using schema version in basic setting instead of migration history.
if
_
,
err
:=
s
.
driver
.
UpsertMigrationHistory
(
ctx
,
&
UpsertMigrationHistory
{
if
_
,
err
:=
s
.
driver
.
UpsertMigrationHistory
(
ctx
,
&
UpsertMigrationHistory
{
Version
:
schemaVersion
,
Version
:
schemaVersion
,
});
err
!=
nil
{
});
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to upsert migration history"
)
return
errors
.
Wrap
(
err
,
"failed to upsert migration history"
)
}
}
if
err
:=
s
.
updateCurrentSchemaVersion
(
ctx
,
schemaVersion
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to update current schema version"
)
}
}
}
if
s
.
Profile
.
Mode
==
"prod"
{
if
s
.
Profile
.
Mode
==
"prod"
{
if
err
:=
s
.
normalizedMigrationHistoryList
(
ctx
);
err
!=
nil
{
if
err
:=
s
.
normalizedMigrationHistoryList
(
ctx
);
err
!=
nil
{
...
@@ -295,3 +305,18 @@ func (s *Store) normalizedMigrationHistoryList(ctx context.Context) error {
...
@@ -295,3 +305,18 @@ func (s *Store) normalizedMigrationHistoryList(ctx context.Context) error {
}
}
return
tx
.
Commit
()
return
tx
.
Commit
()
}
}
func
(
s
*
Store
)
updateCurrentSchemaVersion
(
ctx
context
.
Context
,
schemaVersion
string
)
error
{
workspaceBasicSetting
,
err
:=
s
.
GetWorkspaceBasicSetting
(
ctx
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get workspace basic setting"
)
}
workspaceBasicSetting
.
SchemaVersion
=
schemaVersion
if
_
,
err
:=
s
.
UpsertWorkspaceSetting
(
ctx
,
&
storepb
.
WorkspaceSetting
{
Key
:
storepb
.
WorkspaceSettingKey_BASIC
,
Value
:
&
storepb
.
WorkspaceSetting_BasicSetting
{
BasicSetting
:
workspaceBasicSetting
},
});
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to upsert workspace setting"
)
}
return
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