Skip to content
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

Allow create table with array field constraints #1470

Merged
merged 2 commits into from
Nov 14, 2018

Conversation

natanfelles
Copy link
Contributor

Description

See #976

Testing only ENUM in MySQLi - but all constraints set as array will be escaped and imploded, works with MySQL SET Data Type too.

Checklist:

  • Securely signed commits
  • Component(s) with PHPdocs
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

Copy link
Member

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does only work with Mysql? The test seems to imply that. If that's the case, shouldn't this be in the MySqli/Forge class?

@natanfelles
Copy link
Contributor Author

Perhaps. It is intended to work with any driver.

I am still not aware of Data Types in other DBMS besides MySQL, PostgreSQL and SQLite.

I found only the MySQL ENUM and SET Data Types allowing a list of values so far. So testing only once.

@lonnieezell
Copy link
Member

Well, Postgres definitely has Enum.

sqlite3 has checks, though it's not quite the same thing.

At the very least the Postgres test should happen. Not too concerned about the sqlite one, though that would be a bonus.

@natanfelles
Copy link
Contributor Author

natanfelles commented Nov 13, 2018

But these are the queries:

-- crate the type "mood" as ENUM
CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
-- create table using the "mood" - ENUM is not used inside CRATE TABLE query
CREATE TABLE person (
    name text,
    current_mood mood
);

SQLite CHECK seems to me to be a hack to emulate MySQL ENUM.

Thinking about doing something to not escape constraints like this, if the driver is SQLite:

$db_columns = array(
    'status' => array(
        'type'       => 'TEXT',
        'constraint' => 'CHECK( status IN ("locked","unlocked") )',
        'default'    => 'unlocked',
        'comment'    => 'Status of asset (Locked or Unlocked)'
    )
); 

@natanfelles
Copy link
Contributor Author

Or, convert the type ENUM to TEXT and the CHECK in the constraint... It allow the same code works in mysql and sqlite

@natanfelles
Copy link
Contributor Author

The postgres "created type" can be used in other tables/fields too.

@lonnieezell
Copy link
Member

Oh, good point about Postgres.

And you're right that Check is a hack. I only mentioned it because it looked like it would have been implemented with an array.

@natanfelles
Copy link
Contributor Author

Can be. But the user also can:

$db_columns = array(
    'status' => array(
        'type'       => 'TEXT CHECK( status IN ("locked","unlocked") )',
    )
); 

Or we allow CodeIgniter make the conversion:

// sqlite: status TEXT CHECK( status IN ('unlocked','locked') )
$db_columns = array(
    'status' => array(
        'type'       => 'ENUM',
        'constraint' => ['unlocked', 'locked'],
    )
); 

The same PHP code would works with mysql and sqlite

@lonnieezell
Copy link
Member

for this, I like letting CI convert it. The most common use case I see for SQLite on the web is running tests, even though the web apps are actually using a different engine, like Mysql or Postgres in production. Converting it on the fly would be one less pain when trying to test your app. And, as you said, they can use check for other things as a text item.

Would you mind doing that conversion?

@natanfelles
Copy link
Contributor Author

Yes.

Also being able to "create the type" for postgres on the fly. But I fear that this will lead to collisions.

@lonnieezell
Copy link
Member

Also being able to "create the type" for postgres on the fly. But I fear that this will lead to collisions.

I agree - and since creating the type is done outside of the create table I wouldn't worry about that one. Just sqlite.

@lonnieezell lonnieezell merged commit 9eef536 into codeigniter4:develop Nov 14, 2018
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.

2 participants