feat(medusa): Run shared module migrations (#3109)
This commit is contained in:
@@ -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