From b62af612c7baa244075e546c949b89c4589bd2cf Mon Sep 17 00:00:00 2001 From: Riqwan Thamir Date: Tue, 10 Oct 2023 16:23:38 +0200 Subject: [PATCH] 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 --- .changeset/poor-zebras-mate.md | 7 ++ .../definitions/product-variant-price-set.ts | 2 + packages/link-modules/src/initialize/index.ts | 4 +- packages/modules-sdk/src/medusa-app.ts | 69 ++++++++++++++++--- .../src/migrations/Migration20230929122253.ts | 6 +- packages/pricing/src/models/currency.ts | 9 --- 6 files changed, 74 insertions(+), 23 deletions(-) create mode 100644 .changeset/poor-zebras-mate.md diff --git a/.changeset/poor-zebras-mate.md b/.changeset/poor-zebras-mate.md new file mode 100644 index 0000000000..4cb7b097b1 --- /dev/null +++ b/.changeset/poor-zebras-mate.md @@ -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 diff --git a/packages/link-modules/src/definitions/product-variant-price-set.ts b/packages/link-modules/src/definitions/product-variant-price-set.ts index d121ae6eaf..b95e1d6e0b 100644 --- a/packages/link-modules/src/definitions/product-variant-price-set.ts +++ b/packages/link-modules/src/definitions/product-variant-price-set.ts @@ -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", diff --git a/packages/link-modules/src/initialize/index.ts b/packages/link-modules/src/initialize/index.ts index e928e130b7..28327aacff 100644 --- a/packages/link-modules/src/initialize/index.ts +++ b/packages/link-modules/src/initialize/index.ts @@ -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.`) } diff --git a/packages/modules-sdk/src/medusa-app.ts b/packages/modules-sdk/src/medusa-app.ts index 658bdf7f58..0c779aecdb 100644 --- a/packages/modules-sdk/src/medusa-app.ts +++ b/packages/modules-sdk/src/medusa-app.ts @@ -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, "container">, + injectedDependencies?: Record +) => Promise + export type MedusaModuleConfig = { [key: string | Modules]: | Partial @@ -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 entitiesMap?: Record notFound?: Record> + 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 @@ -211,11 +248,27 @@ export async function MedusaApp( return await remoteQuery.query(query, variables) } + const runMigrations: RunMigrationFn = async (): Promise => { + 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, } } diff --git a/packages/pricing/src/migrations/Migration20230929122253.ts b/packages/pricing/src/migrations/Migration20230929122253.ts index a35ebc4db4..f4d0b843ad 100644 --- a/packages/pricing/src/migrations/Migration20230929122253.ts +++ b/packages/pricing/src/migrations/Migration20230929122253.ts @@ -3,14 +3,14 @@ import { Migration } from "@mikro-orm/migrations" export class Migration20230929122253 extends Migration { async up(): Promise { 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( diff --git a/packages/pricing/src/models/currency.ts b/packages/pricing/src/models/currency.ts index 03a073782f..0bbdf3c794 100644 --- a/packages/pricing/src/models/currency.ts +++ b/packages/pricing/src/models/currency.ts @@ -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