partidodev / partido-server

Java based server and API for Partido - a platform independent tool to split and share expenses in groups.
https://partido.rocks
MIT License
1 stars 1 forks source link

Delete group if last user leaves #12

Open jljabben opened 3 years ago

jljabben commented 3 years ago

In case the last user of a group leaves the group too, there should be a job to delete the group with all it's bills and splits. Currently the groups will remain in database. A SQL script is needed to remove orphan groups for the first time.

Alternative: a cron job that checks the groups and deletes those that don't have any members anymore.

jljabben commented 3 years ago

SQL script to delete orphan groups with all it's bills and splits from database

DELETE FROM "split" WHERE "bill_id" IN
  (SELECT "id" FROM "bill" WHERE "group_id" IN
    (SELECT "id" FROM "group" WHERE "id" NOT IN
      (SELECT "group_id" FROM "groups_users")
    )
  );
DELETE FROM "bill" WHERE "group_id" IN
  (SELECT "id" FROM "group" WHERE "id" NOT IN
    (SELECT "group_id" FROM "groups_users")
  );
DELETE FROM "group" WHERE "id" NOT IN
  (SELECT "group_id" FROM "groups_users");