Skip to content

Commit

Permalink
Sync Migration Files & Support uniqueIds (#8)
Browse files Browse the repository at this point in the history
* sync migration stubs

* - Refactor model to generate unique nanoids for multiple columns
- Support nanoids in models with auto-increment models

* document uniqueIds changes
  • Loading branch information
yondifon authored Feb 11, 2024
1 parent f85cc33 commit 819e35f
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.vscode
.DS_Store
.phpunit.result.cache
.phpunit.cache
.php_cs.cache
_lighthouse_ide_helper.php
composer.lock
Expand Down
10 changes: 9 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ To create a new migration, use the artisan command `make:nanoid-migration`. All
```php
<?php

class YourModel Extends \Illuminate\Database\Eloquent\Model
{
/** @var array|int */
protected $nanoidLength = 10;
// id will be of length 10
Expand All @@ -77,7 +79,13 @@ To create a new migration, use the artisan command `make:nanoid-migration`. All
return 'ABC'; // pay_ACBACB
}


public function uniqueIds()
{
// will create nanonids for 'unique_id' &'another_with'
// Also, won't break if id is not listed.
return ['unique_id', 'another_id'];
}
}
```

Check the upgrade guide if you're [upgrading](UPGRADE.MD) from 0.x
Expand Down
10 changes: 3 additions & 7 deletions src/Console/Commands/stubs/migration.create.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {{ class }} extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::create('{{ table }}', function (Blueprint $table) {
$table->string('id')->primary();
Expand All @@ -22,10 +20,8 @@ class {{ class }} extends Migration

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::dropIfExists('{{ table }}');
}
Expand Down
10 changes: 3 additions & 7 deletions src/Console/Commands/stubs/migration.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {{ class }} extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
//
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
//
}
Expand Down
10 changes: 3 additions & 7 deletions src/Console/Commands/stubs/migration.update.stub
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class {{ class }} extends Migration
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
public function up(): void
{
Schema::table('{{ table }}', function (Blueprint $table) {
//
Expand All @@ -20,10 +18,8 @@ class {{ class }} extends Migration

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
public function down(): void
{
Schema::table('{{ table }}', function (Blueprint $table) {
//
Expand Down
28 changes: 25 additions & 3 deletions src/HasNanoids.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,24 @@ trait HasNanoids
protected static function bootHasNanoids(): void
{
static::creating(function (self $model) {
$model->{$model->getKeyName()} = $model->generateNanoid();
foreach ($model->uniqueIds() as $column) {
if (! $model->getAttribute($column)) {
$model->setAttribute($column, $model->generateNanoid());
}
}
});
}

/**
* Get the columns that should receive a unique identifier.
*
* @return array
*/
public function uniqueIds()
{
return [$this->getKeyName()];
}

/**
* Generate a nanoid.
*/
Expand Down Expand Up @@ -87,7 +101,11 @@ protected function getNanoIdAlphabet(): ?string
*/
public function getKeyType()
{
return 'string';
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return 'string';
}

return $this->keyType;
}

/**
Expand All @@ -97,6 +115,10 @@ public function getKeyType()
*/
public function getIncrementing()
{
return false;
if (in_array($this->getKeyName(), $this->uniqueIds())) {
return false;
}

return $this->incrementing;
}
}
56 changes: 56 additions & 0 deletions tests/HasNanoidsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,51 @@
expect(Str::length($model->getKey()))->toBe(3);
});

it('creates nanoid with multiple ids before saving', function () {
$model = BasicModelWithMultipleIds::create();

expect($model->getKey())->toBeString();
expect($model->another_id)->toBeString();
});

it('creates multiple nanoids that are unique', function () {
$model = BasicModelWithMultipleIds::create();

expect($model->getKey())->toBeString();
expect($model->another_id)->toBeString();

expect($model->getKey())->not->toBe($model->another_id);
});

it("doesn't override existing id", function () {
$model = BasicModelWithFillable::create(['another_id' => '123']);

expect($model->another_id)->toBe('123');
});

it('creates nanoid for columns in models with auto-incrementing id', function () {
$model = BasicModelWithDifferentNanoIdColumn::create();

expect($model->nano_id)->toBeString();
});

abstract class ModelTest extends Model
{
use HasNanoids;

protected $table = 'test_migrations_with_string_id';
}

class BasicModelWithDifferentNanoIdColumn extends ModelTest
{
protected $table = 'test_migration_with_integer_id';

public function uniqueIds(): array
{
return ['nano_id'];
}
}

class BasicModel extends ModelTest
{
}
Expand Down Expand Up @@ -136,3 +174,21 @@ class BasicModelWithAlphabetAndLength extends ModelTest

protected $nanoidLength = 3;
}

class BasicModelWithMultipleIds extends ModelTest
{
public function uniqueIds(): array
{
return ['id', 'another_id'];
}
}

class BasicModelWithFillable extends ModelTest
{
protected $fillable = ['another_id'];

public function uniqueIds(): array
{
return ['id', 'another_id'];
}
}
10 changes: 10 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ protected function setUpDatabase(): void
->getSchemaBuilder()
->create('test_migrations_with_string_id', function (Blueprint $table): void {
$table->string('id')->primary();
$table->string('another_id')->nullable();
$table->timestamps();
});

$this->app['db']
->connection('testing')
->getSchemaBuilder()
->create('test_migration_with_integer_id', function (Blueprint $table): void {
$table->id();
$table->string('nano_id')->nullable();
$table->timestamps();
});
}
Expand Down

0 comments on commit 819e35f

Please sign in to comment.