sea-query icon indicating copy to clipboard operation
sea-query copied to clipboard

Add support for Postgresql's function and trigger

Open HigherOrderLogic opened this issue 2 years ago • 12 comments

Motivation

Give users the ablity to create functions and triggers on database by using sea-query instead of raw SQL.

Proposed Solutions

N/A

Additional Information

https://www.postgresql.org/docs/current/plpgsql-trigger.html

HigherOrderLogic avatar Aug 04 '22 08:08 HigherOrderLogic

Hey @HigherOrderLogic, thanks for the feature request!

I think creating, dropping and altering of function and trigger can be offered under sea_query::extension::postgres module.

billy1624 avatar Aug 08 '22 10:08 billy1624

I'll open this for contributions! :)

billy1624 avatar Aug 08 '22 10:08 billy1624

@billy1624 @tyt2y3 do we really want to add support for this functionality?

ikrivosheev avatar Aug 20 '22 11:08 ikrivosheev

If you are open to contribution for this issue, then I'd like to work on it

anshulxyz avatar Sep 14 '22 21:09 anshulxyz

Hey @anshulxyz, this is still open for contribution! Please go ahead :D

billy1624 avatar Sep 20 '22 07:09 billy1624

@billy1624 I'm not sure whether @anshulxyz is still working on this issue or not but I'm going to start working to add support for the trigger first

stevenhansel avatar Oct 06 '22 07:10 stevenhansel

@stevenhansel I'm not, you can take over the issue.

anshulxyz avatar Oct 06 '22 07:10 anshulxyz

Thanks!! @stevenhansel Let me know if you need any help :)

billy1624 avatar Oct 06 '22 09:10 billy1624

@billy1624 I'm not sure whether @anshulxyz is still working on this issue or not but I'm going to start working to add support for the trigger first

Do you have any idea about the API design in mind?

HigherOrderLogic avatar Oct 06 '22 14:10 HigherOrderLogic

Hey @HigherOrderLogic, you can take this as a reference TypeCreateStatement. We need to create a struct let say TriggerCreateStatement under extension/postgres module since this is Postgres specific. Looking at the docs CREATE TRIGGER.

I imagine the API be like...

enum Event {
    Insert,
    Update(Vec<ColumnRef>),
    Delete,
    Truncate,
}

Trigger::create()
    .or_replace()
    .name(NameIden)
    .table(TableIden)
    // One of the event that fire the trigger
    .before_event(Event::Insert)
    .after_event(Event::Update(vec![ ... ]))
    .instead_of_event(Event::Delete)
    // How the trigger function should be fired
    .for_each_row()
    .for_each_statement()
    // Function to execute
    .execute_function(Func)
    .to_string(PostgresQueryBuilder);

billy1624 avatar Oct 07 '22 08:10 billy1624

Do you have any idea about the API design in mind?

@HigherOrderLogic I'm mostly thinking the same thing with what @billy1624 wrote, for creating a trigger we can just follow the existing TypeCreateStatement pattern which I believe fundamentally follows the Builder Pattern. For the other one, such as ALTER and DROP, well we also have the same counterparts e.g. TableAlterStatement, TableDropStatement (for the tables).

e.g. this is how you would drop a table using seaquery (from the docs)

let table = Table::alter()
    .table(Font::Table)
    .add_column(
        ColumnDef::new(Alias::new("new_col"))
            .integer()
            .not_null()
            .default(100),
    )
    .to_owned();

Well, this is probably a rough counterpart for the Trigger::drop, let me know if you have any comments

Trigger::drop()
    .if_exists()
    .name(NameIden)
    .table(TableIden);

stevenhansel avatar Oct 07 '22 14:10 stevenhansel