Skip to content

Commit

Permalink
feat(prisma/migrations): Default value on new column (#465)
Browse files Browse the repository at this point in the history
- [x] Display a more concise error message for new column that failed
the NON NULL constraint during the migration.
- [x] Enable user to set default value on new NON NULL column.

---------

Signed-off-by: Natoandro <[email protected]>
Signed-off-by: Teo Stocco <[email protected]>
Co-authored-by: Teo Stocco <[email protected]>
  • Loading branch information
Natoandro and zifeo authored Oct 28, 2023
1 parent 3bf54cd commit 0702818
Show file tree
Hide file tree
Showing 24 changed files with 427 additions and 42 deletions.
1 change: 1 addition & 0 deletions libs/common/src/typegraph/runtimes/prisma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub struct ScalarProperty {
pub injection: Option<ManagedInjection>,
pub unique: bool,
pub auto: bool,
pub default_value: Option<serde_json::Value>,
}

#[cfg_attr(feature = "codegen", derive(JsonSchema))]
Expand Down
5 changes: 3 additions & 2 deletions typegate/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"comment1": "echo cwd is by default the directory of deno.json",
"comment2": "echo cannot restrict ffi to a lib https://github.com/denoland/deno/issues/15511",
"run": "cd .. && deno run --config=typegate/deno.json --unstable --allow-run=hostname --allow-sys --allow-env --allow-hrtime --allow-write=tmp --allow-ffi --allow-read=. --allow-net typegate/src/main.ts",
"test": "cd .. && deno test --trace-ops --config=typegate/deno.json --unstable --allow-run=cargo,hostname,target/debug/meta,git,python3,rm,mkdir --allow-sys --allow-env --allow-hrtime --allow-write=tmp,typegate/tests --allow-ffi --allow-read=. --allow-net"
"test": "cd .. && deno test --trace-ops --config=typegate/deno.json --unstable --allow-run=cargo,hostname,target/debug/meta,git,python3,rm,mkdir,bash --allow-sys --allow-env --allow-hrtime --allow-write=tmp,typegate/tests --allow-ffi --allow-read=. --allow-net"
},
"nodeModulesDir": false,
"lock": "deno.lock",
Expand Down Expand Up @@ -36,6 +36,7 @@
"outdent": "https://deno.land/x/[email protected]/mod.ts",
"json-schema-faker": "npm:[email protected]",
"ajv": "https://esm.sh/[email protected]?pin=v131",
"@typegraph/sdk/": "../typegraph/deno/src/"
"@typegraph/sdk/": "../typegraph/deno/src/",
"test-utils/": "./tests/utils/"
}
}
87 changes: 87 additions & 0 deletions typegate/deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions typegate/src/runtimes/prisma/hooks/generate_schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ class FieldBuilder {
}
}

if (prop.defaultValue != null) {
tags.push(`@default(${JSON.stringify(prop.defaultValue)})`);
}

const field = new ModelField(prop.key, typeName + quant, tags);
return field;
}
Expand Down
26 changes: 24 additions & 2 deletions typegate/src/runtimes/prisma/hooks/run_migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import { PrismaRT } from "../mod.ts";
import * as native from "native";
import { nativeResult, pluralSuffix } from "../../../utils.ts";

const NULL_CONSTRAINT_ERROR_REGEX =
/column (?<col>".+") of relation (?<table>".+") contains null values/;

export const runMigrations: PushHandler = async (
typegraph,
secretManager,
Expand Down Expand Up @@ -51,7 +54,7 @@ export const runMigrations: PushHandler = async (

const { reset_reason, applied_migrations } = applyRes.Ok;
if (reset_reason != null) {
response.info(`Database reset: {reset_reason}`);
response.info(`Database reset: ${reset_reason}`);
}
if (applied_migrations.length === 0) {
response.info(`${prefix} No migration applied.`);
Expand Down Expand Up @@ -90,7 +93,26 @@ export const runMigrations: PushHandler = async (
);

if (apply_err != null) {
response.error(apply_err);
const errors = apply_err.split(/\r?\n/).filter(
(line) => line.startsWith("ERROR: "),
)
.map((line) => line.slice("ERROR: ".length))
.map((err) => {
const match = NULL_CONSTRAINT_ERROR_REGEX.exec(err);
if (match != null) {
// TODO detect used for the typegraph language and write
// the message accordingly.
return `${err}: set a default value: add \`config={ "default": defaultValue }\` attribute to the type.`;
}
return err;
});

if (errors.length === 0) {
response.error(apply_err);
} else {
const formattedErrors = errors.map((err) => `\n- ${err}`).join("");
response.error(`Could not apply migration: ${formattedErrors}`);
}
}

if (created_migration_name != null) {
Expand Down
1 change: 1 addition & 0 deletions typegate/src/typegraph/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ export type Property = {
injection?: ManagedInjection | null;
unique: boolean;
auto: boolean;
defaultValue?: any;
} | {
type: "relationship";
key: string;
Expand Down
1 change: 1 addition & 0 deletions typegate/tests/e2e/cli/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/migration.py
Loading

0 comments on commit 0702818

Please sign in to comment.