From 715c122eb76249b119e2ab2680fb08ac9471de78 Mon Sep 17 00:00:00 2001 From: Cody Couperus Date: Sun, 22 Feb 2026 15:06:16 -0700 Subject: [PATCH] implement atlas for db migration --- atlas.hcl | 11 +++++++++++ main.go | 5 ----- migrations/schema.sql | 9 +++++++++ services/database.go | 20 -------------------- 4 files changed, 20 insertions(+), 25 deletions(-) create mode 100644 atlas.hcl create mode 100644 migrations/schema.sql diff --git a/atlas.hcl b/atlas.hcl new file mode 100644 index 0000000..8317aba --- /dev/null +++ b/atlas.hcl @@ -0,0 +1,11 @@ +env "local" { + src = "file://migrations/schema.sql" + url = "postgres://postgres:postgres@localhost:5433/go_weather?sslmode=disable" + dev = "docker://postgres/16/alpine/dev" # for schema validation +} + +env "production" { + src = "file://migrations/schema.sql" + url = "postgres://${env("DB_USER")}:${env("DB_PASSWORD")}@${env("DB_HOST")}:${env("DB_PORT")}/${env("DB_NAME")}?sslmode=require" + dev = "docker://postgres/16/alpine/dev" +} diff --git a/main.go b/main.go index 3237603..52140e8 100644 --- a/main.go +++ b/main.go @@ -60,11 +60,6 @@ func main() { defer database.Close() databaseService := services.NewDatabaseService(database) - // Setup weather_readings table - if err := databaseService.CreateTable(); err != nil { - log.Fatalf("failed to create table: %v", err) - } - // Create services weatherService := services.NewWeatherService(config.APIKey) diff --git a/migrations/schema.sql b/migrations/schema.sql new file mode 100644 index 0000000..b0b80d1 --- /dev/null +++ b/migrations/schema.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS weather_readings ( + id SERIAL PRIMARY KEY, + city TEXT NOT NULL, + temp DOUBLE PRECISION NOT NULL, + feels_like DOUBLE PRECISION NOT NULL, + humidity INTEGER NOT NULL, + conditions TEXT NOT NULL, + fetched_at TIMESTAMP WITH TIME ZONE NOT NULL +) diff --git a/services/database.go b/services/database.go index 26b84a3..a9523f9 100644 --- a/services/database.go +++ b/services/database.go @@ -32,26 +32,6 @@ func NewDatabaseService(db *sql.DB) *DatabaseService { return &DatabaseService{db: db} } -func (s *DatabaseService) CreateTable() error { - _, err := s.db.Exec(` - CREATE TABLE IF NOT EXISTS weather_readings ( - id SERIAL PRIMARY KEY, - city TEXT NOT NULL, - temp DOUBLE PRECISION NOT NULL, - feels_like DOUBLE PRECISION NOT NULL, - humidity INTEGER NOT NULL, - conditions TEXT NOT NULL, - fetched_at TIMESTAMP WITH TIME ZONE NOT NULL - ) - `) - - if err != nil { - log.Printf("failed to create table: %v", err) - } - fmt.Println("weather_readings table ready") - return nil -} - func (s *DatabaseService) SaveReading(weather models.WeatherResponse) (int, error) { conditions := "" if len(weather.Weather) > 0 {