Skip to content

Commit

Permalink
Fixes embedded replica sync issue
Browse files Browse the repository at this point in the history
  • Loading branch information
richan-fongdasen committed Nov 7, 2024
1 parent 1e089c9 commit a5c1623
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 29 deletions.
14 changes: 6 additions & 8 deletions src/Commands/TursoSyncCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ class TursoSyncCommand extends Command

public $description = 'Sync changes from the remote database to the local replica manually.';

protected function compileRunProcess(array $config): string
protected function compileRunProcess(string $connectionName): string
{
return sprintf(
'%s %s "%s" "%s" "%s"',
$this->getNodePath(),
config('turso-laravel.sync_command.script_filename'),
data_get($config, 'db_url'),
data_get($config, 'access_token'),
data_get($config, 'db_replica'),
DB::connection($connectionName)->getConfig('db_url'),
DB::connection($connectionName)->getConfig('access_token'),
DB::connection($connectionName)->getConfig('db_replica'),
);
}

Expand All @@ -42,7 +42,7 @@ public function handle(): int
{
$timeout = (int) config('turso-laravel.sync_command.timeout');

$connectionName = (string) $this->argument('connectionName');
$connectionName = $this->argument('connectionName') ?? DB::getDefaultConnection();

if (DB::connection($connectionName)->getConfig('driver') !== 'turso') {
$this->error('The specified connection is not a Turso connection.');
Expand All @@ -58,9 +58,7 @@ public function handle(): int

$result = Process::timeout($timeout)
->path(config('turso-laravel.sync_command.script_path') ?? base_path())
->run($this->compileRunProcess(
DB::connection($connectionName)->getConfig()
));
->run($this->compileRunProcess($connectionName));

if ($result->failed()) {
throw new RuntimeException('Turso sync command failed: ' . $result->errorOutput());
Expand Down
3 changes: 1 addition & 2 deletions src/Facades/Turso.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@
use RichanFongdasen\Turso\TursoManager;

/**
* @see \RichanFongdasen\Turso\TursoClient
* @see \RichanFongdasen\Turso\TursoManager
*
* @mixin \RichanFongdasen\Turso\TursoManager
* @mixin \RichanFongdasen\Turso\TursoClient
*/
class Turso extends Facade
{
Expand Down
21 changes: 2 additions & 19 deletions src/TursoLaravelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

namespace RichanFongdasen\Turso;

use Illuminate\Console\Events\CommandFinished;
use Illuminate\Console\Events\CommandStarting;
use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Facades\Event;
use RichanFongdasen\Turso\Commands\TursoSyncCommand;
use RichanFongdasen\Turso\Database\TursoConnection;
use RichanFongdasen\Turso\Database\TursoConnector;
Expand All @@ -22,22 +19,8 @@ public function boot(): void
{
parent::boot();

Event::listen(function (CommandStarting $event) {
if (! app()->bound('running-artisan-command')) {
app()->instance('running-artisan-command', data_get($event, 'command'));
}
});

Event::listen(function (CommandFinished $event) {
if (data_get($event, 'command') === 'turso:sync') {
return;
}

if (
(app('running-artisan-command') === data_get($event, 'command'))
) {
Turso::sync();
}
app()->terminating(function () {
Turso::sync();
});
}

Expand Down

0 comments on commit a5c1623

Please sign in to comment.