feat: init migrations for transactional tables (#9946)

# Which Problems Are Solved

To start with transactional tables we need to setup the new `zitadel`
schema in a way that does not rely on the event store later.

# How the Problems Are Solved

Setup step added which calls the function which executes the migrations.

# Additional Changes

none

# Additional Context

- closes #9933
This commit is contained in:
Silvan
2025-05-26 08:20:14 +02:00
committed by GitHub
parent 3ddecca600
commit 362420f62b
16 changed files with 197 additions and 36 deletions

View File

@@ -0,0 +1,16 @@
package migration
import (
_ "embed"
)
var (
//go:embed 001_instance_table/up.sql
up001InstanceTable string
//go:embed 001_instance_table/down.sql
down001InstanceTable string
)
func init() {
registerSQLMigration(1, up001InstanceTable, down001InstanceTable)
}

View File

@@ -0,0 +1 @@
DROP TABLE zitadel.instances;

View File

@@ -0,0 +1,6 @@
CREATE TABLE IF NOT EXISTS zitadel.instances(
id VARCHAR(100) NOT NULL
, PRIMARY KEY (id)
, name VARCHAR(100) NOT NULL
);

View File

@@ -0,0 +1,13 @@
// This package contains the migration logic for the PostgreSQL dialect.
// It uses the [github.com/jackc/tern/v2/migrate] package to handle the migration process.
//
// **Developer Note**:
//
// Each migration MUST be registered in an init function.
// Create a go file for each migration with the sequence of the migration as prefix and some descriptive name.
// The file name MUST be in the format <sequence>_<name>.go.
// Each migration SHOULD provide an up and down migration.
// Prefer to write SQL statements instead of funcs if it is reasonable.
// To keep the folder clean create a folder to store the sql files with the following format: <sequence>_<name>/{up/down}.sql.
// And use the go embed directive to embed the sql files.
package migration

View File

@@ -0,0 +1,33 @@
package migration
import (
"context"
"github.com/jackc/pgx/v5"
"github.com/jackc/tern/v2/migrate"
)
var migrations []*migrate.Migration
func Migrate(ctx context.Context, conn *pgx.Conn) error {
// we need to ensure that the schema exists before we can run the migration
// because creating the migrations table already required the schema
_, err := conn.Exec(ctx, "CREATE SCHEMA IF NOT EXISTS zitadel")
if err != nil {
return err
}
migrator, err := migrate.NewMigrator(ctx, conn, "zitadel.migrations")
if err != nil {
return err
}
migrator.Migrations = migrations
return migrator.Migrate(ctx)
}
func registerSQLMigration(sequence int32, up, down string) {
migrations = append(migrations, &migrate.Migration{
Sequence: sequence,
UpSQL: up,
DownSQL: down,
})
}