Skip to content

v9 is loading config file from disk #1867

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

Open
razvan-p opened this issue Jan 8, 2025 · 2 comments
Open

v9 is loading config file from disk #1867

razvan-p opened this issue Jan 8, 2025 · 2 comments

Comments

@razvan-p
Copy link

razvan-p commented Jan 8, 2025

Hi! We have a multitenancy app which runs a backup every day. After updating from v8 to v9 we remarked that config is always loaded from config.php file from disk. This way we can't configure dynamically the config options according to each tenant. This is our code for changing the config dynamically which is working in v8, but not in v9:

<?php

namespace App\Tasks;

use Spatie\Multitenancy\Contracts\IsTenant;
use Spatie\Multitenancy\Tasks\SwitchTenantTask;

class SwitchBackupDatabaseTask implements SwitchTenantTask
{
    public function __construct(protected ?array $originalBackupDatabase = null, protected ?string $originalBackupFilenamePrefix = null)
    {
        $this->originalBackupDatabase ??= config('backup.backup.source.databases');
        $this->originalBackupFilenamePrefix ??= config('backup.backup.destination.filename_prefix');
    }

    public function makeCurrent(IsTenant $tenant): void
    {
        config()->set('backup.backup.source.databases', ['tenant']);
        config()->set('backup.backup.destination.filename_prefix', $tenant->database . '-');
    }

    public function forgetCurrent(): void
    {
        config()->set('backup.backup.source.databases', $this->originalBackupDatabase);
        config()->set('backup.backup.destination.filename_prefix', $this->originalBackupFilenamePrefix);
    }
}
@chrisk-7777
Copy link

chrisk-7777 commented Feb 24, 2025

I've just hit the same thing. The closest I could get is to keep a full list of tenants in the backup config:

'databases' => [
  'tenant_1',
  'tenant_2',
  'tenant_3',
  ...
],

Then in the schedule, pass in the database(s) that need backing up:

    protected function schedule(Schedule $schedule): void
    {
        $schedule->command('backup:run', [
            '--only-db',
            '--db-name' => [
              'tenant_1' // can be generated dynamically 
            ],
        ])
            ->dailyAt('13:00');

        $schedule->command('monitor:check-uptime')->everyMinute();
    }

This isn't super practical, and is a bit prone to error.

I've attempted to extend the BackupCommand class, passing in my own Config instance, but it always seems to revert to the original config from the backup config file.

I'm probably just misunderstanding the flow, but I can't seem to crack how to set the config dynamically.

@inserve-paul
Copy link

@razvan-p @chrisk-7777 I've created a PR for this issue: #1905

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants