Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upgrade scripts for basic MikroORM migrations #44

Merged
merged 3 commits into from
Jan 14, 2025
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
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();
}
Loading