Skip to content

Commit

Permalink
Refactor version prefix functionality for new version numbers/keys (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
kfriars authored Jan 10, 2025
1 parent 489fac3 commit ffcb450
Show file tree
Hide file tree
Showing 16 changed files with 155 additions and 128 deletions.
22 changes: 22 additions & 0 deletions config/snapshots.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
<?php

return [
/*
|--------------------------------------------------------------------------
| Migration Regex
|--------------------------------------------------------------------------
|
| The format indicated by this regex must be adhered to for all migration files
*/
'migration_regex' => '/\d{4}_\d{2}_\d{2}_\d{6}_[a-zA-Z0-9_]+/',

/*
|--------------------------------------------------------------------------
| Models
Expand All @@ -18,6 +27,19 @@
'history' => \Plank\Snapshots\Models\History::class,
],

/*
|--------------------------------------------------------------------------
| Value Objects
|--------------------------------------------------------------------------
|
| version_number:
| This object adds some helper methods for working with version numbers.
| It must implement the \Plank\Snapshots\Contracts\VersionKey interface.
*/
'value_objects' => [
'version_number' => \Plank\Snapshots\ValueObjects\VersionNumber::class,
],

/*
|--------------------------------------------------------------------------
| Repositories
Expand Down
35 changes: 1 addition & 34 deletions src/Concerns/AsVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Plank\Snapshots\Concerns;

use Illuminate\Support\Facades\Schema;
use Plank\Snapshots\Contracts\Version;

/**
* @mixin \Illuminate\Database\Eloquent\Model
Expand All @@ -20,38 +19,6 @@ public function hasBeenMigrated(): bool
return Schema::hasTable($this->getTable());
}

/**
* {@inheritDoc}
*/
public function addMigrationPrefix(string $name): string
{
return $this->number->key().'_'.$name;
}

/**
* {@inheritDoc}
*/
public static function stripMigrationPrefix(string $name): string
{
return (string) str($name)->replaceMatches('/^v\d+_\d+_\d+_/', '');
}

/**
* {@inheritDoc}
*/
public function resolveVersionFromMigrationName(string $name): ?Version
{
$prefix = str($name)->match('/^v\d+_\d+_\d+/', '');

if ($prefix->isEmpty()) {
return null;
}

$number = (string) $prefix->replace('v', '')->replace('_', '.');

return $this->newQuery()->where('number', $number)->first();
}

/**
* {@inheritDoc}
*/
Expand All @@ -63,7 +30,7 @@ public function addTablePrefix(string $table): string
/**
* {@inheritDoc}
*/
public function stripTablePrefix(string $table): string
public static function stripTablePrefix(string $table): string
{
return (string) str($table)->replaceMatches('/^v\d+_\d+_\d+_/', '');
}
Expand Down
9 changes: 6 additions & 3 deletions src/Concerns/AsVersionedContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Illuminate\Database\Eloquent\Model;
use Plank\LaravelHush\Concerns\HushesHandlers;
use Plank\Snapshots\Contracts\Version as VersionContract;
use Plank\Snapshots\Contracts\Version;
use Plank\Snapshots\Facades\Versions;

/**
Expand All @@ -31,10 +31,13 @@ public function activeVersion(): ?static
*/
public function getTable()
{
$version = app(VersionContract::class);
$table = parent::getTable();

/** @var Version $class */
$class = config('snapshots.models.version');

// Ensure we are starting from the user/framework defined table name
$table = $version::stripMigrationPrefix(parent::getTable());
$table = $class::stripTablePrefix($table);

if ($version = Versions::active()) {
$table = $version->addTablePrefix($table);
Expand Down
58 changes: 53 additions & 5 deletions src/Concerns/HasVersionedSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,68 @@
use Plank\Snapshots\Contracts\ManagesVersions;
use Plank\Snapshots\Contracts\Version;
use Plank\Snapshots\Contracts\Versioned;
use Plank\Snapshots\Contracts\VersionKey;
use Plank\Snapshots\Events\TableCreated;
use Plank\Snapshots\Exceptions\MigrationFormatException;
use Plank\Snapshots\Migrator\SnapshotBlueprint;

trait HasVersionedSchema
{
public function __construct(
Connection $connection,
protected ManagesVersions $versions,
protected ManagesCreatedTables $tables
protected ManagesCreatedTables $tables,
) {
parent::__construct($connection);

$this->blueprintResolver(fn ($table, $callback, $prefix) => new SnapshotBlueprint($table, $callback, $prefix));
}

/**
* {@inheritDoc}
*/
public function addMigrationPrefix(Version $version, string $migration): string
{
return $version->number->key().'_'.$migration;
}

/**
* {@inheritDoc}
*/
public function stripMigrationPrefix(string $migration): string
{
$regex = config('snapshots.migration_regex');

$matches = [];

if (preg_match($regex, $migration, $matches) !== 1) {
throw MigrationFormatException::create($migration);
}

return $matches[0];
}

/**
* {@inheritDoc}
*/
public function versionFromMigration(string $migration): ?Version
{
/** @var class-string<VersionKey> $class */
$keyClass = config('snapshots.value_objects.version_number');

$stripped = $this->stripMigrationPrefix($migration);

$prefix = str($migration)->before('_'.$stripped);

try {
$key = $keyClass::fromVersionString($prefix);
} catch (InvalidArgumentException) {
return null;
}

return $this->versions->byNumber($key);
}

/**
* {@inheritDoc}
*/
Expand All @@ -42,9 +89,7 @@ public function create($table, Closure $callback): void
}

/**
* Create a new table on the schema.
*
* @param class-string<Model> $model
* {@inheritDoc}
*/
public function createForModel(string $model, Closure $callback): void
{
Expand All @@ -54,7 +99,10 @@ public function createForModel(string $model, Closure $callback): void

$active = $this->versions->active();
$table = (new $model)->getTable();
$original = app(Version::class)::stripMigrationPrefix($table);

$original = $active
? $active->stripTablePrefix($table)
: $table;

parent::create($table, $callback);

Expand Down
18 changes: 1 addition & 17 deletions src/Contracts/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@ interface Version
*/
public function hasBeenMigrated(): bool;

/**
* Add a prefix to the migration name which is stored in the repository
* when the migration is run.
*/
public function addMigrationPrefix(string $name): string;

/**
* Remove the prefix from a migration name.
*/
public static function stripMigrationPrefix(string $name): string;

/**
* Resolve a Version from a migration name.
*/
public function resolveVersionFromMigrationName(string $name): ?self;

/**
* Add a prefix for to the table names specified in SnapshotMigrations, and
* also the Eloquent Models getTable() methods.
Expand All @@ -39,7 +23,7 @@ public function addTablePrefix(string $table): string;
/**
* Remove the prefix from a table name.
*/
public function stripTablePrefix(string $table): string;
public static function stripTablePrefix(string $table): string;

/**
* Determine if the Version has already been migrated.
Expand Down
15 changes: 15 additions & 0 deletions src/Contracts/VersionedSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,19 @@ public function createForModel(string $model, Closure $callback): void;
* @param \Closure $callback
*/
public function dropForModel($model): void;

/**
* Add a Version's prefix to a migration name
*/
public function addMigrationPrefix(Version $version, string $migration): string;

/**
* Strip a Version's prefix from a migration name
*/
public function stripMigrationPrefix(string $migration): string;

/**
* Resolve a Version from a migration name.
*/
public function versionFromMigration(string $name): ?Version;
}
11 changes: 11 additions & 0 deletions src/Exceptions/MigrationFormatException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Plank\Snapshots\Exceptions;

class MigrationFormatException extends SnapshotsException
{
public static function create(string $migration): self
{
return new self("The given {$migration} does not meet the format defined in the snapshots configuration file.");
}
}
16 changes: 0 additions & 16 deletions src/Exceptions/VersionException.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Migrator/SnapshotBlueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ protected function indexCommand($type, $columns, $index, $algorithm = null)
$index = $index ?: $this->createIndexName($type, $columns);

if ($version = Versions::active()) {
$index = $version->stripMigrationPrefix($index);
$index = $version->addMigrationPrefix($index);
$index = $version->stripTablePrefix($index);
$index = $version->addTablePrefix($index);
}

return $this->addCommand(
Expand Down
Loading

0 comments on commit ffcb450

Please sign in to comment.