Database Schema and Migrations
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- packages/config/package.json
- packages/core/src/lib/database/migration-executor.ts
- packages/core/src/lib/database/migrations/1726509769379-AddColumnsToOrganizationProjectEmployeeEntity.ts
- packages/mcp-server/src/lib/schema.ts
- packages/plugins/job-proposal/package.json
- packages/plugins/knowledge-base/package.json
The Gauzy platform utilizes a flexible and robust database architecture designed to support multiple database systems and ensure schema integrity across application updates. This is primarily achieved through the use of the TypeORM library, which provides powerful Object-Relational Mapping (ORM) capabilities. The system includes a comprehensive migration mechanism to manage database schema evolution in a controlled and versioned manner.
This document provides an overview of the database technology stack, schema definition approach, and the migration process. It details how migrations are created, generated, and executed, ensuring that developers can maintain and evolve the database schema consistently across different environments and database backends.
Technology Stack
Gauzy is designed to be database-agnostic, leveraging ORM libraries to support various database systems. The core of this is TypeORM, with mentions of MikroORM and Knex, suggesting a flexible data access layer.
Sources: README.md, packages/config/package.json
| Technology | Role | Recommended For |
|---|---|---|
| TypeORM | Primary ORM for data modeling and migrations. | All environments |
| PostgreSQL | Relational Database | Production |
| MySQL / MariaDB | Relational Database | Production / Development |
| SQLite | Relational Database | Default for demo/testing |
| CockroachDB / MS SQL / Oracle | Relational Database | Supported with minimal changes |
This multi-database support is a key architectural feature, allowing deployment in diverse environments. The default for quick starts and demos is SQLite, while PostgreSQL is recommended for production workloads.
Sources: README.md, packages/core/src/lib/database/migration-executor.ts:200-203
Schema Definition
While the definitive database schema is defined by TypeORM entities (not present in the provided files), the @gauzy/mcp-server package contains a comprehensive set of Zod schemas that mirror the structure of the core database entities. These schemas are used for data validation and type safety within the Model Context Protocol (MCP) server. They provide a clear view of the application's data model.
Sources: packages/mcp-server/src/lib/schema.ts
Core Entities and Relationships
The diagram below illustrates the relationships between some of the central entities in the Gauzy platform, as inferred from the Zod schemas.
This Entity-Relationship diagram shows the core entities like ORGANIZATION, EMPLOYEE, PROJECT, TASK, and TIME_LOG and their connections.
Sources: packages/mcp-server/src/lib/schema.ts
Example Schema: Task
The TaskSchema defines the structure for tasks within the system, including fields for status, priority, and relationships to other entities like projects and employees.
| Field | Type | Description |
|---|---|---|
title | string | The title of the task. |
description | string (optional) | A detailed description of the task. |
status | TaskStatusEnum (optional) | The current status (e.g., 'OPEN', 'IN_PROGRESS'). |
priority | TaskPriorityEnum (optional) | The priority level (e.g., 'LOW', 'HIGH'). |
dueDate | date (optional) | The date the task is due. |
projectId | uuid (optional) | Foreign key to the Project entity. |
members | EmployeeSchema[] (optional) | Array of employees assigned to the task. |
Sources: packages/mcp-server/src/lib/schema.ts:590-625
Database Migrations
Database migrations are managed through a set of scripts and a dedicated executor, ensuring that schema changes are applied consistently and safely. The system is built on TypeORM's migration capabilities.
Sources: packages/core/src/lib/database/migration-executor.ts
Migration Executor
The migration-executor.ts file provides a command-line interface for managing the database migration lifecycle. It exposes several key functions for developers.
| Function | Description |
|---|---|
runDatabaseMigrations | Executes all pending migrations against the database. |
revertLastDatabaseMigration | Reverts the most recently applied migration. |
generateMigration | Generates a new migration file containing SQL statements based on changes detected in TypeORM entities. |
createMigration | Creates a new, empty migration file for manual editing. |
Sources: packages/core/src/lib/database/migration-executor.ts
The following diagram illustrates the flow for running database migrations.
This sequence diagram shows the process of running migrations, from the initial CLI command to the final database connection shutdown.
Sources: packages/core/src/lib/database/migration-executor.ts:21-41
Migration File Structure
Each migration is a TypeScript class that implements the MigrationInterface from TypeORM. A key feature of Gauzy's migration architecture is its built-in support for multiple database systems within a single migration file.
A migration file contains:
- An
up(queryRunner: QueryRunner)method to apply changes. - A
down(queryRunner: QueryRunner)method to revert changes. - A
switchstatement insideupanddownto execute database-specific SQL queries (PostgreSQL, SQLite, MySQL).
The following diagram shows the logic inside a typical up migration method.
This flowchart shows how a single migration file handles different database types by routing to a specific implementation.
Example Migration: AddColumnsToOrganizationProjectEmployeeEntity
This migration adds several columns to the organization_project_employee join table to enhance its functionality, such as adding a UUID primary key, tenant/organization context, and timestamps.
The postgresUpQueryRunner method demonstrates a multi-step process:
- Drop existing foreign key constraints.
- Add new columns (
id,deletedAt,tenantId,organizationId, etc.). - Modify the primary key.
- Populate the new
tenantIdandorganizationIdcolumns for existing rows. - Re-establish primary key and foreign key constraints.
Here is a snippet from the migration's up method, showcasing the multi-database support:
// packages/core/src/lib/database/migrations/1726509769379-AddColumnsToOrganizationProjectEmployeeEntity.ts
export class AddColumnsToOrganizationProjectEmployeeEntity1726509769379 implements MigrationInterface {
// ...
public async up(queryRunner: QueryRunner): Promise<void> {
console.log(chalk.yellow(this.name + ' start running!'));
switch (queryRunner.connection.options.type) {
case DatabaseTypeEnum.sqlite:
case DatabaseTypeEnum.betterSqlite3:
await this.sqliteUpQueryRunner(queryRunner);
break;
case DatabaseTypeEnum.postgres:
await this.postgresUpQueryRunner(queryRunner);
break;
case DatabaseTypeEnum.mysql:
await this.mysqlUpQueryRunner(queryRunner);
break;
default:
throw Error(`Unsupported database: ${queryRunner.connection.options.type}`);
}
}
// ...
}
Summary
The Gauzy platform's database layer is built for flexibility and scalability. By using TypeORM, it supports a wide range of SQL databases, with a clear preference for PostgreSQL in production. The schema is managed through a robust migration system that allows for version-controlled, multi-database-aware updates. The migration-executor provides developers with the necessary tools to create, generate, run, and revert migrations, ensuring that the database schema can evolve safely alongside the application's features. This structured approach is critical for maintaining data integrity and simplifying the development and deployment process.
Sources: README.md, packages/core/src/lib/database/migration-executor.ts