implement atlas for db migration
This commit is contained in:
@@ -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"
|
||||||
|
}
|
||||||
@@ -60,11 +60,6 @@ func main() {
|
|||||||
defer database.Close()
|
defer database.Close()
|
||||||
databaseService := services.NewDatabaseService(database)
|
databaseService := services.NewDatabaseService(database)
|
||||||
|
|
||||||
// Setup weather_readings table
|
|
||||||
if err := databaseService.CreateTable(); err != nil {
|
|
||||||
log.Fatalf("failed to create table: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create services
|
// Create services
|
||||||
weatherService := services.NewWeatherService(config.APIKey)
|
weatherService := services.NewWeatherService(config.APIKey)
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
)
|
||||||
@@ -32,26 +32,6 @@ func NewDatabaseService(db *sql.DB) *DatabaseService {
|
|||||||
return &DatabaseService{db: db}
|
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) {
|
func (s *DatabaseService) SaveReading(weather models.WeatherResponse) (int, error) {
|
||||||
conditions := ""
|
conditions := ""
|
||||||
if len(weather.Weather) > 0 {
|
if len(weather.Weather) > 0 {
|
||||||
|
|||||||
Reference in New Issue
Block a user