Skip to content

Commit 39440a2

Browse files
authored
Merge pull request #21 from onozaty/20-bug-multi-file-prisma-schema-configuration-fails-to-correctly-locate-migrations-folder
Fix: Correctly locate migrations folder for multi-file Prisma schema
2 parents a9e8bc7 + 4928584 commit 39440a2

File tree

6 files changed

+151
-5
lines changed

6 files changed

+151
-5
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Blog Post
2+
model Post {
3+
/// Post ID
4+
postId Int @id @default(autoincrement()) @map("post_id")
5+
/// Post Title
6+
title String
7+
/// Post Content
8+
content String?
9+
author User @relation(fields: [authorId], references: [userId])
10+
/// Author ID
11+
authorId Int @map("author_id")
12+
createdAt DateTime @default(now()) @map("created_at")
13+
14+
@@map("posts")
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
generator client {
2+
provider = "prisma-client-js"
3+
}
4+
5+
generator comments {
6+
provider = "node ./dist/generator.cjs"
7+
}
8+
9+
datasource db {
10+
provider = "postgresql"
11+
url = env("DATABASE_URL")
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// User
2+
model User {
3+
/// User ID
4+
userId Int @id @default(autoincrement()) @map("user_id")
5+
/// User Name
6+
name String
7+
/// Email address
8+
email String @unique
9+
createdAt DateTime @default(now()) @map("created_at")
10+
posts Post[]
11+
12+
@@map("users")
13+
}

src/__snapshots__/generator.test.ts.snap

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,89 @@ COMMENT ON COLUMN "shops"."created_at" IS 'Created At';
256256
"
257257
`;
258258

259+
exports[`multi-file-schema > comments-latest.json 1`] = `
260+
"{
261+
"posts": {
262+
"table": {
263+
"tableName": "posts",
264+
"comment": "Blog Post"
265+
},
266+
"columns": [
267+
{
268+
"tableName": "posts",
269+
"columnName": "post_id",
270+
"comment": "Post ID"
271+
},
272+
{
273+
"tableName": "posts",
274+
"columnName": "title",
275+
"comment": "Post Title"
276+
},
277+
{
278+
"tableName": "posts",
279+
"columnName": "content",
280+
"comment": "Post Content"
281+
},
282+
{
283+
"tableName": "posts",
284+
"columnName": "author_id",
285+
"comment": "Author ID"
286+
},
287+
{
288+
"tableName": "posts",
289+
"columnName": "created_at",
290+
"comment": ""
291+
}
292+
]
293+
},
294+
"users": {
295+
"table": {
296+
"tableName": "users",
297+
"comment": "User"
298+
},
299+
"columns": [
300+
{
301+
"tableName": "users",
302+
"columnName": "user_id",
303+
"comment": "User ID"
304+
},
305+
{
306+
"tableName": "users",
307+
"columnName": "name",
308+
"comment": "User Name"
309+
},
310+
{
311+
"tableName": "users",
312+
"columnName": "email",
313+
"comment": "Email address"
314+
},
315+
{
316+
"tableName": "users",
317+
"columnName": "created_at",
318+
"comment": ""
319+
}
320+
]
321+
}
322+
}"
323+
`;
324+
325+
exports[`multi-file-schema > migration.sql 1`] = `
326+
"
327+
-- posts comments
328+
COMMENT ON TABLE "posts" IS 'Blog Post';
329+
COMMENT ON COLUMN "posts"."post_id" IS 'Post ID';
330+
COMMENT ON COLUMN "posts"."title" IS 'Post Title';
331+
COMMENT ON COLUMN "posts"."content" IS 'Post Content';
332+
COMMENT ON COLUMN "posts"."author_id" IS 'Author ID';
333+
334+
-- users comments
335+
COMMENT ON TABLE "users" IS 'User';
336+
COMMENT ON COLUMN "users"."user_id" IS 'User ID';
337+
COMMENT ON COLUMN "users"."name" IS 'User Name';
338+
COMMENT ON COLUMN "users"."email" IS 'Email address';
339+
"
340+
`;
341+
259342
exports[`multi-schema > comments-latest.json 1`] = `
260343
"{
261344
"foo.registered_user": {

src/generator.test.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,27 @@ test("mysql", async () => {
111111
expect(commentsLatestJsonContent).toMatchSnapshot("comments-latest.json");
112112
});
113113

114+
test("multi-file-schema", async () => {
115+
// Arrange
116+
const name = "multi-file-schema";
117+
118+
// Act
119+
executeGenerate(name);
120+
121+
// Assert
122+
const migrationSqlContent = readMigrationSql(name);
123+
expect(migrationSqlContent).toMatchSnapshot("migration.sql");
124+
125+
const commentsLatestJsonContent = readCommentsLatestJson(name);
126+
expect(commentsLatestJsonContent).toMatchSnapshot("comments-latest.json");
127+
});
128+
114129
const executeGenerate = (name: string) => {
115-
const schemaPath = path.join(fixturesDir, name, "schema.prisma");
130+
// For multi-file-schema, use the schema folder instead of schema.prisma file
131+
const schemaPath =
132+
name === "multi-file-schema"
133+
? path.join(fixturesDir, name, "schema")
134+
: path.join(fixturesDir, name, "schema.prisma");
116135
child_process.execSync(`npx prisma generate --schema ${schemaPath}`);
117136
};
118137

@@ -155,5 +174,9 @@ const readMigrationSql = (name: string): string => {
155174
};
156175

157176
const getMigrationsDir = (name: string) => {
177+
// For multi-file-schema, migrations are in the schema subfolder
178+
if (name === "multi-file-schema") {
179+
return path.join(fixturesDir, name, "schema", "migrations");
180+
}
158181
return path.join(fixturesDir, name, "migrations");
159182
};

src/generator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { parse } from "./parser";
1717
import { generateCommentStatements } from "./statement";
1818

1919
const generate = async (options: GeneratorOptions) => {
20-
const { dmmf, schemaPath } = options;
20+
const { dmmf } = options;
2121
const config = readConfig(options);
2222

2323
fs.mkdirSync(config.outputDir, { recursive: true });
@@ -47,7 +47,7 @@ const generate = async (options: GeneratorOptions) => {
4747
}
4848

4949
const migrationDirName = await outputMigrationFile(
50-
path.dirname(schemaPath),
50+
config.outputDir,
5151
commentStatements,
5252
);
5353

@@ -62,7 +62,7 @@ const generate = async (options: GeneratorOptions) => {
6262
};
6363

6464
const outputMigrationFile = async (
65-
baseDirPath: string,
65+
migrationsDir: string,
6666
commentStatements: string[],
6767
) => {
6868
const date = new Date();
@@ -74,7 +74,7 @@ const outputMigrationFile = async (
7474
.replace(".000", "");
7575
const dirName = `${dateStr}_update_comments`;
7676

77-
const migrationDir = path.join(baseDirPath, "migrations", dirName);
77+
const migrationDir = path.join(migrationsDir, dirName);
7878
fs.mkdirSync(migrationDir, { recursive: true });
7979
fs.writeFileSync(
8080
path.join(migrationDir, "migration.sql"),

0 commit comments

Comments
 (0)