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
0398df1d
Commit
0398df1d
authored
Jul 16, 2025
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: update migrator comments
parent
a8db1321
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
6 deletions
+21
-6
00__remove_webhook.sql
store/migration/sqlite/0.25/00__remove_webhook.sql
+0
-2
migrator.go
store/migrator.go
+21
-4
No files found.
store/migration/sqlite/0.25/00__remove_webhook.sql
View file @
0398df1d
DROP
INDEX
IF
EXISTS
idx_webhook_creator_id
;
DROP
TABLE
IF
EXISTS
webhook
;
store/migrator.go
View file @
0398df1d
...
...
@@ -33,13 +33,16 @@ const (
LatestSchemaFileName
=
"LATEST.sql"
)
// Migrate applies the latest schema to the database.
// Migrate migrates the database schema to the latest version.
// It checks the current schema version and applies any necessary migrations.
// It also seeds the database with initial data if in demo mode.
func
(
s
*
Store
)
Migrate
(
ctx
context
.
Context
)
error
{
if
err
:=
s
.
preMigrate
(
ctx
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to pre-migrate"
)
}
if
s
.
profile
.
Mode
==
"prod"
{
switch
s
.
profile
.
Mode
{
case
"prod"
:
workspaceBasicSetting
,
err
:=
s
.
GetWorkspaceBasicSetting
(
ctx
)
if
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to get workspace basic setting"
)
...
...
@@ -94,7 +97,7 @@ func (s *Store) Migrate(ctx context.Context) error {
return
errors
.
Wrap
(
err
,
"failed to update current schema version"
)
}
}
}
else
if
s
.
profile
.
Mode
==
"demo"
{
case
"demo"
:
// In demo mode, we should seed the database.
if
err
:=
s
.
seed
(
ctx
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to seed"
)
...
...
@@ -103,6 +106,7 @@ func (s *Store) Migrate(ctx context.Context) error {
return
nil
}
// preMigrate checks if the database is initialized and applies the latest schema if not.
func
(
s
*
Store
)
preMigrate
(
ctx
context
.
Context
)
error
{
initialized
,
err
:=
s
.
driver
.
IsInitialized
(
ctx
)
if
err
!=
nil
{
...
...
@@ -157,6 +161,9 @@ func (s *Store) getSeedBasePath() string {
return
fmt
.
Sprintf
(
"seed/%s/"
,
s
.
profile
.
Driver
)
}
// seed seeds the database with initial data.
// It reads all seed files from the embedded filesystem and executes them in order.
// This is only supported for SQLite databases.
func
(
s
*
Store
)
seed
(
ctx
context
.
Context
)
error
{
// Only seed for SQLite.
if
s
.
profile
.
Driver
!=
"sqlite"
{
...
...
@@ -205,6 +212,9 @@ func (s *Store) GetCurrentSchemaVersion() (string, error) {
return
s
.
getSchemaVersionOfMigrateScript
(
filePaths
[
len
(
filePaths
)
-
1
])
}
// getSchemaVersionOfMigrateScript extracts the schema version from the migration script file path.
// It returns the schema version in the format "major.minor.patch".
// If the file is the latest schema file, it returns the current schema version.
func
(
s
*
Store
)
getSchemaVersionOfMigrateScript
(
filePath
string
)
(
string
,
error
)
{
// If the file is the latest schema file, return the current schema version.
if
strings
.
HasSuffix
(
filePath
,
LatestSchemaFileName
)
{
...
...
@@ -225,7 +235,8 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
return
fmt
.
Sprintf
(
"%s.%d"
,
minorVersion
,
patchVersion
+
1
),
nil
}
// execute runs a single SQL statement within a transaction.
// execute executes a SQL statement within a transaction context.
// It returns an error if the execution fails.
func
(
*
Store
)
execute
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
stmt
string
)
error
{
if
_
,
err
:=
tx
.
ExecContext
(
ctx
,
stmt
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to execute statement"
)
...
...
@@ -233,6 +244,8 @@ func (*Store) execute(ctx context.Context, tx *sql.Tx, stmt string) error {
return
nil
}
// updateCurrentSchemaVersion updates the current schema version in the workspace basic setting.
// It retrieves the workspace basic setting, updates the schema version, and upserts the setting back to the database.
func
(
s
*
Store
)
updateCurrentSchemaVersion
(
ctx
context
.
Context
,
schemaVersion
string
)
error
{
workspaceBasicSetting
,
err
:=
s
.
GetWorkspaceBasicSetting
(
ctx
)
if
err
!=
nil
{
...
...
@@ -248,6 +261,8 @@ func (s *Store) updateCurrentSchemaVersion(ctx context.Context, schemaVersion st
return
nil
}
// normalizeMigrationHistoryList normalizes the migration history list.
// It checks the existing migration history and updates it to the latest schema version if necessary.
func
(
s
*
Store
)
normalizeMigrationHistoryList
(
ctx
context
.
Context
)
error
{
migrationHistoryList
,
err
:=
s
.
driver
.
FindMigrationHistoryList
(
ctx
,
&
FindMigrationHistory
{})
if
err
!=
nil
{
...
...
@@ -299,6 +314,8 @@ func (s *Store) normalizeMigrationHistoryList(ctx context.Context) error {
return
nil
}
// migrateSchemaVersionToSetting migrates the schema version from the migration history to the workspace basic setting.
// It retrieves the migration history, sorts the versions, and updates the workspace basic setting if necessary
func
(
s
*
Store
)
migrateSchemaVersionToSetting
(
ctx
context
.
Context
)
error
{
migrationHistoryList
,
err
:=
s
.
driver
.
FindMigrationHistoryList
(
ctx
,
&
FindMigrationHistory
{})
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