diff --git a/database/EstimatedCall.go b/database/EstimatedCall.go index f6d0797..ac7cf3b 100644 --- a/database/EstimatedCall.go +++ b/database/EstimatedCall.go @@ -42,7 +42,7 @@ func InsertOrUpdateEstimatedCall(db *sql.DB, values []interface{}) (int, string, var id int err = stmt.QueryRow(values...).Scan(&action, &id) if err != nil { - if 1 == 1 { + if 1 == 0 { fmt.Println("Executing query:", query) for i, v := range values { fmt.Printf("Value %d: (%v)\n", i+1, v) diff --git a/database/RecordedCall.go b/database/RecordedCall.go index cb6bab0..6cd1136 100644 --- a/database/RecordedCall.go +++ b/database/RecordedCall.go @@ -45,7 +45,7 @@ func InsertOrUpdateRecordedCall(db *sql.DB, values []interface{}) (int, string, var id int err = stmt.QueryRow(values...).Scan(&action, &id) if err != nil { - if 1 == 1 { + if 1 == 0 { fmt.Println("Executing query:", query) for i, v := range values { fmt.Printf("Value %d: (%v)\n", i+1, v) diff --git a/database/SetupDB.go b/database/SetupDB.go new file mode 100644 index 0000000..a824dae --- /dev/null +++ b/database/SetupDB.go @@ -0,0 +1,129 @@ +package database + +import ( + "database/sql" + "fmt" + "ti1/config" + + _ "github.com/lib/pq" +) + +func SetupDB() error { + fmt.Println("Setting up the database...") + + // Load configuration + cfg, err := config.LoadConfig("config/conf.json") + if err != nil { + return fmt.Errorf("failed to load config: %w", err) + } + + // Connect to PostgreSQL + connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s", + cfg.Database.Host, cfg.Database.Port, cfg.Database.User, cfg.Database.Password, cfg.Database.DBName, cfg.Database.SSLMode) + db, err := sql.Open("postgres", connStr) + if err != nil { + return fmt.Errorf("failed to connect to database: %w", err) + } + defer db.Close() + + // Create sequences if they do not exist + sequences := []string{ + "CREATE SEQUENCE IF NOT EXISTS public.calls_id_seq", + "CREATE SEQUENCE IF NOT EXISTS public.estimatedvehiclejourney_id_seq", + "CREATE SEQUENCE IF NOT EXISTS public.servicedelivery_id_seq", + } + + for _, seq := range sequences { + _, err := db.Exec(seq) + if err != nil { + return fmt.Errorf("failed to create sequence: %w", err) + } + } + + // Check if tables exist and have the correct structure + tables := map[string]string{ + "calls": `CREATE TABLE IF NOT EXISTS public.calls ( + id BIGINT PRIMARY KEY DEFAULT nextval('public.calls_id_seq'), + estimatedvehiclejourney BIGINT, + "order" INTEGER, + stoppointref VARCHAR, + aimeddeparturetime TIMESTAMP, + expecteddeparturetime TIMESTAMP, + aimedarrivaltime TIMESTAMP, + expectedarrivaltime TIMESTAMP, + cancellation VARCHAR, + actualdeparturetime TIMESTAMP, + actualarrivaltime TIMESTAMP, + estimated_data JSON, + recorded_data JSON + );`, + "estimatedvehiclejourney": `CREATE TABLE IF NOT EXISTS public.estimatedvehiclejourney ( + id BIGINT PRIMARY KEY DEFAULT nextval('public.estimatedvehiclejourney_id_seq'), + servicedelivery INTEGER, + recordedattime TIMESTAMP, + lineref VARCHAR, + directionref VARCHAR, + datasource VARCHAR, + datedvehiclejourneyref VARCHAR, + vehiclemode VARCHAR, + dataframeref VARCHAR, + originref VARCHAR, + destinationref VARCHAR, + operatorref VARCHAR, + vehicleref VARCHAR, + cancellation VARCHAR, + other JSON, + firstservicedelivery INTEGER + );`, + "servicedelivery": `CREATE TABLE IF NOT EXISTS public.servicedelivery ( + id INTEGER PRIMARY KEY DEFAULT nextval('public.servicedelivery_id_seq'), + responsetimestamp TIMESTAMPTZ, + recordedattime TIMESTAMPTZ, + data JSON + );`, + } + + for table, createStmt := range tables { + var exists bool + err := db.QueryRow(fmt.Sprintf("SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = '%s')", table)).Scan(&exists) + if err != nil { + return fmt.Errorf("failed to check if table %s exists: %w", table, err) + } + + if !exists { + _, err := db.Exec(createStmt) + if err != nil { + return fmt.Errorf("failed to create table %s: %w", table, err) + } + fmt.Printf("Table %s created successfully!\n", table) + } else { + fmt.Printf("Table %s already exists.\n", table) + } + } + + // Check if the unique constraint exists before adding it + var constraintExists bool + err = db.QueryRow(` + SELECT EXISTS ( + SELECT 1 + FROM pg_constraint + WHERE conname = 'unique_estimatedvehiclejourney_order' + ); + `).Scan(&constraintExists) + if err != nil { + return fmt.Errorf("failed to check if unique constraint exists: %w", err) + } + + if !constraintExists { + _, err = db.Exec(`ALTER TABLE calls ADD CONSTRAINT unique_estimatedvehiclejourney_order UNIQUE (estimatedvehiclejourney, "order");`) + if err != nil { + return fmt.Errorf("failed to add unique constraint to calls table: %w", err) + } + fmt.Println("Unique constraint added to calls table.") + } else { + fmt.Println("Unique constraint already exists on calls table.") + } + + fmt.Println("Database setup completed successfully!") + return nil +} diff --git a/main.go b/main.go index 31035e6..16bdcf1 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,19 @@ package main import ( "log" "ti1/data" + "ti1/database" "ti1/export" "time" ) func main() { log.Println("Starting...") - //config.PrintDBConfig() + + // Setup the database + err := database.SetupDB() + if err != nil { + log.Fatalf("Database setup failed: %v", err) + } for { start := time.Now() @@ -19,7 +25,6 @@ func main() { log.Fatal(err) } - //export.ExportToCSV(data) export.DBData(data) log.Println("finished in", time.Since(start)) @@ -29,10 +34,4 @@ func main() { time.Sleep(1*time.Minute - elapsed) } } - //export.PrintData(data) - - //log.Printf("Data fetched successfully: %+v", data) - - //export.PrintData(data) - }