Add default db schema for seeded workspace (#2061)

* Add default db schema for seeded workspace

* fix

* add if not exists

* add metadata seeds

* use setup-db for e2e

* fix tests
This commit is contained in:
Weiko 2023-10-16 16:59:46 +02:00 committed by GitHub
parent c324a0a4f4
commit 525603227a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 109 deletions

View File

@ -4,16 +4,5 @@
DIR="$(cd "$(dirname "$0")" && pwd)"
source $DIR/set-env-test.sh
npx ts-node ./test/utils/check-db.ts
EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo '🟡 - Database is not initialized. Running migrations...'
npx ts-node ./test/utils/setup-db.ts
npx prisma migrate reset --force && yarn prisma:generate
yarn typeorm:migrate
else
echo "🟢 - Database is already initialized."
fi
yarn database:setup && yarn database:reset
yarn jest --config ./test/jest-e2e.json

View File

@ -23,4 +23,40 @@ export const seedWorkspaces = async (prisma: PrismaClient) => {
logo: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAACXBIWXMAAA7EAAAOxAGVKw4bAAACb0lEQVR4nO2VO4taQRTHr3AblbjxEVlwCwVhg7BoqqCIjy/gAyyFWNlYBOxsfH0KuxgQGwXRUkGuL2S7i1barGAgiwbdW93SnGOc4BonPiKahf3DwXFmuP/fPM4ZlvmlTxAhCBdzHnEQWYiv7Mr4C3NeuVYhQYDPzOUUQgDLBQGcLHNhvQK8DACPx8PTxiqVyvISG43GbyaT6Qfpn06n0m63e/tPAPF4vJ1MJu8kEsnWTCkWi1yr1RKGw+GDRqPBOTfr44vFQvD7/Q/lcpmaaVQAr9fLp1IpO22c47hGOBz+MB6PH+Vy+VYDAL8qlUoGtVotzOfzq4MAgsHgE/6KojiQyWR/bKVSqbSszHFM8Pl8z1YK48JsNltCOBwOnrYLO+8AAIjb+nHbycoTiUQfDJ7tFq4YAHiVSmXBxcD41u8flQU8z7fhzO0r83atVns3Go3u9Xr9x0O/RQXo9/tsIBBg6vX606a52Wz+bZ7P5/WwG29gxSJzhKgA6XTaDoFNF+krFAocmC//4yWEcSf2wTm7mCO19xFgSsKOLI16vV7b7XY7mRNoLwA0JymJ5uQIzgIAuX5PzDElT2m+E8BqtQ4ymcx7Yq7T6a6ZE4sKgOadTucaCwkxp1UzlEKh0GDxIXOwDWHAdi6Xe3swQDQa/Q7mywoolUpvsaptymazDWKxmBHTlWXZm405BFZoNpuGgwEmk4mE2SGtVivii4f1AO7J3ZopkQCQj7Ar1FeRChCJRJzVapX6DKNIfSc1Ax+wtQWQ55h6bH8FWDfYV4fO3wlwDr0C/BcADYiTPCxHqIEA2QsCZAkAKnRGkMbKN/sTX5YHPQ1e7SkAAAAASUVORK5CYII=',
},
});
await prisma.$queryRawUnsafe('CREATE SCHEMA IF NOT EXISTS workspace_twenty');
await prisma.$queryRawUnsafe(
`INSERT INTO metadata.data_source_metadata(
id, schema, type, workspace_id
)
VALUES (
'80f5e1e3-574a-4bf9-b5bc-98aedd2b76e6', 'workspace_twenty', 'postgres', 'twenty-dev-7ed9d212-1c25-4d02-bf25-6aeccf7ea420'
) ON CONFLICT DO NOTHING`,
);
await prisma.$queryRawUnsafe(`
CREATE TABLE IF NOT EXISTS workspace_twenty.tenant_migrations (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
migrations JSONB,
applied_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT NOW()
);
`);
await prisma.$queryRawUnsafe('CREATE SCHEMA IF NOT EXISTS workspace_apple');
await prisma.$queryRawUnsafe(
`INSERT INTO metadata.data_source_metadata(
id, schema, type, workspace_id
)
VALUES (
'b37b2163-7f63-47a9-b1b3-6c7290ca9fb1', 'workspace_apple', 'postgres', 'twenty-7ed9d212-1c25-4d02-bf25-6aeccf7ea419'
) ON CONFLICT DO NOTHING`,
);
await prisma.$queryRawUnsafe(`
CREATE TABLE IF NOT EXISTS workspace_apple.tenant_migrations (
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
migrations JSONB,
applied_at TIMESTAMP NULL,
created_at TIMESTAMP DEFAULT NOW()
);
`);
};

View File

@ -1,41 +0,0 @@
// check-db.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const schemaDatabaseExists = async (databaseName: string) => {
try {
const result = await prisma.$queryRawUnsafe<[any]>(
`SELECT 1 FROM pg_database WHERE datname = '${databaseName}';`,
);
return result.length > 0;
} catch {
return false;
}
};
const main = async () => {
const databaseName = 'tests';
// Check if schema exists
const databaseExistsResult = await schemaDatabaseExists(databaseName);
if (!databaseExistsResult) {
throw new Error(`Schema ${databaseName} does not exist`);
}
// Check if database is initialized
await prisma.$queryRaw`SELECT 1 FROM pg_tables WHERE tablename='_prisma_migrations';`;
};
main()
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});

View File

@ -1,56 +0,0 @@
// check-db.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const createMetadataSchema = async () => {
try {
await prisma.$queryRawUnsafe<[any]>(
`CREATE SCHEMA IF NOT EXISTS "metadata";`,
);
await prisma.$queryRawUnsafe<[any]>(
`GRANT ALL ON SCHEMA metadata TO twenty;`,
);
return true;
} catch {
return false;
}
};
const activateUUIDExtension = async () => {
try {
const result = await prisma.$queryRawUnsafe<[any]>(
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`,
);
return result;
} catch {
return false;
}
};
const main = async () => {
const metadataSchemaCreationSuccess = await createMetadataSchema();
const uuidExtensionActivationSuccess = await activateUUIDExtension();
if (!metadataSchemaCreationSuccess) {
throw new Error(`Failed to create metadata schema`);
}
if (!uuidExtensionActivationSuccess) {
throw new Error(`Failed to activate uuid extension`);
}
};
main()
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});