upgrade from go-pg to bun

This commit is contained in:
dhax 2024-08-31 18:57:54 +02:00
parent f59f129354
commit 1886be62bc
23 changed files with 415 additions and 385 deletions

View file

@ -0,0 +1,66 @@
package migrations
import (
"context"
"fmt"
"github.com/uptrace/bun"
)
const accountTable = `
CREATE TABLE accounts (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone DEFAULT current_timestamp,
last_login timestamp with time zone NOT NULL DEFAULT current_timestamp,
email text NOT NULL UNIQUE,
name text NOT NULL,
active boolean NOT NULL DEFAULT TRUE,
roles text[] NOT NULL DEFAULT '{"user"}',
PRIMARY KEY (id)
)`
const tokenTable = `
CREATE TABLE tokens (
id serial NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
account_id int NOT NULL REFERENCES accounts(id),
token text NOT NULL UNIQUE,
expiry timestamp with time zone NOT NULL,
mobile boolean NOT NULL DEFAULT FALSE,
identifier text,
PRIMARY KEY (id)
)`
func init() {
up := []string{
accountTable,
tokenTable,
}
down := []string{
`DROP TABLE tokens`,
`DROP TABLE accounts`,
}
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
fmt.Println("creating initial tables")
for _, q := range up {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
fmt.Println("dropping initial tables")
for _, q := range down {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
})
}

View file

@ -0,0 +1 @@
TRUNCATE accounts CASCADE

View file

@ -0,0 +1,8 @@
INSERT INTO accounts (id, email, name, active, roles)
VALUES (DEFAULT, 'admin@example.com', 'Admin Example', true, '{admin}');
--bun:split
INSERT INTO accounts (id, email, name, active)
VALUES (DEFAULT, 'user@example.com', 'User Example', true);

View file

@ -0,0 +1,53 @@
package migrations
import (
"context"
"fmt"
"github.com/uptrace/bun"
)
const profileTable = `
CREATE TABLE profiles (
id serial NOT NULL,
updated_at timestamp with time zone NOT NULL DEFAULT current_timestamp,
account_id int NOT NULL REFERENCES accounts(id),
theme text NOT NULL DEFAULT 'default',
PRIMARY KEY (id)
)`
const bootstrapAccountProfiles = `
INSERT INTO profiles(account_id) VALUES(1);
INSERT INTO profiles(account_id) VALUES(2);
`
func init() {
up := []string{
profileTable,
bootstrapAccountProfiles,
}
down := []string{
`DROP TABLE profiles`,
}
Migrations.MustRegister(func(ctx context.Context, db *bun.DB) error {
fmt.Println("create profile table")
for _, q := range up {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
}, func(ctx context.Context, db *bun.DB) error {
fmt.Println("drop profile table")
for _, q := range down {
_, err := db.Exec(q)
if err != nil {
return err
}
}
return nil
})
}

View file

@ -0,0 +1,49 @@
package migrations
import (
"context"
"embed"
"fmt"
"log"
"github.com/dhax/go-base/database"
"github.com/uptrace/bun/migrate"
)
//go:embed *.sql
var sqlMigrations embed.FS
var Migrations = migrate.NewMigrations()
func init() {
if err := Migrations.Discover(sqlMigrations); err != nil {
panic(err)
}
}
// Migrate runs all migrations
func Migrate() {
db, err := database.DBConn()
if err != nil {
log.Fatal(err)
}
defer db.Close()
migrator := migrate.NewMigrator(db, Migrations)
err = migrator.Init(context.Background())
if err != nil {
log.Fatal(err)
}
group, err := migrator.Migrate(context.Background())
if err != nil {
log.Fatal(err)
}
if group.ID == 0 {
fmt.Printf("there are no new migrations to run\n")
} else {
fmt.Printf("migrated to %s\n", group)
}
}