diff --git a/src/__fixtures__/multi-file-schema/schema/post.prisma b/src/__fixtures__/multi-file-schema/schema/post.prisma new file mode 100644 index 0000000..f857f5e --- /dev/null +++ b/src/__fixtures__/multi-file-schema/schema/post.prisma @@ -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") +} diff --git a/src/__fixtures__/multi-file-schema/schema/schema.prisma b/src/__fixtures__/multi-file-schema/schema/schema.prisma new file mode 100644 index 0000000..29b786c --- /dev/null +++ b/src/__fixtures__/multi-file-schema/schema/schema.prisma @@ -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") +} diff --git a/src/__fixtures__/multi-file-schema/schema/user.prisma b/src/__fixtures__/multi-file-schema/schema/user.prisma new file mode 100644 index 0000000..20a9eaf --- /dev/null +++ b/src/__fixtures__/multi-file-schema/schema/user.prisma @@ -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") +} diff --git a/src/__snapshots__/generator.test.ts.snap b/src/__snapshots__/generator.test.ts.snap index 60d5be9..f3f7ccb 100644 --- a/src/__snapshots__/generator.test.ts.snap +++ b/src/__snapshots__/generator.test.ts.snap @@ -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": { diff --git a/src/generator.test.ts b/src/generator.test.ts index ee2817e..8970698 100644 --- a/src/generator.test.ts +++ b/src/generator.test.ts @@ -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}`); }; @@ -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"); }; diff --git a/src/generator.ts b/src/generator.ts index 0bac7cd..e9af20a 100644 --- a/src/generator.ts +++ b/src/generator.ts @@ -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 }); @@ -47,7 +47,7 @@ const generate = async (options: GeneratorOptions) => { } const migrationDirName = await outputMigrationFile( - path.dirname(schemaPath), + config.outputDir, commentStatements, ); @@ -62,7 +62,7 @@ const generate = async (options: GeneratorOptions) => { }; const outputMigrationFile = async ( - baseDirPath: string, + migrationsDir: string, commentStatements: string[], ) => { const date = new Date(); @@ -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"),