cycle / annotated

Schema generation using annotated entities and mappers
MIT License
24 stars 13 forks source link

Check constraint💡 #99

Open peldax opened 3 months ago

peldax commented 3 months ago

I have an idea!

Hi,

I would love to add a check constraint to my table using attributes.

I imagine the functionality as following, but it is just a draft.

#[Entity]
#[Index(columns: ['a'], unique: true)]
#[Check(expression: 'a > b')]
class EntityDef
{
    #[Column(type: 'integer')]
    private int $a;

    #[Column(type: 'integer')]
    private int $b;
}

I am open to bounty this feature.

roxblnfk commented 3 months ago

Hi.

The feature looks good, however we have the MySQL driver but we don't have separated MariaDB driver. If we want to add DBMS-specific feature then we have to add the driver. Then I suppose we need to add constraint field in the Schema Builder. And then we can implement the attribute.

I'm afraid I don't have enough time for this task. Would you like to do this?

peldax commented 3 months ago

Hi,

Mysql driver will suffice, as the mariadb maintains major compatibility with mysql. However, I am not sure about some other rdbms and its support for the check constraint. I think we should throw a NotSupported exception in cases where the functionality is not available on the db level.

I can try to implement it, I but cannot guarantee any timeline as I dont have much spare time right now.

roxblnfk commented 3 months ago

NotSupported exception

We don't need to throw an exception there because the same schema might be used on different databases (tests: sqlite, prod: mysql). Just subscribe supported DBMS in the docblock of the attribute.

However, I am not sure about some other rdbms and its support for the check constraint

That issue might be useful even though it was 3 years ago.

but cannot guarantee any timeline

Don't worry about it 👌