Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/__fixtures__/multi-file-schema/schema/post.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/// Blog Post
model Post {
/// Post ID
postId Int @id @default(autoincrement()) @map("post_id")
/// Post Title
title String
/// Post Content
content String?
author User @relation(fields: [authorId], references: [userId])
/// Author ID
authorId Int @map("author_id")
createdAt DateTime @default(now()) @map("created_at")

@@map("posts")
}
12 changes: 12 additions & 0 deletions src/__fixtures__/multi-file-schema/schema/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
generator client {
provider = "prisma-client-js"
}

generator comments {
provider = "node ./dist/generator.cjs"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
13 changes: 13 additions & 0 deletions src/__fixtures__/multi-file-schema/schema/user.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/// User
model User {
/// User ID
userId Int @id @default(autoincrement()) @map("user_id")
/// User Name
name String
/// Email address
email String @unique
createdAt DateTime @default(now()) @map("created_at")
posts Post[]

@@map("users")
}
83 changes: 83 additions & 0 deletions src/__snapshots__/generator.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,89 @@ COMMENT ON COLUMN "shops"."created_at" IS 'Created At';
"
`;

exports[`multi-file-schema > comments-latest.json 1`] = `
"{
"posts": {
"table": {
"tableName": "posts",
"comment": "Blog Post"
},
"columns": [
{
"tableName": "posts",
"columnName": "post_id",
"comment": "Post ID"
},
{
"tableName": "posts",
"columnName": "title",
"comment": "Post Title"
},
{
"tableName": "posts",
"columnName": "content",
"comment": "Post Content"
},
{
"tableName": "posts",
"columnName": "author_id",
"comment": "Author ID"
},
{
"tableName": "posts",
"columnName": "created_at",
"comment": ""
}
]
},
"users": {
"table": {
"tableName": "users",
"comment": "User"
},
"columns": [
{
"tableName": "users",
"columnName": "user_id",
"comment": "User ID"
},
{
"tableName": "users",
"columnName": "name",
"comment": "User Name"
},
{
"tableName": "users",
"columnName": "email",
"comment": "Email address"
},
{
"tableName": "users",
"columnName": "created_at",
"comment": ""
}
]
}
}"
`;

exports[`multi-file-schema > migration.sql 1`] = `
"
-- posts comments
COMMENT ON TABLE "posts" IS 'Blog Post';
COMMENT ON COLUMN "posts"."post_id" IS 'Post ID';
COMMENT ON COLUMN "posts"."title" IS 'Post Title';
COMMENT ON COLUMN "posts"."content" IS 'Post Content';
COMMENT ON COLUMN "posts"."author_id" IS 'Author ID';

-- users comments
COMMENT ON TABLE "users" IS 'User';
COMMENT ON COLUMN "users"."user_id" IS 'User ID';
COMMENT ON COLUMN "users"."name" IS 'User Name';
COMMENT ON COLUMN "users"."email" IS 'Email address';
"
`;

exports[`multi-schema > comments-latest.json 1`] = `
"{
"foo.registered_user": {
Expand Down
25 changes: 24 additions & 1 deletion src/generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,27 @@ test("mysql", async () => {
expect(commentsLatestJsonContent).toMatchSnapshot("comments-latest.json");
});

test("multi-file-schema", async () => {
// Arrange
const name = "multi-file-schema";

// Act
executeGenerate(name);

// Assert
const migrationSqlContent = readMigrationSql(name);
expect(migrationSqlContent).toMatchSnapshot("migration.sql");

const commentsLatestJsonContent = readCommentsLatestJson(name);
expect(commentsLatestJsonContent).toMatchSnapshot("comments-latest.json");
});

const executeGenerate = (name: string) => {
const schemaPath = path.join(fixturesDir, name, "schema.prisma");
// For multi-file-schema, use the schema folder instead of schema.prisma file
const schemaPath =
name === "multi-file-schema"
? path.join(fixturesDir, name, "schema")
: path.join(fixturesDir, name, "schema.prisma");
child_process.execSync(`npx prisma generate --schema ${schemaPath}`);
};

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

const getMigrationsDir = (name: string) => {
// For multi-file-schema, migrations are in the schema subfolder
if (name === "multi-file-schema") {
return path.join(fixturesDir, name, "schema", "migrations");
}
return path.join(fixturesDir, name, "migrations");
};
8 changes: 4 additions & 4 deletions src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { parse } from "./parser";
import { generateCommentStatements } from "./statement";

const generate = async (options: GeneratorOptions) => {
const { dmmf, schemaPath } = options;
const { dmmf } = options;
const config = readConfig(options);

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

const migrationDirName = await outputMigrationFile(
path.dirname(schemaPath),
config.outputDir,
commentStatements,
);

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

const outputMigrationFile = async (
baseDirPath: string,
migrationsDir: string,
commentStatements: string[],
) => {
const date = new Date();
Expand All @@ -74,7 +74,7 @@ const outputMigrationFile = async (
.replace(".000", "");
const dirName = `${dateStr}_update_comments`;

const migrationDir = path.join(baseDirPath, "migrations", dirName);
const migrationDir = path.join(migrationsDir, dirName);
fs.mkdirSync(migrationDir, { recursive: true });
fs.writeFileSync(
path.join(migrationDir, "migration.sql"),
Expand Down
Loading