Skip to content

Commit

Permalink
code changes for migration-2
Browse files Browse the repository at this point in the history
Signed-off-by: Aishwarya2001 <[email protected]>
  • Loading branch information
Aishwarya2001A committed Feb 12, 2025
1 parent 83d2150 commit 3581198
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 41 deletions.
30 changes: 28 additions & 2 deletions components/ingest-service/cmd/ingest-service/commands/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
//
Expand All @@ -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")
}

},
}

Expand Down Expand Up @@ -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,
}
}
Expand Down
29 changes: 1 addition & 28 deletions components/ingest-service/cmd/ingest-service/ingest-service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
10 changes: 10 additions & 0 deletions components/ingest-service/serveropts/serveropts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions components/ingest-service/storage/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
);

0 comments on commit 3581198

Please sign in to comment.