Skip to content

Commit

Permalink
Merge branch 'main' into loadLocalTemp
Browse files Browse the repository at this point in the history
  • Loading branch information
wattnpapa authored Dec 30, 2024
2 parents 1363232 + dcb5abd commit cbf2298
Show file tree
Hide file tree
Showing 14 changed files with 110 additions and 73 deletions.
12 changes: 2 additions & 10 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,17 @@ jobs:
fail-fast: false
matrix:
os: [ubuntu-latest]
php: [8.2, 8.1, 8.0]
laravel: ['8.*', '9.*', '10.*', '11.*']
php: [8.4, 8.3, 8.2, 8.1]
laravel: ['10.*', '11.*']
stability: [prefer-lowest, prefer-stable]
include:
- laravel: 10.*
testbench: 8.*
- laravel: 9.*
testbench: 7.*
- laravel: 8.*
testbench: ^6.23
- laravel: 11.*
testbench: 9.*
exclude:
- laravel: 10.*
php: 8.0
- laravel: 11.*
php: 8.1
- laravel: 11.*
php: 8.0

name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }}

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ testbench.yaml
vendor
node_modules
.php-cs-fixer.cache
.phpunit.cache
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@

All notable changes to `laravel-db-snapshots` will be documented in this file

## 2.7.0 - 2024-12-11

### What's Changed

* Add extraOptions to dbDumper by @wattnpapa in https://github.com/spatie/laravel-db-snapshots/pull/150

### New Contributors

* @wattnpapa made their first contribution in https://github.com/spatie/laravel-db-snapshots/pull/150

**Full Changelog**: https://github.com/spatie/laravel-db-snapshots/compare/2.6.2...2.7.0

## 2.6.2 - 2024-12-09

### What's Changed

* Prepend the connection onto the name by @michaeljhopkins in https://github.com/spatie/laravel-db-snapshots/pull/148

### New Contributors

* @michaeljhopkins made their first contribution in https://github.com/spatie/laravel-db-snapshots/pull/148

**Full Changelog**: https://github.com/spatie/laravel-db-snapshots/compare/2.6.1...2.6.2

## 2.6.1 - 2024-04-02

### What's Changed
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,13 @@ Giving your snapshot a name is optional. If you don't pass a name the current da
php artisan snapshot:create
```

If you pass a connection but do not declare a name for the snapshot, the connection will be prepended

```bash
# Creates a snapshot named something like `logging_2017-03-17 14:31`
php artisan snapshot:create --connection=logging
```

Maybe you only want to snapshot a couple of tables. You can do this by passing the `--table` multiple times or as a comma separated list:

```bash
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@
}
],
"require": {
"php": "^8.0",
"illuminate/support": "^7.0|^8.0|^9.0|^10.0|^11.0",
"league/flysystem": "^1.0.41|^2.0|^3.0",
"php": "^8.1",
"illuminate/support": "^10.0|^11.0",
"league/flysystem": "^3.0",
"spatie/db-dumper": "^3.3",
"spatie/laravel-package-tools": "^1.6",
"spatie/temporary-directory": "^2.0"
},
"require-dev": {
"mockery/mockery": "^1.4",
"orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0",
"pestphp/pest-plugin-laravel": "^1.3|^2.3",
"phpunit/phpunit": "^9.5|^10.5"
"orchestra/testbench": "^8.22.3|^9.0.4",
"pestphp/pest-plugin-laravel": "^2.4",
"phpunit/phpunit": "^10.5"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 9 additions & 37 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,39 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="vendor/autoload.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
executionOrder="random"
failOnWarning="true"
failOnRisky="true"
failOnEmptyTestSuite="true"
beStrictAboutOutputDuringTests="true"
verbose="true"
>
<testsuites>
<testsuite name="VendorName Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src</directory>
</include>
<report>
<html outputDirectory="build/coverage"/>
<text outputFile="build/coverage.txt"/>
<clover outputFile="build/logs/clover.xml"/>
</report>
</coverage>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" executionOrder="random" failOnWarning="true" failOnRisky="true" failOnEmptyTestSuite="true" beStrictAboutOutputDuringTests="true" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="VendorName Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<logging>
<junit outputFile="build/report.junit.xml"/>
</logging>
</phpunit>
3 changes: 2 additions & 1 deletion src/Commands/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Spatie\DbSnapshots\Commands;

use Illuminate\Console\Command;
use Spatie\DbSnapshots\Snapshot;
use Spatie\DbSnapshots\SnapshotRepository;

class Cleanup extends Command
Expand All @@ -23,6 +24,6 @@ public function handle()
return;
}

