Skip to content

Commit

Permalink
Fix shenanigans with templates steps and databases
Browse files Browse the repository at this point in the history
  • Loading branch information
TheophileDiot committed Aug 8, 2024
1 parent 0e02081 commit 65ba0f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion docs/assets/img/bunkerweb_db.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 16 additions & 16 deletions src/common/db/Database.py
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ def init_tables(self, default_plugins: List[dict], bunkerweb_version: str) -> Tu
# ? Check if the step already exists and if it has changed
step_found = False
for i, old_step in enumerate(old_data.get("bw_template_steps", [])):
if old_step.template_id == template_id and old_step.step == step_id:
if old_step.template_id == template_id and old_step.id == step_id:
step_found = True
if old_step.title != step["title"] or old_step.subtitle != step["subtitle"]:
self.logger.warning(
Expand All @@ -928,7 +928,7 @@ def init_tables(self, default_plugins: List[dict], bunkerweb_version: str) -> Tu
f'{plugin.get("type", "core").title()} Plugin "{plugin["id"]}"\'s Template "{template_id}"\'s Step "{step["name"]}" does not exist, creating it'
)

to_put.append(Template_steps(step=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))
to_put.append(Template_steps(id=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))

# ? Add step settings and configs for later
for setting in step.get("settings", []):
Expand All @@ -945,15 +945,15 @@ def init_tables(self, default_plugins: List[dict], bunkerweb_version: str) -> Tu
for i, old_step in enumerate(old_data.get("bw_template_steps", [])):
if old_step.template_id == template_id:
self.logger.warning(
f'{plugin.get("type", "core").title()} Plugin "{plugin["id"]}"\'s Template "{template_id}"\'s Step "{old_step.step}" has been removed, deleting it'
f'{plugin.get("type", "core").title()} Plugin "{plugin["id"]}"\'s Template "{template_id}"\'s Step "{old_step.id}" has been removed, deleting it'
)

for j, old_setting in enumerate(old_data.get("bw_template_settings", [])):
if old_setting.step_id == old_step.step:
if old_setting.step_id == old_step.id:
old_data["bw_template_settings"][j]["step_id"] = None

for j, old_config in enumerate(old_data.get("bw_template_configs", [])):
if old_config.step_id == old_step.step:
if old_config.step_id == old_step.id:
old_data["bw_template_configs"][j]["step_id"] = None

