@@ -37,6 +37,57 @@ const getAlterContainersScripts = (schema, provider) => {
3737 } ;
3838} ;
3939
40+ const sortCollectionsByRelationships = ( collections , relationships ) => {
41+ const collectionToChildren = new Map ( ) ; // Map of collection IDs to their children
42+ const collectionParentCount = new Map ( ) ; // Track how many parents each collection has
43+
44+ // Initialize maps
45+ for ( const collection of collections ) {
46+ collectionToChildren . set ( collection . role . id , [ ] ) ;
47+ collectionParentCount . set ( collection . role . id , 0 ) ;
48+ }
49+
50+ for ( const relationship of relationships ) {
51+ const parent = relationship . role . parentCollection ;
52+ const child = relationship . role . childCollection ;
53+ if ( collectionToChildren . has ( parent ) ) {
54+ collectionToChildren . get ( parent ) . push ( child ) ;
55+ }
56+ collectionParentCount . set ( child , ( collectionParentCount . get ( child ) || 0 ) + 1 ) ;
57+ }
58+
59+ // Find collections with no parents
60+ const queue = collections
61+ . filter ( collection => collectionParentCount . get ( collection . role . id ) === 0 )
62+ . map ( collection => collection . role . id ) ;
63+
64+ const sortedIds = [ ] ;
65+
66+ // Sort collections
67+ while ( queue . length > 0 ) {
68+ const current = queue . shift ( ) ;
69+ sortedIds . push ( current ) ;
70+
71+ for ( const child of collectionToChildren . get ( current ) || [ ] ) {
72+ collectionParentCount . set ( child , collectionParentCount . get ( child ) - 1 ) ;
73+ if ( collectionParentCount . get ( child ) <= 0 ) {
74+ queue . push ( child ) ;
75+ }
76+ }
77+ }
78+
79+ // Add any unvisited collection
80+ for ( const collection of collections ) {
81+ if ( ! sortedIds . includes ( collection . role . id ) ) {
82+ sortedIds . unshift ( collection . role . id ) ;
83+ }
84+ }
85+
86+ // Map back to collection objects in sorted order
87+ const idToCollection = Object . fromEntries ( collections . map ( c => [ c . role . id , c ] ) ) ;
88+ return sortedIds . map ( id => idToCollection [ id ] ) ;
89+ } ;
90+
4091const getAlterCollectionsScripts = ( { schema, definitions, provider, data, inlineDeltaRelationships } ) => {
4192 let currentSchemaName = '' ;
4293
@@ -55,11 +106,13 @@ const getAlterCollectionsScripts = ({ schema, definitions, provider, data, inlin
55106 const deletedCollectionsItems = getItems ( schema , 'entities' , 'deleted' ) ;
56107 const modifiedCollectionsItems = getItems ( schema , 'entities' , 'modified' ) ;
57108
58- const addedCollectionsScripts = addedCollectionsItems
59- . filter ( item => item . compMod ?. created )
60- . flatMap ( item =>
61- setCurrentSchemaName ( item , getAddCollectionsScripts ( definitions , data , inlineDeltaRelationships ) ) ,
62- ) ;
109+ const addedCollectionsScripts = sortCollectionsByRelationships (
110+ addedCollectionsItems . filter ( collection => collection . compMod ?. created ) ,
111+ inlineDeltaRelationships ,
112+ ) . flatMap ( item =>
113+ setCurrentSchemaName ( item , getAddCollectionsScripts ( definitions , data , inlineDeltaRelationships ) ) ,
114+ ) ;
115+
63116 const deletedCollectionsScripts = deletedCollectionsItems
64117 . filter ( item => item . compMod ?. deleted )
65118 . flatMap ( getDeleteCollectionsScripts ( provider ) ) ;
0 commit comments