initial commit

This commit is contained in:
Sam 2022-05-02 17:19:37 +02:00
commit 5a75f99720
20 changed files with 2239 additions and 0 deletions

20
scripts/genkey/main.go Normal file
View file

@ -0,0 +1,20 @@
package main
import (
"crypto/rand"
"encoding/base64"
"fmt"
)
func main() {
b := make([]byte, 64)
_, err := rand.Read(b)
if err != nil {
panic(err)
}
s := base64.URLEncoding.EncodeToString(b)
fmt.Println(s)
}

View file

@ -0,0 +1,50 @@
-- +migrate Up
-- 2022-05-02: initial schema
create table users (
id text primary key,
username text not null unique,
display_name text,
bio text,
avatar_source text,
avatar_url text,
links text[],
discord text -- for Discord oauth
);
create table user_fields (
user_id text not null references users (id) on delete cascade,
id bigserial primary key,
name text not null,
favourite text[] not null default array[]::text[],
okay text[] not null default array[]::text[],
jokingly text[] not null default array[]::text[],
friends_only text[] not null default array[]::text[],
avoid text[] not null default array[]::text[]
);
create table members (
id text primary key,
user_id text not null references users (id) on delete cascade,
name text not null,
bio text,
avatar_url text,
links text
);
create table member_fields (
member_id text not null references members (id) on delete cascade,
id bigserial primary key,
name text not null,
favourite text[] not null default array[]::text[],
okay text[] not null default array[]::text[],
jokingly text[] not null default array[]::text[],
friends_only text[] not null default array[]::text[],
avoid text[] not null default array[]::text[]
);

65
scripts/migrate/main.go Normal file
View file

@ -0,0 +1,65 @@
// migrate runs (forward) migrations
package main
import (
"database/sql"
"embed"
"fmt"
"os"
"github.com/joho/godotenv"
migrate "github.com/rubenv/sql-migrate"
// SQL driver
_ "github.com/jackc/pgx/v4/stdlib"
)
//go:embed *.sql
var migrations embed.FS
func main() {
err := godotenv.Load()
if err != nil {
fmt.Println("error loading .env file:", err)
os.Exit(1)
}
db, err := sql.Open("pgx", os.Getenv("DATABASE_URL"))
if err != nil {
fmt.Println("error opening database:", err)
os.Exit(1)
}
fmt.Println("opened database")
defer func() {
err := db.Close()
if err != nil {
fmt.Println("error closing database:", err)
os.Exit(1)
}
}()
if err := db.Ping(); err != nil {
fmt.Println("error pinging database:", err)
return
}
src := &migrate.EmbedFileSystemMigrationSource{
FileSystem: migrations,
Root: ".",
}
fmt.Println("executing migrations")
n, err := migrate.Exec(db, "postgres", src, migrate.Up)
if err != nil {
fmt.Println("error executing migrations:", err)
return
}
if n == 0 {
fmt.Println("database is already up to date")
} else {
fmt.Printf("executed %v migrations!\n", n)
}
}