# Template: PostgreSQL Load with Target Validation
# Copy to your pipeline directory as load.yaml
#
# This template demonstrates target contract validation before loading.
# Data is validated against a contract (schema + coercion + validation rules)
# to ensure it meets the target system's requirements.
#
# Usage:
#   pipeline = Pipeline.from_config_dir("path/to/pipeline")
#   result = await pipeline.run()
#   print(f"Records loaded: {result.rows_loaded}")
#   print(f"Quarantined at load: {result.rows_quarantined_load}")

title: postgres_load_with_validation
description: Load to PostgreSQL with target contract validation
version: "1.0.0"

# Destination type
type: postgres

# Database connection
connection_string: ${DATABASE_URL}
# Or use database object:
# database:
#   url: ${DATABASE_URL}

# Target table
schema: public
table: orders

# Write method
write_method: upsert  # insert, upsert, replace, truncate_and_load
primary_key: order_id

# Batch size
batch_size: 500

# ============================================================================
# Target Contract Validation (Optional)
# ============================================================================
# Validate transformed data against a contract before loading.
# This ensures data conforms to the target table schema and business rules.

validation:
  # Option 1: Path to contract directory or file
  # Contract directory should contain: schema.yaml, coercion_rules.yaml (optional),
  # validation_rules.yaml (optional)
  contract: ./contracts/orders
  
  # Option 2: Reference contract from metadata store (requires settings.yaml metadata_store)
  # contract:
  #   type: database
  #   name: orders_contract
  #   version: "2.0"  # Optional, defaults to latest
  
  # Option 3: Use default contract from settings.yaml
  # use_contract: true
  
  # Action when validation fails:
  # - fail: Stop pipeline immediately (default)
  # - warn: Log warning but continue with all records
  # - skip: Skip invalid records (don't load them)
  # - quarantine: Send invalid records to DLQ
  on_error: quarantine
  
  # DLQ configuration (optional, overrides settings.yaml)
  dlq: true  # Use default DLQ from settings.yaml

# ============================================================================
# Example Contract Structure (create in ./contracts/orders/)
# ============================================================================
#
# contracts/orders/schema.yaml:
# ---
# version: "1.0.0"
# type: object
# properties:
#   order_id:
#     type: string
#     maxLength: 20
#   customer_id:
#     type: integer
#   status:
#     type: string
#     enum: ["pending", "processing", "shipped", "delivered", "cancelled"]
#   total_amount:
#     type: number
#     minimum: 0
#   created_at:
#     type: string
#     format: date-time
# required:
#   - order_id
#   - customer_id
#   - status
#
# contracts/orders/coercion_rules.yaml:
# ---
# order_id: string
# customer_id: int
# total_amount: float
# created_at: datetime
#
# contracts/orders/validation_rules.yaml:
# ---
# order_id:
#   - not_empty
#   - max_length: 20
# total_amount:
#   - min_value: 0
#   - max_value: 1000000
