-
Notifications
You must be signed in to change notification settings - Fork 403
Fix for issue #486 - #487
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
specialtactics
wants to merge
9
commits into
owen-it:master
from
specialtactics:bugfix/fix-for-non-integer-key-models
Closed
Fix for issue #486 - #487
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2f6d9bd
Fix for issue #486 - remove auditable_id integer cast, to allow for n…
specialtactics 23fdb09
Merge branch 'master' into bugfix/fix-for-non-integer-key-models
anteriovieira 3d16642
Merge pull request #1 from owen-it/master
specialtactics 244ac85
Merge branch 'master' into bugfix/fix-for-non-integer-key-models
specialtactics e3846cd
Don't cast id to string in every case, check the key type of the eloq…
specialtactics 6cecff5
Added a test case for a model with a UUID (or other) string key
specialtactics 2494d8a
Added UUID package to dev deps for uuid generation, just in case this…
specialtactics cf54541
StyleCI updates
specialtactics 303240e
Comment update
specialtactics File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
| <?php | ||
|
|
||
| namespace OwenIt\Auditing\Tests\Models; | ||
|
|
||
| use Illuminate\Database\Eloquent\Model; | ||
| use Illuminate\Database\Eloquent\SoftDeletes; | ||
| use OwenIt\Auditing\Contracts\Auditable; | ||
|
|
||
| class ApiModel extends Model implements Auditable | ||
| { | ||
| use \OwenIt\Auditing\Auditable; | ||
| use SoftDeletes; | ||
|
|
||
| /** | ||
| * @var string UUID key | ||
| */ | ||
| public $primaryKey = 'api_model_id'; | ||
|
|
||
| /** | ||
| * @var bool Set to false for UUID keys | ||
| */ | ||
| public $incrementing = false; | ||
|
|
||
| /** | ||
| * @var string Set to string for UUID keys | ||
| */ | ||
| protected $keyType = 'string'; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected $dates = [ | ||
| 'published_at', | ||
| ]; | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| protected $fillable = [ | ||
| 'api_model_id', | ||
| 'content', | ||
| 'published_at', | ||
| ]; | ||
| } |
This file contains hidden or 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 hidden or 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,20 @@ | ||
| <?php | ||
|
|
||
| use Faker\Generator as Faker; | ||
| use OwenIt\Auditing\Tests\Models\ApiModel; | ||
| use Ramsey\Uuid\Uuid; | ||
|
|
||
| /* | ||
| |-------------------------------------------------------------------------- | ||
| | APIModel Factories | ||
| |-------------------------------------------------------------------------- | ||
| | | ||
| */ | ||
|
|
||
| $factory->define(ApiModel::class, function (Faker $faker) { | ||
| return [ | ||
| 'api_model_id' => Uuid::uuid4(), | ||
| 'content' => $faker->unique()->paragraph(6), | ||
| 'published_at' => null, | ||
| ]; | ||
| }); |
34 changes: 34 additions & 0 deletions
34
tests/database/migrations/0000_00_00_000003_create_api_models_test_table.php
This file contains hidden or 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,34 @@ | ||
| <?php | ||
|
|
||
| use Illuminate\Database\Migrations\Migration; | ||
| use Illuminate\Database\Schema\Blueprint; | ||
| use Illuminate\Support\Facades\Schema; | ||
|
|
||
| class CreateApiModelsTestTable extends Migration | ||
| { | ||
| /** | ||
| * Run the migrations. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function up() | ||
| { | ||
| Schema::create('api_models', function (Blueprint $table) { | ||
| $table->uuid('api_model_id'); | ||
| $table->text('content'); | ||
| $table->timestamp('published_at')->nullable(); | ||
| $table->timestamps(); | ||
| $table->softDeletes(); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Reverse the migrations. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function down() | ||
| { | ||
| Schema::drop('api_models'); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why does this break non-integer PKs? If you override the model in your code to use another type of PK, you can also override the
$casts. In my opinion, this should not be changed as it would force others (like me) that are using the default configuration to add this cast manually in a customized model.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Namoshek This is a vendor model, you can't just override it. The reason this breaks non-integer PKs is because it casts UUID PKs (or any string PKs) as integer, thereby changing them completely. I added a test in my PR which demonstrates this.
The cast shouldn't be necessary in any case - because auditable_id could be of any type in theory (but in particular integer or string), so this package shouldn't make assumptions as to it's type - especially given Laravel/Eloquent itself supports string key types directly.
So you understand, if this package casts auditable_id as integer, it will not work with what Laravel directly supports out of the box (ie. string primary keys).
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Of course you can override it. It is the first option in the config:
laravel-auditing/config/audit.php
Lines 5 to 14 in eff1fe2
In fact, I'm using a custom implementation of the model myself as I'm using a different base model for all of my models due to some added functionality. 👍
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Namoshek So can you explain why someone would need to override this model for acquire it's compatibility with default Laravel functionality?
Given it's been working with this functionality for the longest time, do you think it's appropriate to have just suddenly broken it?
If you have some special case where it doesn't work for you, then perhaps you should override it yourself, but the way it is in my PR works for all keys of all major databases (Postgres, MySQL, MariaDB, etc).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I do have a reasoning why it should be an integer cast:
In the end it is the decision of the maintainer. He did announce the change in the changelog, although I have to admit that it should have been a minor release as it may break BC as in your case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Namoshek The fact that it's "default" makes no difference, as both are equally supported functionality, and it will work for both without casting.
You can not go making assumptions what "the majority" of users are using, and it is not relevant anyway - so long as it's a core feature of Laravel, this package shouldn't break that.