1 import type { MedusaAppLoader } from "@medusajs/framework"
2 import { logger } from "@medusajs/framework/logger"
3 import {
4 ConfigModule,
5 Logger,
6 MedusaContainer,
7 SearchTypes,
8 } from "@medusajs/framework/types"
9 import {
10 ContainerRegistrationKeys,
11 getResolvedPlugins,
12 Modules,
13 } from "@medusajs/framework/utils"
14 import { join } from "path"
15
16 /**
17 * Initiates the database connection
18 */
19 export async function initDb() {
20 const { pgConnectionLoader } = await import("@medusajs/framework")
21
22 const pgConnection = await pgConnectionLoader()
23
24 return pgConnection
25 }
26
27 /**
28 * Migrates the database
29 */
30 export async function migrateDatabase(appLoader: MedusaAppLoader) {
31 try {
32 await appLoader.runModulesMigrations()
33 } catch (err) {
34 logger.error("Something went wrong while running the migrations")
35 throw err
36 }
37 }
38
39 /**
40 * Syncs links with the databse
41 */
42 export async function syncLinks(
43 appLoader: MedusaAppLoader,
44 directory: string,
45 container: MedusaContainer,
46 logger: Logger
47 ) {
48 try {
49 await loadCustomLinks(directory, container)
50
51 const planner = await appLoader.getLinksExecutionPlanner()
52 const actionPlan = await planner.createPlan()
53 actionPlan.forEach((action) => {
54 logger.info(`Sync links: "${action.action}" ${action.tableName}`)
55 })
56 await planner.executePlan(actionPlan)
57 } catch (err) {
58 logger.error("Something went wrong while syncing links")
59 throw err
60 }
61 }
62
63 async function loadCustomLinks(directory: string, container: MedusaContainer) {
64 const configModule = container.resolve(
65 ContainerRegistrationKeys.CONFIG_MODULE
66 )
67 const plugins = await getResolvedPlugins(directory, configModule, true)
68 const linksSourcePaths = plugins.map((plugin) =>
69 join(plugin.resolve, "links")
70 )
71 const logger = container.resolve(ContainerRegistrationKeys.LOGGER)
72
73 const { LinkLoader } = await import("@medusajs/framework")
74 await new LinkLoader(linksSourcePaths, logger).load()
75 }
76
77 /**
78 * Filling the indexes is left to application start (and optional `reindex()`).
79 */
80 export async function migrateSearchIndexes(
81 container: MedusaContainer,
82 logger: Logger
83 ) {
84 const configModule = container.resolve(
85 ContainerRegistrationKeys.CONFIG_MODULE
86 ) as ConfigModule
87
88 // Same optional peer import the runner already uses for `loadSearchIndexes`.
89 const { isSearchModuleEnabled } = require("@medusajs/medusa/loaders/search")
90
91 if (!isSearchModuleEnabled(configModule)) {
92 return
93 }
94
95 try {
96 const searchModule = container.resolve(
97 Modules.SEARCH
98 ) as SearchTypes.ISearchModuleService
99
100 // Drops are left out: a test run starts from a fresh database, so a drop
101 // here could only come from a definition this particular suite happens not
102 // to declare, and deleting another suite's index is never the intent.
103 const plan = (await searchModule.createIndexMigrationPlan()).filter(
104 (action) => action.action !== "drop"
105 )
106 const pending = plan.filter((action) => action.action !== "noop")
107
108 if (!pending.length) {
109 logger.info("Search indexes already up-to-date")
110 return
111 }
112
113 logger.info(
114 `Migrating search indexes: ${pending
115 .map((action) => `${action.index} (${action.action})`)
116 .join(", ")}`
117 )
118
119 await searchModule.executeIndexMigrationPlan(plan)
120 } catch (err) {
121 logger.error("Something went wrong while migrating search indexes")
122 throw err
123 }
124 }