del old_data["bw_template_steps"][i]
Expand Down Expand Up @@ -2433,7 +2433,7 @@ def update_external_plugins(self, plugins: List[Dict[str, Any]], *, _type: Liter

saved_templates.add(template_id)

db_ids = [step.step for step in session.query(Template_steps).with_entities(Template_steps.step).filter_by(template_id=template_id)]
db_ids = [step.id for step in session.query(Template_steps).with_entities(Template_steps.id).filter_by(template_id=template_id)]
missing_ids = [x for x in range(1, len(template.get("steps", [])) + 1) if x not in db_ids]

if missing_ids:
Expand All @@ -2442,15 +2442,15 @@ def update_external_plugins(self, plugins: List[Dict[str, Any]], *, _type: Liter
session.query(Template_custom_configs).filter(Template_custom_configs.step_id.in_(missing_ids)).update(
{Template_custom_configs.step_id: None}
)
session.query(Template_steps).filter(Template_steps.step.in_(missing_ids)).delete()
session.query(Template_steps).filter(Template_steps.id.in_(missing_ids)).delete()

steps_settings = {}
steps_configs = {}
for step_id, step in enumerate(template.get("steps", []), start=1):
db_step = session.query(Template_steps).with_entities(Template_steps.step).filter_by(step=step_id, template_id=template_id).first()
db_step = session.query(Template_steps).with_entities(Template_steps.id).filter_by(id=step_id, template_id=template_id).first()
if not db_step:
changes = True
to_put.append(Template_steps(step=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))
to_put.append(Template_steps(id=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))
else:
updates = {}

Expand All @@ -2462,7 +2462,7 @@ def update_external_plugins(self, plugins: List[Dict[str, Any]], *, _type: Liter

if updates:
changes = True
session.query(Template_steps).filter(Template_steps.step == db_step.step).update(updates)
session.query(Template_steps).filter(Template_steps.id == db_step.id).update(updates)

for setting in step.get("settings", []):
if step_id not in steps_settings:
Expand Down Expand Up @@ -2735,7 +2735,7 @@ def update_external_plugins(self, plugins: List[Dict[str, Any]], *, _type: Liter
steps_settings = {}
steps_configs = {}
for step_id, step in enumerate(template_data.get("steps", []), start=1):
to_put.append(Template_steps(step=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))
to_put.append(Template_steps(id=step_id, template_id=template_id, title=step["title"], subtitle=step["subtitle"]))

for setting in step.get("settings", []):
if step_id not in steps_settings:
Expand Down Expand Up @@ -3233,15 +3233,15 @@ def get_templates(self, plugin: Optional[str] = None) -> Dict[str, dict]:

for step in (
session.query(Template_steps)
.with_entities(Template_steps.step, Template_steps.title, Template_steps.subtitle)
.with_entities(Template_steps.id, Template_steps.title, Template_steps.subtitle)
.filter_by(template_id=template.id)
):
templates[template.id]["steps"].append({"title": step.title, "subtitle": step.subtitle})

if step.step in steps_settings:
templates[template.id]["steps"][step.step - 1]["settings"] = steps_settings[step.step]
if step.step in steps_configs:
templates[template.id]["steps"][step.step - 1]["configs"] = steps_configs[step.step]
if step.id in steps_settings:
templates[template.id]["steps"][step.id - 1]["settings"] = steps_settings[step.id]
if step.id in steps_configs:
templates[template.id]["steps"][step.id - 1]["configs"] = steps_configs[step.id]

return templates

Expand Down
14 changes: 4 additions & 10 deletions src/common/db/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,17 +238,13 @@ class Templates(Base):

class Template_steps(Base):
__tablename__ = "bw_template_steps"
__table_args__ = (UniqueConstraint("step", "template_id"),)

id = Column(Integer, Identity(start=1, increment=1), primary_key=True)
step = Column(Integer, nullable=False)
template_id = Column(String(256), ForeignKey("bw_templates.id", onupdate="cascade", ondelete="cascade"), nullable=False)
id = Column(Integer, primary_key=True)
template_id = Column(String(256), ForeignKey("bw_templates.id", onupdate="cascade", ondelete="cascade"), primary_key=True)
title = Column(TEXT, nullable=False)
subtitle = Column(TEXT, nullable=True)

template = relationship("Templates", back_populates="steps")
settings = relationship("Template_settings", back_populates="step", cascade="all")
custom_configs = relationship("Template_custom_configs", back_populates="step", cascade="all")


class Template_settings(Base):
Expand All @@ -258,12 +254,11 @@ class Template_settings(Base):
id = Column(Integer, Identity(start=1, increment=1), primary_key=True)
template_id = Column(String(256), ForeignKey("bw_templates.id", onupdate="cascade", ondelete="cascade"), nullable=False)
setting_id = Column(String(256), ForeignKey("bw_settings.id", onupdate="cascade", ondelete="cascade"), nullable=False)
step_id = Column(Integer, ForeignKey("bw_template_steps.step", onupdate="cascade", ondelete="cascade"), nullable=True)
step_id = Column(Integer, nullable=True)
default = Column(TEXT, nullable=False)
suffix = Column(Integer, nullable=True, default=0)

template = relationship("Templates", back_populates="settings")
step = relationship("Template_steps", back_populates="settings")
setting = relationship("Settings", back_populates="templates")


Expand All @@ -273,14 +268,13 @@ class Template_custom_configs(Base):

id = Column(Integer, Identity(start=1, increment=1), primary_key=True)
template_id = Column(String(256), ForeignKey("bw_templates.id", onupdate="cascade", ondelete="cascade"), nullable=False)
step_id = Column(Integer, ForeignKey("bw_template_steps.step", onupdate="cascade", ondelete="cascade"), nullable=True)
step_id = Column(Integer, nullable=True)
type = Column(CUSTOM_CONFIGS_TYPES_ENUM, nullable=False)
name = Column(String(256), nullable=False)
data = Column(LargeBinary(length=(2**32) - 1), nullable=False)
checksum = Column(String(128), nullable=False)

template = relationship("Templates", back_populates="custom_configs")
step = relationship("Template_steps", back_populates="custom_configs")


class Metadata(Base):
Expand Down

0 comments on commit 65ba0f3

Please sign in to comment.