feat(medusa): Run shared module migrations (#3109)
This commit is contained in:
committed by
GitHub
parent
0326d6cfa3
commit
f776ed234f
@@ -3,9 +3,10 @@ const express = require("express")
|
||||
const importFrom = require("import-from")
|
||||
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.NODE_ENV = process.env.DEV_MODE && "development"
|
||||
|
||||
require("dotenv").config({ path: path.join(__dirname, ".env.development") })
|
||||
|
||||
require("./dev-require")
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const path = require("path")
|
||||
|
||||
const { createConnection } = require("typeorm")
|
||||
const { getConfigFile } = require("medusa-core-utils")
|
||||
|
||||
const DB_HOST = process.env.DB_HOST
|
||||
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_URL = `postgres://${DB_USERNAME}:${DB_PASSWORD}@${DB_HOST}/${DB_NAME}`
|
||||
|
||||
process.env.NODE_ENV = "development"
|
||||
|
||||
require("./dev-require")
|
||||
|
||||
async function createDB() {
|
||||
@@ -25,26 +28,26 @@ module.exports = {
|
||||
initDb: async function () {
|
||||
const cwd = path.resolve(path.join(__dirname, "../.."))
|
||||
|
||||
const configPath = path.resolve(
|
||||
path.join(__dirname, "../api/medusa-config.js")
|
||||
const { configModule } = getConfigFile(
|
||||
path.join(__dirname),
|
||||
`medusa-config`
|
||||
)
|
||||
const { featureFlags } = require(configPath)
|
||||
|
||||
const { featureFlags } = configModule
|
||||
|
||||
const basePath = path.join(cwd, "packages/medusa/src")
|
||||
|
||||
const featureFlagsLoader = require(path.join(
|
||||
basePath,
|
||||
`loaders`,
|
||||
`feature-flags`
|
||||
)).default
|
||||
const featureFlagsLoader =
|
||||
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
||||
|
||||
const featureFlagsRouter = featureFlagsLoader({ featureFlags })
|
||||
|
||||
const modelsLoader = require(path.join(
|
||||
basePath,
|
||||
`loaders`,
|
||||
`models`
|
||||
)).default
|
||||
const modelsLoader = require("@medusajs/medusa/dist/loaders/models").default
|
||||
|
||||
const {
|
||||
getEnabledMigrations,
|
||||
getModuleSharedResources,
|
||||
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||
|
||||
const entities = modelsLoader({}, { register: false })
|
||||
|
||||
@@ -53,16 +56,14 @@ module.exports = {
|
||||
path.join(basePath, `migrations`, `*.{j,t}s`)
|
||||
)
|
||||
|
||||
const { getEnabledMigrations } = require(path.join(
|
||||
basePath,
|
||||
`commands`,
|
||||
`utils`,
|
||||
`get-migrations`
|
||||
))
|
||||
const isFlagEnabled = (flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
||||
|
||||
const enabledMigrations = await getEnabledMigrations(
|
||||
const { migrations: moduleMigrations, models: moduleModels } =
|
||||
getModuleSharedResources(configModule, featureFlagsRouter)
|
||||
|
||||
const enabledMigrations = getEnabledMigrations(
|
||||
[migrationDir],
|
||||
(flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
||||
isFlagEnabled
|
||||
)
|
||||
|
||||
const enabledEntities = entities.filter(
|
||||
@@ -70,12 +71,13 @@ module.exports = {
|
||||
)
|
||||
|
||||
await createDB()
|
||||
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities: enabledEntities,
|
||||
migrations: enabledMigrations,
|
||||
//logging: true,
|
||||
entities: enabledEntities.concat(moduleModels),
|
||||
migrations: enabledMigrations.concat(moduleMigrations),
|
||||
// logging: true,
|
||||
})
|
||||
|
||||
await dbConnection.runMigrations()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
const path = require("path")
|
||||
|
||||
const { getConfigFile } = require("medusa-core-utils")
|
||||
const { dropDatabase } = require("pg-god")
|
||||
const { createConnection } = require("typeorm")
|
||||
const dbFactory = require("./use-template-db")
|
||||
@@ -43,6 +44,7 @@ const DbTestUtil = {
|
||||
forceDelete = forceDelete || []
|
||||
|
||||
const entities = this.db_.entityMetadatas
|
||||
|
||||
const manager = this.db_.manager
|
||||
|
||||
if (connectionType === "sqlite") {
|
||||
@@ -79,8 +81,8 @@ const instance = DbTestUtil
|
||||
|
||||
module.exports = {
|
||||
initDb: async function ({ cwd, database_extra }) {
|
||||
const configPath = path.resolve(path.join(cwd, `medusa-config.js`))
|
||||
const { projectConfig, featureFlags } = require(configPath)
|
||||
const { configModule } = getConfigFile(cwd, `medusa-config`)
|
||||
const { projectConfig, featureFlags } = configModule
|
||||
|
||||
const featureFlagsLoader =
|
||||
require("@medusajs/medusa/dist/loaders/feature-flags").default
|
||||
@@ -122,11 +124,14 @@ module.exports = {
|
||||
|
||||
const {
|
||||
getEnabledMigrations,
|
||||
getModuleSharedResources,
|
||||
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||
|
||||
const enabledMigrations = await getEnabledMigrations(
|
||||
[migrationDir],
|
||||
(flag) => featureFlagsRouter.isFeatureEnabled(flag)
|
||||
const { migrations: moduleMigrations, models: moduleModels } =
|
||||
getModuleSharedResources(configModule, featureFlagsRouter)
|
||||
|
||||
const enabledMigrations = getEnabledMigrations([migrationDir], (flag) =>
|
||||
featureFlagsRouter.isFeatureEnabled(flag)
|
||||
)
|
||||
|
||||
const enabledEntities = entities.filter(
|
||||
@@ -136,8 +141,8 @@ module.exports = {
|
||||
const dbConnection = await createConnection({
|
||||
type: "postgres",
|
||||
url: DB_URL,
|
||||
entities: enabledEntities,
|
||||
migrations: enabledMigrations,
|
||||
entities: enabledEntities.concat(moduleModels),
|
||||
migrations: enabledMigrations.concat(moduleMigrations),
|
||||
extra: database_extra ?? {},
|
||||
name: "integration-tests",
|
||||
})
|
||||
|
||||
@@ -2,6 +2,7 @@ const path = require("path")
|
||||
|
||||
require("dotenv").config({ path: path.join(__dirname, "../.env.test") })
|
||||
|
||||
const { getConfigFile } = require("medusa-core-utils")
|
||||
const { createDatabase, dropDatabase } = require("pg-god")
|
||||
const { createConnection, getConnection } = require("typeorm")
|
||||
|
||||
@@ -24,8 +25,10 @@ class DatabaseFactory {
|
||||
}
|
||||
|
||||
async createTemplateDb_({ cwd }) {
|
||||
// const cwd = path.resolve(path.join(__dirname, ".."))
|
||||
const { configModule } = getConfigFile(cwd, `medusa-config`)
|
||||
|
||||
const connection = await this.getMasterConnection()
|
||||
|
||||
const migrationDir = path.resolve(
|
||||
path.join(
|
||||
__dirname,
|
||||
@@ -41,14 +44,18 @@ class DatabaseFactory {
|
||||
|
||||
const {
|
||||
getEnabledMigrations,
|
||||
getModuleSharedResources,
|
||||
} = require("@medusajs/medusa/dist/commands/utils/get-migrations")
|
||||
|
||||
// filter migrations to only include those that don't have feature flags
|
||||
const enabledMigrations = await getEnabledMigrations(
|
||||
const enabledMigrations = getEnabledMigrations(
|
||||
[migrationDir],
|
||||
(flag) => false
|
||||
)
|
||||
|
||||
const { migrations: moduleMigrations } =
|
||||
getModuleSharedResources(configModule)
|
||||
|
||||
await dropDatabase(
|
||||
{
|
||||
databaseName: this.templateDbName,
|
||||
@@ -65,7 +72,7 @@ class DatabaseFactory {
|
||||
type: "postgres",
|
||||
name: "templateConnection",
|
||||
url: `${DB_URL}/${this.templateDbName}`,
|
||||
migrations: enabledMigrations,
|
||||
migrations: enabledMigrations.concat(moduleMigrations),
|
||||
})
|
||||
|
||||
await templateDbConnection.runMigrations()
|
||||
@@ -76,7 +83,7 @@ class DatabaseFactory {
|
||||
|
||||
async getMasterConnection() {
|
||||
try {
|
||||
return await getConnection(this.masterConnectionName)
|
||||
return getConnection(this.masterConnectionName)
|
||||
} catch (err) {
|
||||
return await this.createMasterConnection()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user