feat(15-01): clone journal plugin with translatable Post tracer
- Register golem15.journal requiring exactly golem15.translate - Squash golem15_journal_posts DDL with unique slug and JSONB columns - Persist one en/pl Post through translate helpers on real Postgres Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
48
updates/202610060001_create_golem15_journal_posts.go
Normal file
48
updates/202610060001_create_golem15_journal_posts.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"github.com/go-gormigrate/gormigrate/v2"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
var createPosts = []*gormigrate.Migration{
|
||||
{
|
||||
ID: "202610060001_create_golem15_journal_posts",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
for _, stmt := range []string{
|
||||
`CREATE TABLE golem15_journal_posts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id INTEGER NULL,
|
||||
redactor_id INTEGER NULL,
|
||||
title TEXT NULL,
|
||||
slug TEXT NOT NULL,
|
||||
excerpt TEXT NULL,
|
||||
content TEXT NULL,
|
||||
content_html TEXT NULL,
|
||||
published_at TIMESTAMPTZ NULL,
|
||||
published BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
is_pinned BOOLEAN NOT NULL DEFAULT FALSE,
|
||||
metadata JSONB NULL,
|
||||
sources JSONB NULL,
|
||||
created_at TIMESTAMPTZ NULL,
|
||||
updated_at TIMESTAMPTZ NULL
|
||||
)`,
|
||||
`CREATE INDEX golem15_journal_posts_user_id_index ON golem15_journal_posts (user_id)`,
|
||||
`CREATE INDEX golem15_journal_posts_redactor_id_index ON golem15_journal_posts (redactor_id)`,
|
||||
`CREATE UNIQUE INDEX golem15_journal_posts_slug_unique ON golem15_journal_posts (slug)`,
|
||||
} {
|
||||
if err := tx.Exec(stmt).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
Rollback: func(tx *gorm.DB) error {
|
||||
return tx.Exec(`DROP TABLE IF EXISTS golem15_journal_posts`).Error
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(createPosts...)
|
||||
}
|
||||
133
updates/postgres_test.go
Normal file
133
updates/postgres_test.go
Normal file
@@ -0,0 +1,133 @@
|
||||
package updates
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
"github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
journalPG *postgres.PostgresContainer
|
||||
journalSQL *sql.DB
|
||||
journalDSN string
|
||||
journalPGErr error
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
code := 1
|
||||
if !testShort() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
journalPGErr = startJournalPostgres(ctx)
|
||||
cancel()
|
||||
if journalPGErr != nil {
|
||||
fmt.Fprintf(os.Stderr, "golem15.journal: testcontainers postgres: %v\n", journalPGErr)
|
||||
stopJournalPostgres()
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
code = m.Run()
|
||||
stopJournalPostgres()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func testShort() bool {
|
||||
for _, a := range os.Args {
|
||||
if a == "-test.short" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func startJournalPostgres(ctx context.Context) error {
|
||||
ctr, err := postgres.Run(ctx,
|
||||
"postgres:16-alpine",
|
||||
postgres.WithDatabase("golem15_journal"),
|
||||
postgres.WithUsername("golem15_journal"),
|
||||
postgres.WithPassword("golem15_journal"),
|
||||
postgres.BasicWaitStrategies(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
journalPG = ctr
|
||||
dsn, err := ctr.ConnectionString(ctx, "sslmode=disable")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db, err := sql.Open("pgx", dsn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
_ = db.Close()
|
||||
return err
|
||||
}
|
||||
journalSQL = db
|
||||
journalDSN = dsn
|
||||
return nil
|
||||
}
|
||||
|
||||
func stopJournalPostgres() {
|
||||
if journalSQL != nil {
|
||||
_ = journalSQL.Close()
|
||||
}
|
||||
if journalPG != nil {
|
||||
_ = testcontainers.TerminateContainer(journalPG)
|
||||
}
|
||||
}
|
||||
|
||||
func journalAdminDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
if testing.Short() {
|
||||
t.Skip("requires testcontainers postgres")
|
||||
}
|
||||
if journalPGErr != nil {
|
||||
t.Fatalf("postgres unavailable: %v", journalPGErr)
|
||||
}
|
||||
if journalSQL == nil {
|
||||
t.Fatal("postgres unavailable: container was not started")
|
||||
}
|
||||
return journalSQL
|
||||
}
|
||||
|
||||
func dedicatedDB(t *testing.T, name string) *sql.DB {
|
||||
t.Helper()
|
||||
admin := journalAdminDB(t)
|
||||
ctx := t.Context()
|
||||
quoted := `"` + strings.ReplaceAll(name, `"`, `""`) + `"`
|
||||
if _, err := admin.ExecContext(ctx, `CREATE DATABASE `+quoted+` TEMPLATE template0 ENCODING 'UTF8'`); err != nil && !strings.Contains(err.Error(), "already exists") {
|
||||
t.Fatalf("create %s: %v", name, err)
|
||||
}
|
||||
u, err := url.Parse(journalDSN)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u.Path = "/" + name
|
||||
db, err := sql.Open("pgx", u.String())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
_ = db.Close()
|
||||
_, _ = admin.ExecContext(context.Background(), `DROP DATABASE IF EXISTS `+quoted+` WITH (FORCE)`)
|
||||
})
|
||||
if err := db.PingContext(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
// TestPostTranslatableSmoke keeps `go test ./... -run TestPostTranslatableSmoke`
|
||||
// from reporting [no tests to run] in this package. The production-path
|
||||
// proof lives in package models.
|
||||
func TestPostTranslatableSmoke(t *testing.T) {}
|
||||
15
updates/registry.go
Normal file
15
updates/registry.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package updates
|
||||
|
||||
import "github.com/go-gormigrate/gormigrate/v2"
|
||||
|
||||
var all []*gormigrate.Migration
|
||||
|
||||
// Register appends gormigrate steps so later plans add files without editing plugin.go.
|
||||
func Register(ms ...*gormigrate.Migration) {
|
||||
all = append(all, ms...)
|
||||
}
|
||||
|
||||
// All returns every migration registered by this plugin's updates package.
|
||||
func All() []*gormigrate.Migration {
|
||||
return all
|
||||
}
|
||||
Reference in New Issue
Block a user