Skip to content

Commit

Permalink
fix: auto add content column to table if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
ryangjchandler committed Nov 7, 2024
1 parent a1bc247 commit f5bce3f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 8 deletions.
14 changes: 8 additions & 6 deletions src/Actions/InitialiseOrbitalTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Orbit\Contracts\Driver;
use Orbit\Contracts\ModifiesSchema;
use Orbit\Contracts\Orbit;
use Orbit\Support\ConfigureBlueprintFromModel;
use ReflectionClass;
Expand All @@ -21,7 +23,7 @@ public function shouldInitialise(Orbit&Model $model): bool
return ($modelFileMTime > $databaseMTime) || ! $schemaBuilder->hasTable($model->getTable());
}

public function migrate(Orbit&Model $model): void
public function migrate(Orbit&Model $model, Driver $driver): void
{
$table = $model->getTable();
$schemaBuilder = $model->resolveConnection()->getSchemaBuilder();
Expand All @@ -30,12 +32,12 @@ public function migrate(Orbit&Model $model): void
$schemaBuilder->drop($table);
}

$blueprint = null;
$schemaBuilder->create($table, static function (Blueprint $table) use ($model, $driver) {
ConfigureBlueprintFromModel::configure($model, $table);

$schemaBuilder->create($table, static function (Blueprint $table) use (&$blueprint, $model) {
$blueprint = $table;

ConfigureBlueprintFromModel::configure($model, $blueprint);
if ($driver instanceof ModifiesSchema) {
$driver->schema($table);
}
});
}
}
2 changes: 1 addition & 1 deletion src/Concerns/Orbital.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static function bootOrbital()
$refreshed = false;

if ($initialiseOrbitTable->shouldInitialise($model)) {
$initialiseOrbitTable->migrate($model);
$initialiseOrbitTable->migrate($model, $driver);
$maybeRefreshDatabaseContent->refresh($model, $driver);

$refreshed = true;
Expand Down
10 changes: 10 additions & 0 deletions src/Contracts/ModifiesSchema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Orbit\Contracts;

use Illuminate\Database\Schema\Blueprint;

interface ModifiesSchema
{
public function schema(Blueprint $table): void;
}
12 changes: 11 additions & 1 deletion src/Drivers/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,22 @@

namespace Orbit\Drivers;

use Illuminate\Database\Schema\Blueprint;
use Orbit\Contracts\Driver;
use Orbit\Contracts\ModifiesSchema;
use Orbit\Support\BlueprintUtilities;
use Spatie\YamlFrontMatter\YamlFrontMatter;
use Symfony\Component\Yaml\Yaml;

class Markdown implements Driver
class Markdown implements Driver, ModifiesSchema
{
public function schema(Blueprint $table): void
{
if (! BlueprintUtilities::hasColumn($table, 'content')) {
$table->text('content')->nullable();
}
}

public function parse(string $fileContents): array
{
$document = YamlFrontMatter::parse($fileContents);
Expand Down
15 changes: 15 additions & 0 deletions src/Support/BlueprintUtilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Orbit\Support;

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Schema\ColumnDefinition;

/** @internal */
class BlueprintUtilities
{
public static function hasColumn(Blueprint $table, string $column): bool
{
return collect($table->getColumns())->contains(static fn (ColumnDefinition $definition): bool => $definition->name === $column);

Check failure on line 13 in src/Support/BlueprintUtilities.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to an undefined property Illuminate\Database\Schema\ColumnDefinition::$name.
}
}
11 changes: 11 additions & 0 deletions tests/Feature/Support/BlueprintUtilitiesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Orbit\Support\BlueprintUtilities;

it('can determine column existence in blueprint', function () {
$blueprint = new Blueprint('test');
$blueprint->text('content')->nullable();

expect(BlueprintUtilities::hasColumn($blueprint, 'content'))->toBeTrue();
});

0 comments on commit f5bce3f

Please sign in to comment.