PhoenixKitSync.Transfer (PhoenixKitSync v0.3.0)

Copy Markdown View Source

Schema for DB Sync data transfers.

Tracks all data transfers between PhoenixKit instances, including both uploads (sending data) and downloads (receiving data).

Direction

  • "send" - This site sent data to another site
  • "receive" - This site received data from another site

Status Flow

pending → pending_approval → approved → in_progress → completed
                          ↘
                          denied
                          ↘
                          expired (approval timed out)

pending → in_progress → completed
                     ↘
                     failed
                     ↘
                     cancelled

Record Tracking

The transfer tracks various record counts:

  • records_requested - Total records requested
  • records_transferred - Records actually transferred
  • records_created - New records inserted
  • records_updated - Existing records updated
  • records_skipped - Records skipped due to conflicts
  • records_failed - Records that failed to import

Usage Examples

# Create a transfer record
{:ok, transfer} = Transfers.create_transfer(%{
  direction: "receive",
  connection_uuid: conn.uuid,
  table_name: "users",
  records_requested: 100,
  conflict_strategy: "skip"
})

# Update transfer progress
{:ok, transfer} = Transfers.update_progress(transfer, %{
  records_transferred: 50,
  records_created: 45,
  records_skipped: 5
})

# Complete a transfer
{:ok, transfer} = Transfers.complete_transfer(transfer)

Summary

Types

t()

A transfer row. An association is Ecto.Association.NotLoaded until preloaded. There is no updated_at: the lifecycle is in the status and its *_at fields.

Functions

Checks if a transfer is currently active.

Checks if a transfer's approval has expired.

Changeset for approving a transfer.

Checks if a transfer can be started.

Changeset for cancelling a transfer.

Creates a changeset for transfer creation.

Changeset for completing a transfer successfully.

Calculates the transfer duration in seconds. Returns nil if transfer hasn't completed.

Changeset for marking a transfer approval as expired.

Changeset for marking a transfer as failed. stats takes the same record counts as complete_changeset/2, for a transfer that moved some rows before it stopped.

Changeset for a transfer recorded once it is over, in one write: the creation fields plus its record counts, its final status ("completed" or "failed") and, for a failure, the error_message.

Checks if a transfer is pending approval.

Changeset for updating transfer progress.

Changeset for starting a transfer.

Calculates the success rate of a transfer. Returns a float between 0.0 and 1.0.

Checks if a transfer is in a terminal state.

Types

t()

@type t() :: %PhoenixKitSync.Transfer{
  __meta__: term(),
  approval_expires_at: DateTime.t() | nil,
  approved_at: DateTime.t() | nil,
  approved_by_user:
    PhoenixKit.Users.Auth.User.t() | Ecto.Association.NotLoaded.t() | nil,
  approved_by_uuid: UUIDv7.t() | nil,
  bytes_transferred: integer() | nil,
  completed_at: DateTime.t() | nil,
  conflict_strategy: String.t() | nil,
  connection:
    PhoenixKitSync.Connection.t() | Ecto.Association.NotLoaded.t() | nil,
  connection_uuid: UUIDv7.t() | nil,
  denial_reason: String.t() | nil,
  denied_at: DateTime.t() | nil,
  denied_by_user:
    PhoenixKit.Users.Auth.User.t() | Ecto.Association.NotLoaded.t() | nil,
  denied_by_uuid: UUIDv7.t() | nil,
  direction: String.t() | nil,
  error_message: String.t() | nil,
  initiated_by_user:
    PhoenixKit.Users.Auth.User.t() | Ecto.Association.NotLoaded.t() | nil,
  initiated_by_uuid: UUIDv7.t() | nil,
  inserted_at: DateTime.t() | nil,
  metadata: map() | nil,
  records_created: integer() | nil,
  records_failed: integer() | nil,
  records_requested: integer() | nil,
  records_skipped: integer() | nil,
  records_transferred: integer() | nil,
  records_updated: integer() | nil,
  remote_site_url: String.t() | nil,
  requester_ip: String.t() | nil,
  requester_user_agent: String.t() | nil,
  requires_approval: boolean() | nil,
  session_code: String.t() | nil,
  started_at: DateTime.t() | nil,
  status: String.t() | nil,
  table_name: String.t() | nil,
  uuid: UUIDv7.t() | nil
}

A transfer row. An association is Ecto.Association.NotLoaded until preloaded. There is no updated_at: the lifecycle is in the status and its *_at fields.

Functions

active?(arg1)

@spec active?(term()) :: boolean()

Checks if a transfer is currently active.

approval_expired?(transfer)

@spec approval_expired?(t()) :: boolean()

Checks if a transfer's approval has expired.

approve_changeset(transfer, admin_user_uuid)

@spec approve_changeset(t(), String.t() | map() | nil) :: Ecto.Changeset.t()

Changeset for approving a transfer.

can_start?(arg1)

@spec can_start?(term()) :: boolean()

Checks if a transfer can be started.

cancel_changeset(transfer)

@spec cancel_changeset(t()) :: Ecto.Changeset.t()

Changeset for cancelling a transfer.

changeset(transfer, attrs)

@spec changeset(t() | Ecto.Changeset.t(), map()) :: Ecto.Changeset.t()

Creates a changeset for transfer creation.

complete_changeset(transfer, attrs \\ %{})

@spec complete_changeset(t(), map()) :: Ecto.Changeset.t()

Changeset for completing a transfer successfully.

deny_changeset(transfer, admin_user_uuid, reason \\ nil)

@spec deny_changeset(t(), String.t() | map() | nil, String.t() | nil) ::
  Ecto.Changeset.t()

Changeset for denying a transfer.

duration_seconds(transfer)

@spec duration_seconds(t()) :: non_neg_integer() | nil

Calculates the transfer duration in seconds. Returns nil if transfer hasn't completed.

expire_changeset(transfer)

@spec expire_changeset(t()) :: Ecto.Changeset.t()

Changeset for marking a transfer approval as expired.

fail_changeset(transfer, error_message, stats \\ %{})

@spec fail_changeset(t(), String.t(), map()) :: Ecto.Changeset.t()

Changeset for marking a transfer as failed. stats takes the same record counts as complete_changeset/2, for a transfer that moved some rows before it stopped.

finished_changeset(transfer, attrs, status, error_message \\ nil)

@spec finished_changeset(t(), map(), String.t(), String.t() | nil) ::
  Ecto.Changeset.t()

Changeset for a transfer recorded once it is over, in one write: the creation fields plus its record counts, its final status ("completed" or "failed") and, for a failure, the error_message.

pending_approval?(arg1)

@spec pending_approval?(term()) :: boolean()

Checks if a transfer is pending approval.

progress_changeset(transfer, attrs)

@spec progress_changeset(t(), map()) :: Ecto.Changeset.t()

Changeset for updating transfer progress.

request_approval_changeset(transfer, expires_in_hours \\ 24)

@spec request_approval_changeset(t(), pos_integer()) :: Ecto.Changeset.t()

Changeset for requesting approval.

start_changeset(transfer)

@spec start_changeset(t()) :: Ecto.Changeset.t()

Changeset for starting a transfer.

success_rate(transfer)

@spec success_rate(t()) :: float()

Calculates the success rate of a transfer. Returns a float between 0.0 and 1.0.

terminal?(arg1)

@spec terminal?(term()) :: boolean()

Checks if a transfer is in a terminal state.