MigrationService.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\File;
  5. use Illuminate\Support\Facades\Artisan;
  6. use App\Models\User;
  7. use App\Database\Migrations\MasterMigration;
  8. use App\Database\Migrations\TenantMigration;
  9. class MigrationService
  10. {
  11. protected $masterDatabase = 'leezard_master';
  12. public function runMasterMigrations()
  13. {
  14. $this->setupMasterConnection();
  15. $masterMigrations = $this->getMasterMigrationFiles();
  16. if (empty($masterMigrations)) {
  17. return ['message' => 'No master migrations found.', 'migrations' => []];
  18. }
  19. // Create a temporary directory for master migrations only
  20. $tempPath = storage_path('app/temp_master_migrations');
  21. if (!File::exists($tempPath)) {
  22. File::makeDirectory($tempPath, 0755, true);
  23. }
  24. // Copy only master migrations to temp directory
  25. foreach ($masterMigrations as $migration) {
  26. $source = database_path('migrations/' . $migration);
  27. $destination = $tempPath . '/' . $migration;
  28. File::copy($source, $destination);
  29. }
  30. try {
  31. // Run migrations from temp directory
  32. Artisan::call('migrate', [
  33. '--database' => 'master',
  34. '--path' => 'storage/app/temp_master_migrations'
  35. ]);
  36. $output = Artisan::output();
  37. } finally {
  38. // Clean up temp directory
  39. File::deleteDirectory($tempPath);
  40. }
  41. return [
  42. 'message' => 'Master migrations completed successfully!',
  43. 'database' => $this->masterDatabase,
  44. 'migrations' => $masterMigrations,
  45. 'output' => $output
  46. ];
  47. }
  48. public function runTenantMigrations()
  49. {
  50. $tenants = $this->getTenants();
  51. $results = [];
  52. if ($tenants->isEmpty()) {
  53. return ['message' => 'No tenants found.', 'results' => []];
  54. }
  55. $tenantMigrations = $this->getTenantMigrationFiles();
  56. if (empty($tenantMigrations)) {
  57. return ['message' => 'No tenant migrations found.', 'results' => []];
  58. }
  59. // Create a temporary directory for tenant migrations only
  60. $tempPath = storage_path('app/temp_tenant_migrations');
  61. if (!File::exists($tempPath)) {
  62. File::makeDirectory($tempPath, 0755, true);
  63. }
  64. // Copy only tenant migrations to temp directory
  65. foreach ($tenantMigrations as $migration) {
  66. $source = database_path('migrations/' . $migration);
  67. $destination = $tempPath . '/' . $migration;
  68. File::copy($source, $destination);
  69. }
  70. foreach ($tenants as $tenant) {
  71. try {
  72. $this->setupTenantConnection($tenant);
  73. Artisan::call('migrate', [
  74. '--database' => 'tenant',
  75. '--path' => 'storage/app/temp_tenant_migrations'
  76. ]);
  77. $results[] = [
  78. 'tenant' => $tenant->tenant_database,
  79. 'status' => 'success',
  80. 'message' => 'Migrations completed successfully'
  81. ];
  82. } catch (\Exception $e) {
  83. $results[] = [
  84. 'tenant' => $tenant->tenant_database,
  85. 'status' => 'error',
  86. 'message' => $e->getMessage()
  87. ];
  88. }
  89. }
  90. // Clean up temp directory
  91. File::deleteDirectory($tempPath);
  92. return [
  93. 'message' => 'Tenant migrations process completed',
  94. 'results' => $results
  95. ];
  96. }
  97. public function rollbackMasterMigrations($steps = 1)
  98. {
  99. $this->setupMasterConnection();
  100. Artisan::call('migrate:rollback', [
  101. '--database' => 'master',
  102. '--step' => $steps
  103. ]);
  104. return [
  105. 'message' => "Master database rolled back {$steps} step(s)",
  106. 'database' => $this->masterDatabase,
  107. 'output' => Artisan::output()
  108. ];
  109. }
  110. public function rollbackTenantMigrations($steps = 1)
  111. {
  112. $tenants = $this->getTenants();
  113. $results = [];
  114. foreach ($tenants as $tenant) {
  115. try {
  116. $this->setupTenantConnection($tenant);
  117. Artisan::call('migrate:rollback', [
  118. '--database' => 'tenant',
  119. '--step' => $steps
  120. ]);
  121. $results[] = [
  122. 'tenant' => $tenant->tenant_database,
  123. 'status' => 'success',
  124. 'message' => "Rolled back {$steps} step(s)"
  125. ];
  126. } catch (\Exception $e) {
  127. $results[] = [
  128. 'tenant' => $tenant->tenant_database,
  129. 'status' => 'error',
  130. 'message' => $e->getMessage()
  131. ];
  132. }
  133. }
  134. return [
  135. 'message' => 'Tenant rollback process completed',
  136. 'results' => $results
  137. ];
  138. }
  139. protected function setupMasterConnection()
  140. {
  141. config(['database.connections.master' => [
  142. 'driver' => 'mysql',
  143. 'host' => env('DB_HOST', '127.0.0.1'),
  144. 'port' => env('DB_PORT', '3306'),
  145. 'database' => $this->masterDatabase,
  146. 'username' => env('DB_USERNAME'),
  147. 'password' => env('DB_PASSWORD'),
  148. 'charset' => 'utf8mb4',
  149. 'collation' => 'utf8mb4_unicode_ci',
  150. 'prefix' => '',
  151. 'strict' => true,
  152. 'engine' => null,
  153. ]]);
  154. config(['database.default' => 'master']);
  155. DB::purge('master');
  156. DB::reconnect('master');
  157. }
  158. protected function setupTenantConnection($tenant)
  159. {
  160. config(['database.connections.tenant' => [
  161. 'driver' => 'mysql',
  162. 'host' => env('DB_HOST', '127.0.0.1'),
  163. 'port' => env('DB_PORT', '3306'),
  164. 'database' => $tenant->tenant_database,
  165. 'username' => $tenant->tenant_username,
  166. 'password' => $tenant->tenant_password,
  167. 'charset' => 'utf8mb4',
  168. 'collation' => 'utf8mb4_unicode_ci',
  169. 'prefix' => '',
  170. 'strict' => true,
  171. 'engine' => null,
  172. ]]);
  173. config(['database.default' => 'tenant']);
  174. DB::purge('tenant');
  175. DB::reconnect('tenant');
  176. }
  177. protected function getTenants()
  178. {
  179. return User::select('tenant_database', 'tenant_username', 'tenant_password')
  180. ->whereNotNull('tenant_database')
  181. ->distinct()
  182. ->get();
  183. }
  184. protected function getMasterMigrationFiles()
  185. {
  186. $migrationPath = database_path('migrations');
  187. $files = File::glob($migrationPath . '/*.php');
  188. $masterMigrations = [];
  189. foreach ($files as $file) {
  190. $content = File::get($file);
  191. if (strpos($content, 'extends MasterMigration') !== false) {
  192. $masterMigrations[] = basename($file);
  193. }
  194. }
  195. return $masterMigrations;
  196. }
  197. protected function getTenantMigrationFiles()
  198. {
  199. $migrationPath = database_path('migrations');
  200. $files = File::glob($migrationPath . '/*.php');
  201. $tenantMigrations = [];
  202. foreach ($files as $file) {
  203. $content = File::get($file);
  204. if (strpos($content, 'extends TenantMigration') !== false) {
  205. $tenantMigrations[] = basename($file);
  206. }
  207. }
  208. return $tenantMigrations;
  209. }
  210. }