Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 100 additions & 0 deletions MIGRATIONS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# TypeORM Migrations Setup

This document explains how to use TypeORM migrations in the TradeFlow API project.

## Overview

Migrations provide a way to safely manage database schema changes without losing data. This replaces the dangerous `synchronize: true` option which should never be used in production.

## Files Created

- `src/datasource.ts` - TypeORM configuration for CLI commands
- `src/entities/user.entity.ts` - User entity definition
- `src/entities/invoice.entity.ts` - Invoice entity definition
- `src/migrations/` - Directory containing migration files
- `src/migration-runner.ts` - Manual migration runner for testing

## Available Scripts

```bash
# Generate a new migration
npm run migration:generate -- MigrationName

# Run all pending migrations
npm run migration:run

# Revert the last migration
npm run migration:revert

# Run migrations manually (for testing)
npm run migration:manual
```

## Initial Migration

The initial migration (`1708665600000-InitialMigration.ts`) creates:

### Users Table
- `id` (UUID, primary key)
- `publicKey` (varchar, unique)
- `email` (varchar, nullable)
- `isActive` (boolean, default: true)
- `createdAt` (timestamp)
- `updatedAt` (timestamp)

### Invoices Table
- `id` (int, primary key, auto-increment)
- `amount` (decimal 10,2)
- `date` (timestamp)
- `customer` (varchar)
- `description` (text, nullable)
- `riskScore` (int, default: 0)
- `status` (varchar, enum: Pending/Approved/High Risk/Rejected)
- `processedAt` (timestamp, default: CURRENT_TIMESTAMP)
- `userId` (uuid, foreign key to users, nullable)
- `createdAt` (timestamp)
- `updatedAt` (timestamp)

### Relationships
- Foreign key from `invoices.userId` to `users.id`
- Index on `invoices.status` for performance

## Migration Process

1. **Make entity changes** - Modify entity files in `src/entities/`
2. **Generate migration** - Run `npm run migration:generate -- DescriptionOfChanges`
3. **Review migration** - Check the generated migration file
4. **Run migration** - Execute `npm run migration:run`
5. **Verify** - Check that tables are created/updated correctly

## Schema Migrations Table

TypeORM automatically creates a `migrations` table to track which migrations have been applied. This prevents re-running the same migration and ensures proper ordering.

## Production Safety

- `synchronize: false` is set in both `datasource.ts` and `database.providers.ts`
- All schema changes must go through migrations
- Migrations are reversible with `up()` and `down()` methods
- Foreign key constraints maintain data integrity

## Environment Variables

Configure database connection using these environment variables:

```bash
DB_HOST=localhost
DB_PORT=5432
DB_USERNAME=postgres
DB_PASSWORD=password
DB_DATABASE=tradeflow
```

## Docker Setup

When using Docker Compose, the database will be available at `localhost:5432` and migrations can be run after the container starts:

```bash
docker-compose up -d db
npm run migration:run
```
7 changes: 5 additions & 2 deletions dist/database.providers.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/database.providers.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions dist/datasource.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { DataSource } from 'typeorm';
export declare const AppDataSource: DataSource;
16 changes: 16 additions & 0 deletions dist/datasource.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/datasource.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions dist/entities/invoice.entity.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { User } from './user.entity';
export declare class Invoice {
id: number;
amount: number;
date: Date;
customer: string;
description?: string;
riskScore: number;
status: string;
processedAt: Date;
user?: User;
userId?: string;
createdAt: Date;
updatedAt: Date;
}
74 changes: 74 additions & 0 deletions dist/entities/invoice.entity.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/entities/invoice.entity.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions dist/entities/user.entity.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export declare class User {
id: string;
publicKey: string;
email?: string;
isActive: boolean;
createdAt: Date;
updatedAt: Date;
}
44 changes: 44 additions & 0 deletions dist/entities/user.entity.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/entities/user.entity.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/migration-runner.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
28 changes: 28 additions & 0 deletions dist/migration-runner.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/migration-runner.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dist/migrations/1708665600000-InitialMigration.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export declare class InitialMigration1708665600000 implements MigrationInterface {
name: string;
up(queryRunner: QueryRunner): Promise<void>;
down(queryRunner: QueryRunner): Promise<void>;
}
Loading
Loading