diff --git a/components/ingest-service/cmd/ingest-service/commands/serve.go b/components/ingest-service/cmd/ingest-service/commands/serve.go index d5d89b21390..7031eeb9f04 100644 --- a/components/ingest-service/cmd/ingest-service/commands/serve.go +++ b/components/ingest-service/cmd/ingest-service/commands/serve.go @@ -8,9 +8,11 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "github.com/chef/automate/components/ingest-service/config" "github.com/chef/automate/components/ingest-service/grpc" "github.com/chef/automate/components/ingest-service/rest" "github.com/chef/automate/components/ingest-service/serveropts" + "github.com/chef/automate/components/ingest-service/storage" "github.com/chef/automate/lib/grpc/secureconn" "github.com/chef/automate/lib/tls/certs" "github.com/chef/automate/lib/tracing" @@ -35,6 +37,21 @@ var serveCmd = &cobra.Command{ // construct GRPC endpoint for gateway endpoint := fmt.Sprintf("%s:%d", conf.Host, conf.Port) + dbConfig := &config.Storage{ + URI: conf.Storage.URI, + DBUser: conf.Storage.DBUser, + Database: conf.Storage.Database, + SchemaPath: conf.Storage.SchemaPath, + MaxOpenConns: conf.Storage.MaxOpenConns, + MaxIdleConns: conf.Storage.MaxIdleConns, + } + + // Initialize PostgreSQL connection and run migrations + _, err = storage.ConnectAndMigrate(dbConfig) + if err != nil { + logrus.WithError(err).Fatal("Error connecting to DB and running migrations") + } + logrus.Info("Database connection and migrations successful!") // Spawn a gRPC Client in a goroutine // @@ -44,14 +61,15 @@ var serveCmd = &cobra.Command{ // // TODO: Figure out how to respawn if client crashes? if os.Getenv(devModeEnvVar) == "true" { - go rest.Spawn(endpoint, conf) // nolint: errcheck + go rest.Spawn(endpoint, conf) // Modified to pass `db` } // Start the gRPC Server - err = grpc.Spawn(conf) + err = grpc.Spawn(conf) // Modified to pass `db` if err != nil { logrus.WithError(err).Fatal("spawn failed") } + }, } @@ -114,6 +132,14 @@ func readCliParams() *serveropts.Opts { MissingNodesForDeletionRunningDefault: missingNodesForDeletionRunningDefault, NodesMissingRunningDefault: nodesMissingRunningDefault, }, + Storage: serveropts.StorageConfig{ //Added Storage Configuration + URI: viper.GetString("postgresql-url"), + DBUser: viper.GetString("postgresql-user"), + Database: viper.GetString("postgresql-database"), + SchemaPath: viper.GetString("postgresql-schema-path"), + MaxOpenConns: viper.GetInt("postgresql-max-open-conns"), + MaxIdleConns: viper.GetInt("postgresql-max-idle-conns"), + }, ConnFactory: factory, } } diff --git a/components/ingest-service/cmd/ingest-service/ingest-service.go b/components/ingest-service/cmd/ingest-service/ingest-service.go index cb40249ee53..bdce0919022 100644 --- a/components/ingest-service/cmd/ingest-service/ingest-service.go +++ b/components/ingest-service/cmd/ingest-service/ingest-service.go @@ -5,38 +5,11 @@ package main -import ( - "os" - - "github.com/chef/automate/components/ingest-service/cmd/ingest-service/commands" - "github.com/chef/automate/components/ingest-service/config" - "github.com/chef/automate/components/ingest-service/storage" - log "github.com/sirupsen/logrus" -) +import "github.com/chef/automate/components/ingest-service/cmd/ingest-service/commands" func main() { - configPath := "/hab/svc/ingest-service/config" - - // ✅ Load configuration dynamically - cfg, err := config.Load(configPath) // Ensure this function is implemented in config package - if err != nil { - log.Fatalf("Failed to load configuration: %v", err) - os.Exit(1) - } - - // ✅ Connect to DB and run migrations before starting the service - db, err := storage.ConnectAndMigrate(&cfg.Storage) - if err != nil { - log.Fatalf("Failed to initialize database: %v", err) - os.Exit(1) - } - - log.Info("✅ Database connection established and migrations applied successfully!") - // Start the ingest service commands.Execute() - // To avoid unused variable errors (remove if db is used elsewhere) - _ = db } diff --git a/components/ingest-service/serveropts/serveropts.go b/components/ingest-service/serveropts/serveropts.go index 584e3487ca0..23dbdff2d97 100644 --- a/components/ingest-service/serveropts/serveropts.go +++ b/components/ingest-service/serveropts/serveropts.go @@ -46,6 +46,16 @@ type Opts struct { CerealAddress string Jobs JobsConfig EventFeedAddress string + Storage StorageConfig +} + +type StorageConfig struct { + URI string `mapstructure:"uri"` + DBUser string `mapstructure:"user"` + Database string `mapstructure:"database"` + SchemaPath string `mapstructure:"schema_path"` + MaxOpenConns int `mapstructure:"max_open_conns"` + MaxIdleConns int `mapstructure:"max_idle_conns"` } // SetLogLevel sets the log level for the service diff --git a/components/ingest-service/storage/db.go b/components/ingest-service/storage/db.go index 16a7319ca4b..2b585d20746 100644 --- a/components/ingest-service/storage/db.go +++ b/components/ingest-service/storage/db.go @@ -53,13 +53,16 @@ func ConnectAndMigrate(dbConf *config.Storage) (*DB, error) { "uri": dbConf.URI, "schema": dbConf.SchemaPath, }).Debug("Initializing database") + err = runMigrations(dbConf) + if err != nil { + return nil, errors.Wrap(err, "Migration failed") + } db := &DB{ DbMap: &gorp.DbMap{Db: dbConn, Dialect: gorp.PostgresDialect{}}, } - - return db, err + return db, nil } // connect opens a connection to the database diff --git a/components/ingest-service/storage/schema/sql/01_tables_creation.up.sql b/components/ingest-service/storage/schema/sql/01_tables_creation.up.sql index ffa69b1f547..ff11711c433 100644 --- a/components/ingest-service/storage/schema/sql/01_tables_creation.up.sql +++ b/components/ingest-service/storage/schema/sql/01_tables_creation.up.sql @@ -1,13 +1,13 @@ -CREATE TABLE reindex_requests ( +CREATE TABLE IF NOT EXISTS reindex_requests ( request_id INT PRIMARY KEY, status VARCHAR(50) CHECK (status IN ('running', 'failed', 'completed')), - created_at TIMESTAMP, - last_updated TIMESTAMP + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + last_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); - -CREATE TABLE reindex_request_detailed ( + +CREATE TABLE IF NOT EXISTS reindex_request_detailed ( id INT PRIMARY KEY, - request_id INT, + request_id INT NOT NULL, index TEXT NOT NULL, from_version TEXT NOT NULL, to_version TEXT NOT NULL, @@ -16,7 +16,7 @@ CREATE TABLE reindex_request_detailed ( heartbeat TIMESTAMP, having_alias BOOLEAN, alias_list TEXT, - created_at TIMESTAMP, - updated_at TIMESTAMP, - FOREIGN KEY (request_id) REFERENCES reindex_requests(request_id) + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (request_id) REFERENCES reindex_requests(request_id) ON DELETE CASCADE );