| 7 | import { RefreshToken } from '../../api/src/auth/entities/refresh-token.entity'; |
| 8 | |
| 9 | export class DatabaseUtils { |
| 10 | private static dataSource: DataSource; |
| 11 | |
| 12 | static async initializeDatabase(): Promise<DataSource> { |
| 13 | if (this.dataSource?.isInitialized) { |
| 14 | return this.dataSource; |
| 15 | } |
| 16 | |
| 17 | // Simple database configuration for testing |
| 18 | this.dataSource = new DataSource({ |
| 19 | type: 'postgres', |
| 20 | host: process.env.DATABASE_HOST || 'localhost', |
| 21 | port: parseInt(process.env.DATABASE_PORT || '5432'), |
| 22 | username: process.env.DATABASE_USERNAME || 'postgres', |
| 23 | password: process.env.DATABASE_PASSWORD || 'postgres', |
| 24 | database: process.env.DATABASE_NAME || 'datakit_e2e', |
| 25 | entities: [User, Workspace, WorkspaceMember, Subscription, CreditUsage, RefreshToken], |
| 26 | synchronize: true, // Auto-create schema for tests |
| 27 | dropSchema: true, // Fresh schema each run |
| 28 | logging: false, |
| 29 | }); |
| 30 | |
| 31 | try { |
| 32 | await this.dataSource.initialize(); |
| 33 | console.log('Test database initialized successfully'); |
| 34 | return this.dataSource; |
| 35 | } catch (error) { |
| 36 | console.error('Failed to initialize test database:', error); |
| 37 | throw error; |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | static async cleanDatabase(): Promise<void> { |
| 42 | if (!this.dataSource?.isInitialized) { |
| 43 | return; |
| 44 | } |
| 45 | |
| 46 | try { |
| 47 | // Simple approach: truncate all tables (PostgreSQL syntax) |
| 48 | const entities = this.dataSource.entityMetadatas; |
| 49 | |
| 50 | for (const entity of entities) { |
| 51 | await this.dataSource.query(`TRUNCATE TABLE "${entity.tableName}" RESTART IDENTITY CASCADE`); |
| 52 | } |
| 53 | |
| 54 | } catch (error) { |
| 55 | console.error('Failed to clean database:', error); |
| 56 | throw error; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static async closeDatabase(): Promise<void> { |
| 61 | if (this.dataSource?.isInitialized) { |
| 62 | await this.dataSource.destroy(); |
| 63 | this.dataSource = null; |
| 64 | } |
| 65 | } |
| 66 | } |
nothing calls this directly
no outgoing calls
no test coverage detected