Files
sm-journal-plugin/updates/category_content_migration_test.go
Jakub Zych 305f94afd9 feat: H1 to H2 setting with the admin save conversion (tracer)
- content_h1_to_h2 and generic_anchors on the settings row (migration
  202610280001), Headings and links tab, config key
  golem15.journal.content_h1_to_h2 used when no settings row exists
- classes.ConvertH1 on the x/net/html tokenizer; the Blog Editor types
  convert while the setting is on and answer editor.h1_converted
- GET /posts/{slug} reports content_h1_to_h2 (additive key)
2026-11-04 09:44:58 +01:00

94 lines
2.9 KiB
Go

package updates
import (
"database/sql"
"fmt"
"testing"
"time"
"git.golem15.com/golem15/summercms/modules/lagoon"
"github.com/go-gormigrate/gormigrate/v2"
)
func TestCategoryContentMigration(t *testing.T) {
sqlDB := dedicatedDB(t, fmt.Sprintf("journal_catcontent_%d", time.Now().UnixNano()))
gdb, err := lagoon.Use(t.Context(), sqlDB)
if err != nil {
t.Fatal(err)
}
backend := gormigrate.New(gdb, &gormigrate.Options{
TableName: "summer_migrations_summercms_cabana",
UseTransaction: true,
}, lagoon.BackendAdminMigrations)
if err := backend.Migrate(); err != nil {
t.Fatalf("backend_users migrations: %v", err)
}
m := journalMigrator(gdb)
if err := m.Migrate(); err != nil {
t.Fatalf("migrate: %v", err)
}
// The content column's step and the one before it: later migrations
// are rolled back with it, so the test does not depend on being last.
const contentStep = "202610090001_add_content_to_golem15_journal_categories"
previous := ""
for i, mig := range All() {
if mig.ID == contentStep {
if i == 0 {
t.Fatalf("%s is the first migration", contentStep)
}
previous = All()[i-1].ID
}
}
if previous == "" {
t.Fatalf("migration %s is not registered", contentStep)
}
if !gdb.Migrator().HasColumn("golem15_journal_categories", "content") {
t.Fatal("golem15_journal_categories.content missing")
}
assertUdt(t, gdb, "golem15_journal_categories", "content", "text")
var nullable string
if err := gdb.WithContext(t.Context()).Raw(
`SELECT is_nullable FROM information_schema.columns WHERE table_name = 'golem15_journal_categories' AND column_name = 'content'`,
).Scan(&nullable).Error; err != nil {
t.Fatal(err)
}
if nullable != "YES" {
t.Fatalf("content is_nullable = %q, want YES", nullable)
}
if err := gdb.Exec(`UPDATE golem15_journal_categories SET content = '<p>x</p>' WHERE slug = 'uncategorized'`).Error; err != nil {
t.Fatal(err)
}
if err := m.RollbackTo(previous); err != nil {
t.Fatalf("rollback to %s: %v", previous, err)
}
if !gdb.Migrator().HasTable("golem15_journal_categories") {
t.Fatal("golem15_journal_categories dropped by the content rollback")
}
if gdb.Migrator().HasColumn("golem15_journal_categories", "content") {
t.Fatal("content column survived rollback")
}
var rows int64
if err := gdb.Raw(`SELECT COUNT(*) FROM golem15_journal_categories WHERE slug = 'uncategorized'`).Scan(&rows).Error; err != nil {
t.Fatal(err)
}
if rows != 1 {
t.Fatalf("uncategorized rows after rollback = %d, want 1", rows)
}
if err := m.Migrate(); err != nil {
t.Fatalf("re-migrate: %v", err)
}
if !gdb.Migrator().HasColumn("golem15_journal_categories", "content") {
t.Fatal("content column missing after re-migrate")
}
var content sql.NullString
if err := gdb.Raw(`SELECT content FROM golem15_journal_categories WHERE slug = 'uncategorized'`).Scan(&content).Error; err != nil {
t.Fatal(err)
}
if content.Valid {
t.Fatalf("uncategorized content after re-migrate = %q, want NULL", content.String)
}
}