Data model and persistence lifecycle
The data model is the durable record of Redash workspaces, people, access rules, queries, dashboards, and query results. It gives the web application and background workers one shared view of tenant-owned data. Redis holds short-lived state that supports activity updates, schema lookup, and scheduling. See Application runtime and service composition, Configuration and organization settings, and Background jobs and scheduling for the surrounding services.
Model layer
The model layer turns product concepts into related records with common rules for identity, ownership, time, and serialization. The application uses these records as the shared boundary between request handlers, workers, and external data systems.
Organization records
An organization is the main tenant boundary. It owns users, groups, data sources, queries, dashboards, alerts, and other workspace records. It also carries settings that shape behavior for that tenant. Organization-scoped access is part of the model contract, so a record from one workspace is not treated as a record from another workspace. See Configuration and organization settings for the wider tenant settings flow.
References: redash/…/organizations.py• L13-25 redash/…/organizations.py• L39-90 redash/…/mixins.py• L20-28 redash/…/__init__.py• L111-135
User and group records
Users represent people and service-facing accounts inside an organization. Groups provide the permission set for a user, while the built-in default group gives each user a base workspace role. Account state remains separate from access state. A user can be active, disabled, pending an invitation, or linked to an external sign-in provider. API users provide request identity through an API key without creating a second durable person record.
References: redash/…/users.py• L77-105 redash/…/users.py• L115-158 redash/…/users.py• L245-301 redash/…/users.py• L377-422 redash/…/__init__.py• L1520-1538
Product record relationships
Queries are the central product record for saved analysis. A query belongs to an organization and user. It points to a data source and can point to its latest result. Visualizations and dashboards use that saved analysis for presentation. Alerts use the latest result to assess a condition and notify subscribers.
Query results remain separate from query definitions. This lets workers reuse a result by its query hash and data source while queries point to the newest matching result. Parameterized queries add a validated input layer before execution, so declared values stay tied to the saved query and organization.
The model also stores permissions, favorites, events, API keys, alert subscriptions, and notification destinations. These supporting records extend the main product relationships without replacing the organization boundary.
References: redash/…/__init__.py• L311-324 redash/…/__init__.py• L368-386 redash/…/__init__.py• L446-485 redash/…/parameterized_query.py• L20-36 redash/…/parameterized_query.py• L120-211 redash/…/__init__.py• L899-912 redash/…/__init__.py• L1114-1135 redash/…/__init__.py• L1240-1279
Shared model behavior
Common model behavior keeps records consistent across product areas. Timestamp fields show when a record was created or changed. Organization helpers enforce tenant-aware lookup. Mutable JSON and list values remain visible to the database session when their contents change.
Some records refer to more than one product type through a generic object reference. This shared shape lets permissions, changes, and API keys point to objects such as queries or dashboards without separate record forms for each target type. Search support also belongs to the shared model setup, so query text and metadata can be searched through the model layer.
References: redash/…/mixins.py• L6-28 redash/…/types.py• L32-46 redash/…/types.py• L48-106 redash/…/base.py• L28-65 redash/…/base.py• L68-92
Persistence lifecycle
The persistence lifecycle moves data between model objects, the database session, and durable database tables. The same lifecycle supports web requests and worker jobs, so a change becomes visible to other processes only when its transaction is committed.
The main flow is:
flowchart LR
A[Request or background job] --> B[Load or create model records]
B --> C[Change shared model state]
C --> D[Flush and validate relationships]
D --> E[Commit database transaction]
E --> F[Other requests and workers read durable state]
C -. short-lived derived state .-> G[Redis]
Database session lifecycle
The Flask application binds the shared model session to the configured Redash database during startup. Handlers and tasks add records or update relationships in that session. They commit when the business action is complete. A flush can make new record identities available inside the current transaction, but only a commit makes the change durable for other processes.
References: redash/app.py• L25-57 redash/…/base.py• L13-38 redash/…/users.py• L28-43 redash/…/__init__.py• L368-386
Query result persistence
Query results are durable records linked to an organization and a data source. A saved query points to its latest result, while older results remain available for reuse or later cleanup when no query uses them. This separates the saved query definition from the data returned by a query run.
The query model uses result time and schedule state to decide when a background worker should refresh data. If a query is archived, its schedule is removed and related presentation or alert records are cleared. The archived record then stops driving active work. See Background jobs and scheduling for the worker side of this lifecycle.
References: redash/…/__init__.py• L311-366 redash/…/__init__.py• L368-386 redash/…/__init__.py• L490-507 redash/…/__init__.py• L581-646 tests/test_models.py• L329-383
Transient state boundaries
Not every value used by the product belongs in the main database. Redis carries short-lived user activity timestamps and data-source schema cache entries. The database stores durable user details and the data-source records that own those values.
This split reduces database writes and lets workers refresh derived state. A worker must complete the sync or refresh path before the database reflects the latest transient value.
References: redash/…/users.py• L25-60 redash/…/__init__.py• L206-228 redash/…/__init__.py• L236-256 redash/…/__init__.py• L88-108
Change tracking
Change tracking gives important editable records a history in addition to their current state. It records the user responsible for a change, the object version, and the previous and current values that explain how the record moved from one state to the next.
Versioned records
Queries and dashboards use a version value to coordinate edits from more than one client. The database checks the expected version during an update. A stale edit is rejected instead of silently overwriting newer data. This protects saved analysis and dashboard layout from lost changes.
References: redash/…/__init__.py• L446-488 redash/…/__init__.py• L1114-1135 redash/…/dashboards.py• L233-248
Change history
The change record is an audit entry linked to the edited object and the user who made the edit. Tracking captures a field-level view of the transition. Callers can request the last known change for an object when the product needs to explain a destructive or visible edit.
References: redash/…/changes.py• L8-45 redash/…/changes.py• L48-98 redash/…/__init__.py• L490-507 redash/…/dashboards.py• L254-270
Schema migration lifecycle
Schema migrations keep deployed database tables aligned with the model contract over time. Flask-Migrate and Alembic use the application database URL and model metadata to update existing installations. Ordered revision files describe both structural changes and data moves that must happen as the schema evolves.
Database initialization
An empty database can be created from model metadata after required database extensions and mapper setup are ready. The database is then marked at the current migration position, so later upgrades apply only revisions added afterward. Existing databases keep their history and move through the migration chain instead of being recreated.
References: redash/…/database.py• L18-64 redash/app.py• L47-51 redash/__init__.py• L45-50
Migration revisions
Each revision describes an upgrade and a downgrade for one schema or data change. The history includes data moves such as extracting dashboard tags from names, moving widget positions into the current layout shape, converting user details to JSONB, and recalculating query hashes. It also maintains search expressions and query search vectors when search behavior changes.
Revisions run in order against existing data. A migration history must have one current head. A focused migration check protects this rule because two heads can leave deployments with different schema paths.
References: migrations/…/0f740a081d20_inline_tags.py• L23-38 migrations/…/6b5be7e0a0ef_.py• L20-52 migrations/…/969126bd800f_.py• L24-83 migrations/…/fd4fc850d7ea_.py• L21-60 migrations/…/9e8c841d1a30_fix_hash.py• L24-64 migrations/…/db0aca1ebd32_12_column_dashboard_layout.py• L19-38 migrations/…/2f2a8a6f9c21_update_sqlalchemy_searchable_expressions.py• L22-27 migrations/env.py• L16-27 migrations/env.py• L51-87 tests/test_migrations.py• L7-21
Change checklist
When these files or sections are changed, remember to consider these:
- A change to an organization-owned model must preserve the organization relation used by tenant-aware lookups and access joins.
- A change to a versioned query or dashboard must preserve both the optimistic update check and the change-history record shape.
- A change to a JSON or list field must keep in-place edits visible to the session and keep any indexed or cast access paths valid.
- A new or changed database field must have a matching ordered migration, including a safe path for databases that already contain data.
- A data migration that changes stored meaning must preserve the matching model contract for old and new records.
- A change to query-result or user activity state must identify whether the source of truth is PostgreSQL or the Redis-derived path.
- A change to search fields or expressions must keep stored search vectors and database search functions aligned.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A record is loaded without its organization scope | A request can read or change data from the wrong tenant | Use organization-aware model lookups and relationships |
| Two clients edit the same query or dashboard version | A stale edit can replace newer state or fail at the update boundary | Keep the version check and return a conflict on stale data |
| A mutable JSON or list value changes without model change tracking | The session can miss the update and leave durable data unchanged | Use the shared mutable value types for in-place edits |
| A migration branch creates more than one head | Deployments can stop on different schema paths | Keep one migration head and run the migration-head check |
| A data migration changes stored values without converting existing rows | Old records can no longer match the current model contract | Make the revision transform existing data before or with the schema change |
| A Redis activity or schema value is not synchronized | Durable activity or displayed schema can remain out of date | Run the relevant refresh or sync path before relying on the value |
| Search expressions or query vectors do not match the current search library | Search can return incomplete or invalid results | Update the database search functions and stored vectors in the migration chain |
Key files
| File | Why to read it |
|---|---|
| redash/…/base.py | Defines the shared database service, search setup, key rules, and generic object references. |
| redash/…/mixins.py | Shows common timestamp and organization-scope behavior. |
| redash/…/organizations.py | Defines the tenant record and organization boundary. |
| redash/…/users.py | Defines users, groups, account state, and activity sync. |
| redash/…/changes.py | Defines version history and field-change records. |
| redash/…/__init__.py | Connects queries, results, dashboards, visualizations, events, and supporting records. |
| redash/…/parameterized_query.py | Shows how saved query parameters use result data and validation before execution. |
| redash/app.py | Shows when database and migration services join application startup. |
| redash/…/database.py | Shows empty-database creation and migration stamping behavior. |
| migrations/env.py | Shows how Alembic receives application metadata and the live database URL. |
| migrations/…/9e8c841d1a30_fix_hash.py | Illustrates a data migration that recomputes stored query identity from current query rules. |
| migrations/…/db0aca1ebd32_12_column_dashboard_layout.py | Illustrates a migration that converts stored dashboard layout values. |
| migrations/…/2f2a8a6f9c21_update_sqlalchemy_searchable_expressions.py | Illustrates a migration that updates database search behavior after a library change. |
| tests/test_migrations.py | States the single-head rule for the migration history. |