Skip to content

Commit

Permalink
Add upgrade scripts for basic MikroORM migrations (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyomair authored Jan 14, 2025
1 parent 92f0a75 commit 86578a5
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/util/package-json.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { existsSync, readFileSync, writeFileSync } from "fs";
type Json = {
dependencies?: Record<string, string>;
devDependencies?: Record<string, string>;
scripts?: Record<string, string>;
};

export class PackageJson {
Expand Down Expand Up @@ -46,4 +47,9 @@ export class PackageJson {
save() {
writeFileSync(this.path, JSON.stringify(this.json, null, 4));
}

addScript(name: string, script: string) {
this.json.scripts ??= {};
this.json.scripts[name] = script;
}
}
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);
}
}
19 changes: 19 additions & 0 deletions src/v8/mikro-orm-dotenv.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { existsSync } from "fs";

import { PackageJson } from "../util/package-json.util";

/**
* Adds a `mikro-orm` script to package.json that calls dotenv.
* 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/package.json")) {
return;
}

const packageJson = new PackageJson("api/package.json");

packageJson.addScript("mikro-orm", "dotenv -e .env.secrets -e .env.local -e .env -e .env.site-configs -- mikro-orm");

packageJson.save();
}
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 86578a5

Please sign in to comment.