Skip to content

Commit

Permalink
logging in prisma/migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
Natoandro committed May 10, 2024
1 parent a27ce48 commit 415e847
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
25 changes: 24 additions & 1 deletion typegate/src/runtimes/prisma/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import * as native from "native";
import { nativeResult } from "../../utils.ts";
import { makeDatasource } from "./prisma.ts";
import type { PrismaRT } from "./mod.ts";
import { getLogger } from "@typegate/log.ts";

const logger = getLogger(import.meta);

type PrismaRuntimeDS = PrismaRT.DS<PrismaRT.DataWithDatamodel>;

Expand Down Expand Up @@ -60,6 +63,7 @@ export class PrismaMigrate {

apply: Resolver<{ resetDatabase: boolean }> = async ({ resetDatabase }) => {
const { datamodel } = this.runtime.data;
logger.info("prisma apply");
const res = await native.prisma_apply({
datasource: this.datasource,
datamodel,
Expand All @@ -68,15 +72,21 @@ export class PrismaMigrate {
});

if ("Err" in res) {
logger.error(`prisma apply error: ${res.Err.message}`);
throw new Error(res.Err.message);
}

if ("ResetRequired" in res) {
logger.error(
`database reset required: ${res.ResetRequired.reset_reason}`,
);
throw new Error(
`database reset required: ${res.ResetRequired.reset_reason}`,
);
}

logger.info("prisma apply: successful");

const { reset_reason, applied_migrations } = res.Ok;

return {
Expand Down Expand Up @@ -108,11 +118,14 @@ export class PrismaMigrate {
};

reset: Resolver<boolean> = async () => {
logger.info("prisma reset");
nativeResult(
await native.prisma_reset({
datasource: this.datasource,
}),
);
logger.info("prisma reset: successful");

return true;
};
}
Expand Down Expand Up @@ -193,9 +206,17 @@ export class PrismaMigrationRuntime extends Runtime {
),
);

logger.info("prisma diff");
const diff = await native.prisma_diff({
datasource,
datamodel,
script,
});
logger.info("prisma diff: successful");

return {
runtimeName: name,
diff: await native.prisma_diff({ datasource, datamodel, script }),
diff,
};
}) as Resolver;
break;
Expand All @@ -215,13 +236,15 @@ export class PrismaMigrationRuntime extends Runtime {
),
);

logger.info("prisma deploy");
const res = nativeResult(
await native.prisma_deploy({
datasource,
datamodel,
migrations,
}),
);
logger.info("prisma deploy: successful");

return {
migrationCount: res.migration_count,
Expand Down
4 changes: 4 additions & 0 deletions typegate/src/runtimes/prisma/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,25 @@ export class PrismaRuntime extends Runtime {
args.name,
datamodel,
);
logger.info(`registering prisma engine '${instance.name}': ${instance.id}`);
nativeVoid(
await native.prisma_register_engine({
engine_name: instance.id,
datamodel,
}),
);
logger.info(`prisma engine '${instance.name}' registered`);
return instance;
}

async deinit(): Promise<void> {
logger.info(`unregistering prisma engine '${this.name}': ${this.id}`);
nativeVoid(
await native.prisma_unregister_engine({
engine_name: this.id,
}),
);
logger.info(`prisma engine '${this.name}' unregistered`);
}

async query(query: SingleQuery | BatchQuery) {
Expand Down

0 comments on commit 415e847

Please sign in to comment.