feat(medusa): Run shared module migrations (#3109)
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/medusa": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(medusa): Run shared module migrations
|
||||||
@@ -3,9 +3,10 @@ const express = require("express")
|
|||||||
const importFrom = require("import-from")
|
const importFrom = require("import-from")
|
||||||
const chokidar = require("chokidar")
|
const chokidar = require("chokidar")
|
||||||
|
|
||||||
require("dotenv").config({ path: path.join(__dirname, ".env.development") })
|
|
||||||
|
|
||||||
process.env.DEV_MODE = !!process[Symbol.for("ts-node.register.instance")]
|
process.env.DEV_MODE = !!process[Symbol.for("ts-node.register.instance")]
|
||||||
|
process.env.NODE_ENV = process.env.DEV_MODE && "development"
|
||||||
|
|
||||||
|
require("dotenv").config({ path: path.join(__dirname, ".env.development") })
|
||||||
|
|
||||||
require("./dev-require")
|
require("./dev-require")
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const path = require("path")
|
const path = require("path")
|
||||||
|
|
||||||
const { createConnection } = require("typeorm")
|
const { createConnection } = require("typeorm")
|
||||||
|
const { getConfigFile } = require("medusa-core-utils")
|
||||||
|
|
||||||
const DB_HOST = process.env.DB_HOST
|
const DB_HOST = process.env.DB_HOST
|
||||||
const DB_USERNAME = process.env.DB_USERNAME
|
const DB_USERNAME = process.env.DB_USERNAME
|
||||||
@@ -8,6 +9,8 @@ const DB_PASSWORD = process.env.DB_PASSWORD
|
|||||||
const DB_NAME = process.env.DB_NAME
|
const DB_NAME = process.env.DB_NAME
|
||||||
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
const DB_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
||||||
|
|
||||||
|
process.env.NODE_ENV = "development"
|
||||||
|
|
||||||
require("./dev-require")
|
require("./dev-require")
|
||||||
|
|
||||||
async function createDB() {
|
async function createDB() {
|
||||||
@@ -25,26 +28,26 @@ module.exports = {
|
|||||||
initDb: async function () {
|
initDb: async function () {
|
||||||
const cwd = path.resolve(path.join(__dirname, "../.."))
|
const cwd = path.resolve(path.join(__dirname, "../.."))
|
||||||
|
|
||||||
const configPath = path.resolve(
|
const { configModule } = getConfigFile(
|
||||||
path.join(__dirname, "../api/medusa-config.js")
|
path.join(__dirname),
|
||||||
|
`medusa-config`
|
||||||
)
|
)
|
||||||
const { featureFlags } = require(configPath)
|
|
||||||
|
const { featureFlags } = configModule
|
||||||
|
|
||||||
const basePath = path.join(cwd, "packages/medusa/src")
|
const basePath = path.join(cwd, "packages/medusa/src")
|
||||||
|
|
||||||
const featureFlagsLoader = require(path.join(
|
const featureFlagsLoader =
|
||||||
basePath,
|
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
||||||
`loaders`,
|
|
||||||
`feature-flags`
|
|
||||||
)).default
|
|
||||||
|
|
||||||
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
|
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
|
||||||
|
|
||||||
const modelsLoader = require(path.join(
|
const modelsLoader = require("@medusajs/medusa/dist/loaders/models").default
|
||||||
basePath,
|
|
||||||
`loaders`,
|
const {
|
||||||
`models`
|
getEnabledMigrations,
|
||||||
)).default
|
getModuleSharedResources,
|
||||||
|
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||||
|
|
||||||
const entities = modelsLoader({}, { register: false })
|
const entities = modelsLoader({}, { register: false })
|
||||||
|
|
||||||
@@ -53,16 +56,14 @@ module.exports = {
|
|||||||
path.join(basePath, `migrations`, `*.{j,t}s`)
|
path.join(basePath, `migrations`, `*.{j,t}s`)
|
||||||
)
|
)
|
||||||
|
|
||||||
const { getEnabledMigrations } = require(path.join(
|
const isFlagEnabled = (flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
||||||
basePath,
|
|
||||||
`commands`,
|
|
||||||
`utils`,
|
|
||||||
`get-migrations`
|
|
||||||
))
|
|
||||||
|
|
||||||
const enabledMigrations = await getEnabledMigrations(
|
const { migrations: moduleMigrations, models: moduleModels } =
|
||||||
|
getModuleSharedResources(configModule, featureFlagsRouter)
|
||||||
|
|
||||||
|
const enabledMigrations = getEnabledMigrations(
|
||||||
[migrationDir],
|
[migrationDir],
|
||||||
(flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
isFlagEnabled
|
||||||
)
|
)
|
||||||
|
|
||||||
const enabledEntities = entities.filter(
|
const enabledEntities = entities.filter(
|
||||||
@@ -70,12 +71,13 @@ module.exports = {
|
|||||||
)
|
)
|
||||||
|
|
||||||
await createDB()
|
await createDB()
|
||||||
|
|
||||||
const dbConnection = await createConnection({
|
const dbConnection = await createConnection({
|
||||||
type: "postgres",
|
type: "postgres",
|
||||||
url: DB_URL,
|
url: DB_URL,
|
||||||
entities: enabledEntities,
|
entities: enabledEntities.concat(moduleModels),
|
||||||
migrations: enabledMigrations,
|
migrations: enabledMigrations.concat(moduleMigrations),
|
||||||
//logging: true,
|
// logging: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
await dbConnection.runMigrations()
|
await dbConnection.runMigrations()
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const path = require("path")
|
const path = require("path")
|
||||||
|
|
||||||
|
const { getConfigFile } = require("medusa-core-utils")
|
||||||
const { dropDatabase } = require("pg-god")
|
const { dropDatabase } = require("pg-god")
|
||||||
const { createConnection } = require("typeorm")
|
const { createConnection } = require("typeorm")
|
||||||
const dbFactory = require("./use-template-db")
|
const dbFactory = require("./use-template-db")
|
||||||
@@ -43,6 +44,7 @@ const DbTestUtil = {
|
|||||||
forceDelete = forceDelete || []
|
forceDelete = forceDelete || []
|
||||||
|
|
||||||
const entities = this.db_.entityMetadatas
|
const entities = this.db_.entityMetadatas
|
||||||
|
|
||||||
const manager = this.db_.manager
|
const manager = this.db_.manager
|
||||||
|
|
||||||
if (connectionType === "sqlite") {
|
if (connectionType === "sqlite") {
|
||||||
@@ -79,8 +81,8 @@ const instance = DbTestUtil
|
|||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
initDb: async function ({ cwd, database_extra }) {
|
initDb: async function ({ cwd, database_extra }) {
|
||||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
|
const { configModule } = getConfigFile(cwd, `medusa-config`)
|
||||||
const { projectConfig, featureFlags } = require(configPath)
|
const { projectConfig, featureFlags } = configModule
|
||||||
|
|
||||||
const featureFlagsLoader =
|
const featureFlagsLoader =
|
||||||
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
||||||
@@ -122,11 +124,14 @@ module.exports = {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
getEnabledMigrations,
|
getEnabledMigrations,
|
||||||
|
getModuleSharedResources,
|
||||||
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||||
|
|
||||||
const enabledMigrations = await getEnabledMigrations(
|
const { migrations: moduleMigrations, models: moduleModels } =
|
||||||
[migrationDir],
|
getModuleSharedResources(configModule, featureFlagsRouter)
|
||||||
(flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
|
||||||
|
const enabledMigrations = getEnabledMigrations([migrationDir], (flag) =>
|
||||||
|
featureFlagsRouter.isFeatureEnabled(flag)
|
||||||
)
|
)
|
||||||
|
|
||||||
const enabledEntities = entities.filter(
|
const enabledEntities = entities.filter(
|
||||||
@@ -136,8 +141,8 @@ module.exports = {
|
|||||||
const dbConnection = await createConnection({
|
const dbConnection = await createConnection({
|
||||||
type: "postgres",
|
type: "postgres",
|
||||||
url: DB_URL,
|
url: DB_URL,
|
||||||
entities: enabledEntities,
|
entities: enabledEntities.concat(moduleModels),
|
||||||
migrations: enabledMigrations,
|
migrations: enabledMigrations.concat(moduleMigrations),
|
||||||
extra: database_extra ?? {},
|
extra: database_extra ?? {},
|
||||||
name: "integration-tests",
|
name: "integration-tests",
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ const path = require("path")
|
|||||||
|
|
||||||
require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
|
require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
|
||||||
|
|
||||||
|
const { getConfigFile } = require("medusa-core-utils")
|
||||||
const { createDatabase, dropDatabase } = require("pg-god")
|
const { createDatabase, dropDatabase } = require("pg-god")
|
||||||
const { createConnection, getConnection } = require("typeorm")
|
const { createConnection, getConnection } = require("typeorm")
|
||||||
|
|
||||||
@@ -24,8 +25,10 @@ class DatabaseFactory {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createTemplateDb_({ cwd }) {
|
async createTemplateDb_({ cwd }) {
|
||||||
// const cwd = path.resolve(path.join(__dirname, ".."))
|
const { configModule } = getConfigFile(cwd, `medusa-config`)
|
||||||
|
|
||||||
const connection = await this.getMasterConnection()
|
const connection = await this.getMasterConnection()
|
||||||
|
|
||||||
const migrationDir = path.resolve(
|
const migrationDir = path.resolve(
|
||||||
path.join(
|
path.join(
|
||||||
__dirname,
|
__dirname,
|
||||||
@@ -41,14 +44,18 @@ class DatabaseFactory {
|
|||||||
|
|
||||||
const {
|
const {
|
||||||
getEnabledMigrations,
|
getEnabledMigrations,
|
||||||
|
getModuleSharedResources,
|
||||||
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||||
|
|
||||||
// filter migrations to only include those that don't have feature flags
|
// filter migrations to only include those that don't have feature flags
|
||||||
const enabledMigrations = await getEnabledMigrations(
|
const enabledMigrations = getEnabledMigrations(
|
||||||
[migrationDir],
|
[migrationDir],
|
||||||
(flag) => false
|
(flag) => false
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const { migrations: moduleMigrations } =
|
||||||
|
getModuleSharedResources(configModule)
|
||||||
|
|
||||||
await dropDatabase(
|
await dropDatabase(
|
||||||
{
|
{
|
||||||
databaseName: this.templateDbName,
|
databaseName: this.templateDbName,
|
||||||
@@ -65,7 +72,7 @@ class DatabaseFactory {
|
|||||||
type: "postgres",
|
type: "postgres",
|
||||||
name: "templateConnection",
|
name: "templateConnection",
|
||||||
url: `${DB_URL}/${this.templateDbName}`,
|
url: `${DB_URL}/${this.templateDbName}`,
|
||||||
migrations: enabledMigrations,
|
migrations: enabledMigrations.concat(moduleMigrations),
|
||||||
})
|
})
|
||||||
|
|
||||||
await templateDbConnection.runMigrations()
|
await templateDbConnection.runMigrations()
|
||||||
@@ -76,7 +83,7 @@ class DatabaseFactory {
|
|||||||
|
|
||||||
async getMasterConnection() {
|
async getMasterConnection() {
|
||||||
try {
|
try {
|
||||||
return await getConnection(this.masterConnectionName)
|
return getConnection(this.masterConnectionName)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return await this.createMasterConnection()
|
return await this.createMasterConnection()
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-2
@@ -77,8 +77,8 @@
|
|||||||
"generate:entities": "typedoc --options typedoc.entities.js",
|
"generate:entities": "typedoc --options typedoc.entities.js",
|
||||||
"release:snapshot": "changeset publish --no-git-tags --snapshot --tag snapshot",
|
"release:snapshot": "changeset publish --no-git-tags --snapshot --tag snapshot",
|
||||||
"generate:announcement": "node ./scripts/doc-change-release.js",
|
"generate:announcement": "node ./scripts/doc-change-release.js",
|
||||||
"develop": "NODE_ENV=development ts-node --transpile-only ./integration-tests/development/server.js",
|
"develop": "ts-node --transpile-only ./integration-tests/development/server.js",
|
||||||
"develop:create:db": "NODE_ENV=development ts-node --transpile-only ./integration-tests/development/create-database.js",
|
"develop:create:db": "ts-node --transpile-only ./integration-tests/development/create-database.js",
|
||||||
"release:next": "changeset publish --no-git-tags --snapshot --tag next",
|
"release:next": "changeset publish --no-git-tags --snapshot --tag next",
|
||||||
"version:next": "changeset version --snapshot next",
|
"version:next": "changeset version --snapshot next",
|
||||||
"check:freshness": "node ./scripts/freshness-check.js"
|
"check:freshness": "node ./scripts/freshness-check.js"
|
||||||
|
|||||||
@@ -1,51 +1,73 @@
|
|||||||
import { createConnection } from "typeorm"
|
import { createConnection } from "typeorm"
|
||||||
import { getConfigFile } from "medusa-core-utils"
|
|
||||||
import featureFlagLoader from "../loaders/feature-flags"
|
import featureFlagLoader from "../loaders/feature-flags"
|
||||||
import { handleConfigError } from "../loaders/config"
|
import configModuleLoader from "../loaders/config"
|
||||||
import Logger from "../loaders/logger"
|
import Logger from "../loaders/logger"
|
||||||
|
|
||||||
import getMigrations from "./utils/get-migrations"
|
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"
|
||||||
|
|
||||||
const t = async function ({ directory }) {
|
const getDataSource = async (directory) => {
|
||||||
|
const configModule = configModuleLoader(directory)
|
||||||
|
|
||||||
|
const featureFlagRouter = featureFlagLoader(configModule)
|
||||||
|
|
||||||
|
const { coreMigrations } = getMigrations(directory, featureFlagRouter)
|
||||||
|
|
||||||
|
const { migrations: moduleMigrations } = getModuleSharedResources(
|
||||||
|
configModule,
|
||||||
|
featureFlagRouter
|
||||||
|
)
|
||||||
|
|
||||||
|
return await createConnection({
|
||||||
|
type: configModule.projectConfig.database_type,
|
||||||
|
url: configModule.projectConfig.database_url,
|
||||||
|
extra: configModule.projectConfig.database_extra || {},
|
||||||
|
schema: configModule.projectConfig.database_schema,
|
||||||
|
migrations: coreMigrations.concat(moduleMigrations),
|
||||||
|
logging: true,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const main = async function ({ directory }) {
|
||||||
const args = process.argv
|
const args = process.argv
|
||||||
args.shift()
|
args.shift()
|
||||||
args.shift()
|
args.shift()
|
||||||
args.shift()
|
args.shift()
|
||||||
|
|
||||||
const { configModule, error } = getConfigFile(directory, `medusa-config`)
|
if (args[0] === "run") {
|
||||||
|
const dataSource = await getDataSource(directory)
|
||||||
|
|
||||||
if (error) {
|
await dataSource.runMigrations()
|
||||||
handleConfigError(error)
|
await dataSource.close()
|
||||||
}
|
Logger.info("Migrations completed.")
|
||||||
|
process.exit()
|
||||||
|
} else if (args[0] === "revert") {
|
||||||
|
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 featureFlagRouter = featureFlagLoader(configModule)
|
||||||
|
|
||||||
const enabledMigrations = await getMigrations(directory, featureFlagRouter)
|
const { coreMigrations } = getMigrations(directory, featureFlagRouter)
|
||||||
|
|
||||||
const connection = await createConnection({
|
const connection = await createConnection({
|
||||||
type: configModule.projectConfig.database_type,
|
type: configModule.projectConfig.database_type,
|
||||||
url: configModule.projectConfig.database_url,
|
url: configModule.projectConfig.database_url,
|
||||||
schema: configModule.projectConfig.database_schema,
|
|
||||||
extra: configModule.projectConfig.database_extra || {},
|
extra: configModule.projectConfig.database_extra || {},
|
||||||
migrations: enabledMigrations,
|
schema: configModule.projectConfig.database_schema,
|
||||||
|
migrations: coreMigrations,
|
||||||
logging: true,
|
logging: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
if (args[0] === "run") {
|
|
||||||
await connection.runMigrations()
|
|
||||||
await connection.close()
|
|
||||||
Logger.info("Migrations completed.")
|
|
||||||
process.exit()
|
|
||||||
} else if (args[0] === "revert") {
|
|
||||||
await connection.undoLastMigration({ transaction: "all" })
|
|
||||||
await connection.close()
|
|
||||||
Logger.info("Migrations reverted.")
|
|
||||||
process.exit()
|
|
||||||
} else if (args[0] === "show") {
|
|
||||||
const unapplied = await connection.showMigrations()
|
const unapplied = await connection.showMigrations()
|
||||||
await connection.close()
|
await connection.close()
|
||||||
process.exit(unapplied ? 1 : 0)
|
process.exit(unapplied ? 1 : 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default t
|
export default main
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ import {
|
|||||||
} from "../services"
|
} from "../services"
|
||||||
import { ConfigModule } from "../types/global"
|
import { ConfigModule } from "../types/global"
|
||||||
import { CreateProductInput } from "../types/product"
|
import { CreateProductInput } from "../types/product"
|
||||||
import getMigrations from "./utils/get-migrations"
|
import getMigrations, { getModuleSharedResources } from "./utils/get-migrations"
|
||||||
|
|
||||||
type SeedOptions = {
|
type SeedOptions = {
|
||||||
directory: string
|
directory: string
|
||||||
@@ -58,7 +58,12 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
|
|
||||||
const dbType = configModule.projectConfig.database_type
|
const dbType = configModule.projectConfig.database_type
|
||||||
if (migrate && dbType !== "sqlite") {
|
if (migrate && dbType !== "sqlite") {
|
||||||
const migrationDirs = await getMigrations(directory, featureFlagRouter)
|
const { coreMigrations } = getMigrations(directory, featureFlagRouter)
|
||||||
|
|
||||||
|
const { migrations: moduleMigrations } = getModuleSharedResources(
|
||||||
|
configModule,
|
||||||
|
featureFlagRouter
|
||||||
|
)
|
||||||
|
|
||||||
const connectionOptions = {
|
const connectionOptions = {
|
||||||
type: configModule.projectConfig.database_type,
|
type: configModule.projectConfig.database_type,
|
||||||
@@ -66,7 +71,7 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
schema: configModule.projectConfig.database_schema,
|
schema: configModule.projectConfig.database_schema,
|
||||||
url: configModule.projectConfig.database_url,
|
url: configModule.projectConfig.database_url,
|
||||||
extra: configModule.projectConfig.database_extra || {},
|
extra: configModule.projectConfig.database_extra || {},
|
||||||
migrations: migrationDirs,
|
migrations: coreMigrations.concat(moduleMigrations),
|
||||||
logging: true,
|
logging: true,
|
||||||
} as ConnectionOptions
|
} as ConnectionOptions
|
||||||
|
|
||||||
@@ -91,9 +96,15 @@ const seed = async function ({ directory, migrate, seedFile }: SeedOptions) {
|
|||||||
const regionService: RegionService = container.resolve("regionService")
|
const regionService: RegionService = container.resolve("regionService")
|
||||||
const productService: ProductService = container.resolve("productService")
|
const productService: ProductService = container.resolve("productService")
|
||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
const productVariantService: ProductVariantService = container.resolve("productVariantService")
|
const productVariantService: ProductVariantService = container.resolve(
|
||||||
const shippingOptionService: ShippingOptionService = container.resolve("shippingOptionService")
|
"productVariantService"
|
||||||
const shippingProfileService: ShippingProfileService = container.resolve("shippingProfileService")
|
)
|
||||||
|
const shippingOptionService: ShippingOptionService = container.resolve(
|
||||||
|
"shippingOptionService"
|
||||||
|
)
|
||||||
|
const shippingProfileService: ShippingProfileService = container.resolve(
|
||||||
|
"shippingProfileService"
|
||||||
|
)
|
||||||
/* eslint-enable */
|
/* eslint-enable */
|
||||||
|
|
||||||
await manager.transaction(async (tx) => {
|
await manager.transaction(async (tx) => {
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { isString } from "lodash"
|
|||||||
import { sync as existsSync } from "fs-exists-cached"
|
import { sync as existsSync } from "fs-exists-cached"
|
||||||
import { getConfigFile, createRequireFromPath } from "medusa-core-utils"
|
import { getConfigFile, createRequireFromPath } from "medusa-core-utils"
|
||||||
import { handleConfigError } from "../../loaders/config"
|
import { handleConfigError } from "../../loaders/config"
|
||||||
import logger from "../../loaders/logger"
|
import registerModuleDefinitions from "../../loaders/module-definitions"
|
||||||
|
|
||||||
function createFileContentHash(path, files) {
|
function createFileContentHash(path, files) {
|
||||||
return path + files
|
return path + files
|
||||||
@@ -89,7 +89,37 @@ function resolvePlugin(pluginName) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default async (directory, featureFlagRouter) => {
|
export function getInternalModules(configModule) {
|
||||||
|
const modules = []
|
||||||
|
|
||||||
|
const moduleResolutions = registerModuleDefinitions(configModule)
|
||||||
|
|
||||||
|
for (const moduleResolution of Object.values(moduleResolutions)) {
|
||||||
|
if (
|
||||||
|
!moduleResolution.resolutionPath ||
|
||||||
|
moduleResolution.moduleDeclaration.scope !== "internal"
|
||||||
|
) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
let loadedModule = null
|
||||||
|
try {
|
||||||
|
loadedModule = require(moduleResolution.moduleDeclaration.resolve).default
|
||||||
|
} catch (error) {
|
||||||
|
console.log("Error loading Module", error)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
modules.push({
|
||||||
|
moduleDeclaration: moduleResolution.moduleDeclaration,
|
||||||
|
loadedModule,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return modules
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (directory, featureFlagRouter) => {
|
||||||
const { configModule, error } = getConfigFile(directory, `medusa-config`)
|
const { configModule, error } = getConfigFile(directory, `medusa-config`)
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
@@ -118,11 +148,11 @@ export default async (directory, featureFlagRouter) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
const migrationDirs = []
|
const migrationDirs = []
|
||||||
const coreMigrations = path.resolve(
|
const corePackageMigrations = path.resolve(
|
||||||
path.join(__dirname, "..", "..", "migrations")
|
path.join(__dirname, "..", "..", "migrations")
|
||||||
)
|
)
|
||||||
|
|
||||||
migrationDirs.push(path.join(coreMigrations, "*.js"))
|
migrationDirs.push(path.join(corePackageMigrations, "*.js"))
|
||||||
|
|
||||||
for (const p of resolved) {
|
for (const p of resolved) {
|
||||||
const exists = existsSync(`${p.resolve}/migrations`)
|
const exists = existsSync(`${p.resolve}/migrations`)
|
||||||
@@ -131,9 +161,15 @@ export default async (directory, featureFlagRouter) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return getEnabledMigrations(migrationDirs, (flag) =>
|
const isFeatureFlagEnabled = (flag) =>
|
||||||
featureFlagRouter.isFeatureEnabled(flag)
|
featureFlagRouter.isFeatureEnabled(flag)
|
||||||
|
|
||||||
|
const coreMigrations = getEnabledMigrations(
|
||||||
|
migrationDirs,
|
||||||
|
isFeatureFlagEnabled
|
||||||
)
|
)
|
||||||
|
|
||||||
|
return { coreMigrations }
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getEnabledMigrations = (migrationDirs, isFlagEnabled) => {
|
export const getEnabledMigrations = (migrationDirs, isFlagEnabled) => {
|
||||||
@@ -154,3 +190,66 @@ export const getEnabledMigrations = (migrationDirs, isFlagEnabled) => {
|
|||||||
})
|
})
|
||||||
.filter(Boolean)
|
.filter(Boolean)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getModuleMigrations = (configModule, isFlagEnabled) => {
|
||||||
|
const loadedModules = getInternalModules(configModule)
|
||||||
|
|
||||||
|
const allModules = []
|
||||||
|
|
||||||
|
for (const loadedModule of loadedModules) {
|
||||||
|
const mod = loadedModule.loadedModule
|
||||||
|
|
||||||
|
const isolatedMigrations = {}
|
||||||
|
const moduleMigrations = (mod.migrations ?? [])
|
||||||
|
.map((migrations) => {
|
||||||
|
const all = []
|
||||||
|
for (const migration of Object.values(migrations)) {
|
||||||
|
// TODO: revisit how Modules export their migration entrypoints up/down
|
||||||
|
if (["up", "down"].includes(migration.name)) {
|
||||||
|
isolatedMigrations[migration.name] = migration
|
||||||
|
} else if (
|
||||||
|
typeof migration.featureFlag === "undefined" ||
|
||||||
|
isFlagEnabled(migration.featureFlag)
|
||||||
|
) {
|
||||||
|
all.push(migration)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return all
|
||||||
|
})
|
||||||
|
.flat()
|
||||||
|
|
||||||
|
allModules.push({
|
||||||
|
moduleDeclaration: loadedModule.moduleDeclaration,
|
||||||
|
models: mod.models ?? [],
|
||||||
|
migrations: moduleMigrations,
|
||||||
|
externalMigrations: isolatedMigrations,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return allModules
|
||||||
|
}
|
||||||
|
|
||||||
|
export const getModuleSharedResources = (configModule, featureFlagsRouter) => {
|
||||||
|
const isFlagEnabled = (flag) =>
|
||||||
|
featureFlagsRouter && featureFlagsRouter.isFeatureEnabled(flag)
|
||||||
|
|
||||||
|
const loadedModules = getModuleMigrations(configModule, isFlagEnabled)
|
||||||
|
|
||||||
|
let migrations = []
|
||||||
|
let models = []
|
||||||
|
|
||||||
|
for (const mod of loadedModules) {
|
||||||
|
if (mod.moduleDeclaration.resources !== "shared") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
migrations = migrations.concat(mod.migrations)
|
||||||
|
|
||||||
|
models = models.concat(mod.models ?? [])
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
models,
|
||||||
|
migrations,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user