Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

fix(upgrade): stop dev builds from re-sealing their own database

Jonas Kanindac606f7a8d47b
diff --git a/internal/storage/migration/upgrade/upgrade.go b/internal/storage/migration/upgrade/upgrade.go
index 42c37f3..e133e55 100644
--- a/internal/storage/migration/upgrade/upgrade.go
+++ b/internal/storage/migration/upgrade/upgrade.go
@@ -76,6 +76,13 @@ func runLocked(ctx context.Context, db *gorm.DB, binaryVersion string, opts Opti
if err != nil {
return fmt.Errorf("upgrade: read stored version: %w", err)
}
+ if fresh {
+ applied, err := loadAppliedIDs(db)
+ if err != nil {
+ return fmt.Errorf("upgrade: load applied steps: %w", err)
+ }
+ fresh = len(applied) == 0
+ }
switch {
case fresh:
diff --git a/internal/storage/migration/upgrade/upgrade_test.go b/internal/storage/migration/upgrade/upgrade_test.go
new file mode 100644
index 0000000..3a8f717
--- /dev/null
+++ b/internal/storage/migration/upgrade/upgrade_test.go
@@ -0,0 +1,72 @@
+// Copyright (c) 2026 Jonas Kaninda.
+//
+// This file is part of Posta.
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+//
+// SPDX-License-Identifier: AGPL-3.0-or-later
+package upgrade
+
+import (
+ "context"
+ "os"
+ "testing"
+
+ "github.com/goposta/posta/internal/models"
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+ "gorm.io/gorm/logger"
+)
+
+// A dev binary never writes app.version, so the database it created looks
+// unversioned on the next boot. That boot must not seal the registry again.
+func TestDevBinaryRestartsOnItsOwnDatabase(t *testing.T) {
+ dsn := os.Getenv("TEST_DATABASE_DSN")
+ if dsn == "" {
+ dsn = "host=localhost user=posta password=posta dbname=posta port=5432 sslmode=disable"
+ }
+ db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
+ if err != nil {
+ t.Skipf("skipping: no test database available: %v", err)
+ }
+ if err := db.AutoMigrate(&models.Setting{}, &models.UpgradeStep{}); err != nil {
+ t.Skipf("skipping: cannot migrate test schema: %v", err)
+ }
+ tx := db.Begin()
+ t.Cleanup(func() { tx.Rollback() })
+
+ if err := tx.Exec("DELETE FROM upgrade_steps").Error; err != nil {
+ t.Fatalf("clear steps: %v", err)
+ }
+ if err := tx.Where("key = ?", KeyAppVersion).Delete(&models.Setting{}).Error; err != nil {
+ t.Fatalf("clear version: %v", err)
+ }
+
+ for boot := 1; boot <= 2; boot++ {
+ if err := runLocked(context.Background(), tx, devVersion, Options{}); err != nil {
+ t.Fatalf("boot %d: %v", boot, err)
+ }
+ }
+
+ var steps int64
+ if err := tx.Model(&models.UpgradeStep{}).Count(&steps).Error; err != nil {
+ t.Fatalf("count steps: %v", err)
+ }
+ if steps != int64(len(registry)) {
+ t.Fatalf("recorded %d steps, want %d", steps, len(registry))
+ }
+ if _, fresh, err := readVersion(tx); err != nil || !fresh {
+ t.Fatalf("dev boots wrote app.version (fresh=%v, err=%v); the regression needs it absent", fresh, err)
+ }
+}