Skip to content

Better support for enums. #53

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
wants to merge 3 commits into from
Closed

Conversation

TheDoctor0
Copy link

@TheDoctor0 TheDoctor0 commented Jun 27, 2020

Fix proposed in #51 for issue #50 will remove SQL error, but also make enum fields always a $faker->word in generated factories.
PostgreSQL implementation of enum does not store allowed values in type definition, instead there is a field constraint.

I came up with solution that supports both MySQL and PostgreSQL.
Right now it should work even without a custom enum type for Doctrine.

I tested it with PostgreSQL and this migration:

Schema::create('tests', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->enum('test', ['one', 'two', 'three']);
    $table->timestamps();
});            

Generated factory:

$factory->define(App\Models\Test::class, function (Faker $faker) {
    return [
        'test' => $faker->randomElement(['one', 'two', 'three']),
    ];
});

Changes that were made:

  • Model object is passed all the way to enumValues function to check connection driver name.
  • Function enumValues is not static anymore (I honestly don't know why it was).
  • Enum values parsing from field constraint for PostgreSQL has been implemented based on this question on StackOverflow.
  • Field mapped in setProperty is returned instantly if matches any of $fakeableNames, so it won't make a DB call for enum.
  • Field is set to enum with $faker->randomElement if any enum values are present (only if it's an actual enum field in database).

@duellsy
Copy link

duellsy commented Aug 19, 2020

Gave this a run in my project today which is using postgres, worked perfectly 💯

@TheDoctor0
Copy link
Author

@jasonmccreary, can u review and potentially merge this PR?

@jasonmccreary
Copy link
Collaborator

I'd like to see more verify it. I don't use enums or Postgres. So I'm worried about merging it and breaking something else...

@TheDoctor0
Copy link
Author

I completely understand the concerns.
I'll try to make some time in coming days and test this for various field types (with and without enums)
on Postgres, MySQL and SQLite to ensure that everything is working as it should.

@TheDoctor0
Copy link
Author

TheDoctor0 commented Sep 3, 2020

Finally I had some time to check this. Two additional changes were made:

  1. Model connection is used when checking enum column.
  2. Fixed enum column check on PostgreSQL 12+.

I created the test repository, it contains:

  1. Docker configuration for databases containers.
  2. Test models and migrations for SQLite, MySQL and PostgreSQL with all possible fields.
  3. Generated factories.

@jasonmccreary, as you can see, everything is working as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants