good morning!!!!
Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
E
Erigon
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Harbor Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
a
Erigon
Commits
088bb580
Unverified
Commit
088bb580
authored
4 years ago
by
Alex Sharov
Committed by
GitHub
4 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Migrations validation: duplicate names and commit call (#902)
parent
3899f2de
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
migrations/migrations.go
+24
-0
24 additions, 0 deletions
migrations/migrations.go
migrations/migrations_test.go
+47
-0
47 additions, 0 deletions
migrations/migrations_test.go
with
71 additions
and
0 deletions
migrations/migrations.go
+
24
−
0
View file @
088bb580
...
...
@@ -3,6 +3,7 @@ package migrations
import
(
"bytes"
"errors"
"fmt"
"github.com/ledgerwatch/turbo-geth/common"
"github.com/ledgerwatch/turbo-geth/common/dbutils"
...
...
@@ -63,6 +64,11 @@ type Migration struct {
Up
func
(
db
ethdb
.
Database
,
dataDir
string
,
OnLoadCommit
etl
.
LoadCommitHandler
)
error
}
var
(
ErrMigrationNonUniqueName
=
fmt
.
Errorf
(
"please provide unique migration name"
)
ErrMigrationCommitNotCalled
=
fmt
.
Errorf
(
"migraion commit function was not called"
)
)
func
NewMigrator
()
*
Migrator
{
return
&
Migrator
{
Migrations
:
migrations
,
...
...
@@ -96,16 +102,31 @@ func (m *Migrator) Apply(db ethdb.Database, datadir string) error {
return
err
}
// migration names must be unique, protection against people's mistake
uniqueNameCheck
:=
map
[
string
]
bool
{}
for
i
:=
range
m
.
Migrations
{
_
,
ok
:=
uniqueNameCheck
[
m
.
Migrations
[
i
]
.
Name
]
if
ok
{
return
fmt
.
Errorf
(
"%w, duplicate: %s"
,
ErrMigrationNonUniqueName
,
m
.
Migrations
[
i
]
.
Name
)
}
uniqueNameCheck
[
m
.
Migrations
[
i
]
.
Name
]
=
true
}
for
i
:=
range
m
.
Migrations
{
v
:=
m
.
Migrations
[
i
]
if
_
,
ok
:=
applied
[
v
.
Name
];
ok
{
continue
}
commitFuncCalled
:=
false
// commit function must be called if no error, protection against people's mistake
log
.
Info
(
"Apply migration"
,
"name"
,
v
.
Name
)
if
err
:=
v
.
Up
(
db
,
datadir
,
func
(
putter
ethdb
.
Putter
,
key
[]
byte
,
isDone
bool
)
error
{
if
!
isDone
{
return
nil
// don't save partial progress
}
commitFuncCalled
=
true
stagesProgress
,
err
:=
MarshalMigrationPayload
(
db
)
if
err
!=
nil
{
return
err
...
...
@@ -119,6 +140,9 @@ func (m *Migrator) Apply(db ethdb.Database, datadir string) error {
return
err
}
if
!
commitFuncCalled
{
return
ErrMigrationCommitNotCalled
}
log
.
Info
(
"Applied migration"
,
"name"
,
v
.
Name
)
}
return
nil
...
...
This diff is collapsed.
Click to expand it.
migrations/migrations_test.go
+
47
−
0
View file @
088bb580
package
migrations
import
(
"errors"
"github.com/ledgerwatch/turbo-geth/eth/stagedsync/stages"
"testing"
...
...
@@ -152,3 +153,49 @@ func TestMarshalStages(t *testing.T) {
require
.
True
(
ok
)
require
.
NotNil
(
v
)
}
func
TestValidation
(
t
*
testing
.
T
)
{
require
,
db
:=
require
.
New
(
t
),
ethdb
.
NewMemDatabase
()
migrations
=
[]
Migration
{
{
Name
:
"repeated_name"
,
Up
:
func
(
db
ethdb
.
Database
,
datadir
string
,
OnLoadCommit
etl
.
LoadCommitHandler
)
error
{
return
OnLoadCommit
(
db
,
nil
,
true
)
},
},
{
Name
:
"repeated_name"
,
Up
:
func
(
db
ethdb
.
Database
,
datadir
string
,
OnLoadCommit
etl
.
LoadCommitHandler
)
error
{
return
OnLoadCommit
(
db
,
nil
,
true
)
},
},
}
migrator
:=
NewMigrator
()
migrator
.
Migrations
=
migrations
err
:=
migrator
.
Apply
(
db
,
""
)
require
.
True
(
errors
.
Is
(
err
,
ErrMigrationNonUniqueName
))
applied
,
err
:=
AppliedMigrations
(
db
,
false
)
require
.
NoError
(
err
)
require
.
Equal
(
0
,
len
(
applied
))
}
func
TestCommitCallRequired
(
t
*
testing
.
T
)
{
require
,
db
:=
require
.
New
(
t
),
ethdb
.
NewMemDatabase
()
migrations
=
[]
Migration
{
{
Name
:
"one"
,
Up
:
func
(
db
ethdb
.
Database
,
datadir
string
,
OnLoadCommit
etl
.
LoadCommitHandler
)
error
{
return
nil
// don't call OnLoadCommit
},
},
}
migrator
:=
NewMigrator
()
migrator
.
Migrations
=
migrations
err
:=
migrator
.
Apply
(
db
,
""
)
require
.
True
(
errors
.
Is
(
err
,
ErrMigrationCommitNotCalled
))
applied
,
err
:=
AppliedMigrations
(
db
,
false
)
require
.
NoError
(
err
)
require
.
Equal
(
0
,
len
(
applied
))
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment