Files
Containr/app/backend/internal/database/migrations.go
Tomas Dvorak 9e3aa3aa17 Consolidate backend: drop vendored APwhy, converge on goose + sqlc
Phase 0 cleanup verified end-to-end:

- Delete vendored APwhy runtime (cmd/apwhy, internal/apwhy) and the
  unwired copy (internal/api/server.go + static bundle + cloned
  internal/{storage,analytics,auth,rbac}) — ~7.5k lines of dead code.
- Keep Containr's native internal/gateway; move APwhy management
  routes under /api/v1/gateway/* so they stop colliding with the
  native /services paths. Fix handlers against the real schema:
  UUID ids via RETURNING, integer enabled flags, plan validation,
  service validate endpoint, unique-violation mapping.
- Port agents.go off GORM onto sqlc (sqlc/queries/agents.sql);
  drop gorm dependencies; fix sqlc schema drift (services.environment_id).
- Replace the SELECT 1 goose baseline with the real consolidated
  schema (52 tables) minus APwhy's parallel auth/RBAC objects;
  idempotent via IF NOT EXISTS / OR REPLACE / guarded DO blocks so it
  replays cleanly over legacy installs. Add a drop migration for the
  dead objects and delete the legacy filename-tracked migrations/
  loader, directory, and Dockerfile copy. Verified via cmd/migrate:
  fresh install 53 tables, legacy upgrade clean to 20260917000000.
- Canvas: add invisible React Flow handles to ServiceNode so inferred
  auto-connections stop emitting warning #008.
- Remove reactflow dep (superseded by @xyflow/react), test harness
  cmds, and legacy 0001_init.sql backups.

Authored By: TDvorak <[email protected]>
2026-09-17 09:40:08 +02:00

65 lines
1.7 KiB
Go

package database
import (
"fmt"
"log"
)
func (db *DB) ensureRequiredExtensions() error {
if _, err := db.Exec(`CREATE EXTENSION IF NOT EXISTS pgcrypto`); err != nil {
return fmt.Errorf("failed to ensure pgcrypto extension required for UUID defaults: %w", err)
}
return nil
}
// SeedData inserts initial data for development
func (db *DB) SeedData() error {
// Check if we already have users
var count int
err := db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count)
if err != nil {
return fmt.Errorf("failed to check existing users: %w", err)
}
if count > 0 {
log.Println("Database already has data, skipping seed")
return nil
}
// Insert demo user
hashedPassword := "$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi" // "password"
_, err = db.Exec(`
INSERT INTO users (email, password_hash, name)
VALUES ($1, $2, $3)
`, "[email protected]", hashedPassword, "Demo User")
if err != nil {
return fmt.Errorf("failed to create demo user: %w", err)
}
// Insert demo project
var projectID string
err = db.QueryRow(`
INSERT INTO projects (name, description, owner_id)
VALUES ($1, $2, (SELECT id FROM users WHERE email = $3))
RETURNING id
`, "Demo Project", "A sample project to showcase Containr features", "[email protected]").Scan(&projectID)
if err != nil {
return fmt.Errorf("failed to create demo project: %w", err)
}
// Insert environments
environments := []string{"production", "preview", "development"}
for _, env := range environments {
_, err = db.Exec(`
INSERT INTO environments (name, project_id)
VALUES ($1, $2)
`, env, projectID)
if err != nil {
return fmt.Errorf("failed to create environment %s: %w", env, err)
}
}
log.Println("Database seeded successfully")
return nil
}