@strapi/content-type-builder 5.55.1 → 5.57.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (152) hide show
  1. package/dist/admin/components/AIChat/lib/transforms/schemas/toCTB.js +117 -13
  2. package/dist/admin/components/AIChat/lib/transforms/schemas/toCTB.js.map +1 -1
  3. package/dist/admin/components/AIChat/lib/transforms/schemas/toCTB.mjs +118 -13
  4. package/dist/admin/components/AIChat/lib/transforms/schemas/toCTB.mjs.map +1 -1
  5. package/dist/admin/components/AIChat/providers/SchemaProvider.js +31 -19
  6. package/dist/admin/components/AIChat/providers/SchemaProvider.js.map +1 -1
  7. package/dist/admin/components/AIChat/providers/SchemaProvider.mjs +31 -19
  8. package/dist/admin/components/AIChat/providers/SchemaProvider.mjs.map +1 -1
  9. package/dist/admin/components/DataManager/DataManagerContext.js.map +1 -1
  10. package/dist/admin/components/DataManager/DataManagerContext.mjs.map +1 -1
  11. package/dist/admin/components/DataManager/DataManagerProvider.js +166 -6
  12. package/dist/admin/components/DataManager/DataManagerProvider.js.map +1 -1
  13. package/dist/admin/components/DataManager/DataManagerProvider.mjs +165 -7
  14. package/dist/admin/components/DataManager/DataManagerProvider.mjs.map +1 -1
  15. package/dist/admin/components/DataManager/RenameMigrationModal.js +285 -0
  16. package/dist/admin/components/DataManager/RenameMigrationModal.js.map +1 -0
  17. package/dist/admin/components/DataManager/RenameMigrationModal.mjs +257 -0
  18. package/dist/admin/components/DataManager/RenameMigrationModal.mjs.map +1 -0
  19. package/dist/admin/components/DataManager/reducer.js +107 -2
  20. package/dist/admin/components/DataManager/reducer.js.map +1 -1
  21. package/dist/admin/components/DataManager/reducer.mjs +106 -2
  22. package/dist/admin/components/DataManager/reducer.mjs.map +1 -1
  23. package/dist/admin/components/DataManager/utils/cleanData.js +8 -1
  24. package/dist/admin/components/DataManager/utils/cleanData.js.map +1 -1
  25. package/dist/admin/components/DataManager/utils/cleanData.mjs +8 -1
  26. package/dist/admin/components/DataManager/utils/cleanData.mjs.map +1 -1
  27. package/dist/admin/components/DataManager/utils/groupRenameChains.js +86 -0
  28. package/dist/admin/components/DataManager/utils/groupRenameChains.js.map +1 -0
  29. package/dist/admin/components/DataManager/utils/groupRenameChains.mjs +84 -0
  30. package/dist/admin/components/DataManager/utils/groupRenameChains.mjs.map +1 -0
  31. package/dist/admin/components/DataManager/utils/isStorageCompatibleRename.js +29 -0
  32. package/dist/admin/components/DataManager/utils/isStorageCompatibleRename.js.map +1 -0
  33. package/dist/admin/components/DataManager/utils/isStorageCompatibleRename.mjs +27 -0
  34. package/dist/admin/components/DataManager/utils/isStorageCompatibleRename.mjs.map +1 -0
  35. package/dist/admin/components/DataManager/utils/resolveAfterEditRenameConsent.js +35 -0
  36. package/dist/admin/components/DataManager/utils/resolveAfterEditRenameConsent.js.map +1 -0
  37. package/dist/admin/components/DataManager/utils/resolveAfterEditRenameConsent.mjs +33 -0
  38. package/dist/admin/components/DataManager/utils/resolveAfterEditRenameConsent.mjs.map +1 -0
  39. package/dist/admin/components/DataManager/utils/splitChainsByInheritedConsent.js +48 -0
  40. package/dist/admin/components/DataManager/utils/splitChainsByInheritedConsent.js.map +1 -0
  41. package/dist/admin/components/DataManager/utils/splitChainsByInheritedConsent.mjs +45 -0
  42. package/dist/admin/components/DataManager/utils/splitChainsByInheritedConsent.mjs.map +1 -0
  43. package/dist/admin/components/FormModal/FormModal.js +143 -9
  44. package/dist/admin/components/FormModal/FormModal.js.map +1 -1
  45. package/dist/admin/components/FormModal/FormModal.mjs +144 -10
  46. package/dist/admin/components/FormModal/FormModal.mjs.map +1 -1
  47. package/dist/admin/components/FormModal/attributes/validation/common.js +6 -5
  48. package/dist/admin/components/FormModal/attributes/validation/common.js.map +1 -1
  49. package/dist/admin/components/FormModal/attributes/validation/common.mjs +1 -1
  50. package/dist/admin/components/FormModal/attributes/validation/common.mjs.map +1 -1
  51. package/dist/admin/components/FormModal/component/createComponentSchema.js +10 -7
  52. package/dist/admin/components/FormModal/component/createComponentSchema.js.map +1 -1
  53. package/dist/admin/components/FormModal/component/createComponentSchema.mjs +1 -1
  54. package/dist/admin/components/FormModal/component/createComponentSchema.mjs.map +1 -1
  55. package/dist/admin/components/FormModal/contentType/createContentTypeSchema.js +24 -21
  56. package/dist/admin/components/FormModal/contentType/createContentTypeSchema.js.map +1 -1
  57. package/dist/admin/components/FormModal/contentType/createContentTypeSchema.mjs +1 -1
  58. package/dist/admin/components/FormModal/contentType/createContentTypeSchema.mjs.map +1 -1
  59. package/dist/admin/components/FormModal/forms/utils/createCollectionName.js +3 -2
  60. package/dist/admin/components/FormModal/forms/utils/createCollectionName.js.map +1 -1
  61. package/dist/admin/components/FormModal/forms/utils/createCollectionName.mjs +1 -1
  62. package/dist/admin/components/FormModal/forms/utils/createCollectionName.mjs.map +1 -1
  63. package/dist/admin/components/FormModal/utils/getRenameStorageChange.js +73 -0
  64. package/dist/admin/components/FormModal/utils/getRenameStorageChange.js.map +1 -0
  65. package/dist/admin/components/FormModal/utils/getRenameStorageChange.mjs +70 -0
  66. package/dist/admin/components/FormModal/utils/getRenameStorageChange.mjs.map +1 -0
  67. package/dist/admin/src/components/AIChat/components/AnimatedBox.d.ts +2 -2
  68. package/dist/admin/src/components/AIChat/components/Input.d.ts +2 -2
  69. package/dist/admin/src/components/AIChat/lib/transforms/schemas/toCTB.d.ts +4 -3
  70. package/dist/admin/src/components/AIChat/lib/types/schema.d.ts +6 -1
  71. package/dist/admin/src/components/AttributeOptions/OptionBoxWrapper.d.ts +2 -2
  72. package/dist/admin/src/components/ComponentRow.d.ts +2 -2
  73. package/dist/admin/src/components/DataManager/DataManagerContext.d.ts +28 -2
  74. package/dist/admin/src/components/DataManager/RenameMigrationModal.d.ts +73 -0
  75. package/dist/admin/src/components/DataManager/reducer.d.ts +17 -2
  76. package/dist/admin/src/components/DataManager/utils/cleanData.d.ts +16 -0
  77. package/dist/admin/src/components/DataManager/utils/groupRenameChains.d.ts +31 -0
  78. package/dist/admin/src/components/DataManager/utils/isStorageCompatibleRename.d.ts +10 -0
  79. package/dist/admin/src/components/DataManager/utils/resolveAfterEditRenameConsent.d.ts +24 -0
  80. package/dist/admin/src/components/DataManager/utils/splitChainsByInheritedConsent.d.ts +26 -0
  81. package/dist/admin/src/components/FormModal/utils/getRenameStorageChange.d.ts +40 -0
  82. package/dist/admin/src/components/List.d.ts +2 -2
  83. package/dist/admin/src/components/Relation/RelationNaturePicker/Components.d.ts +4 -4
  84. package/dist/admin/src/types.d.ts +23 -0
  85. package/dist/admin/translations/en.json.js +12 -0
  86. package/dist/admin/translations/en.json.js.map +1 -1
  87. package/dist/admin/translations/en.json.mjs +12 -0
  88. package/dist/admin/translations/en.json.mjs.map +1 -1
  89. package/dist/server/config.js +24 -3
  90. package/dist/server/config.js.map +1 -1
  91. package/dist/server/config.mjs +21 -3
  92. package/dist/server/config.mjs.map +1 -1
  93. package/dist/server/controllers/schema.js +2 -2
  94. package/dist/server/controllers/schema.js.map +1 -1
  95. package/dist/server/controllers/schema.mjs +1 -1
  96. package/dist/server/controllers/schema.mjs.map +1 -1
  97. package/dist/server/controllers/validation/content-type.js +9 -11
  98. package/dist/server/controllers/validation/content-type.js.map +1 -1
  99. package/dist/server/controllers/validation/content-type.mjs +7 -9
  100. package/dist/server/controllers/validation/content-type.mjs.map +1 -1
  101. package/dist/server/controllers/validation/model-schema.js +2 -3
  102. package/dist/server/controllers/validation/model-schema.js.map +1 -1
  103. package/dist/server/controllers/validation/model-schema.mjs +1 -2
  104. package/dist/server/controllers/validation/model-schema.mjs.map +1 -1
  105. package/dist/server/controllers/validation/relations.js +1 -2
  106. package/dist/server/controllers/validation/relations.js.map +1 -1
  107. package/dist/server/controllers/validation/relations.mjs +1 -2
  108. package/dist/server/controllers/validation/relations.mjs.map +1 -1
  109. package/dist/server/controllers/validation/schema.js +42 -23
  110. package/dist/server/controllers/validation/schema.js.map +1 -1
  111. package/dist/server/controllers/validation/schema.mjs +43 -24
  112. package/dist/server/controllers/validation/schema.mjs.map +1 -1
  113. package/dist/server/index.js +1 -1
  114. package/dist/server/index.js.map +1 -1
  115. package/dist/server/services/content-types.js +1 -2
  116. package/dist/server/services/content-types.js.map +1 -1
  117. package/dist/server/services/content-types.mjs +2 -3
  118. package/dist/server/services/content-types.mjs.map +1 -1
  119. package/dist/server/services/migration-builder/index.js +580 -0
  120. package/dist/server/services/migration-builder/index.js.map +1 -0
  121. package/dist/server/services/migration-builder/index.mjs +573 -0
  122. package/dist/server/services/migration-builder/index.mjs.map +1 -0
  123. package/dist/server/services/schema-builder/content-type-builder.js +31 -1
  124. package/dist/server/services/schema-builder/content-type-builder.js.map +1 -1
  125. package/dist/server/services/schema-builder/content-type-builder.mjs +31 -1
  126. package/dist/server/services/schema-builder/content-type-builder.mjs.map +1 -1
  127. package/dist/server/services/schema-mutation.js +30 -2
  128. package/dist/server/services/schema-mutation.js.map +1 -1
  129. package/dist/server/services/schema-mutation.mjs +10 -2
  130. package/dist/server/services/schema-mutation.mjs.map +1 -1
  131. package/dist/server/services/schema.js +259 -15
  132. package/dist/server/services/schema.js.map +1 -1
  133. package/dist/server/services/schema.mjs +252 -9
  134. package/dist/server/services/schema.mjs.map +1 -1
  135. package/dist/server/src/config.d.ts +23 -2
  136. package/dist/server/src/config.d.ts.map +1 -1
  137. package/dist/server/src/controllers/validation/content-type.d.ts +0 -1
  138. package/dist/server/src/controllers/validation/content-type.d.ts.map +1 -1
  139. package/dist/server/src/controllers/validation/relations.d.ts.map +1 -1
  140. package/dist/server/src/controllers/validation/schema.d.ts +28 -0
  141. package/dist/server/src/controllers/validation/schema.d.ts.map +1 -1
  142. package/dist/server/src/index.d.ts +10 -2
  143. package/dist/server/src/index.d.ts.map +1 -1
  144. package/dist/server/src/services/content-types.d.ts.map +1 -1
  145. package/dist/server/src/services/migration-builder/index.d.ts +83 -0
  146. package/dist/server/src/services/migration-builder/index.d.ts.map +1 -0
  147. package/dist/server/src/services/schema-builder/content-type-builder.d.ts.map +1 -1
  148. package/dist/server/src/services/schema-mutation.d.ts +4 -2
  149. package/dist/server/src/services/schema-mutation.d.ts.map +1 -1
  150. package/dist/server/src/services/schema.d.ts +19 -0
  151. package/dist/server/src/services/schema.d.ts.map +1 -1
  152. package/package.json +11 -10
