From 19d7a5fe53b591db1f81027972275cfbc795c993 Mon Sep 17 00:00:00 2001 From: Ciro Spaciari Date: Fri, 22 Nov 2024 20:23:39 -0300 Subject: [PATCH] fix(CI) make prisma avoid env url because of CI and rely on getSecret (#15352) --- test/js/third_party/pg/pg.test.ts | 8 +-- test/js/third_party/prisma/helper.ts | 12 +++-- test/js/third_party/prisma/prisma.test.ts | 25 ++++++---- .../20240316000224_init/migration.sql | 49 ------------------- .../20240316042522_users/migration.sql | 7 --- .../migration.sql | 16 ------ .../postgres/migrations/migration_lock.toml | 3 -- 7 files changed, 26 insertions(+), 94 deletions(-) delete mode 100644 test/js/third_party/prisma/prisma/postgres/migrations/20240316000224_init/migration.sql delete mode 100644 test/js/third_party/prisma/prisma/postgres/migrations/20240316042522_users/migration.sql delete mode 100644 test/js/third_party/prisma/prisma/postgres/migrations/20240316044713_lowercaseusers/migration.sql delete mode 100644 test/js/third_party/prisma/prisma/postgres/migrations/migration_lock.toml diff --git a/test/js/third_party/pg/pg.test.ts b/test/js/third_party/pg/pg.test.ts index 5b7f03f72c..3d55f4c1ce 100644 --- a/test/js/third_party/pg/pg.test.ts +++ b/test/js/third_party/pg/pg.test.ts @@ -16,7 +16,7 @@ async function insertUsers(client: Client) { // Prepare the query to insert multiple rows const insertQuery = ` - INSERT INTO users (name, email, age) + INSERT INTO pg_users (name, email, age) VALUES ${users.map((_, i) => `($${i * 3 + 1}, $${i * 3 + 2}, $${i * 3 + 3})`).join(", ")}; `; @@ -34,7 +34,7 @@ async function connect() { await client.connect().then(() => { // Define the SQL query to create a table const createTableQuery = ` - CREATE TABLE IF NOT EXISTS users ( + CREATE TABLE IF NOT EXISTS pg_users ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, @@ -47,7 +47,7 @@ async function connect() { return client.query(createTableQuery); }); // check if we need to populate the data - const { rows } = await client.query("SELECT COUNT(*) AS count FROM users"); + const { rows } = await client.query("SELECT COUNT(*) AS count FROM pg_users"); const userCount = Number.parseInt(rows[0].count, 10); if (userCount === 0) await insertUsers(client); return client; @@ -68,7 +68,7 @@ describe.skipIf(!databaseUrl)("pg", () => { test("should execute big query and end connection", async () => { const client = await connect(); - const res = await client.query(`SELECT * FROM users LIMIT 300`); + const res = await client.query(`SELECT * FROM pg_users LIMIT 300`); expect(res.rows.length).toBe(300); await client.end(); }, 20_000); diff --git a/test/js/third_party/prisma/helper.ts b/test/js/third_party/prisma/helper.ts index 2f94da797a..47a3d787c9 100644 --- a/test/js/third_party/prisma/helper.ts +++ b/test/js/third_party/prisma/helper.ts @@ -3,19 +3,19 @@ import { bunEnv, bunExe, isLinux } from "harness"; import path from "path"; const cwd = import.meta.dir; -export async function generateClient(type: string) { - generate(type); +export async function generateClient(type: string, env: Record) { + generate(type, env); // This should run the first time on a fresh db try { - migrate(type); + migrate(type, env); } catch (err: any) { if (err.message.indexOf("Environment variable not found:") !== -1) throw err; } return (await import(`./prisma/${type}/client`)).PrismaClient; } -export function migrate(type: string) { +export function migrate(type: string, env: Record) { const result = Bun.spawnSync( [ bunExe(), @@ -33,13 +33,14 @@ export function migrate(type: string) { env: { ...bunEnv, NODE_ENV: undefined, + ...env, }, }, ); if (!result.success) throw new Error(result.stderr.toString("utf8")); } -export function generate(type: string) { +export function generate(type: string, env: Record) { const schema = path.join(cwd, "prisma", type, "schema.prisma"); const content = fs @@ -60,6 +61,7 @@ export function generate(type: string) { env: { ...bunEnv, NODE_ENV: undefined, + ...env, }, }); if (!result.success) throw new Error(result.stderr.toString("utf8")); diff --git a/test/js/third_party/prisma/prisma.test.ts b/test/js/third_party/prisma/prisma.test.ts index ad88f69515..d470225b7a 100644 --- a/test/js/third_party/prisma/prisma.test.ts +++ b/test/js/third_party/prisma/prisma.test.ts @@ -22,23 +22,28 @@ async function cleanTestId(prisma: PrismaClient, testId: number) { ["sqlite", "postgres" /*"mssql", "mongodb"*/].forEach(async type => { let Client: typeof PrismaClient; - if (!isCI) { - if (type !== "sqlite" && !process.env[`TLS_${type.toUpperCase()}_DATABASE_URL`]) { - throw new Error(`$TLS_${type.toUpperCase()}_DATABASE_URL is not set`); - } - } else if (type !== "sqlite") { - process.env[`TLS_${type.toUpperCase()}_DATABASE_URL`] ||= getSecret(`TLS_${type.toUpperCase()}_DATABASE_URL`); - } + const env_name = `TLS_${type.toUpperCase()}_DATABASE_URL`; + let database_url = type !== "sqlite" ? getSecret(env_name) : null; - Client = await generateClient(type); + Client = await generateClient(type, { + [env_name]: (database_url || "") as string, + }); async function test(label: string, callback: Function, timeout: number = 5000) { - const it = Client ? bunTest : bunTest.skip; + const it = Client && (database_url || type === "sqlite") ? bunTest : bunTest.skip; it( label, async () => { - const prisma = new Client(); + const prisma = database_url + ? new Client({ + datasources: { + db: { + url: database_url as string, + }, + }, + }) + : new Client(); const currentTestId = test_id.next().value; await cleanTestId(prisma, currentTestId); try { diff --git a/test/js/third_party/prisma/prisma/postgres/migrations/20240316000224_init/migration.sql b/test/js/third_party/prisma/prisma/postgres/migrations/20240316000224_init/migration.sql deleted file mode 100644 index 84dadf9681..0000000000 --- a/test/js/third_party/prisma/prisma/postgres/migrations/20240316000224_init/migration.sql +++ /dev/null @@ -1,49 +0,0 @@ --- CreateTable -CREATE TABLE "User" ( - "id" SERIAL NOT NULL, - "testId" INTEGER NOT NULL, - "email" TEXT NOT NULL, - "name" TEXT, - - CONSTRAINT "User_pkey" PRIMARY KEY ("id") -); - --- CreateTable -CREATE TABLE "Post" ( - "id" SERIAL NOT NULL, - "testId" INTEGER NOT NULL, - "title" TEXT NOT NULL, - "content" TEXT, - "published" BOOLEAN NOT NULL DEFAULT false, - "authorId" INTEGER NOT NULL, - "option1" INTEGER, - "option2" INTEGER, - "option3" INTEGER, - "option4" INTEGER, - "option5" INTEGER, - "option6" INTEGER, - "option7" INTEGER, - "option8" INTEGER, - "option9" INTEGER, - "option10" INTEGER, - "option11" INTEGER, - "option12" INTEGER, - "option13" INTEGER, - "option14" INTEGER, - "option15" INTEGER, - "option16" INTEGER, - "option17" INTEGER, - "option18" INTEGER, - "option19" INTEGER, - "option20" INTEGER, - "option21" INTEGER, - "option22" INTEGER, - "option23" INTEGER, - "option24" INTEGER, - "option25" INTEGER, - - CONSTRAINT "Post_pkey" PRIMARY KEY ("id") -); - --- AddForeignKey -ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; diff --git a/test/js/third_party/prisma/prisma/postgres/migrations/20240316042522_users/migration.sql b/test/js/third_party/prisma/prisma/postgres/migrations/20240316042522_users/migration.sql deleted file mode 100644 index 6a65b0b62f..0000000000 --- a/test/js/third_party/prisma/prisma/postgres/migrations/20240316042522_users/migration.sql +++ /dev/null @@ -1,7 +0,0 @@ --- CreateTable -CREATE TABLE "Users" ( - "id" SERIAL NOT NULL, - "alive" BOOLEAN NOT NULL, - - CONSTRAINT "Users_pkey" PRIMARY KEY ("id") -); diff --git a/test/js/third_party/prisma/prisma/postgres/migrations/20240316044713_lowercaseusers/migration.sql b/test/js/third_party/prisma/prisma/postgres/migrations/20240316044713_lowercaseusers/migration.sql deleted file mode 100644 index 62c35d72c6..0000000000 --- a/test/js/third_party/prisma/prisma/postgres/migrations/20240316044713_lowercaseusers/migration.sql +++ /dev/null @@ -1,16 +0,0 @@ -/* - Warnings: - - - You are about to drop the `Users` table. If the table is not empty, all the data it contains will be lost. - -*/ --- DropTable -DROP TABLE "Users"; - --- CreateTable -CREATE TABLE "users" ( - "id" SERIAL NOT NULL, - "alive" BOOLEAN NOT NULL, - - CONSTRAINT "users_pkey" PRIMARY KEY ("id") -); diff --git a/test/js/third_party/prisma/prisma/postgres/migrations/migration_lock.toml b/test/js/third_party/prisma/prisma/postgres/migrations/migration_lock.toml deleted file mode 100644 index fbffa92c2b..0000000000 --- a/test/js/third_party/prisma/prisma/postgres/migrations/migration_lock.toml +++ /dev/null @@ -1,3 +0,0 @@ -# Please do not edit this file manually -# It should be added in your version-control system (i.e. Git) -provider = "postgresql" \ No newline at end of file