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:
1 parent
57bd38bb4b
commit
b62af612c7
6 files changed
+74
-23
No files matched your search
@@ -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: [
|
relationships: [
|
||||||
{
|
{
|
||||||
serviceName: Modules.PRODUCT,
|
serviceName: Modules.PRODUCT,
|
||||||
|
// TODO: Remove this when product module is the default product service
|
||||||
|
isInternalService: true,
|
||||||
primaryKey: "id",
|
primaryKey: "id",
|
||||||
foreignKey: "variant_id",
|
foreignKey: "variant_id",
|
||||||
alias: "variant",
|
alias: "variant",
|
||||||
|
|||||||
@@ -171,9 +171,7 @@ export async function runMigrations(
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
if (modulesLoadedKeys.includes(serviceKey)) {
|
if (allLinks.has(serviceKey)) {
|
||||||
continue
|
|
||||||
} else if (allLinks.has(serviceKey)) {
|
|
||||||
throw new Error(`Link module ${serviceKey} already exists.`)
|
throw new Error(`Link module ${serviceKey} already exists.`)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
ExternalModuleDeclaration,
|
ExternalModuleDeclaration,
|
||||||
InternalModuleDeclaration,
|
InternalModuleDeclaration,
|
||||||
LoadedModule,
|
LoadedModule,
|
||||||
|
LoaderOptions,
|
||||||
MODULE_RESOURCE_TYPE,
|
MODULE_RESOURCE_TYPE,
|
||||||
MODULE_SCOPE,
|
MODULE_SCOPE,
|
||||||
ModuleDefinition,
|
ModuleDefinition,
|
||||||
@@ -23,6 +24,13 @@ import { RemoteLink } from "./remote-link"
|
|||||||
import { RemoteQuery } from "./remote-query"
|
import { RemoteQuery } from "./remote-query"
|
||||||
import { cleanGraphQLSchema } from "./utils"
|
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 = {
|
export type MedusaModuleConfig = {
|
||||||
[key: string | Modules]:
|
[key: string | Modules]:
|
||||||
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
| Partial<InternalModuleDeclaration | ExternalModuleDeclaration>
|
||||||
@@ -51,6 +59,7 @@ export type SharedResources = {
|
|||||||
|
|
||||||
async function loadModules(modulesConfig, injectedDependencies) {
|
async function loadModules(modulesConfig, injectedDependencies) {
|
||||||
const allModules = {}
|
const allModules = {}
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
Object.keys(modulesConfig).map(async (moduleName) => {
|
Object.keys(modulesConfig).map(async (moduleName) => {
|
||||||
const mod = modulesConfig[moduleName]
|
const mod = modulesConfig[moduleName]
|
||||||
@@ -99,16 +108,24 @@ async function loadModules(modulesConfig, injectedDependencies) {
|
|||||||
return allModules
|
return allModules
|
||||||
}
|
}
|
||||||
|
|
||||||
async function initializeLinks(linkModules, injectedDependencies) {
|
async function initializeLinks(config, linkModules, injectedDependencies) {
|
||||||
try {
|
try {
|
||||||
const { initialize: initializeLinks } = await import(
|
const { initialize, runMigrations } = await import(LinkModulePackage)
|
||||||
"@medusajs/link-modules" as string
|
const linkResolution = await initialize(
|
||||||
|
config,
|
||||||
|
linkModules,
|
||||||
|
injectedDependencies
|
||||||
)
|
)
|
||||||
await initializeLinks({}, linkModules, injectedDependencies)
|
|
||||||
return new RemoteLink()
|
return { remoteLink: new RemoteLink(), linkResolution, runMigrations }
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.warn("Error initializing link modules.", 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>
|
) => Promise<any>
|
||||||
entitiesMap?: Record<string, any>
|
entitiesMap?: Record<string, any>
|
||||||
notFound?: Record<string, Record<string, string>>
|
notFound?: Record<string, Record<string, string>>
|
||||||
|
runMigrations: RunMigrationFn
|
||||||
}> {
|
}> {
|
||||||
const modules: MedusaModuleConfig =
|
const modules: MedusaModuleConfig =
|
||||||
modulesConfig ??
|
modulesConfig ??
|
||||||
@@ -175,6 +193,7 @@ export async function MedusaApp(
|
|||||||
process.cwd() + (modulesConfigFileName ?? "/modules-config")
|
process.cwd() + (modulesConfigFileName ?? "/modules-config")
|
||||||
)
|
)
|
||||||
).default
|
).default
|
||||||
|
|
||||||
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
const dbData = ModulesSdkUtils.loadDatabaseConfig(
|
||||||
"medusa",
|
"medusa",
|
||||||
sharedResourcesConfig as ModuleServiceInitializeOptions,
|
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 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 loadedSchema = getLoadedSchema()
|
||||||
const { schema, notFound } = cleanAndMergeSchema(loadedSchema)
|
const { schema, notFound } = cleanAndMergeSchema(loadedSchema)
|
||||||
@@ -204,6 +240,7 @@ export async function MedusaApp(
|
|||||||
servicesConfig,
|
servicesConfig,
|
||||||
customRemoteFetchData: remoteFetchData,
|
customRemoteFetchData: remoteFetchData,
|
||||||
})
|
})
|
||||||
|
|
||||||
const query = async (
|
const query = async (
|
||||||
query: string | RemoteJoinerQuery | object,
|
query: string | RemoteJoinerQuery | object,
|
||||||
variables?: Record<string, unknown>
|
variables?: Record<string, unknown>
|
||||||
@@ -211,11 +248,27 @@ export async function MedusaApp(
|
|||||||
return await remoteQuery.query(query, variables)
|
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 {
|
return {
|
||||||
modules: allModules,
|
modules: allModules,
|
||||||
link,
|
link: remoteLink,
|
||||||
query,
|
query,
|
||||||
entitiesMap: schema.getTypeMap(),
|
entitiesMap: schema.getTypeMap(),
|
||||||
notFound,
|
notFound,
|
||||||
|
runMigrations,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -3,14 +3,14 @@ import { Migration } from "@mikro-orm/migrations"
|
|||||||
export class Migration20230929122253 extends Migration {
|
export class Migration20230929122253 extends Migration {
|
||||||
async up(): Promise<void> {
|
async up(): Promise<void> {
|
||||||
this.addSql(
|
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(
|
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(
|
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(
|
this.addSql(
|
||||||
|
|||||||
@@ -13,15 +13,6 @@ class Currency {
|
|||||||
|
|
||||||
@Property({ columnType: "text" })
|
@Property({ columnType: "text" })
|
||||||
name: string
|
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
|
export default Currency
|
||||||
Reference in new issue
Block a user