{"version":3,"file":"rename-component-uids.mjs","sources":["../../../../server/src/services/schema-builder/rename-component-uids.ts"],"sourcesContent":["import type { Core } from '@strapi/types';\n\n/**\n * The pivot column of every `*_cmps` join table that stores a component uid\n * (see `getComponentTypeColumn` in `@strapi/core`'s content type to model transform).\n */\nconst COMPONENT_TYPE_COLUMN = 'component_type';\n\n/**\n * The database shapes this module reads, derived from `Core.Strapi` so the\n * Content-Type Builder does not depend on `@strapi/database` directly.\n */\ntype Database = Core.Strapi['db'];\ntype ModelMeta = NonNullable<ReturnType<Database['metadata']['get']>>;\n\n/**\n * The type column of a polymorphic relation (`MorphColumn` in `@strapi/database`).\n */\ntype MorphColumnMeta = { typeColumn?: { name?: string } };\n\n/**\n * A metadata attribute, widened with the join-table and morph keys this module\n * inspects (the metadata union only declares them on some members).\n */\ntype AttributeMeta = ModelMeta['attributes'][string] & {\n  relation?: string;\n  joinTable?: { name?: string; pivotColumns?: string[]; morphColumn?: MorphColumnMeta };\n  morphColumn?: MorphColumnMeta;\n};\n\n/**\n * A table column that stores a model uid, and therefore a component uid when the\n * model is a component.\n */\ninterface UIDColumn {\n  tableName: string;\n  column: string;\n}\n\nexport interface ComponentUIDRename {\n  oldUID: string;\n  newUID: string;\n}\n\n/**\n * Collects the distinct table columns that can hold a component uid:\n * - `component_type` of the `*_cmps` join tables, which every `component` and\n *   `dynamiczone` attribute of a content type or a component points at;\n * - the type column of every polymorphic join table, e.g. `related_type` of upload's\n *   `files_related_mph`, which holds the uid of the component a media field lives in;\n * - the type column a `morphToOne` without join table keeps on its owner's table.\n *\n * Several attributes can share one table (every `media` attribute is the inverse of\n * `plugin::upload.file.related`), so the result is deduplicated.\n */\nconst getComponentUIDColumns = (db: Database): UIDColumn[] => {\n  const columns = new Map<string, UIDColumn>();\n\n  const add = (tableName: string | undefined, column: string | undefined) => {\n    if (tableName && column) {\n      columns.set(`${tableName}.${column}`, { tableName, column });\n    }\n  };\n\n  for (const model of db.metadata.values()) {\n    for (const attribute of Object.values(model.attributes ?? {}) as AttributeMeta[]) {\n      const { joinTable } = attribute;\n\n      if (joinTable?.pivotColumns?.includes(COMPONENT_TYPE_COLUMN)) {\n        add(joinTable.name, COMPONENT_TYPE_COLUMN);\n      }\n\n      if (joinTable?.morphColumn) {\n        add(joinTable.name, joinTable.morphColumn.typeColumn?.name);\n      }\n\n      if (attribute.relation === 'morphToOne' && !joinTable && attribute.morphColumn) {\n        add(model.tableName, attribute.morphColumn.typeColumn?.name);\n      }\n    }\n  }\n\n  return Array.from(columns.values());\n};\n\nexport interface RenameComponentUIDsOptions {\n  /**\n   * Runs after the rows are rewritten and before the transaction commits. If it\n   * rejects, the transaction rolls the rows back and `renameComponentUIDs` rejects\n   * with the same error. Use it for a later step that must succeed together with\n   * the rows (e.g. the `groups.json` folder commit).\n   *\n   * It runs while the transaction holds a connection, so it must not use `db`: with\n   * a pool of one connection (SQLite) a second query would wait forever. Without\n   * renames it still runs, without a transaction.\n   */\n  beforeCommit?: () => Promise<unknown>;\n}\n\n/**\n * Rewrites every stored reference to a renamed component uid (category move or\n * category rename): the `component_type` of the component join rows and the type\n * column of the polymorphic rows, so the entries and their media keep resolving\n * after the reload. Runs in one transaction, which commits only after `beforeCommit`\n * resolves. Must run while `db.metadata` still describes the pre-rename schema: that\n * is what locates the tables.\n */\nexport const renameComponentUIDs = async (\n  db: Database,\n  renames: ComponentUIDRename[],\n  { beforeCommit }: RenameComponentUIDsOptions = {}\n) => {\n  if (renames.length === 0) {\n    await beforeCommit?.();\n    return;\n  }\n\n  const columns = getComponentUIDColumns(db);\n\n  await db.transaction(async ({ trx }) => {\n    for (const { tableName, column } of columns) {\n      for (const { oldUID, newUID } of renames) {\n        await db\n          .getConnection(tableName)\n          .where(column, oldUID)\n          .update({ [column]: newUID })\n          .transacting(trx);\n      }\n    }\n\n    // A rejection here rejects the callback, and `db.transaction` rolls the rows back.\n    await beforeCommit?.();\n  });\n};\n"],"names":["COMPONENT_TYPE_COLUMN","getComponentUIDColumns","db","columns","Map","add","tableName","column","set","model","metadata","values","attribute","Object","attributes","joinTable","pivotColumns","includes","name","morphColumn","typeColumn","relation","Array","from","renameComponentUIDs","renames","beforeCommit","length","transaction","trx","oldUID","newUID","getConnection","where","update","transacting"],"mappings":"AAEA;;;AAGC,IACD,MAAMA,qBAAAA,GAAwB,gBAAA;AAsC9B;;;;;;;;;;IAWA,MAAMC,yBAAyB,CAACC,EAAAA,GAAAA;AAC9B,IAAA,MAAMC,UAAU,IAAIC,GAAAA,EAAAA;IAEpB,MAAMC,GAAAA,GAAM,CAACC,SAAAA,EAA+BC,MAAAA,GAAAA;AAC1C,QAAA,IAAID,aAAaC,MAAAA,EAAQ;AACvBJ,YAAAA,OAAAA,CAAQK,GAAG,CAAC,CAAA,EAAGF,UAAU,CAAC,EAAEC,QAAQ,EAAE;AAAED,gBAAAA,SAAAA;AAAWC,gBAAAA;AAAO,aAAA,CAAA;AAC5D,QAAA;AACF,IAAA,CAAA;AAEA,IAAA,KAAK,MAAME,KAAAA,IAASP,EAAAA,CAAGQ,QAAQ,CAACC,MAAM,EAAA,CAAI;QACxC,KAAK,MAAMC,aAAaC,MAAAA,CAAOF,MAAM,CAACF,KAAAA,CAAMK,UAAU,IAAI,EAAC,CAAA,CAAuB;YAChF,MAAM,EAAEC,SAAS,EAAE,GAAGH,SAAAA;YAEtB,IAAIG,SAAAA,EAAWC,YAAAA,EAAcC,QAAAA,CAASjB,qBAAAA,CAAAA,EAAwB;gBAC5DK,GAAAA,CAAIU,SAAAA,CAAUG,IAAI,EAAElB,qBAAAA,CAAAA;AACtB,YAAA;AAEA,YAAA,IAAIe,WAAWI,WAAAA,EAAa;AAC1Bd,gBAAAA,GAAAA,CAAIU,UAAUG,IAAI,EAAEH,UAAUI,WAAW,CAACC,UAAU,EAAEF,IAAAA,CAAAA;AACxD,YAAA;YAEA,IAAIN,SAAAA,CAAUS,QAAQ,KAAK,YAAA,IAAgB,CAACN,SAAAA,IAAaH,SAAAA,CAAUO,WAAW,EAAE;AA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