-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kurt Friars
committed
Oct 3, 2024
1 parent
1b77fe0
commit c035e4c
Showing
8 changed files
with
279 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/Database/Migrations/schema/fks/create_documents_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Plank\Snapshots\Migrator\SnapshotBlueprint; | ||
use Plank\Snapshots\Migrator\SnapshotMigration; | ||
|
||
return new class extends SnapshotMigration | ||
{ | ||
public function up() | ||
{ | ||
$this->schema->create('documents', function (SnapshotBlueprint $table) { | ||
$table->id(); | ||
$table->string('title'); | ||
$table->string('text'); | ||
$table->timestamp('released_at')->nullable(); | ||
$table->timestamps(); | ||
|
||
$table->index('title', 'idx_title'); | ||
$table->index('released_at'); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->schema->drop('documents'); | ||
} | ||
}; |
22 changes: 22 additions & 0 deletions
22
tests/Database/Migrations/schema/fks/y_add_folders_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
use Plank\Snapshots\Migrator\SnapshotBlueprint; | ||
use Plank\Snapshots\Migrator\SnapshotMigration; | ||
|
||
return new class extends SnapshotMigration | ||
{ | ||
public function up() | ||
{ | ||
$this->schema->create('folders', function (SnapshotBlueprint $table) { | ||
$table->id(); | ||
$table->string('name'); | ||
$table->string('disk'); | ||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->schema->drop('folders'); | ||
} | ||
}; |
26 changes: 26 additions & 0 deletions
26
tests/Database/Migrations/schema/fks/z_alter_documents_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
use Plank\Snapshots\Migrator\SnapshotBlueprint; | ||
use Plank\Snapshots\Migrator\SnapshotMigration; | ||
use Plank\Snapshots\Tests\Models\Folder; | ||
|
||
return new class extends SnapshotMigration | ||
{ | ||
public function up() | ||
{ | ||
$this->schema->table('documents', function (SnapshotBlueprint $table) { | ||
$table->string('folder_id')->after('id'); | ||
|
||
$table->foreign('folder_id') | ||
->references('id') | ||
->onSnapshot('folders'); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
$this->schema->table('documents', function (SnapshotBlueprint $table) { | ||
$table->dropForeignIdFor(Folder::class); | ||
}); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace Plank\Snapshots\Tests\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Plank\Snapshots\Concerns\AsVersionedContent; | ||
use Plank\Snapshots\Concerns\HasHistory; | ||
use Plank\Snapshots\Contracts\Trackable; | ||
use Plank\Snapshots\Contracts\Versioned; | ||
|
||
class Folder extends Model implements Trackable, Versioned | ||
{ | ||
use AsVersionedContent; | ||
use HasFactory; | ||
use HasHistory; | ||
|
||
protected $guarded = []; | ||
|
||
/** | ||
* The attributes that should be visible in serialization. | ||
* | ||
* @var array<string> | ||
*/ | ||
protected $visible = [ | ||
'name', | ||
'disk', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Events\MigrationsEnded; | ||
use Illuminate\Support\Facades\Event; | ||
use Plank\Snapshots\Contracts\ManagesCreatedTables; | ||
|
||
use function Pest\Laravel\artisan; | ||
|
||
describe('The Table Repository Gathers TableCreated events correctly', function () { | ||
beforeEach(function () { | ||
artisan('migrate', [ | ||
'--path' => migrationPath('query'), | ||
'--realpath' => true, | ||
])->run(); | ||
}); | ||
|
||
it('gathers tables correctly', function () { | ||
Event::forget(MigrationsEnded::class); | ||
|
||
Event::listen(MigrationsEnded::class, function () { | ||
expect(app(ManagesCreatedTables::class)->all()) | ||
->toHaveKeys([ | ||
'posts', | ||
'post_post', | ||
'seos', | ||
'taggables', | ||
]); | ||
}); | ||
|
||
versions()->setActive(createFirstVersion('query')); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Connectors\ConnectionFactory; | ||
use Illuminate\Database\Schema\MySqlBuilder; | ||
use Illuminate\Database\Schema\PostgresBuilder; | ||
use Illuminate\Database\Schema\SQLiteBuilder; | ||
use Illuminate\Database\Schema\SqlServerBuilder; | ||
use Plank\Snapshots\Contracts\ManagesCreatedTables; | ||
use Plank\Snapshots\Contracts\ManagesVersions; | ||
use Plank\Snapshots\Factory\SchemaBuilderFactory; | ||
use Plank\Snapshots\Schema\MySqlSnapshotBuilder; | ||
use Plank\Snapshots\Schema\PostgresSnapshotBuilder; | ||
use Plank\Snapshots\Schema\SQLiteSnapshotBuilder; | ||
use Plank\Snapshots\Schema\SqlServerSnapshotBuilder; | ||
|
||
describe('The Schema Builder is resolved in the container based on the db connection', function () { | ||
it('resolves the MySql Snapshot Builder correctly', function () { | ||
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.mysql')); | ||
|
||
$instance = SchemaBuilderFactory::make( | ||
$connection, | ||
app(ManagesVersions::class), | ||
app(ManagesCreatedTables::class) | ||
); | ||
|
||
expect($instance)->toBeInstanceOf(MySqlBuilder::class); | ||
expect($instance)->toBeInstanceOf(MySqlSnapshotBuilder::class); | ||
}); | ||
|
||
it('resolves the Postgres Snapshot Builder correctly', function () { | ||
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.pgsql')); | ||
|
||
$instance = SchemaBuilderFactory::make( | ||
$connection, | ||
app(ManagesVersions::class), | ||
app(ManagesCreatedTables::class) | ||
); | ||
|
||
expect($instance)->toBeInstanceOf(PostgresBuilder::class); | ||
expect($instance)->toBeInstanceOf(PostgresSnapshotBuilder::class); | ||
}); | ||
|
||
it('resolves the SQLite Snapshot Builder correctly', function () { | ||
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.sqlite')); | ||
|
||
$instance = SchemaBuilderFactory::make( | ||
$connection, | ||
app(ManagesVersions::class), | ||
app(ManagesCreatedTables::class) | ||
); | ||
|
||
expect($instance)->toBeInstanceOf(SQLiteBuilder::class); | ||
expect($instance)->toBeInstanceOf(SQLiteSnapshotBuilder::class); | ||
}); | ||
|
||
it('resolves the SqlServer Snapshot Builder correctly', function () { | ||
$connection = (new ConnectionFactory(app()))->make(config()->get('database.connections.sqlsrv')); | ||
|
||
$instance = SchemaBuilderFactory::make( | ||
$connection, | ||
app(ManagesVersions::class), | ||
app(ManagesCreatedTables::class) | ||
); | ||
|
||
expect($instance)->toBeInstanceOf(SqlServerBuilder::class); | ||
expect($instance)->toBeInstanceOf(SqlServerSnapshotBuilder::class); | ||
}); | ||
}); |