Skip to content

Commit

Permalink
Add upgrade scripts for basic MikroORM migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair committed Dec 18, 2024
1 parent efec37e commit c2289c3
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/v8/mikro-orm-base-entity-generic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFile, writeFile } from "fs/promises";
import { glob } from "glob";

/**
* BaseEntity no longer has generic type arguments.
* See https://mikro-orm.io/docs/upgrading-v5-to-v6#baseentity-no-longer-has-generic-type-arguments.
*/
export default async function removeGenericFromBaseEntity() {
const files: string[] = glob.sync(["api/src/**/*.entity.ts"]);

for (const filePath of files) {
let fileContent = await readFile(filePath, "utf-8");

fileContent = fileContent.replaceAll(/BaseEntity<.*>/g, "BaseEntity");

await writeFile(filePath, fileContent);
}
}
18 changes: 18 additions & 0 deletions src/v8/mikro-orm-custom-type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFile, writeFile } from "fs/promises";
import { glob } from "glob";

/**
* Custom type has been removed in favor of just type.
* See https://mikro-orm.io/docs/upgrading-v5-to-v6#removed-propertyoptionscustomtype-in-favour-of-just-type.
*/
export default async function replaceCustomType() {
const files: string[] = glob.sync(["api/src/**/*.entity.ts"]);

for (const filePath of files) {
let fileContent = await readFile(filePath, "utf-8");

fileContent = fileContent.replaceAll("customType:", "type:");

await writeFile(filePath, fileContent);
}
}
18 changes: 18 additions & 0 deletions src/v8/mikro-orm-delete-rule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFile, writeFile } from "fs/promises";
import { glob } from "glob";

/**
* onDelete has been renamed to deleteRule.
* See https://mikro-orm.io/docs/upgrading-v5-to-v6#renames.
*/
export default async function renameOnDelete() {
const files: string[] = glob.sync(["api/src/**/*.entity.ts"]);

for (const filePath of files) {
let fileContent = await readFile(filePath, "utf-8");

fileContent = fileContent.replaceAll("onDelete:", "deleteRule:");

await writeFile(filePath, fileContent);
}
}
18 changes: 18 additions & 0 deletions src/v8/mikro-orm-dotenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { existsSync } from "fs";
import { readFile, writeFile } from "fs/promises";

/**
* Add a dotenv call to config.
* See https://mikro-orm.io/docs/upgrading-v5-to-v6#env-files-are-no-longer-automatically-loaded.
*/
export default async function addDotenvCallToConfig() {
if (!existsSync("api/src/db/ormconfig.cli.ts")) {
return;
}

let fileContent = await readFile("api/src/db/ormconfig.cli.ts", "utf-8");

fileContent = `import "dotenv/config";\n\n${fileContent}`;

await writeFile("api/src/db/ormconfig.cli.ts", fileContent);
}
18 changes: 18 additions & 0 deletions src/v8/mikro-orm-imports.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { readFile, writeFile } from "fs/promises";
import { glob } from "glob";

/**
* Always import form `@mikro-orm/postgresql`.
* See https://mikro-orm.io/docs/upgrading-v5-to-v6#all-drivers-now-re-export-the-mikro-ormcore-package.
*/
export default async function replaceImports() {
const files: string[] = glob.sync(["api/src/**/*.entity.ts"]);

for (const filePath of files) {
let fileContent = await readFile(filePath, "utf-8");

fileContent = fileContent.replaceAll("@mikro-orm/core", "@mikro-orm/postgresql");

await writeFile(filePath, fileContent);
}
}
29 changes: 29 additions & 0 deletions src/v8/mikro-orm-ormconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Project, SyntaxKind } from "ts-morph";

/**
* Wrap the config in createOrmConfig with defineConfig
*/
export default async function replaceCustomType() {
const project = new Project({ tsConfigFilePath: "./api/tsconfig.json" });

const sourceFile = project.getSourceFile("api/src/db/ormconfig.ts");

if (!sourceFile) {
return;
}

sourceFile.addImportDeclaration({
namedImports: ["defineConfig"],
moduleSpecifier: "@mikro-orm/postgresql",
});

const config = sourceFile
.getVariableStatementOrThrow("ormConfig")
.getDeclarations()[0]
.getInitializerIfKindOrThrow(SyntaxKind.CallExpression)
.getArguments()[0];

config.replaceWithText(`defineConfig(${config.getText()})`);

await sourceFile.save();
}

0 comments on commit c2289c3

Please sign in to comment.