Skip to content

Commit

Permalink
[IMP]bi_sql_editor: archive Scheduled Action to not recreate when res…
Browse files Browse the repository at this point in the history
…etting the SQL View

Based on the agreed solution: #903 (review). It is best to archive the Scheduled Action every time you set back to the draft state. Then, when creating the SQL view, if it is a materialized view, unarchive the Scheduled Action if already exists, otherwise create it from scratch.
  • Loading branch information
GuillemCForgeFlow committed Sep 2, 2024
1 parent c3f636d commit b2d1aec
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions bi_sql_editor/models/bi_sql_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

@api.model
def _instanciate(self, model_data):
""" Return a class for the custom model given by
parameters ``model_data``. """
"""Return a class for the custom model given by
parameters ``model_data``."""

# This monkey patch is meant to avoid create/search tables for those
# materialized views. Doing "super" doesn't work.
class CustomModel(models.Model):
Expand Down Expand Up @@ -310,9 +311,13 @@ def button_create_sql_view_and_model(self):
sql_view._create_index()

if sql_view.is_materialized:
sql_view.cron_id = (
self.env["ir.cron"].create(sql_view._prepare_cron()).id
)
# Create Cron only if it has not been created yet, otherwise unarchive it
if sql_view.cron_id and not sql_view.cron_id.active:
sql_view.cron_id.write({"active": True})
else:
sql_view.cron_id = (
self.env["ir.cron"].create(sql_view._prepare_cron()).id
)
sql_view.state = "model_valid"

def button_set_draft(self):
Expand All @@ -324,7 +329,7 @@ def button_set_draft(self):
sql_view.pivot_view_id.unlink()
sql_view.search_view_id.unlink()
if sql_view.cron_id:
sql_view.cron_id.unlink()
sql_view.cron_id.write({"active": False})

if sql_view.state in ("model_valid", "ui_valid"):
# Drop SQL View (and indexes by cascade)
Expand Down

0 comments on commit b2d1aec

Please sign in to comment.