Skip to content
This repository has been archived by the owner on Jan 22, 2020. It is now read-only.

Commit

Permalink
Add db clear command
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstyle committed Mar 20, 2017
1 parent 345a15b commit b0e5f48
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 16 deletions.
54 changes: 38 additions & 16 deletions src/github.com/stellar/horizon/cmd/horizon/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ var dbCmd = &cobra.Command{
Short: "commands to manage horizon's postgres db",
}

var dbClearCmd = &cobra.Command{
Use: "clear",
Short: "clears all imported historical data",
Run: func(cmd *cobra.Command, args []string) {
initConfig()
hlog.DefaultLogger.Logger.Level = config.LogLevel

i := ingestSystem()
err := i.ClearAll()
if err != nil {
hlog.Error(err)
os.Exit(1)
}
},
}

var dbInitCmd = &cobra.Command{
Use: "init",
Short: "install schema",
Expand Down Expand Up @@ -101,22 +117,7 @@ var dbReingestCmd = &cobra.Command{
initConfig()
hlog.DefaultLogger.Logger.Level = config.LogLevel

hdb, err := db.Open("postgres", config.DatabaseURL)
if err != nil {
log.Fatal(err)
}

cdb, err := db.Open("postgres", config.StellarCoreDatabaseURL)
if err != nil {
log.Fatal(err)
}

passphrase := viper.GetString("network-passphrase")
if passphrase == "" {
log.Fatal("network-passphrase is blank: reingestion requires manually setting passphrase")
}

i := ingest.New(passphrase, config.StellarCoreURL, cdb, hdb)
i := ingestSystem()
i.SkipCursorUpdate = true
logStatus := func(stage string) {
count := i.Metrics.IngestLedgerTimer.Count()
Expand Down Expand Up @@ -159,11 +160,32 @@ var dbReingestCmd = &cobra.Command{

func init() {
dbCmd.AddCommand(dbInitCmd)
dbCmd.AddCommand(dbClearCmd)
dbCmd.AddCommand(dbMigrateCmd)
dbCmd.AddCommand(dbReapCmd)
dbCmd.AddCommand(dbReingestCmd)
}

func ingestSystem() *ingest.System {
hdb, err := db.Open("postgres", config.DatabaseURL)
if err != nil {
log.Fatal(err)
}

cdb, err := db.Open("postgres", config.StellarCoreDatabaseURL)
if err != nil {
log.Fatal(err)
}

passphrase := viper.GetString("network-passphrase")
if passphrase == "" {
log.Fatal("network-passphrase is blank: reingestion requires manually setting passphrase")
}

i := ingest.New(passphrase, config.StellarCoreURL, cdb, hdb)
return i
}

func reingest(i *ingest.System, args []string) (int, error) {
if len(args) == 0 {
count, err := i.ReingestAll()
Expand Down
7 changes: 7 additions & 0 deletions src/github.com/stellar/horizon/ingest/ingestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"time"

"math"

"github.com/guregu/null"
sq "github.com/lann/squirrel"
"github.com/stellar/go/xdr"
Expand All @@ -13,6 +15,11 @@ import (
"github.com/stellar/horizon/db2/sqx"
)

// ClearAll clears the entire history database
func (ingest *Ingestion) ClearAll() error {
return ingest.Clear(0, math.MaxInt64)
}

// Clear removes a range of data from the history database, exclusive of the end
// id provided.
func (ingest *Ingestion) Clear(start int64, end int64) error {
Expand Down
27 changes: 27 additions & 0 deletions src/github.com/stellar/horizon/ingest/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,33 @@ import (
"github.com/stellar/horizon/toid"
)

// ClearAll removes all previously ingested historical data from the horizon
// database.
func (i *System) ClearAll() error {

hdb := i.HorizonDB.Clone()
ingestion := &Ingestion{DB: hdb}

err := ingestion.Start()
if err != nil {
return errors.Wrap(err, "failed to begin ingestion")
}

err = ingestion.ClearAll()
if err != nil {
return errors.Wrap(err, "failed to clear history tables")
}

err = ingestion.Close()
if err != nil {
return errors.Wrap(err, "failed to close ingestion")
}

log.Infof("cleared all history")

return nil
}

// ReingestAll re-ingests all ledgers
func (i *System) ReingestAll() (int, error) {

Expand Down
16 changes: 16 additions & 0 deletions src/github.com/stellar/horizon/ingest/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@ import (
"github.com/stellar/horizon/test"
)

func TestClearAll(t *testing.T) {
tt := test.Start(t).Scenario("kahuna")
defer tt.Finish()
is := sys(tt)

err := is.ClearAll()

tt.Require.NoError(err)

// ensure no ledgers
var found int
err = tt.HorizonRepo().GetRaw(&found, "SELECT COUNT(*) FROM history_ledgers")
tt.Require.NoError(err)
tt.Assert.Equal(0, found)
}

func TestValidation(t *testing.T) {
tt := test.Start(t).ScenarioWithoutHorizon("kahuna")
defer tt.Finish()
Expand Down

0 comments on commit b0e5f48

Please sign in to comment.