fix(CI) make prisma avoid env url because of CI and rely on getSecret (#15352)

This commit is contained in:
Ciro Spaciari
2024-11-22 20:23:39 -03:00
committed by GitHub
parent c04a2d1dfc
commit 19d7a5fe53
7 changed files with 26 additions and 94 deletions

View File

@@ -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);

View File

@@ -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<string, string>) {
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<string, string>) {
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<string, string>) {
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"));

View File

@@ -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 {

View File

@@ -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;

View File

@@ -1,7 +0,0 @@
-- CreateTable
CREATE TABLE "Users" (
"id" SERIAL NOT NULL,
"alive" BOOLEAN NOT NULL,
CONSTRAINT "Users_pkey" PRIMARY KEY ("id")
);

View File

@@ -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")
);

View File

@@ -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"