from migrations.fixtures.delphi_tables.v8.triggers.base_trigger import BaseTrigger, TriggerType from migrations.fixtures.delphi_tables.v7.triggers.queries.user_projects_queries import UserProjectsQueries class ArtistTeamUserTrigger(BaseTrigger): table = "ArtistTeamUser" function_name = "log_artist_team_user_changes" type = TriggerType.STATEMENT.value def on_insert(self): return f""" DELETE FROM user_projects where user_id IN (SELECT new_table.user_id FROM new_table); INSERT INTO user_projects(project_id, user_id) {UserProjectsQueries.get_user_projects( where_clause='project.is_deleted IS FALSE AND dec_user.id IN (SELECT new_table.user_id FROM new_table)' )}; """ def on_delete(self): return f""" DELETE FROM user_projects where user_id IN (SELECT old_table.user_id FROM old_table); -- We need to restore user_projects when user is owner in some of projects or is a collaborator INSERT INTO user_projects(project_id, user_id) {UserProjectsQueries.get_user_projects( where_clause='project.is_deleted IS FALSE AND dec_user.id IN (SELECT old_table.user_id FROM old_table)' )}; """