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
3c58953e
Commit
3c58953e
authored
Jul 01, 2022
by
boojack
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: add version checker
parent
0d317839
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
64 additions
and
2 deletions
+64
-2
db.go
store/db/db.go
+10
-2
migration_history.go
store/db/migration_history.go
+54
-0
No files found.
store/db/db.go
View file @
3c58953e
...
...
@@ -153,12 +153,20 @@ func (db *DB) compareMigrationHistory() error {
}
currentVersion
:=
common
.
Version
migrationHistory
,
err
:=
upsertMigrationHistory
(
db
.
Db
,
currentVersion
)
migrationHistoryFind
:=
MigrationHistoryFind
{
Version
:
currentVersion
,
}
migrationHistory
,
err
:=
findMigrationHistory
(
db
.
Db
,
&
migrationHistoryFind
)
if
err
!=
nil
{
return
err
}
if
migrationHistory
==
nil
{
return
fmt
.
Errorf
(
"failed to upsert migration history"
)
// ...do schema migration,
// then upsert the newest version to migration_history
_
,
err
:=
upsertMigrationHistory
(
db
.
Db
,
currentVersion
)
if
err
!=
nil
{
return
err
}
}
return
nil
...
...
store/db/migration_history.go
View file @
3c58953e
...
...
@@ -2,6 +2,7 @@ package db
import
(
"database/sql"
"strings"
)
type
MigrationHistory
struct
{
...
...
@@ -9,6 +10,59 @@ type MigrationHistory struct {
Version
string
}
type
MigrationHistoryFind
struct
{
Version
string
}
func
findMigrationHistoryList
(
db
*
sql
.
DB
,
find
*
MigrationHistoryFind
)
([]
*
MigrationHistory
,
error
)
{
where
,
args
:=
[]
string
{
"1 = 1"
},
[]
interface
{}{}
where
,
args
=
append
(
where
,
"version = ?"
),
append
(
args
,
find
.
Version
)
rows
,
err
:=
db
.
Query
(
`
SELECT
version,
created_ts
FROM
migration_history
WHERE `
+
strings
.
Join
(
where
,
" AND "
)
+
`
ORDER BY created_ts DESC`
,
args
...
,
)
if
err
!=
nil
{
return
nil
,
err
}
defer
rows
.
Close
()
migrationHistoryList
:=
make
([]
*
MigrationHistory
,
0
)
for
rows
.
Next
()
{
var
migrationHistory
MigrationHistory
if
err
:=
rows
.
Scan
(
&
migrationHistory
.
Version
,
&
migrationHistory
.
CreatedTs
,
);
err
!=
nil
{
return
nil
,
err
}
migrationHistoryList
=
append
(
migrationHistoryList
,
&
migrationHistory
)
}
return
migrationHistoryList
,
nil
}
func
findMigrationHistory
(
db
*
sql
.
DB
,
find
*
MigrationHistoryFind
)
(
*
MigrationHistory
,
error
)
{
list
,
err
:=
findMigrationHistoryList
(
db
,
find
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
list
)
==
0
{
return
nil
,
nil
}
else
{
return
list
[
0
],
nil
}
}
func
upsertMigrationHistory
(
db
*
sql
.
DB
,
version
string
)
(
*
MigrationHistory
,
error
)
{
row
,
err
:=
db
.
Query
(
`
INSERT INTO migration_history (
...
...
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