(collections: CollectionDelta[])
| 62 | ) as CollectionDelta[]; |
| 63 | |
| 64 | const createCollections = async (collections: CollectionDelta[]) => { |
| 65 | for (const { collection, diff } of collections) { |
| 66 | if (diff?.[0]?.kind === DiffKind.NEW && diff[0].rhs) { |
| 67 | // We'll nest the to-be-created fields in the same collection creation, to prevent |
| 68 | // creating a collection without a primary key |
| 69 | const fields = snapshotDiff.fields |
| 70 | .filter((fieldDiff) => fieldDiff.collection === collection) |
| 71 | .map((fieldDiff) => (fieldDiff.diff[0] as DiffNew<Field>).rhs) |
| 72 | .map((fieldDiff) => { |
| 73 | // Casts field type to UUID when applying non-PostgreSQL schema onto PostgreSQL database. |
| 74 | // This is needed because they snapshots UUID fields as char/varchar with length 36. |
| 75 | if ( |
| 76 | ['char', 'varchar'].includes(String(fieldDiff.schema?.data_type).toLowerCase()) && |
| 77 | fieldDiff.schema?.max_length === 36 && |
| 78 | (fieldDiff.schema?.is_primary_key || |
| 79 | (fieldDiff.schema?.foreign_key_table && fieldDiff.schema?.foreign_key_column)) |
| 80 | ) { |
| 81 | return merge(fieldDiff, { type: 'uuid', schema: { data_type: 'uuid', max_length: null } }); |
| 82 | } else { |
| 83 | return fieldDiff; |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | try { |
| 88 | await collectionsService.createOne( |
| 89 | { |
| 90 | ...diff[0].rhs, |
| 91 | fields, |
| 92 | }, |
| 93 | mutationOptions, |
| 94 | ); |
| 95 | } catch (err: any) { |
| 96 | logger.error(`Failed to create collection "${collection}"`); |
| 97 | throw err; |
| 98 | } |
| 99 | |
| 100 | // Now that the fields are in for this collection, we can strip them from the field edits |
| 101 | snapshotDiff.fields = snapshotDiff.fields.filter((fieldDiff) => fieldDiff.collection !== collection); |
| 102 | |
| 103 | await createCollections(getNestedCollectionsToCreate(collection)); |
| 104 | } |
| 105 | } |
| 106 | }; |
| 107 | |
| 108 | const deleteCollections = async (collections: CollectionDelta[]) => { |
| 109 | for (const { collection, diff } of collections) { |
no test coverage detected