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
70837f88
Commit
70837f88
authored
Aug 26, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: fix linter
parent
525223c2
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
9 additions
and
10 deletions
+9
-10
migrator.go
store/migrator.go
+9
-10
No files found.
store/migrator.go
View file @
70837f88
...
@@ -18,12 +18,12 @@ import (
...
@@ -18,12 +18,12 @@ import (
)
)
const
(
const
(
// M
IGRATE_FILE_NAME_SPLIT
is the split character between the patch version and the description in the migration file name.
// M
igrateFileNameSplit
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".
M
IGRATE_FILE_NAME_SPLIT
=
"__"
M
igrateFileNameSplit
=
"__"
// L
ATEST_SCHEMA_FILE_NAME
is the name of the latest schema file.
// L
atestSchemaFileName
is the name of the latest schema file.
// This file is used to apply the latest schema when no migration history is found.
// This file is used to apply the latest schema when no migration history is found.
L
ATEST_SCHEMA_FILE_NAME
=
"LATEST__SCHEMA
.sql"
L
atestSchemaFileName
=
"latest_schema
.sql"
)
)
//go:embed migration
//go:embed migration
...
@@ -118,7 +118,7 @@ func (s *Store) preMigrate(ctx context.Context) error {
...
@@ -118,7 +118,7 @@ func (s *Store) preMigrate(ctx context.Context) error {
if
err
!=
nil
{
if
err
!=
nil
{
slog
.
Warn
(
"failed to find migration history in pre-migrate"
,
slog
.
String
(
"error"
,
err
.
Error
()))
slog
.
Warn
(
"failed to find migration history in pre-migrate"
,
slog
.
String
(
"error"
,
err
.
Error
()))
}
}
filePath
:=
s
.
getMigrationBasePath
()
+
L
ATEST_SCHEMA_FILE_NAME
filePath
:=
s
.
getMigrationBasePath
()
+
L
atestSchemaFileName
bytes
,
err
:=
migrationFS
.
ReadFile
(
filePath
)
bytes
,
err
:=
migrationFS
.
ReadFile
(
filePath
)
if
err
!=
nil
{
if
err
!=
nil
{
return
errors
.
Errorf
(
"failed to read latest schema file: %s"
,
err
)
return
errors
.
Errorf
(
"failed to read latest schema file: %s"
,
err
)
...
@@ -206,14 +206,13 @@ func (s *Store) GetCurrentSchemaVersion() (string, error) {
...
@@ -206,14 +206,13 @@ func (s *Store) GetCurrentSchemaVersion() (string, error) {
sort
.
Strings
(
filePaths
)
sort
.
Strings
(
filePaths
)
if
len
(
filePaths
)
==
0
{
if
len
(
filePaths
)
==
0
{
return
fmt
.
Sprintf
(
"%s.0"
,
minorVersion
),
nil
return
fmt
.
Sprintf
(
"%s.0"
,
minorVersion
),
nil
}
else
{
return
s
.
getSchemaVersionOfMigrateScript
(
filePaths
[
len
(
filePaths
)
-
1
])
}
}
return
s
.
getSchemaVersionOfMigrateScript
(
filePaths
[
len
(
filePaths
)
-
1
])
}
}
func
(
s
*
Store
)
getSchemaVersionOfMigrateScript
(
filePath
string
)
(
string
,
error
)
{
func
(
s
*
Store
)
getSchemaVersionOfMigrateScript
(
filePath
string
)
(
string
,
error
)
{
// If the file is the latest schema file, return the current schema version.
// If the file is the latest schema file, return the current schema version.
if
strings
.
HasSuffix
(
filePath
,
L
ATEST_SCHEMA_FILE_NAME
)
{
if
strings
.
HasSuffix
(
filePath
,
L
atestSchemaFileName
)
{
return
s
.
GetCurrentSchemaVersion
()
return
s
.
GetCurrentSchemaVersion
()
}
}
...
@@ -223,7 +222,7 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
...
@@ -223,7 +222,7 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
return
""
,
errors
.
Errorf
(
"invalid file path: %s"
,
filePath
)
return
""
,
errors
.
Errorf
(
"invalid file path: %s"
,
filePath
)
}
}
minorVersion
:=
elements
[
len
(
elements
)
-
2
]
minorVersion
:=
elements
[
len
(
elements
)
-
2
]
rawPatchVersion
:=
strings
.
Split
(
elements
[
len
(
elements
)
-
1
],
M
IGRATE_FILE_NAME_SPLIT
)[
0
]
rawPatchVersion
:=
strings
.
Split
(
elements
[
len
(
elements
)
-
1
],
M
igrateFileNameSplit
)[
0
]
patchVersion
,
err
:=
strconv
.
Atoi
(
rawPatchVersion
)
patchVersion
,
err
:=
strconv
.
Atoi
(
rawPatchVersion
)
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
errors
.
Wrapf
(
err
,
"failed to convert patch version to int: %s"
,
rawPatchVersion
)
return
""
,
errors
.
Wrapf
(
err
,
"failed to convert patch version to int: %s"
,
rawPatchVersion
)
...
@@ -232,7 +231,7 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
...
@@ -232,7 +231,7 @@ func (s *Store) getSchemaVersionOfMigrateScript(filePath string) (string, error)
}
}
// execute runs a single SQL statement within a transaction.
// execute runs a single SQL statement within a transaction.
func
(
s
*
Store
)
execute
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
stmt
string
)
error
{
func
(
*
Store
)
execute
(
ctx
context
.
Context
,
tx
*
sql
.
Tx
,
stmt
string
)
error
{
if
_
,
err
:=
tx
.
ExecContext
(
ctx
,
stmt
);
err
!=
nil
{
if
_
,
err
:=
tx
.
ExecContext
(
ctx
,
stmt
);
err
!=
nil
{
return
errors
.
Wrap
(
err
,
"failed to execute statement"
)
return
errors
.
Wrap
(
err
,
"failed to execute statement"
)
}
}
...
...
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