Skip to content

Commit

Permalink
Fix table creation
Browse files Browse the repository at this point in the history
Table creation should be called on  request, not on service creation
  • Loading branch information
lcharette committed Nov 19, 2023
1 parent 15827dd commit 24ea4e9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
33 changes: 22 additions & 11 deletions app/src/Database/Migrator/DatabaseMigrationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ public function __construct(
protected Capsule $db,
protected MigrationTable $model,
) {
// Make sure repository exist
if (!$this->exists()) {
$this->create();
}
}

/**
Expand All @@ -51,7 +47,7 @@ public function __construct(
*/
public function all(?int $steps = null, bool $asc = true): Collection
{
$query = $this->model::orderBy('id', ($asc) ? 'asc' : 'desc');
$query = $this->getTable()::orderBy('id', ($asc) ? 'asc' : 'desc');

if (!is_null($steps)) {
$batch = max($this->getNextBatchNumber() - $steps, 1);
Expand All @@ -74,7 +70,7 @@ public function list(?int $steps = null, bool $asc = true): array
*/
public function get(string $migration): object
{
$result = $this->model::forMigration($migration)->first();
$result = $this->getTable()::forMigration($migration)->first();

// Throw error if null
if ($result === null) {
Expand All @@ -89,15 +85,15 @@ public function get(string $migration): object
*/
public function has(string $migration): bool
{
return $this->model::forMigration($migration)->exists();
return $this->getTable()::forMigration($migration)->exists();
}

/**
* {@inheritDoc}
*/
public function last(): array
{
$query = $this->model::where('batch', $this->getLastBatchNumber());
$query = $this->getTable()::where('batch', $this->getLastBatchNumber());

return $query->orderBy('id', 'desc')->get()->pluck('migration')->all();
}
Expand All @@ -112,7 +108,8 @@ public function log(string $migration, ?int $batch = null): bool
$batch = $this->getNextBatchNumber();
}

$entry = new $this->model([
$table = $this->getTable();
$entry = new $table([
'migration' => $migration,
'batch' => $batch,
]);
Expand All @@ -125,7 +122,7 @@ public function log(string $migration, ?int $batch = null): bool
*/
public function remove(string $migration): void
{
$this->model::forMigration($migration)->delete();
$this->getTable()::forMigration($migration)->delete();
}

/**
Expand All @@ -141,7 +138,7 @@ public function getNextBatchNumber(): int
*/
public function getLastBatchNumber(): int
{
$batch = $this->model::max('batch');
$batch = $this->getTable()::max('batch');

// Default to 0 if it's null (empty table)
return ($batch === null) ? 0 : intval($batch);
Expand Down Expand Up @@ -178,6 +175,20 @@ public function exists(): bool
return $this->getSchemaBuilder()->hasTable($this->model->getTable());
}

/**
* Returns the table to use for the repository.
* Create the physical table if it doesn't exist.
*/
public function getTable(): MigrationTable
{
// Make sure repository exist
if (!$this->exists()) {
$this->create();
}

return $this->model;
}

/**
* Returns the schema builder instance.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public function testRepositoryCreation(): void
$this->ci->set(MigrationTable::class, new TestMigration());
$repository = $this->ci->get(DatabaseMigrationRepository::class);

// Create table
$repository->create();

// Table should exist
$this->assertTrue($builder->hasTable('migrationTest'));
$this->assertTrue($repository->exists());
Expand Down

0 comments on commit 24ea4e9

Please sign in to comment.