Skip to content

Conversation

@Ashraf8ila
Copy link

Improve PeerDB for Postgres-to-Postgres migrations — add schema, trigger, and index migration support (with tests)

…ger, and index migration support (with tests)
@CLAassistant
Copy link

CLAassistant commented Nov 11, 2025

CLA assistant check
All committers have signed the CLA.

@Ashraf8ila
Copy link
Author

  1. Schema Migration (MigrateSchemaFromSource)
    Detects source table schemas (columns, data types, nullability, defaults)
    Creates tables if missing in target
    Alters existing tables to add missing columns
    Preserves primary keys and constraints
    Handles column data types, nullability, and default values

  2. Trigger Migration (MigrateTriggersFromSource)
    Detects triggers using pg_get_triggerdef()
    Extracts timing (BEFORE/AFTER/INSTEAD OF) and events (INSERT/UPDATE/DELETE/TRUNCATE)
    Drops existing triggers on target before recreating
    Creates triggers with proper table name replacement
    Uses transactions for atomicity

  3. Index Migration (MigrateIndexesFromSource)
    Migrates non-primary key indexes
    Preserves unique constraints
    Handles index methods (btree, hash, etc.)
    Skips primary key indexes (handled by schema migration)
    Drops existing indexes before recreating


Testing
4 tests (all passing):
TestSchemaMigration — Verifies table creation, column migration, and primary key preservation
TestTriggerMigration — Verifies trigger migration with function dependencies
TestIndexMigration — Verifies index migration including unique indexes
TestFullMigration — Integration test combining all three migration types

Comment on lines +243 to +244
switch dstPgConn := any(dstConn).(type) {
case *connpostgres.PostgresConnector:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use type assertion like in flowable.go

// MigrateTriggersAndIndexesForPostgresToPostgres migrates triggers and indexes
// from source to target for Postgres-to-Postgres flows.
// This should be called after schema deltas are replayed or during initial setup.
func (c *PostgresConnector) MigrateTriggersAndIndexesForPostgresToPostgres(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this is method & other methods are free standing functions in migration.go

@serprex serprex requested a review from ilidemi November 11, 2025 23:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants