-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add upgrade scripts for basic MikroORM migrations
- Loading branch information
1 parent
efec37e
commit c2289c3
Showing
6 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |