sync

Function sync 

Source
pub async fn sync(
    source_url: &str,
    target_url: &str,
    filter: Option<ReplicationFilter>,
    publication_name: Option<&str>,
    subscription_name: Option<&str>,
    sync_timeout_secs: Option<u64>,
    force: bool,
) -> Result<()>
Expand description

Set up logical replication between source and target databases

This command performs Phase 3 of the migration process:

  1. Discovers all databases on the source
  2. Filters databases based on the provided filter criteria
  3. For each database:
    • Checks if subscription already exists (skips or recreates based on state and force flag)
    • Creates a publication on the source database (filtered tables if specified)
    • Creates a subscription on the target database pointing to the source
    • Waits for the initial sync to complete

After this command succeeds, changes on the source databases will continuously replicate to the target until the subscriptions are dropped.

This command is idempotent - it can be safely re-run if interrupted or if setup failed partially.

§Arguments

  • source_url - PostgreSQL connection string for source database
  • target_url - PostgreSQL connection string for target (Seren) database
  • filter - Optional replication filter for database and table selection
  • publication_name - Optional publication name template (defaults to “seren_migration_pub”)
  • subscription_name - Optional subscription name template (defaults to “seren_migration_sub”)
  • sync_timeout_secs - Optional timeout in seconds per database (defaults to 300)
  • force - Force recreate subscriptions even if they already exist (defaults to false)

§Returns

Returns Ok(()) if replication setup completes successfully for all databases.

§Errors

This function will return an error if:

  • Cannot connect to source or target database
  • Cannot discover databases on source
  • Publication creation fails for any database
  • Subscription creation fails for any database
  • Initial sync doesn’t complete within timeout for any database

§Examples

// Replicate all databases
sync(
    "postgresql://user:pass@source.example.com/postgres",
    "postgresql://user:pass@target.example.com/postgres",
    None,  // No filter - replicate all databases
    None,  // Use default publication name
    None,  // Use default subscription name
    Some(600),  // 10 minute timeout per database
    false,  // Don't force recreate
).await?;

// Replicate only specific databases
let filter = ReplicationFilter::new(
    Some(vec!["mydb".to_string(), "analytics".to_string()]),
    None,
    None,
    None,
)?;
sync(
    "postgresql://user:pass@source.example.com/postgres",
    "postgresql://user:pass@target.example.com/postgres",
    Some(filter),
    None,
    None,
    Some(600),
    false,  // Don't force recreate
).await?;