postgres migrations
This commit is contained in:
parent
6c0611d074
commit
296744940d
|
@ -0,0 +1 @@
|
|||
.idea
|
|
@ -0,0 +1,5 @@
|
|||
module ewintr.nl/yogai
|
||||
|
||||
go 1.20
|
||||
|
||||
require github.com/lib/pq v1.10.9
|
|
@ -0,0 +1,2 @@
|
|||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
|
@ -0,0 +1,22 @@
|
|||
I would like a 25 minute yoga flow to stretch.
|
||||
|
||||
I would like a yoga practice especially to sooth the low back. Preferably all standing.
|
||||
|
||||
I would like a yin yoga practice about 1 hour long, using no props, except maybe one block and/or a bolster. Specifically dedicated to relax the body. But without deep stretching.
|
||||
|
||||
I would like a yoga nidra practice. No longer than 30 minutes, but no shorter than 20 minutes.
|
||||
|
||||
I would like a yoga flow between 35 and 45 minutes, without downward facing dogs. Focus on heart opening and back bends.
|
||||
|
||||
Give me an easy morning yoga flowto energize. No more than 20 minutes. I like the practices of Well with Hels, but an other yoga teacher might be good as well.
|
||||
|
||||
I have terrible upper back pain. Can you recommend a yoga practice to combat that? Preferably all low to the ground.
|
||||
|
||||
What yoga practice can you recommend to combatting the after lunch dip? Please no more than 15 minutes and all standing.
|
||||
|
||||
I am very tired at the moment. I would like to practice some yoga to energize, but not to strenuous. I have about an hour to spare.
|
||||
|
||||
Can you give me a yoga practice in which I can practice the yogi squat? Doesn’t have to be a tutorial, preferably a practice with other poses.
|
||||
|
||||
I’m very restless. Can you find me a yoga practice to calm down? I prefer some movement in it, I don’t want to ly still most of the time.
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"ewintr.nl/yogai/storage"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
pgInfo := struct {
|
||||
Host string
|
||||
Port string
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
}{
|
||||
Host: getParam("POSTGRES_HOST", "localhost"),
|
||||
Port: getParam("POSTGRES_PORT", "5432"),
|
||||
User: getParam("POSTGRES_USER", "yogai"),
|
||||
Password: getParam("POSTGRES_PASSWORD", "yogai"),
|
||||
Database: getParam("POSTGRES_DB", "yogai"),
|
||||
}
|
||||
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 {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
_, err = storage.NewPostgres(db)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func getParam(param, def string) string {
|
||||
if val, ok := os.LookupEnv(param); ok {
|
||||
return val
|
||||
}
|
||||
return def
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewPostgres(db *sql.DB) (*Postgres, error) {
|
||||
p := &Postgres{db: db}
|
||||
if err := p.migrate(pgMigration); err != nil {
|
||||
return &Postgres{}, err
|
||||
}
|
||||
|
||||
return p, nil
|
||||
}
|
||||
|
||||
var pgMigration = []string{
|
||||
`CREATE TABLE channel (
|
||||
id SERIAL PRIMARY KEY,
|
||||
url VARCHAR(255) NOT NULL,
|
||||
feed_url VARCHAR(255) NOT NULL,
|
||||
name VARCHAR(255) NOT NULL
|
||||
)`,
|
||||
`CREATE TABLE video (
|
||||
id SERIAL PRIMARY KEY,
|
||||
channel_id INTEGER REFERENCES channel(id) ON DELETE CASCADE,
|
||||
url VARCHAR(255) NOT NULL,
|
||||
title VARCHAR(255) NOT NULL,
|
||||
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
|
||||
}
|
Loading…
Reference in New Issue