feat(link-modules,modules-sdk,pricing): Medusa App Migrations + Core compatible migrations (#5317)
* chore: remove skipping logic for migrations * chore: medusa app returns migrations to run for modules * chore: added migration for feature compatible * chore: added changelog * chore: create table only if it does not exist * chore: update migration to pluck from registered modules * chore: cleanup * chore: make product an internal service temp * chore: added options and deps to module * chore: added link module options * chore: remove duplicate * chore: added missing column names in core + remove from model * chore: scope migrations to only to create if not exist - money amount, currency --------- Co-authored-by: Sebastian Rindom <skrindom@gmail.com>
This commit is contained in:
7
.changeset/poor-zebras-mate.md
Normal file
7
.changeset/poor-zebras-mate.md
Normal file
@@ -0,0 +1,7 @@
|
||||
---
|
||||
"@medusajs/link-modules": patch
|
||||
"@medusajs/modules-sdk": patch
|
||||
"@medusajs/pricing": patch
|
||||
---
|
||||
|
||||
feat(link-modules,modules-sdk,pricing): Medusa App Migrations + Core compatible migrations
|
||||
@@ -21,6 +21,8 @@ export const ProductVariantPriceSet: ModuleJoinerConfig = {
|
||||
relationships: [
|
||||
{
|
||||
serviceName: Modules.PRODUCT,
|
||||
// TODO: Remove this when product module is the default product service
|
||||
isInternalService: true,
|
||||
primaryKey: "id",
|
||||
foreignKey: "variant_id",
|
||||
alias: "variant",
|
||||
|
||||
@@ -171,9 +171,7 @@ export async function runMigrations(
|
||||
)
|
||||
)
|
||||
|
||||
if (modulesLoadedKeys.includes(serviceKey)) {
|
||||
continue
|
||||
} else if (allLinks.has(serviceKey)) {
|
||||
if (allLinks.has(serviceKey)) {
|
||||
throw new Error(`Link module ${serviceKey} already exists.`)
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
ExternalModuleDeclaration,
|
||||
InternalModuleDeclaration,
|
||||
LoadedModule,
|
||||
LoaderOptions,
|
||||
MODULE_RESOURCE_TYPE,
|
||||
MODULE_SCOPE,
|
||||
ModuleDefinition,
|
||||
@@ -23,6 +24,13 @@ import { RemoteLink } from "./remote-link"
|
||||
import { RemoteQuery } from "./remote-query"
|
||||
import { cleanGraphQLSchema } from "./utils"
|
||||
|
||||
const LinkModulePackage = "@medusajs/link-modules"
|
||||
|
||||
export type RunMigrationFn = (
|
||||
options: Omit<LoaderOptions<ModuleServiceInitializeOptions>, "container">,
|
||||
injectedDependencies?: Record<any, any>
|
||||
) => Promise<void>
|
||||
|
||||
export type MedusaModuleConfig = {
|
||||
[key: string | Modules]:
|
||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||
@@ -51,6 +59,7 @@ export type SharedResources = {
|
||||
|
||||
async function loadModules(modulesConfig, injectedDependencies) {
|
||||
const allModules = {}
|
||||
|
||||
await Promise.all(
|
||||
Object.keys(modulesConfig).map(async (moduleName) => {
|
||||
const mod = modulesConfig[moduleName]
|
||||
@@ -99,16 +108,24 @@ async function loadModules(modulesConfig, injectedDependencies) {
|
||||
return allModules
|
||||
}
|
||||
|
||||
async function initializeLinks(linkModules, injectedDependencies) {
|
||||
async function initializeLinks(config, linkModules, injectedDependencies) {
|
||||
try {
|
||||
const { initialize: initializeLinks } = await import(
|
||||
"@medusajs/link-modules" as string
|
||||
const { initialize, runMigrations } = await import(LinkModulePackage)
|
||||
const linkResolution = await initialize(
|
||||
config,
|
||||
linkModules,
|
||||
injectedDependencies
|
||||
)
|
||||
await initializeLinks({}, linkModules, injectedDependencies)
|
||||
return new RemoteLink()
|
||||
|
||||
return { remoteLink: new RemoteLink(), linkResolution, runMigrations }
|
||||
} catch (err) {
|
||||
console.warn("Error initializing link modules.", err)
|
||||
return undefined
|
||||
|
||||
return {
|
||||
remoteLink: undefined,
|
||||
linkResolution: undefined,
|
||||
runMigrations: undefined,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -166,6 +183,7 @@ export async function MedusaApp(
|
||||
) => Promise<any>
|
||||
entitiesMap?: Record<string, any>
|
||||
notFound?: Record<string, Record<string, string>>
|
||||
runMigrations: RunMigrationFn
|
||||
}> {
|
||||
const modules: MedusaModuleConfig =
|
||||
modulesConfig ??
|
||||
@@ -175,6 +193,7 @@ export async function MedusaApp(
|
||||
process.cwd() + (modulesConfigFileName ?? "/modules-config")
|
||||
)
|
||||
).default
|
||||
|
||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||
"medusa",
|
||||
sharedResourcesConfig as ModuleServiceInitializeOptions,
|
||||
@@ -194,8 +213,25 @@ export async function MedusaApp(
|
||||
})
|
||||
}
|
||||
|
||||
// remove the link module from the modules
|
||||
const linkModule = modules[LinkModulePackage]
|
||||
delete modules[LinkModulePackage]
|
||||
let linkModuleOptions = {}
|
||||
|
||||
if (isObject(linkModule)) {
|
||||
linkModuleOptions = linkModule
|
||||
}
|
||||
|
||||
const allModules = await loadModules(modules, injectedDependencies)
|
||||
const link = await initializeLinks(linkModules, injectedDependencies)
|
||||
const {
|
||||
remoteLink,
|
||||
linkResolution,
|
||||
runMigrations: linkModuleMigration,
|
||||
} = await initializeLinks(
|
||||
linkModuleOptions,
|
||||
linkModules,
|
||||
injectedDependencies
|
||||
)
|
||||
|
||||
const loadedSchema = getLoadedSchema()
|
||||
const { schema, notFound } = cleanAndMergeSchema(loadedSchema)
|
||||
@@ -204,6 +240,7 @@ export async function MedusaApp(
|
||||
servicesConfig,
|
||||
customRemoteFetchData: remoteFetchData,
|
||||
})
|
||||
|
||||
const query = async (
|
||||
query: string | RemoteJoinerQuery | object,
|
||||
variables?: Record<string, unknown>
|
||||
@@ -211,11 +248,27 @@ export async function MedusaApp(
|
||||
return await remoteQuery.query(query, variables)
|
||||
}
|
||||
|
||||
const runMigrations: RunMigrationFn = async (): Promise<void> => {
|
||||
for (const moduleName of Object.keys(allModules)) {
|
||||
const loadedModule = allModules[moduleName]
|
||||
|
||||
await MedusaModule.migrateUp(
|
||||
loadedModule.definition.key,
|
||||
loadedModule.resolutionPath,
|
||||
loadedModule.options
|
||||
)
|
||||
}
|
||||
|
||||
linkModuleMigration &&
|
||||
(await linkModuleMigration(linkResolution.options, injectedDependencies))
|
||||
}
|
||||
|
||||
return {
|
||||
modules: allModules,
|
||||
link,
|
||||
link: remoteLink,
|
||||
query,
|
||||
entitiesMap: schema.getTypeMap(),
|
||||
notFound,
|
||||
runMigrations,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,14 +3,14 @@ import { Migration } from "@mikro-orm/migrations"
|
||||
export class Migration20230929122253 extends Migration {
|
||||
async up(): Promise<void> {
|
||||
this.addSql(
|
||||
'create table "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
|
||||
'create table if not exists "currency" ("code" text not null, "symbol" text not null, "symbol_native" text not null, "name" text not null, constraint "currency_pkey" primary key ("code"));'
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
'create table "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
|
||||
'create table if not exists "money_amount" ("id" text not null, "currency_code" text null, "amount" numeric null, "min_quantity" numeric null, "max_quantity" numeric null, constraint "money_amount_pkey" primary key ("id"));'
|
||||
)
|
||||
this.addSql(
|
||||
'create index "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
|
||||
'create index if not exists "IDX_money_amount_currency_code" on "money_amount" ("currency_code");'
|
||||
)
|
||||
|
||||
this.addSql(
|
||||
|
||||
@@ -13,15 +13,6 @@ class Currency {
|
||||
|
||||
@Property({ columnType: "text" })
|
||||
name: string
|
||||
|
||||
// @Property({ persist: false })
|
||||
// get includes_tax() {
|
||||
// // TODO: This comes from a feature flag
|
||||
// // Figure out how we're handling FF in modules
|
||||
// // For now, returning default as true
|
||||
// // This should also not fall on the hands of the model
|
||||
// return true
|
||||
// }
|
||||
}
|
||||
|
||||
export default Currency
|
||||
|
||||
Reference in New Issue
Block a user