$snapshots->splice($keep)->each(fn ($snapshot) => $snapshot->delete());
$snapshots->splice($keep)->each(fn (Snapshot $snapshot) => $snapshot->delete());
}
}
19 changes: 16 additions & 3 deletions src/Commands/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Create extends Command
{
protected $signature = 'snapshot:create {name?} {--connection=} {--compress} {--table=*} {--exclude=*}';
protected $signature = 'snapshot:create {name?} {--connection=} {--compress} {--table=*} {--exclude=*} {--extraOptions=*}';

protected $description = 'Create a new snapshot.';

Expand All @@ -21,7 +21,7 @@ public function handle()
?: config('db-snapshots.default_connection')
?? config('database.default');

$snapshotName = $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s');
$snapshotName = $this->getSnapshotName();

$compress = $this->option('compress') || config('db-snapshots.compress', false);

Expand All @@ -35,18 +35,31 @@ public function handle()
$exclude = null;
}

$extraOptions = $this->option('extraOptions') ?: config('db-snapshots.extraOptions', []);
$extraOptions = is_string($extraOptions) ? explode(',', $extraOptions) : $extraOptions;


$snapshot = app(SnapshotFactory::class)->create(
$snapshotName,
config('db-snapshots.disk'),
$connectionName,
$compress,
$tables,
$exclude
$exclude,
$extraOptions
);

$size = Format::humanReadableSize($snapshot->size());

$this->info("Snapshot `{$snapshotName}` created (size: {$size})");
}

private function getSnapshotName(): string
{
if (! is_null($this->option('connection')) && is_null($this->argument('name'))) {
return $this->option('connection'). "_". Carbon::now()->format('Y-m-d_H-i-s');
}

return $this->argument('name') ?? Carbon::now()->format('Y-m-d_H-i-s');
}
}
3 changes: 2 additions & 1 deletion src/Events/CreatingSnapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public function __construct(
public FilesystemAdapter $disk,
public string $connectionName,
public ?array $tables = null,
public ?array $exclude = null
public ?array $exclude = null,
public ?array $extraOptions = null
) {
//
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/CannotCreateDisk.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ public static function diskNotDefined(string $diskName): static

$existingDiskNames = implode(', ', array_keys($disks));

return new static("Cannot create a disk `{$diskName}`. Known disknames are {$existingDiskNames}.");
return new static("Cannot create a disk `{$diskName}`. Known disk names are {$existingDiskNames}.");
}
}
20 changes: 12 additions & 8 deletions src/Snapshot.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public function __construct(Disk $disk, string $fileName)
$this->filesystemFactory = app(Factory::class);
}

public function useStream()
public function useStream(): self
{
$this->useStream = true;

return $this;
}

public function load(string $connectionName = null, bool $dropTables = true): void
public function load(?string $connectionName = null, bool $dropTables = true): void
{
event(new LoadingSnapshot($this));

Expand All @@ -71,7 +71,7 @@ public function load(string $connectionName = null, bool $dropTables = true): vo
event(new LoadedSnapshot($this));
}