@@ -0,0 +1,573 @@
1
+ import path from 'node:path';
2
+ import { contentTypes } from '@strapi/utils';
3
+
4
+ /**
5
+ * Whether a relation definition has a counterpart on its target. The live
6
+ * schema carries `inversedBy` / `mappedBy`; the CTB payload carries
7
+ * `targetAttribute`.
8
+ */ const isBidirectionalRelation = (attribute)=>Boolean(attribute.inversedBy || attribute.mappedBy || attribute.targetAttribute);
9
+ /**
10
+ * Whether the storage of `oldAttribute` can host `newAttribute` after a plain
11
+ * rename of its physical artifact. Anything else must go through the legacy
12
+ * drop-and-recreate path, so the hop is reported as `type-changed`.
13
+ *
14
+ * `resolveType` maps a definition to its storage type. The builder passes one
15
+ * that resolves custom fields (sent as `type: 'customField'` by the admin, but
16
+ * already converted to their underlying type in the live schema).
17
+ */ const isCompatibleRename = (oldAttribute, newAttribute, resolveType = (attribute)=>attribute.type)=>{
18
+ const oldType = oldAttribute ? resolveType(oldAttribute) : undefined;
19
+ const newType = newAttribute ? resolveType(newAttribute) : undefined;
20
+ // Without both definitions there is nothing to compare; the metadata-based
21
+ // classification is the only guard we have.
22
+ if (!oldAttribute || !newAttribute || !oldType || !newType) {
23
+ return true;
24
+ }
25
+ if (oldType !== newType) {
26
+ return false;
27
+ }
28
+ // A different custom field on the same underlying type is still a different
29
+ // field.
30
+ if ((oldAttribute.customField || newAttribute.customField) && oldAttribute.customField !== newAttribute.customField) {
31
+ return false;
32
+ }
33
+ switch(newType){
34
+ case 'relation':
35
+ // `manyWay` and a bidirectional `oneToMany` share `relation: 'oneToMany'`,
36
+ // but switching between them moves join-table ownership.
37
+ return oldAttribute.relation === newAttribute.relation && oldAttribute.target === newAttribute.target && isBidirectionalRelation(oldAttribute) === isBidirectionalRelation(newAttribute);
38
+ case 'component':
39
+ return oldAttribute.component === newAttribute.component && Boolean(oldAttribute.repeatable) === Boolean(newAttribute.repeatable);
40
+ default:
41
+ return true;
42
+ }
43
+ };
44
+ const MIGRATION_NAME = 'rename-fields';
45
+ const createMigrationBuilder = ({ strapi })=>{
46
+ const { db } = strapi;
47
+ const migrationFileBuilder = db.migrations.createFileBuilder();
48
+ const unsupported = [];
49
+ // Physical-name rules shared with the metadata loader, so the *new* side of a
50
+ // rename is derived exactly as `db.metadata` will derive it after the reload.
51
+ const { naming } = db.metadata;
52
+ /**
53
+ * The app's *source* migrations dir. Generated files always land here (see
54
+ * `writeFiles`), and it is also the dir discovery reads from when
55
+ * `useTypescriptMigrations` is off or no tsconfig `outDir` resolves.
56
+ */ const sourceMigrationsDir = ()=>path.join(strapi.dirs.app.root, 'database', 'migrations');
57
+ let format;
58
+ /**
59
+ * Mirror the database's discovery decision (`Strapi.ts`): it reads from
60
+ * `<outDir>/database/migrations` only when `useTypescriptMigrations` is on
61
+ * *and* a tsconfig `outDir` resolves. In that case the source file must be
62
+ * `.ts` so `tsc` carries it into `dist`. Otherwise discovery reads the source
63
+ * dir and only loads `.js`, so a `.ts` file would never run.
64
+ *
65
+ * Memoised so the fallback warning is logged once per builder (both `build`
66
+ * and `writeFiles` call this).
67
+ */ const getFormat = ()=>{
68
+ if (format) {
69
+ return format;
70
+ }
71
+ const useTSM = strapi.config.get('database.settings.useTypescriptMigrations') === true;
72
+ const discoveryDir = db.config.settings.migrations?.dir;
73
+ const discoversFromDist = typeof discoveryDir === 'string' && path.resolve(discoveryDir) !== path.resolve(sourceMigrationsDir());
74
+ if (useTSM && !discoversFromDist) {
75
+ strapi.log.warn('database.settings.useTypescriptMigrations is enabled but no TypeScript outDir was resolved for this app; ' + 'writing the rename migration as JavaScript so it is discovered from database/migrations.');
76
+ }
77
+ format = useTSM && discoversFromDist ? 'typescript' : 'javascript';
78
+ return format;
79
+ };
80
+ // Artifacts "in flight" for the current save, i.e. produced by an earlier
81
+ // rename hop in this same batch (e.g. the intermediate `tmp` field a user
82
+ // routes a swap through). Keyed per uid by the *logical* field name the hop
83
+ // produced; the value records how that field resolves to a physical artifact
84
+ // and its current `from` identifier, so a continuation hop replays from the
85
+ // right place instead of being treated as a brand-new, unknown attribute.
86
+ const inFlight = new Map();
87
+ // CTB-level definition of each in-flight field (per uid, by logical name), so
88
+ // a continuation hop can still be checked for a storage change against the
89
+ // definition the chain started from.
90
+ const inFlightDefinitions = new Map();
91
+ // Logical names vacated by an earlier accepted hop of this batch (per uid). A
92
+ // refused hop never vacates its source: the artifact stays live until schema
93
+ // sync drops it. A hop may only target a name that is free: not live in the
94
+ // pre-reload schema (unless an earlier accepted hop vacated it) and not
95
+ // produced by an earlier hop still in flight. At migration time the schema-sync drop of a still-live field has
96
+ // not happened yet, so renaming onto it would collide and the runtime guard
97
+ // would silently skip the hop. Refusing it here surfaces the problem (e.g. a
98
+ // truncated chain sent by a client) instead of losing data.
99
+ const vacated = new Map();
100
+ const isTargetOccupied = (uid, newName)=>{
101
+ if (inFlight.get(uid)?.has(newName)) {
102
+ return true;
103
+ }
104
+ return Boolean(schemaAttributeOf(uid, newName)) && !(vacated.get(uid)?.has(newName) ?? false);
105
+ };
106
+ const trackVacated = (uid, oldName, newName)=>{
107
+ let names = vacated.get(uid);
108
+ if (!names) {
109
+ names = new Set();
110
+ vacated.set(uid, names);
111
+ }
112
+ names.add(oldName);
113
+ // The target is occupied again by the in-flight field.
114
+ names.delete(newName);
115
+ };
116
+ const markInFlight = (uid, name, resolved)=>{
117
+ let entries = inFlight.get(uid);
118
+ if (!entries) {
119
+ entries = new Map();
120
+ inFlight.set(uid, entries);
121
+ }
122
+ entries.set(name, resolved);
123
+ };
124
+ // Per uid, logical origin name -> current name, folded over every received hop.
125
+ const origins = new Map();
126
+ const trackOrigin = (uid, oldName, newName)=>{
127
+ let byOrigin = origins.get(uid);
128
+ if (!byOrigin) {
129
+ byOrigin = new Map();
130
+ origins.set(uid, byOrigin);
131
+ }
132
+ for (const [origin, current] of byOrigin){
133
+ if (current === oldName) {
134
+ byOrigin.set(origin, newName);
135
+ return;
136
+ }
137
+ }
138
+ // A name that is already an origin but no longer current was re-occupied by
139
+ // something this save did not rename; do not overwrite its logical field.
140
+ if (!byOrigin.has(oldName)) {
141
+ byOrigin.set(oldName, newName);
142
+ }
143
+ };
144
+ const isMorphRelation = (attribute)=>typeof attribute.relation === 'string' && attribute.relation.startsWith('morph');
145
+ const schemaModelOf = (uid)=>{
146
+ const contentTypes = strapi.contentTypes;
147
+ const components = strapi.components;
148
+ return contentTypes?.[uid] ?? components?.[uid];
149
+ };
150
+ const schemaAttributeOf = (uid, name)=>schemaModelOf(uid)?.attributes?.[name];
151
+ /**
152
+ * The type an attribute is stored as. Custom fields are sent by the admin as
153
+ * `type: 'customField'`, while the live schema already carries their
154
+ * underlying type, so both sides are resolved through the registry.
155
+ */ const storageTypeOf = (attribute)=>{
156
+ if (attribute.type !== 'customField' && typeof attribute.customField !== 'string') {
157
+ return attribute.type;
158
+ }
159
+ try {
160
+ const customField = strapi.get('custom-fields').get(attribute.customField);
161
+ return customField?.type ?? attribute.type;
162
+ } catch {
163
+ // Unknown custom field (e.g. its plugin was removed).
164
+ return attribute.type;
165
+ }
166
+ };
167
+ const schemaTypeOf = (uid, name)=>schemaAttributeOf(uid, name)?.type;
168
+ // Resolves the shared upload morph table (e.g. `files_related_morphs`) and its
169
+ // `related_type` column from the file model's `related` morph attribute. This
170
+ // is the single table every media field across all types writes to, keyed by
171
+ // (`field` = attribute name, `related_type` = owning uid).
172
+ const resolveUploadMorphTable = ()=>{
173
+ if (!db.metadata.has('plugin::upload.file')) {
174
+ return undefined;
175
+ }
176
+ const fileMeta = db.metadata.get('plugin::upload.file');
177
+ const related = fileMeta.attributes?.related;
178
+ const joinTable = related?.joinTable;
179
+ const morphColumn = joinTable?.morphColumn ?? related?.morphColumn;
180
+ if (!joinTable?.name || !morphColumn?.typeColumn?.name) {
181
+ return undefined;
182
+ }
183
+ return {
184
+ table: joinTable.name,
185
+ fieldColumn: db.metadata.identifiers.FIELD_COLUMN,
186
+ typeColumn: morphColumn.typeColumn.name
187
+ };
188
+ };
189
+ /**
190
+ * Classifies the *first* hop of a rename by inspecting the live metadata, which
191
+ * still reflects the pre-rename schema. Continuation hops never reach here —
192
+ * they inherit their classification from the in-flight map.
193
+ */ const classifyMedia = (uid, oldName, joinTable, upload)=>{
194
+ // Media: the attribute name is a value in the shared `files_related_morphs`
195
+ // `field` column, scoped by `related_type` (the owning uid). Detect it by
196
+ // schema type OR by the join table being the shared upload table — the
197
+ // latter guard is critical so a media field can never fall through to the
198
+ // unscoped component branch and corrupt other types' media on the shared
199
+ // table.
200
+ const isMedia = schemaTypeOf(uid, oldName) === 'media' || !!upload && joinTable?.name === upload.table;
201
+ if (!isMedia) {
202
+ return undefined;
203
+ }
204
+ if (!upload) {
205
+ return {
206
+ kind: 'unsupported',
207
+ reason: 'unsupported-type'
208
+ };
209
+ }
210
+ return {
211
+ kind: 'media',
212
+ table: upload.table,
213
+ fieldColumn: upload.fieldColumn,
214
+ typeColumn: upload.typeColumn,
215
+ typeValue: uid,
216
+ from: oldName
217
+ };
218
+ };
219
+ const classifyComponent = (oldName, joinTable)=>{
220
+ const on = joinTable?.on;
221
+ if (!joinTable?.name || !on || typeof on !== 'object') {
222
+ return undefined;
223
+ }
224
+ const fieldColumn = Object.keys(on).find((key)=>on[key] === oldName);
225
+ if (!fieldColumn) {
226
+ return undefined;
227
+ }
228
+ return {
229
+ kind: 'component',
230
+ table: joinTable.name,
231
+ fieldColumn,
232
+ from: oldName
233
+ };
234
+ };
235
+ const classifyRelation = (meta, attribute, joinTable)=>{
236
+ // Polymorphic morph relations need shared-table handling and are not
237
+ // creatable through the CTB UI, so they are left unsupported.
238
+ if (isMorphRelation(attribute) || attribute.morphColumn) {
239
+ return {
240
+ kind: 'unsupported',
241
+ reason: 'unsupported-type'
242
+ };
243
+ }
244
+ // Join column on the owner's own table (`<field>_id`), e.g. useJoinTable:false.
245
+ // The inverse side of a bidirectional join-column relation also carries a
246
+ // `joinColumn` (pointing at `id`), but owns no column.
247
+ if (attribute.joinColumn?.name && !joinTable) {
248
+ if (attribute.mappedBy) {
249
+ return {
250
+ kind: 'skip'
251
+ };
252
+ }
253
+ return {
254
+ kind: 'joinColumn',
255
+ table: meta.tableName,
256
+ from: attribute.joinColumn.name
257
+ };
258
+ }
259
+ if (joinTable?.name) {
260
+ // The inverse side of a bidirectional relation: the join table is named
261
+ // from the owning attribute, so renaming this side touches no artifact.
262
+ if (attribute.mappedBy) {
263
+ return {
264
+ kind: 'skip'
265
+ };
266
+ }
267
+ return {
268
+ kind: 'joinTable',
269
+ from: joinTable.name
270
+ };
271
+ }
272
+ return {
273
+ kind: 'unsupported',
274
+ reason: 'unsupported-type'
275
+ };
276
+ };
277
+ /**
278
+ * Whether the schema lets users rename this attribute at all. System
279
+ * attributes (`documentId`, `createdBy`, `locale`, `publishedAt`, review
280
+ * workflow stages…) are also in the metadata and would otherwise be renamed
281
+ * away, only for sync to re-add them empty.
282
+ */ const isRenamableAttribute = (uid, name)=>{
283
+ const schemaAttribute = schemaAttributeOf(uid, name);
284
+ // Same rule as payload validation applies to a hop's old name: `status` is
285
+ // only reserved with draft and publish, and a legacy field may carry it.
286
+ if (schemaAttribute?.configurable === false || contentTypes.isReservedAttributeName(name, {
287
+ draftAndPublish: false
288
+ })) {
289
+ return false;
290
+ }
291
+ const model = schemaModelOf(uid);
292
+ return !contentTypes.getNonVisibleAttributes(model).includes(name);
293
+ };
294
+ const classify = (uid, oldName)=>{
295
+ if (!schemaAttributeOf(uid, oldName)) {
296
+ return {
297
+ kind: 'unsupported',
298
+ reason: 'attribute-not-found'
299
+ };
300
+ }
301
+ if (!isRenamableAttribute(uid, oldName)) {
302
+ return {
303
+ kind: 'unsupported',
304
+ reason: 'unsupported-type'
305
+ };
306
+ }
307
+ const meta = db.metadata.get(uid);
308
+ const attribute = meta.attributes?.[oldName];
309
+ if (!attribute) {
310
+ return {
311
+ kind: 'unsupported',
312
+ reason: 'attribute-not-found'
313
+ };
314
+ }
315
+ const joinTable = attribute.joinTable;
316
+ const upload = resolveUploadMorphTable();
317
+ const media = classifyMedia(uid, oldName, joinTable, upload);
318
+ if (media) {
319
+ return media;
320
+ }
321
+ const component = classifyComponent(oldName, joinTable);
322
+ if (component) {
323
+ return component;
324
+ }
325
+ if (attribute.type === 'relation') {
326
+ return classifyRelation(meta, attribute, joinTable);
327
+ }
328
+ // Plain scalar column on the model's own table.
329
+ if ('columnName' in attribute && attribute.columnName) {
330
+ return {
331
+ kind: 'scalarColumn',
332
+ table: meta.tableName,
333
+ from: attribute.columnName
334
+ };
335
+ }
336
+ return {
337
+ kind: 'unsupported',
338
+ reason: 'unsupported-type'
339
+ };
340
+ };
341
+ /**
342
+ * Computes the destination identifier for a resolved artifact given the new
343
+ * field name, and the next in-flight state (with `from` advanced to `to`).
344
+ */ const advance = (uid, resolved, oldName, newName)=>{
345
+ const meta = db.metadata.get(uid);
346
+ const comment = `${uid}: rename field "${oldName}" -> "${newName}"`;
347
+ switch(resolved.kind){
348
+ case 'scalarColumn':
349
+ case 'joinColumn':
350
+ {
351
+ const to = resolved.kind === 'joinColumn' ? naming.joinColumnName(newName) : naming.columnName(newName);
352
+ if (resolved.from !== to) {
353
+ migrationFileBuilder.renameColumn({
354
+ table: resolved.table,
355
+ from: resolved.from,
356
+ to,
357
+ comment
358
+ });
359
+ }
360
+ return {
361
+ ...resolved,
362
+ from: to
363
+ };
364
+ }
365
+ case 'joinTable':
366
+ {
367
+ const to = naming.joinTableName(meta.tableName, newName);
368
+ if (resolved.from !== to) {
369
+ migrationFileBuilder.renameTable({
370
+ from: resolved.from,
371
+ to,
372
+ comment
373
+ });
374
+ }
375
+ return {
376
+ ...resolved,
377
+ from: to
378
+ };
379
+ }
380
+ case 'component':
381
+ {
382
+ const to = newName;
383
+ if (resolved.from !== to) {
384
+ migrationFileBuilder.updateRows({
385
+ table: resolved.table,
386
+ guardColumn: resolved.fieldColumn,
387
+ where: {
388
+ [resolved.fieldColumn]: resolved.from
389
+ },
390
+ set: {
391
+ [resolved.fieldColumn]: to
392
+ },
393
+ comment
394
+ });
395
+ }
396
+ return {
397
+ ...resolved,
398
+ from: to
399
+ };
400
+ }
401
+ case 'media':
402
+ {
403
+ const to = newName;
404
+ if (resolved.from !== to) {
405
+ migrationFileBuilder.updateRows({
406
+ table: resolved.table,
407
+ guardColumn: resolved.fieldColumn,
408
+ where: {
409
+ [resolved.fieldColumn]: resolved.from,
410
+ [resolved.typeColumn]: resolved.typeValue
411
+ },
412
+ set: {
413
+ [resolved.fieldColumn]: to
414
+ },
415
+ comment
416
+ });
417
+ }
418
+ return {
419
+ ...resolved,
420
+ from: to
421
+ };
422
+ }
423
+ default:
424
+ return resolved;
425
+ }
426
+ };
427
+ /**
428
+ * Records one rename hop exactly in the order the user performed it. Because the
429
+ * Content-Type Builder forbids two fields sharing a name at any instant, the
430
+ * recorded path is inherently collision-free: replaying each hop verbatim never
431
+ * targets an occupied artifact, so no synthetic temporary name is ever needed
432
+ * (a "swap" is already expressed by the user's own intermediate-name hop).
433
+ */ const addRename = (uid, { oldName, newName, newAttribute })=>{
434
+ // `metadata.get` throws when the model is unknown, so guard with `has` first.
435
+ if (!db.metadata.has(uid)) {
436
+ unsupported.push({
437
+ uid,
438
+ oldName,
439
+ newName,
440
+ reason: 'model-not-found'
441
+ });
442
+ return;
443
+ }
444
+ trackOrigin(uid, oldName, newName);
445
+ if (isTargetOccupied(uid, newName)) {
446
+ unsupported.push({
447
+ uid,
448
+ oldName,
449
+ newName,
450
+ reason: 'target-occupied'
451
+ });
452
+ // Keep the chain consistent so a later continuation hop stays silent. The
453
+ // source is not vacated: its artifact was not moved.
454
+ markInFlight(uid, newName, {
455
+ kind: 'unsupported',
456
+ reason: 'target-occupied'
457
+ });
458
+ return;
459
+ }
460
+ const entries = inFlight.get(uid);
461
+ const definitions = inFlightDefinitions.get(uid);
462
+ const oldAttribute = definitions?.get(oldName) ?? schemaAttributeOf(uid, oldName);
463
+ let resolved = entries?.get(oldName) ?? classify(uid, oldName);
464
+ // A rename that also changes the attribute's storage cannot be expressed as a
465
+ // rename of the physical artifact: leave it to the drop-and-recreate path.
466
+ if (resolved.kind !== 'unsupported' && !isCompatibleRename(oldAttribute, newAttribute, storageTypeOf)) {
467
+ resolved = {
468
+ kind: 'unsupported',
469
+ reason: 'type-changed'
470
+ };
471
+ }
472
+ if (newAttribute ?? oldAttribute) {
473
+ let byName = inFlightDefinitions.get(uid);
474
+ if (!byName) {
475
+ byName = new Map();
476
+ inFlightDefinitions.set(uid, byName);
477
+ }
478
+ byName.set(newName, newAttribute ?? oldAttribute);
479
+ }
480
+ if (resolved.kind === 'unsupported') {
481
+ unsupported.push({
482
+ uid,
483
+ oldName,
484
+ newName,
485
+ reason: resolved.reason
486
+ });
487
+ // Keep the chain consistent so a later continuation hop stays silent. The
488
+ // source keeps its in-flight entry and is not vacated: its physical
489
+ // artifact stays where it is until schema sync drops it.
490
+ markInFlight(uid, newName, resolved);
491
+ return;
492
+ }
493
+ // Accepted from here on: the source name is free and the field moves.
494
+ entries?.delete(oldName);
495
+ definitions?.delete(oldName);
496
+ trackVacated(uid, oldName, newName);
497
+ if (resolved.kind === 'skip') {
498
+ markInFlight(uid, newName, resolved);
499
+ return;
500
+ }
501
+ const next = advance(uid, resolved, oldName, newName);
502
+ markInFlight(uid, newName, next);
503
+ };
504
+ return {
505
+ addRenameAttribute (uid, names) {
506
+ addRename(uid, names);
507
+ },
508
+ /**
509
+ * The composed `origin -> final` name of every schema attribute that
510
+ * received hops in this save, per uid. Identity entries (`a -> b -> a`) and
511
+ * origins that are not schema attributes are left out.
512
+ */ attributeRenamesMapping () {
513
+ const mapping = {};
514
+ for (const [uid, byOrigin] of origins){
515
+ for (const [origin, final] of byOrigin){
516
+ var _mapping, _uid;
517
+ if (origin === final || !schemaAttributeOf(uid, origin)) {
518
+ continue;
519
+ }
520
+ (_mapping = mapping)[_uid = uid] ?? (_mapping[_uid] = {});
521
+ mapping[uid][origin] = final;
522
+ }
523
+ }
524
+ return mapping;
525
+ },
526
+ /**
527
+ * Adds the logical op that carries this save's renames to the stores keyed
528
+ * by attribute name (admin field permissions, via handlers registered on
529
+ * the database). Call it after every hop so it is the last operation.
530
+ */ addAttributeRenames (renames) {
531
+ if (Object.keys(renames).length === 0) {
532
+ return;
533
+ }
534
+ migrationFileBuilder.attributeRenames({
535
+ renames,
536
+ comment: "Update stores keyed by attribute name (admin field permissions) for this save's renames"
537
+ });
538
+ },
539
+ hasChanges () {
540
+ return migrationFileBuilder.hasChanges();
541
+ },
542
+ getUnsupported () {
543
+ return [
544
+ ...unsupported
545
+ ];
546
+ },
547
+ getOperations () {
548
+ return migrationFileBuilder.getOperations();
549
+ },
550
+ build () {
551
+ return migrationFileBuilder.build({
552
+ name: MIGRATION_NAME,
553
+ format: getFormat()
554
+ });
555
+ },
556
+ async writeFiles () {
557
+ // Always write to the app's *source* migrations dir, never the database's
558
+ // configured dir: when `useTypescriptMigrations` is enabled that points at
559
+ // build output (e.g. `dist/database/migrations`), which is gitignored and
560
+ // wiped on the next build, so the generated migration would not be a
561
+ // portable record.
562
+ const dir = sourceMigrationsDir();
563
+ return migrationFileBuilder.writeFiles({
564
+ name: MIGRATION_NAME,
565
+ format: getFormat(),
566
+ dir
567
+ });
568
+ }
569
+ };
570
+ };
571
+
572
+ export { createMigrationBuilder, isCompatibleRename };
573
+ //# sourceMappingURL=index.mjs.map