-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(medusa): Run shared module migrations (#3109)
- Loading branch information
1 parent
0326d6c
commit f776ed2
Showing
9 changed files
with
225 additions
and
73 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,5 @@ | ||
--- | ||
"@medusajs/medusa": patch | ||
--- | ||
|
||
feat(medusa): Run shared module migrations |
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
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
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
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
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
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 |
---|---|---|
@@ -1,51 +1,73 @@ | ||
import { createConnection } from "typeorm" | ||
import { getConfigFile } from "medusa-core-utils" | ||
import featureFlagLoader from "../loaders/feature-flags" | ||
import { handleConfigError } from "../loaders/config" | ||
import configModuleLoader from "../loaders/config" | ||
import Logger from "../loaders/logger" | ||
|
||
import getMigrations from "./utils/get-migrations" | ||
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations" | ||
|
||
const t = async function ({ directory }) { | ||
const args = process.argv | ||
args.shift() | ||
args.shift() | ||
args.shift() | ||
|
||
const { configModule, error } = getConfigFile(directory, `medusa-config`) | ||
|
||
if (error) { | ||
handleConfigError(error) | ||
} | ||
const getDataSource = async (directory) => { | ||
const configModule = configModuleLoader(directory) | ||
|
||
const featureFlagRouter = featureFlagLoader(configModule) | ||
|
||
const enabledMigrations = await getMigrations(directory, featureFlagRouter) | ||
const { coreMigrations } = getMigrations(directory, featureFlagRouter) | ||
|
||
const connection = await createConnection({ | ||
const { migrations: moduleMigrations } = getModuleSharedResources( | ||
configModule, | ||
featureFlagRouter | ||
) | ||
|
||
return await createConnection({ | ||
type: configModule.projectConfig.database_type, | ||
url: configModule.projectConfig.database_url, | ||
schema: configModule.projectConfig.database_schema, | ||
extra: configModule.projectConfig.database_extra || {}, | ||
migrations: enabledMigrations, | ||
schema: configModule.projectConfig.database_schema, | ||
migrations: coreMigrations.concat(moduleMigrations), | ||
logging: true, | ||
}) | ||
} | ||
|
||
const main = async function ({ directory }) { | ||
const args = process.argv | ||
args.shift() | ||
args.shift() | ||
args.shift() | ||
|
||
if (args[0] === "run") { | ||
await connection.runMigrations() | ||
await connection.close() | ||
const dataSource = await getDataSource(directory) | ||
|
||
await dataSource.runMigrations() | ||
await dataSource.close() | ||
Logger.info("Migrations completed.") | ||
process.exit() | ||
} else if (args[0] === "revert") { | ||
await connection.undoLastMigration({ transaction: "all" }) | ||
await connection.close() | ||
const dataSource = await getDataSource(directory) | ||
|
||
await dataSource.undoLastMigration({ transaction: "all" }) | ||
await dataSource.close() | ||
Logger.info("Migrations reverted.") | ||
|
||
process.exit() | ||
} else if (args[0] === "show") { | ||
const configModule = configModuleLoader(directory) | ||
|
||
const featureFlagRouter = featureFlagLoader(configModule) | ||
|
||
const { coreMigrations } = getMigrations(directory, featureFlagRouter) | ||
|
||
const connection = await createConnection({ | ||
type: configModule.projectConfig.database_type, | ||
url: configModule.projectConfig.database_url, | ||
extra: configModule.projectConfig.database_extra || {}, | ||
schema: configModule.projectConfig.database_schema, | ||
migrations: coreMigrations, | ||
logging: true, | ||
}) | ||
|
||
const unapplied = await connection.showMigrations() | ||
await connection.close() | ||
process.exit(unapplied ? 1 : 0) | ||
} | ||
} | ||
|
||
export default t | ||
export default main |
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
Oops, something went wrong.