yogai/storage/postgres.go

140 lines
2.9 KiB
Go
Raw Normal View History

2023-05-06 12:08:34 +02:00
package storage
import (
"database/sql"
2023-05-08 15:53:06 +02:00
"ewintr.nl/yogai/model"
2023-05-06 12:08:34 +02:00
"fmt"
_ "github.com/lib/pq"
)
2023-05-08 15:53:06 +02:00
type PostgresInfo struct {
Host string
Port string
User string
Password string
Database string
}
2023-05-06 12:08:34 +02:00
type Postgres struct {
db *sql.DB
}
2023-05-08 15:53:06 +02:00
func NewPostgres(pgInfo PostgresInfo) (*Postgres, error) {
db, err := sql.Open("postgres", fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
pgInfo.Host, pgInfo.Port, pgInfo.User, pgInfo.Password, pgInfo.Database))
if err != nil {
return &Postgres{}, err
}
2023-05-06 12:08:34 +02:00
p := &Postgres{db: db}
if err := p.migrate(pgMigration); err != nil {
return &Postgres{}, err
}
return p, nil
}
2023-05-08 15:53:06 +02:00
type PostgresVideoRepository struct {
*Postgres
}
func NewPostgresVideoRepository(postgres *Postgres) *PostgresVideoRepository {
return &PostgresVideoRepository{postgres}
}
func (p *PostgresVideoRepository) Save(v *model.Video) error {
2023-05-10 16:28:45 +02:00
query := `INSERT INTO video (id, status, youtube_id, feed_id, title, description)
2023-05-08 15:53:06 +02:00
VALUES ($1, $2, $3, $4, $5, $6)
ON CONFLICT (id)
DO UPDATE SET
id = EXCLUDED.id,
status = EXCLUDED.status,
2023-05-10 16:28:45 +02:00
youtube_id = EXCLUDED.youtube_id,
2023-05-08 15:53:06 +02:00
feed_id = EXCLUDED.feed_id,
title = EXCLUDED.title,
description = EXCLUDED.description;`
2023-05-10 16:28:45 +02:00
_, err := p.db.Exec(query, v.ID, v.Status, v.YoutubeID, v.FeedID, v.Title, v.Description)
2023-05-08 15:53:06 +02:00
return err
}
2023-05-06 12:08:34 +02:00
var pgMigration = []string{
2023-05-10 16:28:45 +02:00
`CREATE TYPE video_status AS ENUM ('new', 'ready')`,
2023-05-06 12:08:34 +02:00
`CREATE TABLE video (
2023-05-08 15:53:06 +02:00
id uuid PRIMARY KEY,
status video_status NOT NULL,
2023-05-10 19:32:57 +02:00
youtube_id VARCHAR(255) NOT NULL UNIQUE,
2023-05-06 12:08:34 +02:00
title VARCHAR(255) NOT NULL,
2023-05-08 15:53:06 +02:00
feed_id VARCHAR(255) NOT NULL,
2023-05-06 12:08:34 +02:00
description TEXT,
summary TEXT
)`,
}
func (p *Postgres) migrate(wanted []string) error {
query := `CREATE TABLE IF NOT EXISTS migration
("id" SERIAL PRIMARY KEY, "query" TEXT)`
_, err := p.db.Exec(query)
if err != nil {
return err
}
// find existing
rows, err := p.db.Query(`SELECT query FROM migration ORDER BY id`)
if err != nil {
return err
}
existing := []string{}
for rows.Next() {
var query string
if err := rows.Scan(&query); err != nil {
return err
}
existing = append(existing, query)
}
rows.Close()
// compare
missing, err := compareMigrations(wanted, existing)
if err != nil {
return err
}
// execute missing
for _, query := range missing {
if _, err := p.db.Exec(query); err != nil {
return err
}
// register
if _, err := p.db.Exec(`
INSERT INTO migration
(query) VALUES ($1)
`, query); err != nil {
return err
}
}
return nil
}
func compareMigrations(wanted, existing []string) ([]string, error) {
needed := []string{}
if len(wanted) < len(existing) {
return []string{}, fmt.Errorf("not enough migrations")
}
for i, want := range wanted {
switch {
case i >= len(existing):
needed = append(needed, want)
case want == existing[i]:
// do nothing
case want != existing[i]:
return []string{}, fmt.Errorf("incompatible migration: %v", want)
}
}
return needed, nil
}