protected function loadAsync(string $connectionName = null)
protected function loadAsync(?string $connectionName = null): void
{
$dbDumpContents = $this->disk->get($this->fileName);

Expand All @@ -84,7 +84,7 @@ protected function loadAsync(string $connectionName = null)

protected function isASqlComment(string $line): bool
{
return substr($line, 0, 2) === '--';
return str_starts_with($line, '--');
}

protected function shouldIgnoreLine(string $line): bool
Expand Down Expand Up @@ -117,7 +117,7 @@ protected function loadStream(string $connectionName = null)
: $localDisk->readStream($this->fileName);

$statement = '';
while (! feof($stream)) {
while (!feof($stream)) {
$chunk = $this->compressionExtension === 'gz'
? gzread($stream, self::STREAM_BUFFER_SIZE)
: fread($stream, self::STREAM_BUFFER_SIZE);
Expand All @@ -137,13 +137,17 @@ protected function loadStream(string $connectionName = null)
break;
}

if (substr(trim($statement), -1, 1) === ';') {
if (str_ends_with(trim($statement), ';')) {
yield $statement;
$statement = '';
}
}
}

if (str_ends_with(trim($statement), ';')) {
yield $statement;
}

if ($this->compressionExtension === 'gz') {
gzclose($stream);
} else {
Expand All @@ -156,7 +160,7 @@ protected function loadStream(string $connectionName = null)
$directory->delete();
}
}

public function delete(): void
{
event(new DeletingSnapshot($this));
Expand All @@ -176,7 +180,7 @@ public function createdAt(): Carbon
return Carbon::createFromTimestamp($this->disk->lastModified($this->fileName));
}

protected function dropAllCurrentTables()
protected function dropAllCurrentTables(): void
{
DB::connection(DB::getDefaultConnection())
->getSchemaBuilder()
Expand Down
13 changes: 9 additions & 4 deletions src/SnapshotFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
//
}

public function create(string $snapshotName, string $diskName, string $connectionName, bool $compress = false, ?array $tables = null, ?array $exclude = null): Snapshot
public function create(string $snapshotName, string $diskName, string $connectionName, bool $compress = false, ?array $tables = null, ?array $exclude = null, array $extraOptions = []): Snapshot
{
$disk = $this->getDisk($diskName);

Expand All @@ -36,10 +36,11 @@ public function create(string $snapshotName, string $diskName, string $connectio
$disk,
$connectionName,
$tables,
$exclude
$exclude,
$extraOptions
));

$this->createDump($connectionName, $fileName, $disk, $compress, $tables, $exclude);
$this->createDump($connectionName, $fileName, $disk, $compress, $tables, $exclude, $extraOptions);

$snapshot = new Snapshot($disk, $fileName);

Expand All @@ -64,7 +65,7 @@ protected function getDbDumper(string $connectionName): DbDumper
return $factory::createForConnection($connectionName);
}

protected function createDump(string $connectionName, string $fileName, FilesystemAdapter $disk, bool $compress = false, ?array $tables = null, ?array $exclude = null): void
protected function createDump(string $connectionName, string $fileName, FilesystemAdapter $disk, bool $compress = false, ?array $tables = null, ?array $exclude = null, array $extraOptions = []): void
{
$directory = (new TemporaryDirectory(config('db-snapshots.temporary_directory_path')))->create();

Expand All @@ -84,6 +85,10 @@ protected function createDump(string $connectionName, string $fileName, Filesyst
$dbDumper->excludeTables($exclude);
}

foreach ($extraOptions as $extraOption) {
$dbDumper->addExtraOption($extraOption);
}

$dbDumper->dumpToFile($dumpPath);

$file = fopen($dumpPath, 'r');
Expand Down
5 changes: 4 additions & 1 deletion src/SnapshotRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public function __construct(
//
}

/**
* @return Collection<Snapshot>
*/
public function getAll(): Collection
{
return collect($this->disk->allFiles())
Expand All @@ -29,7 +32,7 @@ public function getAll(): Collection
->sortByDesc(fn (Snapshot $snapshot) => $snapshot->createdAt()->toDateTimeString());
}

public function findByName(string $name)
public function findByName(string $name): ?Snapshot
{
return $this->getAll()->first(fn (Snapshot $snapshot) => $snapshot->name === $name);
}
Expand Down
Loading

0 comments on commit cbf2298

Please sign in to comment.