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
045819c3
Unverified
Commit
045819c3
authored
Nov 27, 2022
by
boojack
Committed by
GitHub
Nov 27, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: initial database schema (#601)
parent
2fa01886
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
45 deletions
+46
-45
db.go
store/db/db.go
+46
-45
No files found.
store/db/db.go
View file @
045819c3
...
@@ -49,8 +49,8 @@ func (db *DB) Open(ctx context.Context) (err error) {
...
@@ -49,8 +49,8 @@ func (db *DB) Open(ctx context.Context) (err error) {
}
}
db
.
Db
=
sqlDB
db
.
Db
=
sqlDB
// If mode is dev, we should migrate and seed the database.
if
db
.
profile
.
Mode
==
"dev"
{
if
db
.
profile
.
Mode
==
"dev"
{
// In dev mode, we should migrate and seed the database.
if
_
,
err
:=
os
.
Stat
(
db
.
profile
.
DSN
);
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
if
_
,
err
:=
os
.
Stat
(
db
.
profile
.
DSN
);
errors
.
Is
(
err
,
os
.
ErrNotExist
)
{
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
...
@@ -65,19 +65,20 @@ func (db *DB) Open(ctx context.Context) (err error) {
...
@@ -65,19 +65,20 @@ func (db *DB) Open(ctx context.Context) (err error) {
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
if
err
:=
db
.
applyLatestSchema
(
ctx
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to apply latest schema: %w"
,
err
)
}
}
}
else
{
}
currentVersion
:=
version
.
GetCurrentVersion
(
db
.
profile
.
Mode
)
currentVersion
:=
version
.
GetCurrentVersion
(
db
.
profile
.
Mode
)
migrationHistory
,
err
:=
db
.
FindMigrationHistory
(
ctx
,
&
MigrationHistoryFind
{})
migrationHistory
,
err
:=
db
.
FindMigrationHistory
(
ctx
,
&
MigrationHistoryFind
{})
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"failed to find migration history, err: %w"
,
err
)
}
}
if
migrationHistory
==
nil
{
if
migrationHistory
==
nil
{
migrationHistory
,
err
=
db
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
if
_
,
err
=
db
.
UpsertMigrationHistory
(
ctx
,
&
MigrationHistoryUpsert
{
Version
:
currentVersion
,
Version
:
currentVersion
,
})
});
err
!=
nil
{
if
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to upsert migration history, err: %w"
,
err
)
return
err
}
}
return
nil
}
}
if
version
.
IsVersionGreaterThan
(
version
.
GetSchemaVersion
(
currentVersion
),
migrationHistory
.
Version
)
{
if
version
.
IsVersionGreaterThan
(
version
.
GetSchemaVersion
(
currentVersion
),
migrationHistory
.
Version
)
{
...
@@ -92,8 +93,8 @@ func (db *DB) Open(ctx context.Context) (err error) {
...
@@ -92,8 +93,8 @@ func (db *DB) Open(ctx context.Context) (err error) {
if
err
:=
os
.
WriteFile
(
backupDBFilePath
,
rawBytes
,
0644
);
err
!=
nil
{
if
err
:=
os
.
WriteFile
(
backupDBFilePath
,
rawBytes
,
0644
);
err
!=
nil
{
return
fmt
.
Errorf
(
"failed to write raw database file, err: %w"
,
err
)
return
fmt
.
Errorf
(
"failed to write raw database file, err: %w"
,
err
)
}
}
println
(
"succeed to copy a backup database file"
)
println
(
"succeed to copy a backup database file"
)
println
(
"start migrate"
)
println
(
"start migrate"
)
for
_
,
minorVersion
:=
range
minorVersionList
{
for
_
,
minorVersion
:=
range
minorVersionList
{
normalizedVersion
:=
minorVersion
+
".0"
normalizedVersion
:=
minorVersion
+
".0"
...
@@ -104,15 +105,14 @@ func (db *DB) Open(ctx context.Context) (err error) {
...
@@ -104,15 +105,14 @@ func (db *DB) Open(ctx context.Context) (err error) {
}
}
}
}
}
}
println
(
"end migrate"
)
println
(
"end migrate"
)
// remove the created backup db file after migrate succeed
// remove the created backup db file after migrate succeed
if
err
:=
os
.
Remove
(
backupDBFilePath
);
err
!=
nil
{
if
err
:=
os
.
Remove
(
backupDBFilePath
);
err
!=
nil
{
println
(
fmt
.
Sprintf
(
"Failed to remove temp database file, err %v"
,
err
))
println
(
fmt
.
Sprintf
(
"Failed to remove temp database file, err %v"
,
err
))
}
}
}
}
}
}
}
return
nil
return
nil
}
}
...
@@ -137,7 +137,7 @@ func (db *DB) applyLatestSchema(ctx context.Context) error {
...
@@ -137,7 +137,7 @@ func (db *DB) applyLatestSchema(ctx context.Context) error {
func
(
db
*
DB
)
applyMigrationForMinorVersion
(
ctx
context
.
Context
,
minorVersion
string
)
error
{
func
(
db
*
DB
)
applyMigrationForMinorVersion
(
ctx
context
.
Context
,
minorVersion
string
)
error
{
filenames
,
err
:=
fs
.
Glob
(
migrationFS
,
fmt
.
Sprintf
(
"%s/%s/*.sql"
,
"migration/prod"
,
minorVersion
))
filenames
,
err
:=
fs
.
Glob
(
migrationFS
,
fmt
.
Sprintf
(
"%s/%s/*.sql"
,
"migration/prod"
,
minorVersion
))
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"failed to read ddl files, err: %w"
,
err
)
}
}
sort
.
Strings
(
filenames
)
sort
.
Strings
(
filenames
)
...
@@ -163,10 +163,11 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
...
@@ -163,10 +163,11 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
defer
tx
.
Rollback
()
defer
tx
.
Rollback
()
// upsert the newest version to migration_history
// upsert the newest version to migration_history
version
:=
minorVersion
+
".0"
if
_
,
err
=
upsertMigrationHistory
(
ctx
,
tx
,
&
MigrationHistoryUpsert
{
if
_
,
err
=
upsertMigrationHistory
(
ctx
,
tx
,
&
MigrationHistoryUpsert
{
Version
:
minorVersion
+
".0"
,
Version
:
version
,
});
err
!=
nil
{
});
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"failed to upsert migration history with version: %s, err: %w"
,
version
,
err
)
}
}
return
tx
.
Commit
()
return
tx
.
Commit
()
...
@@ -175,7 +176,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
...
@@ -175,7 +176,7 @@ func (db *DB) applyMigrationForMinorVersion(ctx context.Context, minorVersion st
func
(
db
*
DB
)
seed
(
ctx
context
.
Context
)
error
{
func
(
db
*
DB
)
seed
(
ctx
context
.
Context
)
error
{
filenames
,
err
:=
fs
.
Glob
(
seedFS
,
fmt
.
Sprintf
(
"%s/*.sql"
,
"seed"
))
filenames
,
err
:=
fs
.
Glob
(
seedFS
,
fmt
.
Sprintf
(
"%s/*.sql"
,
"seed"
))
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"failed to read seed files, err: %w"
,
err
)
}
}
sort
.
Strings
(
filenames
)
sort
.
Strings
(
filenames
)
...
@@ -203,7 +204,7 @@ func (db *DB) execute(ctx context.Context, stmt string) error {
...
@@ -203,7 +204,7 @@ func (db *DB) execute(ctx context.Context, stmt string) error {
defer
tx
.
Rollback
()
defer
tx
.
Rollback
()
if
_
,
err
:=
tx
.
ExecContext
(
ctx
,
stmt
);
err
!=
nil
{
if
_
,
err
:=
tx
.
ExecContext
(
ctx
,
stmt
);
err
!=
nil
{
return
err
return
fmt
.
Errorf
(
"failed to execute statement, err: %w"
,
err
)
}
}
return
tx
.
Commit
()
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