|
1 | 1 | import { createTestProject, getTestDbProvider } from '@zenstackhq/testtools'; |
2 | | -import { createHash } from 'node:crypto'; |
3 | 2 | import { execSync } from 'node:child_process'; |
4 | 3 | import fs from 'node:fs'; |
5 | 4 | import path from 'node:path'; |
6 | 5 | import { expect } from 'vitest'; |
| 6 | +import { Client as PostgresClient } from 'pg'; |
| 7 | +import { createHash } from 'node:crypto'; |
| 8 | + |
| 9 | +const ZMODEL_PRELUDE = `datasource db { |
| 10 | + provider = "sqlite" |
| 11 | + url = "file:./dev.db" |
| 12 | +} |
| 13 | +`; |
| 14 | +export function createProject(zmodel: string, addPrelude = true) { |
| 15 | + const workDir = createTestProject(); |
| 16 | + fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true }); |
| 17 | + const schemaPath = path.join(workDir, 'zenstack/schema.zmodel'); |
| 18 | + fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n\n${zmodel}` : zmodel); |
| 19 | + return workDir; |
| 20 | +} |
7 | 21 |
|
8 | 22 | const TEST_PG_CONFIG = { |
9 | 23 | host: process.env['TEST_PG_HOST'] ?? 'localhost', |
@@ -34,24 +48,26 @@ function getTestDbName(provider: string) { |
34 | 48 | ); |
35 | 49 | } |
36 | 50 |
|
37 | | -export function createProject(zmodel: string, addPrelude = true) { |
| 51 | +export async function createTestEnvironment(zmodel: string, addPrelude = true) { |
38 | 52 | const provider = getTestDbProvider() ?? 'sqlite'; |
39 | 53 | const dbName = getTestDbName(provider); |
40 | 54 | const dbUrl = |
41 | 55 | provider === 'sqlite' |
42 | 56 | ? `file:${dbName}` |
43 | 57 | : `postgres://${TEST_PG_CONFIG.user}:${TEST_PG_CONFIG.password}@${TEST_PG_CONFIG.host}:${TEST_PG_CONFIG.port}/${dbName}`; |
44 | 58 |
|
| 59 | + if (provider === 'postgresql') { |
| 60 | + const pg = new PostgresClient({ connectionString: dbUrl }); |
| 61 | + await pg.connect(); |
| 62 | + await pg.query(`CREATE DATABASE ${dbName};`); |
| 63 | + await pg.end(); |
| 64 | + } |
45 | 65 | const ZMODEL_PRELUDE = `datasource db { |
46 | 66 | provider = "${provider}" |
47 | 67 | url = "${dbUrl}" |
48 | | -} |
49 | | -`; |
50 | | - const workDir = createTestProject(); |
51 | | - fs.mkdirSync(path.join(workDir, 'zenstack'), { recursive: true }); |
52 | | - const schemaPath = path.join(workDir, 'zenstack/schema.zmodel'); |
53 | | - fs.writeFileSync(schemaPath, addPrelude ? `${ZMODEL_PRELUDE}\n${zmodel}` : zmodel); |
54 | | - return workDir; |
| 68 | +}`; |
| 69 | + |
| 70 | + return createProject(addPrelude ? `${ZMODEL_PRELUDE}\n${zmodel}` : zmodel, false); |
55 | 71 | } |
56 | 72 |
|
57 | 73 | export function runCli(command: string, cwd: string) { |
|
0 commit comments