generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from envor/main
Added verbs
- Loading branch information
Showing
7 changed files
with
179 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
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
36 changes: 36 additions & 0 deletions
36
database/migrations/2024_04_16_115559_create_verb_events_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,36 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
// If they've already migrated under the previous migration name, just skip | ||
if (Schema::hasTable($this->tableName())) { | ||
return; | ||
} | ||
|
||
Schema::create($this->tableName(), function (Blueprint $table) { | ||
$table->snowflakeId(); | ||
|
||
$table->string('type')->index(); | ||
$table->json('data'); | ||
$table->json('metadata'); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists($this->tableName()); | ||
} | ||
|
||
protected function tableName(): string | ||
{ | ||
return config('verbs.tables.events', 'verb_events'); | ||
} | ||
}; |
77 changes: 77 additions & 0 deletions
77
database/migrations/2024_04_16_115559_create_verb_snapshots_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,77 @@ | ||
<?php | ||
|
||
use Glhd\Bits\Contracts\MakesSnowflakes; | ||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Date; | ||
use Illuminate\Support\Facades\DB; | ||
use Illuminate\Support\Facades\Schema; | ||
use Thunk\Verbs\Facades\Id; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
// If we migrated before Verbs 0.5.0 we need to do a little extra work | ||
$migrating = Schema::hasTable($this->tableName()); | ||
|
||
if ($migrating) { | ||
Schema::rename($this->tableName(), '__verbs_snapshots_pre_050'); | ||
} | ||
|
||
Schema::create($this->tableName(), function (Blueprint $table) { | ||
$table->snowflakeId(); | ||
|
||
// The 'state_id' column needs to be set up differently depending on | ||
// if you're using Snowflakes vs. ULIDs/etc. | ||
Id::createColumnDefinition($table, 'state_id'); | ||
|
||
$table->string('type')->index(); | ||
$table->json('data'); | ||
|
||
$table->snowflake('last_event_id')->nullable(); | ||
|
||
$table->timestamp('expires_at')->nullable()->index(); | ||
$table->timestamps(); | ||
|
||
$table->index(['state_id', 'type']); | ||
}); | ||
|
||
if ($migrating) { | ||
DB::table('__verbs_snapshots_pre_050') | ||
->select('*') | ||
->chunkById(100, $this->migrateChunk(...)); | ||
} | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists($this->tableName()); | ||
|
||
if (Schema::hasTable('__verbs_snapshots_pre_050')) { | ||
Schema::rename('__verbs_snapshots_pre_050', $this->tableName()); | ||
} | ||
} | ||
|
||
protected function migrateChunk(Collection $chunk): void | ||
{ | ||
$rows = $chunk->map(fn ($row) => [ | ||
'id' => app(MakesSnowflakes::class)->makeFromTimestamp(Date::parse($row->created_at))->id(), | ||
'type' => $row->type, | ||
'state_id' => $row->id, | ||
'data' => $row->data, | ||
'last_event_id' => $row->last_event_id, | ||
'expires_at' => null, | ||
'created_at' => $row->created_at, | ||
'updated_at' => $row->updated_at, | ||
]); | ||
|
||
DB::table($this->tableName())->insert($rows->toArray()); | ||
} | ||
|
||
protected function tableName(): string | ||
{ | ||
return config('verbs.tables.snapshots', 'verb_snapshots'); | ||
} | ||
}; |
41 changes: 41 additions & 0 deletions
41
database/migrations/2024_04_16_115559_create_verb_state_events_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,41 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
use Thunk\Verbs\Facades\Id; | ||
|
||
return new class extends Migration | ||
{ | ||
public function up() | ||
{ | ||
// If they've already migrated under the previous migration name, just skip | ||
if (Schema::hasTable($this->tableName())) { | ||
return; | ||
} | ||
|
||
Schema::create($this->tableName(), function (Blueprint $table) { | ||
$table->snowflakeId(); | ||
|
||
$table->snowflake('event_id')->index(); | ||
|
||
// The 'state_id' column needs to be set up differently depending | ||
// on if you're using Snowflakes vs. ULIDs/etc. | ||
Id::createColumnDefinition($table, 'state_id')->index(); | ||
|
||
$table->string('state_type')->index(); | ||
|
||
$table->timestamps(); | ||
}); | ||
} | ||
|
||
public function down() | ||
{ | ||
Schema::dropIfExists($this->tableName()); | ||
} | ||
|
||
protected function tableName(): string | ||
{ | ||
return config('verbs.tables.state_events', 'verb_state_events'); | ||
} | ||
}; |
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