Skip to content

Commit e4d3e1c

Browse files
committedOct 18, 2022
Check ingest flag before executing horizon db migrate commands
1 parent 11b0616 commit e4d3e1c

File tree

3 files changed

+75
-12
lines changed

3 files changed

+75
-12
lines changed
 

‎services/horizon/cmd/db.go

+30-11
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"log"
99
"os"
1010
"strconv"
11+
"strings"
1112

1213
"github.com/spf13/cobra"
1314
"github.com/spf13/viper"
@@ -32,23 +33,36 @@ var dbMigrateCmd = &cobra.Command{
3233
Short: "commands to run schema migrations on horizon's postgres db",
3334
}
3435

35-
func requireAndSetFlag(name string) error {
36+
func requireAndSetFlags(names ...string) error {
37+
set := map[string]bool{}
38+
for _, name := range names {
39+
set[name] = true
40+
}
3641
for _, flag := range flags {
37-
if flag.Name == name {
42+
if set[flag.Name] {
3843
flag.Require()
39-
flag.SetValue()
40-
return nil
44+
if err := flag.SetValue(); err != nil {
45+
return err
46+
}
47+
delete(set, flag.Name)
4148
}
4249
}
43-
return fmt.Errorf("could not find %s flag", name)
50+
if len(set) == 0 {
51+
return nil
52+
}
53+
var missing []string
54+
for name := range set {
55+
missing = append(missing, name)
56+
}
57+
return fmt.Errorf("could not find %s flags", strings.Join(missing, ","))
4458
}
4559

4660
var dbInitCmd = &cobra.Command{
4761
Use: "init",
4862
Short: "install schema",
4963
Long: "init initializes the postgres database used by horizon.",
5064
RunE: func(cmd *cobra.Command, args []string) error {
51-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
65+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName, horizon.IngestFlagName); err != nil {
5266
return err
5367
}
5468

@@ -72,6 +86,11 @@ var dbInitCmd = &cobra.Command{
7286
}
7387

7488
func migrate(dir schema.MigrateDir, count int) error {
89+
if !config.Ingest {
90+
log.Println("Skipping migrations because ingest flag is not enabled")
91+
return nil
92+
}
93+
7594
dbConn, err := db.Open("postgres", config.DatabaseURL)
7695
if err != nil {
7796
return err
@@ -95,7 +114,7 @@ var dbMigrateDownCmd = &cobra.Command{
95114
Short: "run downwards db schema migrations",
96115
Long: "performs a downards schema migration command",
97116
RunE: func(cmd *cobra.Command, args []string) error {
98-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
117+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName, horizon.IngestFlagName); err != nil {
99118
return err
100119
}
101120

@@ -119,7 +138,7 @@ var dbMigrateRedoCmd = &cobra.Command{
119138
Short: "redo db schema migrations",
120139
Long: "performs a redo schema migration command",
121140
RunE: func(cmd *cobra.Command, args []string) error {
122-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
141+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName, horizon.IngestFlagName); err != nil {
123142
return err
124143
}
125144

@@ -143,7 +162,7 @@ var dbMigrateStatusCmd = &cobra.Command{
143162
Short: "print current database migration status",
144163
Long: "print current database migration status",
145164
RunE: func(cmd *cobra.Command, args []string) error {
146-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
165+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName); err != nil {
147166
return err
148167
}
149168

@@ -173,7 +192,7 @@ var dbMigrateUpCmd = &cobra.Command{
173192
Short: "run upwards db schema migrations",
174193
Long: "performs an upwards schema migration command",
175194
RunE: func(cmd *cobra.Command, args []string) error {
176-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
195+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName, horizon.IngestFlagName); err != nil {
177196
return err
178197
}
179198

@@ -453,7 +472,7 @@ var dbDetectGapsCmd = &cobra.Command{
453472
Short: "detects ingestion gaps in Horizon's database",
454473
Long: "detects ingestion gaps in Horizon's database and prints a list of reingest commands needed to fill the gaps",
455474
RunE: func(cmd *cobra.Command, args []string) error {
456-
if err := requireAndSetFlag(horizon.DatabaseURLFlagName); err != nil {
475+
if err := requireAndSetFlags(horizon.DatabaseURLFlagName); err != nil {
457476
return err
458477
}
459478

‎services/horizon/internal/flags.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
const (
2626
// DatabaseURLFlagName is the command line flag for configuring the Horizon postgres URL
2727
DatabaseURLFlagName = "db-url"
28+
// IngestFlagName is the command line flag for enabling ingestion on the Horizon instance
29+
IngestFlagName = "ingest"
2830
// StellarCoreDBURLFlagName is the command line flag for configuring the postgres Stellar Core URL
2931
StellarCoreDBURLFlagName = "stellar-core-db-url"
3032
// StellarCoreURLFlagName is the command line flag for configuring the URL fore Stellar Core HTTP endpoint
@@ -456,7 +458,7 @@ func Flags() (*Config, support.ConfigOptions) {
456458
Usage: "TLS private key file to use for securing connections to horizon",
457459
},
458460
&support.ConfigOption{
459-
Name: "ingest",
461+
Name: IngestFlagName,
460462
ConfigKey: &config.Ingest,
461463
OptType: types.Bool,
462464
FlagDefault: true,

‎services/horizon/internal/integration/db_test.go

+42
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,48 @@ func command(horizonConfig horizon.Config, args ...string) []string {
529529
}, args...)
530530
}
531531

532+
func TestMigrateIngestIsTrueByDefault(t *testing.T) {
533+
tt := assert.New(t)
534+
// Create a fresh Horizon database
535+
newDB := dbtest.Postgres(t)
536+
freshHorizonPostgresURL := newDB.DSN
537+
538+
horizoncmd.RootCmd.SetArgs([]string{
539+
// ingest is set to true by default
540+
"--db-url", freshHorizonPostgresURL,
541+
"db", "migrate", "up",
542+
})
543+
tt.NoError(horizoncmd.RootCmd.Execute())
544+
545+
dbConn, err := db.Open("postgres", freshHorizonPostgresURL)
546+
tt.NoError(err)
547+
548+
status, err := schema.Status(dbConn.DB.DB)
549+
tt.NoError(err)
550+
tt.NotContains(status, "1_initial_schema.sql\t\t\t\t\t\tno")
551+
}
552+
553+
func TestMigrateChecksIngestFlag(t *testing.T) {
554+
tt := assert.New(t)
555+
// Create a fresh Horizon database
556+
newDB := dbtest.Postgres(t)
557+
freshHorizonPostgresURL := newDB.DSN
558+
559+
horizoncmd.RootCmd.SetArgs([]string{
560+
"--ingest=false",
561+
"--db-url", freshHorizonPostgresURL,
562+
"db", "migrate", "up",
563+
})
564+
tt.NoError(horizoncmd.RootCmd.Execute())
565+
566+
dbConn, err := db.Open("postgres", freshHorizonPostgresURL)
567+
tt.NoError(err)
568+
569+
status, err := schema.Status(dbConn.DB.DB)
570+
tt.NoError(err)
571+
tt.Contains(status, "1_initial_schema.sql\t\t\t\t\t\tno")
572+
}
573+
532574
func TestFillGaps(t *testing.T) {
533575
itest, reachedLedger := initializeDBIntegrationTest(t)
534576
tt := assert.New(t)

0 commit comments

Comments
 (0)