-
-
Notifications
You must be signed in to change notification settings - Fork 13
Set tenant connection when using make:migration-tenant command
#1216
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Hi @alaincodes24, Can you explain how this works? Because I'm a little confused. I don't see how this fixes the problem from the issue. |
Hey @beesaferoot the issue is that when you run |
I understand it now. Thanks for the explanation. Your current solution makes assumptions about the file name using a timestamp. Can we guarantee this to always be correct? I was considering other possible solutions and landed with this one below. I don't fully like it, but we can guarantee it will work correctly in all scenarios. <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;
class MakeMigrationTenant extends Command {
protected $signature = 'make:migration-tenant {migration-name}';
protected $description = 'Create new migration file for tenant database(s)';
public function handle(): int {
$stubPath = base_path('stubs/migration.create.stub');
// Backup original stub content
$originalStub = File::get($stubPath);
try {
$this->modifyStub($stubPath, $originalStub);
$migrationName = $this->argument('migration-name');
$this->call('make:migration', [
'name' => $migrationName,
'--path' => '/database/migrations/tenant',
]);
} finally {
File::put($stubPath, $originalStub);
}
return 0;
}
protected function modifyStub(string $stubPath, string $originalStub): void {
// Replace micropowermanager connection reference to tenant connection
$stub = str_replace(
'micropowermanager',
'tenant',
$originalStub
);
File::put($stubPath, $stub);
}
} |
I get where you are coming from, especially with the timestamp, but this code will produce this kind of migration below where the connection is stripped out, yet we want it to be set by default to |
|
Hey @alaincodes24 and @beesaferoot So the way this works is, that we provide "custom" stubs, here: https://github.com/EnAccess/micropowermanager/blob/main/src/backend/stubs/migration.create.stub If the migration detects, that it's a "create" migration (i.e. the migrate name contains create) it will use this stub (which has hardcoded to Schema to Maybe, we can instead inherit from https://github.com/laravel/framework/blob/12.x/src/Illuminate/Database/Console/Migrations/MigrateMakeCommand.php#L12 and create our own command here? |
I like the idea, has some major upsides; like customizing MigrationCreator to replace connection we can add to our stubs i.e {{ connection }}. @alaincodes24 Do you want to look into this? |
closes: #1207
Pull Request checklist
Please confirm you have completed any of the necessary steps below.