2929final class SchemaMigrator
3030{
3131 /**
32- * Fingerprint of the migration set, computed from the files without running them.
32+ * Discovers the project's migrations without running them, so the caller can fingerprint and apply
33+ * the same set without scanning twice.
34+ *
35+ * @return list<stdClass>
36+ */
37+ public function discover (Connection $ db , ?string $ namespace = null ): array
38+ {
39+ $ runner = new MigrationRunner (config (Migrations::class), $ db );
40+
41+ // A null namespace makes the runner scan every registered namespace (the app plus installed
42+ // packages), so a library analyzed on its own and an app's vendor migrations are both found.
43+ $ runner ->setNamespace ($ namespace );
44+
45+ return array_values (array_filter (
46+ $ runner ->findMigrations (),
47+ static fn (mixed $ migration ): bool => $ migration instanceof stdClass,
48+ ));
49+ }
50+
51+ /**
52+ * Fingerprint of a discovered migration set, computed from the files without running them.
53+ *
54+ * @param list<stdClass> $migrations
3355 */
34- public function fingerprint (Connection $ db , ? string $ namespace = null ): string
56+ public function fingerprint (array $ migrations ): string
3557 {
3658 $ fingerprint = '' ;
3759
38- foreach ($ this -> discoverMigrations ( $ db , $ namespace ) as $ migration ) {
60+ foreach ($ migrations as $ migration ) {
3961 $ path = $ migration ->path ;
4062 $ uid = $ migration ->uid ;
4163
@@ -50,11 +72,14 @@ public function fingerprint(Connection $db, ?string $namespace = null): string
5072 return hash ('sha256 ' , $ fingerprint );
5173 }
5274
53- public function migrate (Connection $ db , ?string $ namespace = null ): void
75+ /**
76+ * @param list<stdClass> $migrations
77+ */
78+ public function migrate (Connection $ db , array $ migrations ): void
5479 {
5580 $ forge = Database::forge ($ db );
5681
57- foreach ($ this -> discoverMigrations ( $ db , $ namespace ) as $ migration ) {
82+ foreach ($ migrations as $ migration ) {
5883 $ path = $ migration ->path ;
5984 $ class = $ migration ->class ;
6085
@@ -82,23 +107,6 @@ public function migrate(Connection $db, ?string $namespace = null): void
82107 }
83108 }
84109
85- /**
86- * @return list<stdClass>
87- */
88- private function discoverMigrations (Connection $ db , ?string $ namespace ): array
89- {
90- $ runner = new MigrationRunner (config (Migrations::class), $ db );
91-
92- // A null namespace makes the runner scan every registered namespace (the app plus installed
93- // packages), so a library analyzed on its own and an app's vendor migrations are both found.
94- $ runner ->setNamespace ($ namespace );
95-
96- return array_values (array_filter (
97- $ runner ->findMigrations (),
98- static fn (mixed $ migration ): bool => $ migration instanceof stdClass,
99- ));
100- }
101-
102110 /**
103111 * Reads the migration's `$DBGroup` default without instantiating it, since instantiating a
104112 * pinned migration would open a connection to that group.
0 commit comments