NtimYeboah / laravel-database-trigger

Generate migration file for database trigger
MIT License
20 stars 5 forks source link

Trigger API #1

Closed NtimYeboah closed 6 years ago

NtimYeboah commented 6 years ago

Description:

Currently, the trigger api has 3 methods:- insert, update and statement

  1. insert - Run statements to insert into a table when a trigger is called

    Schema::create('before_employees_update, 'employees', function(QueryStatement $query) {
    $query->insert('employees_audit', function() {
         return 'first_name=old.first_name';
    })
    })->before()->update();
  2. update - Run statements to update a table when a trigger is called

    Schema::create('before_employees_update', 'employees', function(QueryStatement $query) {
    $query->update('employees_audit', function() {
         return 'first_name=old.first_name';
    })
    })->before()->update();
  3. statement - Run arbitrary sql statements when trigger is called

    Schema::create('after_orders_update', 'employees', function(QueryStatement $query) {
    $query->statement(function() {
         return "delete from orders where status = 'pending'";
    })
    })->after()->update();

Can the API be simplified, do we need to add or take away some methods?

faddai commented 6 years ago

The current API looks OK to me.

Though, instead of after()->update() and before()->update(), they can be collapsed into;

NtimYeboah commented 6 years ago

@faddai That is a good one, it will be added to the API.

NtimYeboah commented 6 years ago

@faddai I have changed the trigger API to the following.

Schema::create('after_users_update')
       ->on('users')
       ->statement(function() {
              return 'insert into `users_audit` (`name`, `email`) values (old.name, old.email);';
       })
       ->after()
       ->update();

There will only be one method statement which will be used to run insert, update and arbitrary statements.