70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
//go:build ignore
|
|
|
|
package main
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
func main() {
|
|
// Built from the environment, not hardcoded. This line used to carry the
|
|
// production host and password in plaintext, in a file tracked by git — so
|
|
// the live database credential shipped with every clone. Export DB_HOST,
|
|
// DB_USER, DB_PASSWORD, DB_NAME and DB_PORT (or source .env) before running.
|
|
dsn := fmt.Sprintf(
|
|
"host=%s user=%s password=%s dbname=%s port=%s sslmode=disable",
|
|
os.Getenv("DB_HOST"), os.Getenv("DB_USER"), os.Getenv("DB_PASSWORD"),
|
|
os.Getenv("DB_NAME"), os.Getenv("DB_PORT"),
|
|
)
|
|
db, err := sql.Open("postgres", dsn)
|
|
if err != nil {
|
|
log.Fatalf("Open failed: %v", err)
|
|
}
|
|
defer db.Close()
|
|
|
|
if err := db.Ping(); err != nil {
|
|
log.Fatalf("Ping failed: %v", err)
|
|
}
|
|
fmt.Println("Connected OK")
|
|
|
|
// Drop the old constraint
|
|
_, err = db.Exec(`ALTER TABLE pickupbookings DROP CONSTRAINT IF EXISTS pickupbookings_status_check`)
|
|
if err != nil {
|
|
log.Fatalf("Drop constraint failed: %v", err)
|
|
}
|
|
fmt.Println("Old constraint dropped.")
|
|
|
|
// Recreate with all status values from constants
|
|
_, err = db.Exec(`
|
|
ALTER TABLE pickupbookings ADD CONSTRAINT pickupbookings_status_check
|
|
CHECK (status IN (
|
|
'Pending_Pickup',
|
|
'Created',
|
|
'Miler_Assigned',
|
|
'Pickup_Scheduled',
|
|
'Picked_Up',
|
|
'Converted_To_Consignment',
|
|
'Cancelled'
|
|
))
|
|
`)
|
|
if err != nil {
|
|
log.Fatalf("Add constraint failed: %v", err)
|
|
}
|
|
fmt.Println("New constraint added with Pending_Pickup included.")
|
|
|
|
// Verify
|
|
var def string
|
|
db.QueryRow(`
|
|
SELECT pg_get_constraintdef(con.oid)
|
|
FROM pg_constraint con
|
|
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
WHERE rel.relname = 'pickupbookings' AND con.contype = 'c'
|
|
`).Scan(&def)
|
|
fmt.Println("Current constraint:", def)
|
|
}
|