diff --git a/CHANGELOG.md b/CHANGELOG.md index 77914d1e..ff0a61af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - ([\#77](https://github.com/forbole/juno/pull/77)) Add wait group to handle messages concurrently - ([\#79](https://github.com/forbole/juno/pull/79)) Use `sqlx` instead of `sql` while dealing with a PostgreSQL database - ([\#83](https://github.com/forbole/juno/pull/83)) Bump `github.com/tendermint/tendermint` to `v0.34.22` +- ([\#84](https://github.com/forbole/juno/pull/84)) Replace database configuration params with URI ## v3.4.0 ### Changes diff --git a/cmd/migrate/cmd.go b/cmd/migrate/cmd.go index 0e2565b9..bd48b6ad 100644 --- a/cmd/migrate/cmd.go +++ b/cmd/migrate/cmd.go @@ -8,16 +8,14 @@ import ( "github.com/spf13/cobra" - v2 "github.com/forbole/juno/v3/cmd/migrate/v2" - v3 "github.com/forbole/juno/v3/cmd/migrate/v3" + v4 "github.com/forbole/juno/v3/cmd/migrate/v4" ) type Migrator func(parseCfg *parsecmdtypes.Config) error var ( migrations = map[string]Migrator{ - "v2": v2.RunMigration, - "v3": v3.RunMigration, + "v4": v4.RunMigration, } ) @@ -35,8 +33,7 @@ func NewMigrateCmd(appName string, parseConfig *parsecmdtypes.Config) *cobra.Com Use: "migrate [to-version]", Short: "Perform the migrations from the current version to the specified one", Long: `Migrates all the necessary things (config file, database, etc) from the current version to the new one. -If you are upgrading from a very old version to the latest one, migrations must be performed in order -(eg. to migrate from v1 to v3 you need to do v1 -> v2 and then v2 -> v3). +Note that migrations must be performed in order: to migrate from vX to vX+2 you need to do vX -> vX+1 and then vX+1 -> vX+2. `, Example: fmt.Sprintf("%s migrate v3", appName), Args: cobra.RangeArgs(0, 1), diff --git a/cmd/migrate/v1/types.go b/cmd/migrate/v1/types.go deleted file mode 100644 index e5b8f073..00000000 --- a/cmd/migrate/v1/types.go +++ /dev/null @@ -1,66 +0,0 @@ -package v1 - -type Config struct { - RPC *RPCConfig `toml:"rpc"` - Grpc *GrpcConfig `toml:"grpc"` - Cosmos *CosmosConfig `toml:"cosmos"` - Database *DatabaseConfig `toml:"database"` - Logging *LoggingConfig `toml:"logging"` - Parsing *ParsingConfig `toml:"parsing"` - Pruning *PruningConfig `toml:"pruning"` - Telemetry *TelemetryConfig `toml:"telemetry"` -} - -type RPCConfig struct { - ClientName string `toml:"client_name"` - Address string `toml:"address"` - MaxConnections int `toml:"max_connections"` -} - -type GrpcConfig struct { - Address string `toml:"address"` - Insecure bool `toml:"insecure"` -} - -type CosmosConfig struct { - Prefix string `toml:"prefix"` - Modules []string `toml:"modules"` -} - -type DatabaseConfig struct { - Name string `toml:"name"` - Host string `toml:"host"` - Port int64 `toml:"port"` - User string `toml:"user"` - Password string `toml:"password"` - SSLMode string `toml:"ssl_mode"` - Schema string `toml:"schema"` - MaxOpenConnections int `toml:"max_open_connections"` - MaxIdleConnections int `toml:"max_idle_connections"` -} - -type LoggingConfig struct { - LogLevel string `toml:"level"` - LogFormat string `toml:"format"` -} - -type ParsingConfig struct { - GenesisFilePath string `toml:"genesis_file_path"` - Workers int64 `toml:"workers"` - StartHeight int64 `toml:"start_height"` - ParseNewBlocks bool `toml:"listen_new_blocks"` - ParseOldBlocks bool `toml:"parse_old_blocks"` - ParseGenesis bool `toml:"parse_genesis"` - FastSync bool `toml:"fast_sync"` -} - -type PruningConfig struct { - KeepRecent int64 `toml:"keep_recent"` - KeepEvery int64 `toml:"keep_every"` - Interval int64 `toml:"interval"` -} - -type TelemetryConfig struct { - Enabled bool `toml:"enabled"` - Port uint `toml:"port"` -} diff --git a/cmd/migrate/v1/utils.go b/cmd/migrate/v1/utils.go deleted file mode 100644 index aea7ff67..00000000 --- a/cmd/migrate/v1/utils.go +++ /dev/null @@ -1,31 +0,0 @@ -package v1 - -import ( - "fmt" - "io/ioutil" - "os" - "path" - - "github.com/pelletier/go-toml" - - "github.com/forbole/juno/v3/types/config" -) - -// GetConfig returns the configuration reading it from the config.toml file present inside the home directory -func GetConfig() (Config, error) { - file := path.Join(config.HomePath, "config.toml") - - // Make sure the path exists - if _, err := os.Stat(file); os.IsNotExist(err) { - return Config{}, fmt.Errorf("config file does not exist") - } - - bz, err := ioutil.ReadFile(file) - if err != nil { - return Config{}, fmt.Errorf("error while reading config files: %s", err) - } - - var cfg Config - err = toml.Unmarshal(bz, &cfg) - return cfg, err -} diff --git a/cmd/migrate/v2/migrate.go b/cmd/migrate/v2/migrate.go deleted file mode 100644 index 76296d56..00000000 --- a/cmd/migrate/v2/migrate.go +++ /dev/null @@ -1,130 +0,0 @@ -package v2 - -import ( - "fmt" - "io/ioutil" - - parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types" - - "gopkg.in/yaml.v3" - - v1 "github.com/forbole/juno/v3/cmd/migrate/v1" - - loggingconfig "github.com/forbole/juno/v3/logging/config" - "github.com/forbole/juno/v3/modules/pruning" - "github.com/forbole/juno/v3/modules/telemetry" - nodeconfig "github.com/forbole/juno/v3/node/config" - "github.com/forbole/juno/v3/node/remote" - "github.com/forbole/juno/v3/types/config" -) - -// RunMigration runs the migration that migrates the data from v1 to v2 -func RunMigration(_ *parsecmdtypes.Config) error { - v2Config, err := migrateConfig() - if err != nil { - return err - } - - bz, err := yaml.Marshal(&v2Config) - if err != nil { - return fmt.Errorf("error while serializing v2 config: %s", err) - } - - v2File := config.GetConfigFilePath() - return ioutil.WriteFile(v2File, bz, 0600) -} - -func migrateConfig() (Config, error) { - cfg, err := v1.GetConfig() - if err != nil { - return Config{}, fmt.Errorf("error while parsing v1 config: %s", err) - } - - v2Cfg := &Config{ - Node: nodeconfig.Config{ - Type: nodeconfig.TypeRemote, - Details: remote.NewDetails( - remote.NewRPCConfig( - cfg.RPC.ClientName, - cfg.RPC.Address, - cfg.RPC.MaxConnections, - ), - remote.NewGrpcConfig( - cfg.Grpc.Address, - cfg.Grpc.Insecure, - ), - ), - }, - Chain: config.ChainConfig{ - Bech32Prefix: cfg.Cosmos.Prefix, - Modules: cfg.Cosmos.Modules, - }, - Database: DatabaseConfig{ - Name: cfg.Database.Name, - Host: cfg.Database.Host, - Port: cfg.Database.Port, - User: cfg.Database.User, - Password: cfg.Database.Password, - SSLMode: cfg.Database.SSLMode, - Schema: cfg.Database.Schema, - MaxOpenConnections: cfg.Database.MaxOpenConnections, - MaxIdleConnections: cfg.Database.MaxIdleConnections, - }, - Parser: ParserConfig{ - Workers: cfg.Parsing.Workers, - ParseNewBlocks: cfg.Parsing.ParseNewBlocks, - ParseOldBlocks: cfg.Parsing.ParseOldBlocks, - ParseGenesis: cfg.Parsing.ParseGenesis, - GenesisFilePath: cfg.Parsing.GenesisFilePath, - StartHeight: cfg.Parsing.StartHeight, - FastSync: cfg.Parsing.FastSync, - }, - Logging: loggingconfig.Config{ - LogLevel: cfg.Logging.LogLevel, - LogFormat: cfg.Logging.LogFormat, - }, - } - - var telemetryConfig *telemetry.Config - if cfg.Telemetry != nil { - telemetryConfig = telemetry.NewConfig(cfg.Telemetry.Port) - - if cfg.Telemetry.Enabled { - v2Cfg.Chain.Modules = appendModuleIfNotExisting(v2Cfg.Chain.Modules, telemetry.ModuleName) - } - } - - var pruningConfig *pruning.Config - if cfg.Pruning != nil { - pruningConfig = pruning.NewConfig( - cfg.Pruning.KeepRecent, - cfg.Pruning.KeepEvery, - cfg.Pruning.Interval, - ) - } - - return Config{ - Chain: v2Cfg.Chain, - Node: v2Cfg.Node, - Parser: v2Cfg.Parser, - Database: v2Cfg.Database, - Logging: v2Cfg.Logging, - Telemetry: telemetryConfig, - Pruning: pruningConfig, - }, nil -} - -func appendModuleIfNotExisting(modules []string, module string) []string { - var found = false - for _, m := range modules { - if m == module { - found = true - } - } - - if !found { - return append(modules, module) - } - - return modules -} diff --git a/cmd/migrate/v2/types.go b/cmd/migrate/v2/types.go deleted file mode 100644 index 2157cd21..00000000 --- a/cmd/migrate/v2/types.go +++ /dev/null @@ -1,59 +0,0 @@ -package v2 - -import ( - "time" - - loggingconfig "github.com/forbole/juno/v3/logging/config" - "github.com/forbole/juno/v3/modules/pruning" - "github.com/forbole/juno/v3/modules/telemetry" - nodeconfig "github.com/forbole/juno/v3/node/config" - pricefeedconfig "github.com/forbole/juno/v3/pricefeed" - "github.com/forbole/juno/v3/types/config" -) - -type Config struct { - Chain config.ChainConfig `yaml:"chain"` - Node nodeconfig.Config `yaml:"node"` - Parser ParserConfig `yaml:"parsing"` - Database DatabaseConfig `yaml:"database"` - Logging loggingconfig.Config `yaml:"logging"` - - // The following are there to support modules which config are present if they are enabled - - Telemetry *telemetry.Config `yaml:"telemetry,omitempty"` - Pruning *pruning.Config `yaml:"pruning,omitempty"` - PriceFeed *pricefeedconfig.Config `yaml:"pricefeed,omitempty"` -} - -type ParserConfig struct { - GenesisFilePath string `yaml:"genesis_file_path,omitempty"` - Workers int64 `yaml:"workers"` - StartHeight int64 `yaml:"start_height"` - ParseNewBlocks bool `yaml:"listen_new_blocks"` - ParseOldBlocks bool `yaml:"parse_old_blocks"` - ParseGenesis bool `yaml:"parse_genesis"` - FastSync bool `yaml:"fast_sync,omitempty"` - - // Following there are the new fields that have been added into v3. We use pointers and the "omitempty" clause - // to make sure that if they are not already specified, then we get nil as values - - AvgBlockTime *time.Duration `yaml:"average_block_time,omitempty"` -} - -type DatabaseConfig struct { - Name string `yaml:"name"` - Host string `yaml:"host"` - Port int64 `yaml:"port"` - User string `yaml:"user"` - Password string `yaml:"password"` - SSLMode string `yaml:"ssl_mode,omitempty"` - Schema string `yaml:"schema,omitempty"` - MaxOpenConnections int `yaml:"max_open_connections"` - MaxIdleConnections int `yaml:"max_idle_connections"` - - // Following there are the new fields that have been added into v3. We use pointers and the "omitempty" clause - // to make sure that if they are not already specified, then we get nil as values - - PartitionSize *int64 `yaml:"partition_size,omitempty"` - PartitionBatchSize *int64 `yaml:"partition_batch,omitempty"` -} diff --git a/cmd/migrate/v3/migrate.go b/cmd/migrate/v3/migrate.go deleted file mode 100644 index d653bf2b..00000000 --- a/cmd/migrate/v3/migrate.go +++ /dev/null @@ -1,123 +0,0 @@ -package v3 - -import ( - "fmt" - "io/ioutil" - "time" - - parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types" - parserconfig "github.com/forbole/juno/v3/parser/config" - - "gopkg.in/yaml.v3" - - v2 "github.com/forbole/juno/v3/cmd/migrate/v2" - "github.com/forbole/juno/v3/database" - databaseconfig "github.com/forbole/juno/v3/database/config" - v3db "github.com/forbole/juno/v3/database/legacy/v3" - "github.com/forbole/juno/v3/database/postgresql" - "github.com/forbole/juno/v3/types/config" -) - -// RunMigration runs the migrations from v2 to v3 -func RunMigration(parseConfig *parsecmdtypes.Config) error { - // Migrate the config - cfg, err := migrateConfig() - if err != nil { - return fmt.Errorf("error while migrating config: %s", err) - } - - // Refresh the global configuration - err = parsecmdtypes.UpdatedGlobalCfg(parseConfig) - if err != nil { - return err - } - - bz, err := yaml.Marshal(&cfg) - if err != nil { - return fmt.Errorf("error while serializing config: %s", err) - } - - err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0600) - if err != nil { - return fmt.Errorf("error while writing v3 config: %s", err) - } - - // Migrate the database - err = migrateDb(cfg, parseConfig) - if err != nil { - return fmt.Errorf("error while migrating database: %s", err) - } - - return nil -} - -func migrateConfig() (Config, error) { - cfg, err := v2.GetConfig() - if err != nil { - return Config{}, fmt.Errorf("error while reading v2 config: %s", err) - } - - // Get the new fields added inside the various configurations - var partitionSize int64 - if cfg.Database.PartitionSize != nil { - partitionSize = *cfg.Database.PartitionSize - } - - var partitionBatchSize int64 - if cfg.Database.PartitionBatchSize != nil { - partitionBatchSize = *cfg.Database.PartitionBatchSize - } - - var averageBlockTime = 3 * time.Second - if cfg.Parser.AvgBlockTime != nil { - averageBlockTime = *cfg.Parser.AvgBlockTime - } - - return Config{ - Node: cfg.Node, - Chain: cfg.Chain, - Database: databaseconfig.Config{ - Name: cfg.Database.Name, - Host: cfg.Database.Host, - Port: cfg.Database.Port, - User: cfg.Database.User, - Password: cfg.Database.Password, - SSLMode: cfg.Database.SSLMode, - Schema: cfg.Database.Schema, - MaxOpenConnections: cfg.Database.MaxOpenConnections, - MaxIdleConnections: cfg.Database.MaxIdleConnections, - PartitionSize: partitionSize, - PartitionBatchSize: partitionBatchSize, - }, - Parser: parserconfig.Config{ - Workers: cfg.Parser.Workers, - ParseNewBlocks: cfg.Parser.ParseNewBlocks, - ParseOldBlocks: cfg.Parser.ParseOldBlocks, - GenesisFilePath: cfg.Parser.GenesisFilePath, - ParseGenesis: cfg.Parser.ParseGenesis, - StartHeight: cfg.Parser.StartHeight, - FastSync: cfg.Parser.FastSync, - AvgBlockTime: &averageBlockTime, - }, - Logging: cfg.Logging, - Telemetry: cfg.Telemetry, - Pruning: cfg.Pruning, - PriceFeed: cfg.PriceFeed, - }, nil -} - -func migrateDb(cfg Config, parseConfig *parsecmdtypes.Config) error { - // Build the codec - encodingConfig := parseConfig.GetEncodingConfigBuilder()() - - // Get the db - databaseCtx := database.NewContext(cfg.Database, &encodingConfig, parseConfig.GetLogger()) - db, err := postgresql.Builder(databaseCtx) - if err != nil { - return fmt.Errorf("error while building the db: %s", err) - } - - // Build the migrator and perform the migrations - migrator := v3db.NewMigrator(db.(*postgresql.Database)) - return migrator.Migrate() -} diff --git a/cmd/migrate/v3/types.go b/cmd/migrate/v3/types.go index 8f9f21fc..77815707 100644 --- a/cmd/migrate/v3/types.go +++ b/cmd/migrate/v3/types.go @@ -1,7 +1,6 @@ package v3 import ( - databaseconfig "github.com/forbole/juno/v3/database/config" loggingconfig "github.com/forbole/juno/v3/logging/config" "github.com/forbole/juno/v3/modules/pruning" "github.com/forbole/juno/v3/modules/telemetry" @@ -11,13 +10,12 @@ import ( "github.com/forbole/juno/v3/types/config" ) -// Config defines all necessary juno configuration parameters. type Config struct { - Chain config.ChainConfig `yaml:"chain"` - Node nodeconfig.Config `yaml:"node"` - Parser parserconfig.Config `yaml:"parsing"` - Database databaseconfig.Config `yaml:"database"` - Logging loggingconfig.Config `yaml:"logging"` + Chain config.ChainConfig `yaml:"chain"` + Node nodeconfig.Config `yaml:"node"` + Parser parserconfig.Config `yaml:"parsing"` + Database DatabaseConfig `yaml:"database"` + Logging loggingconfig.Config `yaml:"logging"` // The following are there to support modules which config are present if they are enabled @@ -25,3 +23,17 @@ type Config struct { Pruning *pruning.Config `yaml:"pruning,omitempty"` PriceFeed *pricefeedconfig.Config `yaml:"pricefeed,omitempty"` } + +type DatabaseConfig struct { + Name string `yaml:"name"` + Host string `yaml:"host"` + Port int64 `yaml:"port"` + User string `yaml:"user"` + Password string `yaml:"password"` + SSLMode string `yaml:"ssl_mode,omitempty"` + Schema string `yaml:"schema,omitempty"` + MaxOpenConnections int `yaml:"max_open_connections"` + MaxIdleConnections int `yaml:"max_idle_connections"` + PartitionSize int64 `yaml:"partition_size,omitempty"` + PartitionBatchSize int64 `yaml:"partition_batch,omitempty"` +} diff --git a/cmd/migrate/v2/utils.go b/cmd/migrate/v3/utils.go similarity index 98% rename from cmd/migrate/v2/utils.go rename to cmd/migrate/v3/utils.go index bc5aff1d..02eec01b 100644 --- a/cmd/migrate/v2/utils.go +++ b/cmd/migrate/v3/utils.go @@ -1,4 +1,4 @@ -package v2 +package v3 import ( "fmt" diff --git a/cmd/migrate/v4/migrate.go b/cmd/migrate/v4/migrate.go new file mode 100644 index 00000000..cc72027e --- /dev/null +++ b/cmd/migrate/v4/migrate.go @@ -0,0 +1,83 @@ +package v4 + +import ( + "fmt" + "io/ioutil" + + parsecmdtypes "github.com/forbole/juno/v3/cmd/parse/types" + + "gopkg.in/yaml.v3" + + v3 "github.com/forbole/juno/v3/cmd/migrate/v3" + databaseconfig "github.com/forbole/juno/v3/database/config" + "github.com/forbole/juno/v3/types/config" +) + +// RunMigration runs the migrations from v3 to v4 +func RunMigration(parseConfig *parsecmdtypes.Config) error { + // Migrate the config + cfg, err := migrateConfig() + if err != nil { + return fmt.Errorf("error while migrating config: %s", err) + } + + // Refresh the global configuration + err = parsecmdtypes.UpdatedGlobalCfg(parseConfig) + if err != nil { + return err + } + + bz, err := yaml.Marshal(&cfg) + if err != nil { + return fmt.Errorf("error while serializing config: %s", err) + } + + err = ioutil.WriteFile(config.GetConfigFilePath(), bz, 0600) + if err != nil { + return fmt.Errorf("error while writing v4 config: %s", err) + } + + return nil +} + +func migrateConfig() (Config, error) { + cfg, err := v3.GetConfig() + if err != nil { + return Config{}, fmt.Errorf("error while reading v3 config: %s", err) + } + + sslMode := cfg.Database.SSLMode + if sslMode == "" { + sslMode = "disable" + } + + schema := cfg.Database.Schema + if schema == "" { + schema = "public" + } + + return Config{ + Node: cfg.Node, + Chain: cfg.Chain, + Database: databaseconfig.Config{ + URL: fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=%s&search_path=%s", + cfg.Database.User, + cfg.Database.Password, + cfg.Database.Host, + cfg.Database.Port, + cfg.Database.Name, + sslMode, + schema, + ), + MaxOpenConnections: cfg.Database.MaxOpenConnections, + MaxIdleConnections: cfg.Database.MaxIdleConnections, + PartitionSize: cfg.Database.PartitionSize, + PartitionBatchSize: cfg.Database.PartitionBatchSize, + }, + Parser: cfg.Parser, + Logging: cfg.Logging, + Telemetry: cfg.Telemetry, + Pruning: cfg.Pruning, + PriceFeed: cfg.PriceFeed, + }, nil +} diff --git a/cmd/migrate/v4/types.go b/cmd/migrate/v4/types.go new file mode 100644 index 00000000..5d14fe0b --- /dev/null +++ b/cmd/migrate/v4/types.go @@ -0,0 +1,27 @@ +package v4 + +import ( + databaseconfig "github.com/forbole/juno/v3/database/config" + loggingconfig "github.com/forbole/juno/v3/logging/config" + "github.com/forbole/juno/v3/modules/pruning" + "github.com/forbole/juno/v3/modules/telemetry" + nodeconfig "github.com/forbole/juno/v3/node/config" + parserconfig "github.com/forbole/juno/v3/parser/config" + pricefeedconfig "github.com/forbole/juno/v3/pricefeed" + "github.com/forbole/juno/v3/types/config" +) + +// Config defines all necessary juno configuration parameters. +type Config struct { + Chain config.ChainConfig `yaml:"chain"` + Node nodeconfig.Config `yaml:"node"` + Parser parserconfig.Config `yaml:"parsing"` + Database databaseconfig.Config `yaml:"database"` + Logging loggingconfig.Config `yaml:"logging"` + + // The following are there to support modules which config are present if they are enabled + + Telemetry *telemetry.Config `yaml:"telemetry,omitempty"` + Pruning *pruning.Config `yaml:"pruning,omitempty"` + PriceFeed *pricefeedconfig.Config `yaml:"pricefeed,omitempty"` +} diff --git a/database/config/config.go b/database/config/config.go index 1286fca1..0a2ab224 100644 --- a/database/config/config.go +++ b/database/config/config.go @@ -1,33 +1,30 @@ package config +import "net/url" + type Config struct { - Name string `yaml:"name"` - Host string `yaml:"host"` - Port int64 `yaml:"port"` - User string `yaml:"user"` - Password string `yaml:"password"` - SSLMode string `yaml:"ssl_mode,omitempty"` - Schema string `yaml:"schema,omitempty"` + URL string `yaml:"url"` MaxOpenConnections int `yaml:"max_open_connections"` MaxIdleConnections int `yaml:"max_idle_connections"` PartitionSize int64 `yaml:"partition_size"` PartitionBatchSize int64 `yaml:"partition_batch"` } +func (c *Config) GetUser() string { + parsedURL, err := url.Parse(c.URL) + if err != nil { + panic(err) + } + return parsedURL.User.Username() +} + func NewDatabaseConfig( - name, host string, port int64, user string, password string, - sslMode string, schema string, + url string, maxOpenConnections int, maxIdleConnections int, partitionSize int64, batchSize int64, ) Config { return Config{ - Name: name, - Host: host, - Port: port, - User: user, - Password: password, - SSLMode: sslMode, - Schema: schema, + URL: url, MaxOpenConnections: maxOpenConnections, MaxIdleConnections: maxIdleConnections, PartitionSize: partitionSize, @@ -38,13 +35,7 @@ func NewDatabaseConfig( // DefaultDatabaseConfig returns the default instance of Config func DefaultDatabaseConfig() Config { return NewDatabaseConfig( - "database-name", - "localhost", - 5432, - "user", - "password", - "", - "public", + "postgresql://user:password@localhost:5432/database-name?sslmode=disable&search_path=public", 1, 1, 100000, diff --git a/database/legacy/v3/prepare.go b/database/legacy/v3/prepare.go index 5e948971..59e8587e 100644 --- a/database/legacy/v3/prepare.go +++ b/database/legacy/v3/prepare.go @@ -91,7 +91,7 @@ CREATE INDEX transaction_height_index ON transaction (height); CREATE INDEX transaction_partition_id_index ON transaction (partition_id); GRANT ALL PRIVILEGES ON transaction TO "%s"; `, - config.Cfg.Database.User) + config.Cfg.Database.GetUser()) _, err := db.SQL.Exec(stmt) return err @@ -118,7 +118,7 @@ CREATE INDEX message_transaction_hash_index ON message (transaction_hash); CREATE INDEX message_type_index ON message (type); CREATE INDEX message_involved_accounts_index ON message USING GIN(involved_accounts_addresses); GRANT ALL PRIVILEGES ON message TO "%s"; -`, config.Cfg.Database.User) +`, config.Cfg.Database.GetUser()) _, err := db.SQL.Exec(stmt) return err diff --git a/database/postgresql/postgresql.go b/database/postgresql/postgresql.go index 77d2c2ad..a8fe1e5d 100644 --- a/database/postgresql/postgresql.go +++ b/database/postgresql/postgresql.go @@ -22,26 +22,7 @@ import ( // from config. It returns a database connection handle or an error if the // connection fails. func Builder(ctx *database.Context) (database.Database, error) { - sslMode := "disable" - if ctx.Cfg.SSLMode != "" { - sslMode = ctx.Cfg.SSLMode - } - - schema := "public" - if ctx.Cfg.Schema != "" { - schema = ctx.Cfg.Schema - } - - connStr := fmt.Sprintf( - "host=%s port=%d dbname=%s user=%s sslmode=%s search_path=%s", - ctx.Cfg.Host, ctx.Cfg.Port, ctx.Cfg.Name, ctx.Cfg.User, sslMode, schema, - ) - - if ctx.Cfg.Password != "" { - connStr += fmt.Sprintf(" password=%s", ctx.Cfg.Password) - } - - postgresDb, err := sqlx.Open("postgres", connStr) + postgresDb, err := sqlx.Open("postgres", ctx.Cfg.URL) if err != nil { return nil, err } diff --git a/database/postgresql/postgresql_test.go b/database/postgresql/postgresql_test.go index 2578440e..07a04766 100644 --- a/database/postgresql/postgresql_test.go +++ b/database/postgresql/postgresql_test.go @@ -33,13 +33,7 @@ func (suite *DbTestSuite) SetupTest() { // Build the database dbCfg := databaseconfig.NewDatabaseConfig( - "bdjuno", - "localhost", - 6433, - "bdjuno", - "password", - "", - "public", + "postgres://bdjuno:password@localhost:6433/bdjuno?sslmode=disable&search_path=public", -1, -1, 100000, diff --git a/go.mod b/go.mod index 75567d12..d660b44e 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,6 @@ require ( github.com/gorilla/mux v1.8.0 github.com/jmoiron/sqlx v1.3.5 github.com/lib/pq v1.10.6 - github.com/pelletier/go-toml v1.9.5 github.com/prometheus/client_golang v1.12.2 github.com/rs/zerolog v1.27.0 github.com/spf13/cobra v1.5.0 @@ -169,6 +168,7 @@ require ( github.com/nishanths/exhaustive v0.8.1 // indirect github.com/nishanths/predeclared v0.2.2 // indirect github.com/olekukonko/tablewriter v0.0.5 // indirect + github.com/pelletier/go-toml v1.9.5 // indirect github.com/pelletier/go-toml/v2 v2.0.5 // indirect github.com/petermattis/goid v0.0.0-20180202154549-b0b1615b78e5 // indirect github.com/phayes/checkstyle v0.0.0-20170904204023-bfd46e6a821d // indirect