Datastore Schema & Postgres Migrations
Relevant source files
The following files were used as context for generating this wiki page:
The Datastore layer provides a persistent storage engine for superglue entities, including tools (configurations), systems (integrations), execution runs, and organization settings. It uses PostgreSQL as the primary relational database to ensure data integrity and support complex multi-tenant queries through organization-based isolation.
This system manages the transition of data from legacy Redis stores to Postgres and handles incremental schema updates through an initialization process. It enforces strict organizational boundaries by requiring an orgId for nearly all operations, preventing cross-tenant data leakage.
Core Architecture
The PostgresService class implements the DataStore interface and serves as the central hub for database interactions. It initializes a connection pool and executes schema setup logic during startup to ensure all required tables and indices exist.
Data Isolation and Security
The service enforces multi-tenancy at the application level. Every query targeting tenant data includes an org_id filter. Sensitive information, such as system credentials and notification tokens, are encrypted before storage using a utility called credentialEncryption.
Sources: packages/core/datastore/postgres.ts:28-44, packages/core/datastore/postgres.ts:1174-1215
Schema Relationships
The following ER diagram illustrates the primary entities and their relationships within the Postgres datastore.
The diagram shows how organizations serve as the root for all primary entities, while runs and schedules link back to specific configurations.
Sources: packages/core/datastore/postgres.ts:98-380
Database Schema Definition
The datastore initializes several key tables to manage the lifecycle of integrations and workflows.
Configurations and Tools
The configurations table stores both API configs and multi-step workflows. It uses a JSONB column named data to hold the flexible structure of tool definitions while maintaining integration_ids in a GIN-indexed array for fast lookup of tools associated with specific systems.
Sources: packages/core/datastore/postgres.ts:98-111
Systems and Integrations
Systems use a composite primary key consisting of (id, environment, org_id). This allows the platform to support separate dev and prod configurations for the same integration ID within a single organization.
Sources: packages/core/datastore/postgres.ts:182-202, packages/core/datastore/postgres.ts:241-260
Key Data Models
| Table Name | Primary Key | Description |
|---|---|---|
configurations | (id, type, org_id) | Stores tool definitions and API configurations in JSONB format. |
integrations | (id, environment, org_id) | Stores external system connection details and encrypted credentials. |
runs | (id, org_id) | Records execution history, statuses, and metadata for every tool run. |
file_references | (id, org_id) | Manages metadata for files stored in S3, used for system documentation. |
workflow_schedules | (id, org_id) | Stores cron-based triggers for automated tool executions. |
Sources: packages/core/datastore/postgres.ts:98-380, packages/core/api/systems-documentation.ts:32-75
Execution Tracking and Metrics
The runs table tracks the lifecycle of tool executions. It captures the request_source (e.g., api, scheduler, webhook) and the final status (SUCCESS, FAILED, ABORTED).
Run Status Mapping
The system maps internal RunStatus enums to string values compatible with the OpenAPI specification for external consumption.
| Internal Status | API Representation |
|---|---|
RunStatus.RUNNING | running |
RunStatus.SUCCESS | success |
RunStatus.FAILED | failed |
RunStatus.ABORTED | aborted |
Sources: packages/core/api/response-helpers.ts:63-71, packages/core/datastore/postgres.ts:140-180
Performance Monitoring
PostgresService provides methods to calculate P95 latency for runs and total success/failure counts over a specific window. These metrics are grouped by request_source to allow granular performance analysis.
Sources: packages/core/datastore/postgres.ts:588-649
Redis to Postgres Migration
The migrate.ts script handles the transfer of data from legacy Redis instances to the Postgres datastore. This is a batch-based process designed for reliability and progress tracking.
Migration Flow
The script follows a specific sequence to ensure data integrity:
- Identify all unique Organization IDs stored in Redis by parsing keys.
- For each organization, iterate through data types: API Configs, Extract Configs, Transform Configs, Workflows, and Integrations.
- Process items in batches of 500 to prevent memory overflow.
- Attempt to
upserteach record into Postgres. - Log errors per item while continuing the overall migration.
The flow demonstrates the sequential migration of entities, prioritized by their dependency order.
Sources: scripts/migrate.ts:19-50, scripts/migrate.ts:258-300
Documentation and File Management
Documentation for systems is managed via file_references. The datastore tracks the status of documentation processing (e.g., PENDING, PROCESSING, COMPLETED).
Documentation Persistence Logic
- A user triggers a documentation scrape or upload.
- The
systems-documentationAPI creates afile_referencein thePENDINGstate. - The file is uploaded to S3, and the
storage_uriis recorded in Postgres. - The system updates the
integrationstable to include the new file ID indocumentation_files(a JSONB object containingscrapeFileIds,uploadFileIds, oropenApiFileIds).
Sources: packages/core/api/systems-documentation.ts:86-118, packages/core/systems/system-manager.ts:109-158
Schema Migration Logic
The initialization routine includes idempotent SQL commands to update existing tables without data loss. For example, it uses DO blocks to safely migrate primary keys and add columns like environment to the integrations table while defaulting existing rows to prod.
Sources: packages/core/datastore/postgres.ts:219-245
Summary
The Datastore module provides a robust, multi-tenant relational foundation for superglue. It uses PostgreSQL's advanced features like JSONB for flexible workflow data and GIN indices for efficient querying. The architecture supports complex operational needs, including dev/prod environment switching for integrations, detailed execution logging for debugging, and automated data migration from legacy key-value stores. By enforcing organizational isolation and encrypting sensitive credentials, the datastore maintains high security standards for enterprise integration data.