@@ -185,7 +185,9 @@ func applyFixtures(db *sql.DB, root string, fixtures []string, verbose bool) ([]
185185 }
186186
187187 var applied []string
188+ var yamlFixtures []string
188189
190+ // First pass: execute SQL fixtures immediately, collect YAML fixtures
189191 for _ , f := range fixtures {
190192 if isSQLFixture (f ) {
191193 if verbose {
@@ -197,27 +199,35 @@ func applyFixtures(db *sql.DB, root string, fixtures []string, verbose bool) ([]
197199 applied = append (applied , f )
198200 } else {
199201 name := trimYAMLExt (f )
200- if verbose {
202+ yamlFixtures = append (yamlFixtures , name )
203+ }
204+ }
205+
206+ // Second pass: apply all YAML fixtures in a single transaction
207+ // This ensures dependencies are resolved correctly and not re-applied
208+ if len (yamlFixtures ) > 0 {
209+ if verbose {
210+ for _ , name := range yamlFixtures {
201211 fmt .Printf (" Applying fixture: %s\n " , name )
202212 }
203- if err := applyYAMLFixture (fm , name ); err != nil {
204- return nil , fmt .Errorf ("fixture %q: %w" , name , err )
205- }
206- applied = append (applied , name )
207213 }
214+ if err := applyYAMLFixtures (fm , yamlFixtures ); err != nil {
215+ return nil , err
216+ }
217+ applied = append (applied , yamlFixtures ... )
208218 }
209219
210220 return applied , nil
211221}
212222
213- func applyYAMLFixture (fm * FixtureManager , name string ) error {
223+ func applyYAMLFixtures (fm * FixtureManager , names [] string ) error {
214224 if err := fm .BeginTransaction (); err != nil {
215225 return err
216226 }
217227
218228 _ = fm .IntrospectSchema ()
219229
220- if err := fm .ApplyFixtures ([] string { name } ); err != nil {
230+ if err := fm .ApplyFixtures (names ); err != nil {
221231 fm .Rollback ()
222232 return err
223233 }
0 commit comments