Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

refactor(models): relocate DatabaseSnapshot/SyncJob; untrack stale gui/email copies

Juan Denis2ffab5bf86d2
diff --git a/.gitignore b/.gitignore
index 608fcd3a..b5b74831 100644
--- a/.gitignore
+++ b/.gitignore
@@ -182,6 +182,7 @@ backend/app/plugins/serverkit-analytics/
backend/app/plugins/serverkit-cloud-provision/
backend/app/plugins/serverkit-crowdsec/
backend/app/plugins/serverkit-dns-server/
+backend/app/plugins/serverkit-email/
backend/app/plugins/serverkit-ftp/
backend/app/plugins/serverkit-gpu/
backend/app/plugins/serverkit-gui/
@@ -193,7 +194,9 @@ backend/app/plugins/serverkit-tramo/
frontend/src/plugins/serverkit-analytics/
frontend/src/plugins/serverkit-crowdsec/
frontend/src/plugins/serverkit-dns-server/
+frontend/src/plugins/serverkit-email/
frontend/src/plugins/serverkit-gpu/
+frontend/src/plugins/serverkit-gui/
frontend/src/plugins/serverkit-k8s/
frontend/src/plugins/serverkit-mail/
frontend/src/plugins/serverkit-tramo/
diff --git a/backend/app/models/db_snapshot.py b/backend/app/models/db_snapshot.py
new file mode 100644
index 00000000..ab42149c
--- /dev/null
+++ b/backend/app/models/db_snapshot.py
@@ -0,0 +1,145 @@
+"""
+Database snapshot & sync-job models.
+
+These are generic database point-in-time snapshot and environment-sync
+records. They physically lived in ``wordpress_site.py`` historically; they were
+relocated here (plan 52 Phase 1) so the WordPress model file no longer owns
+generic machinery. The tables (``database_snapshots``, ``sync_jobs``) are
+unchanged — this was a pure module move. Their foreign keys still reference
+``wordpress_sites`` (the only environment kind that uses them today); that is a
+stable core-managed schema seam, not a reason to keep them in the WP file.
+
+``wordpress_site`` re-exports both names for import compatibility, so existing
+``from app.models.wordpress_site import DatabaseSnapshot`` callers keep working.
+"""
+
+from datetime import datetime
+from app import db
+import json
+
+
+class DatabaseSnapshot(db.Model):
+ """Point-in-time database snapshots for WordPress sites."""
+
+ __tablename__ = 'database_snapshots'
+
+ id = db.Column(db.Integer, primary_key=True)
+ site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
+
+ # Snapshot info
+ name = db.Column(db.String(200), nullable=False)
+ description = db.Column(db.Text)
+ tag = db.Column(db.String(100)) # e.g., 'pre-deploy', 'v1.2.0', 'auto-nightly'
+
+ # File info
+ file_path = db.Column(db.String(500), nullable=False)
+ size_bytes = db.Column(db.BigInteger, default=0)
+ compressed = db.Column(db.Boolean, default=True)
+
+ # Git context (optional)
+ commit_sha = db.Column(db.String(40)) # Git commit at snapshot time
+ commit_message = db.Column(db.Text)
+
+ # Metadata
+ tables_included = db.Column(db.Text) # JSON list of tables
+ row_count = db.Column(db.Integer)
+
+ # Status
+ status = db.Column(db.String(20), default='completed') # creating, completed, failed, deleted
+ error_message = db.Column(db.Text)
+
+ # Timestamps
+ created_at = db.Column(db.DateTime, default=datetime.utcnow)
+ expires_at = db.Column(db.DateTime) # Auto-cleanup date
+
+ def to_dict(self):
+ return {
+ 'id': self.id,
+ 'site_id': self.site_id,
+ 'name': self.name,
+ 'description': self.description,
+ 'tag': self.tag,
+ 'file_path': self.file_path,
+ 'size_bytes': self.size_bytes,
+ 'size_human': self._format_size(self.size_bytes),
+ 'compressed': self.compressed,
+ 'commit_sha': self.commit_sha,
+ 'commit_message': self.commit_message,
+ 'tables_included': json.loads(self.tables_included) if self.tables_included else None,
+ 'row_count': self.row_count,
+ 'status': self.status,
+ 'error_message': self.error_message,
+ 'created_at': self.created_at.isoformat() if self.created_at else None,
+ 'expires_at': self.expires_at.isoformat() if self.expires_at else None,
+ }
+
+ @staticmethod
+ def _format_size(size_bytes):
+ if not size_bytes:
+ return '0 B'
+ for unit in ['B', 'KB', 'MB', 'GB']:
+ if size_bytes < 1024:
+ return f'{size_bytes:.1f} {unit}'
+ size_bytes /= 1024
+ return f'{size_bytes:.1f} TB'
+
+ def __repr__(self):
+ return f'<DatabaseSnapshot {self.id} "{self.name}">'
+
+
+class SyncJob(db.Model):
+ """Scheduled database synchronization jobs between WordPress environments."""
+
+ __tablename__ = 'sync_jobs'
+
+ id = db.Column(db.Integer, primary_key=True)
+
+ # Source and target sites
+ source_site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
+ target_site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
+
+ # Job name
+ name = db.Column(db.String(200))
+
+ # Schedule (cron expression)
+ schedule = db.Column(db.String(100)) # e.g., "0 3 * * 0" = Sunday 3 AM
+ enabled = db.Column(db.Boolean, default=True)
+
+ # Configuration (JSON)
+ config = db.Column(db.Text) # search_replace, anonymize, exclude_tables, truncate_tables
+
+ # Execution tracking
+ last_run = db.Column(db.DateTime)
+ last_run_status = db.Column(db.String(20)) # success, failed, running
+ last_run_duration = db.Column(db.Integer) # seconds
+ last_run_error = db.Column(db.Text)
+ next_run = db.Column(db.DateTime)
+ run_count = db.Column(db.Integer, default=0)
+
+ # Timestamps
+ created_at = db.Column(db.DateTime, default=datetime.utcnow)
+ updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
+
+ def to_dict(self):
+ return {
+ 'id': self.id,
+ 'name': self.name,
+ 'source_site_id': self.source_site_id,
+ 'target_site_id': self.target_site_id,
+ 'schedule': self.schedule,
+ 'enabled': self.enabled,
+ 'config': json.loads(self.config) if self.config else None,
+ 'last_run': self.last_run.isoformat() if self.last_run else None,
+ 'last_run_status': self.last_run_status,
+ 'last_run_duration': self.last_run_duration,
+ 'last_run_error': self.last_run_error,
+ 'next_run': self.next_run.isoformat() if self.next_run else None,
+ 'run_count': self.run_count,
+ 'created_at': self.created_at.isoformat() if self.created_at else None,
+ 'updated_at': self.updated_at.isoformat() if self.updated_at else None,
+ 'source_site': self.source_site.to_dict() if self.source_site else None,
+ 'target_site': self.target_site.to_dict() if self.target_site else None,
+ }
+
+ def __repr__(self):
+ return f'<SyncJob {self.id} {self.source_site_id}->{self.target_site_id}>'
diff --git a/backend/app/models/wordpress_site.py b/backend/app/models/wordpress_site.py
index 4653b394..7bbc5bf4 100644
--- a/backend/app/models/wordpress_site.py
+++ b/backend/app/models/wordpress_site.py
@@ -11,6 +11,13 @@ from datetime import datetime
from app import db
import json
+# DatabaseSnapshot + SyncJob were relocated to app/models/db_snapshot.py
+# (plan 52 Phase 1 — generic snapshot/sync machinery no longer owned by the WP
+# model file). Re-exported here so existing
+# `from app.models.wordpress_site import DatabaseSnapshot` (and SyncJob) callers
+# keep working, and so WordPressSite.to_dict can reference DatabaseSnapshot.
+from app.models.db_snapshot import DatabaseSnapshot, SyncJob # noqa: F401
+
class WordPressSite(db.Model):
"""Extended WordPress-specific data linked to Application."""
@@ -187,133 +194,6 @@ class WordPressSite(db.Model):
return f'<WordPressSite {self.id} app={self.application_id}>'
-class DatabaseSnapshot(db.Model):
- """Point-in-time database snapshots for WordPress sites."""
-
- __tablename__ = 'database_snapshots'
-
- id = db.Column(db.Integer, primary_key=True)
- site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
-
- # Snapshot info
- name = db.Column(db.String(200), nullable=False)
- description = db.Column(db.Text)
- tag = db.Column(db.String(100)) # e.g., 'pre-deploy', 'v1.2.0', 'auto-nightly'
-
- # File info
- file_path = db.Column(db.String(500), nullable=False)
- size_bytes = db.Column(db.BigInteger, default=0)
- compressed = db.Column(db.Boolean, default=True)
-
- # Git context (optional)
- commit_sha = db.Column(db.String(40)) # Git commit at snapshot time
- commit_message = db.Column(db.Text)
-
- # Metadata
- tables_included = db.Column(db.Text) # JSON list of tables
- row_count = db.Column(db.Integer)
-
- # Status
- status = db.Column(db.String(20), default='completed') # creating, completed, failed, deleted
- error_message = db.Column(db.Text)
-
- # Timestamps
- created_at = db.Column(db.DateTime, default=datetime.utcnow)
- expires_at = db.Column(db.DateTime) # Auto-cleanup date
-
- def to_dict(self):
- return {
- 'id': self.id,
- 'site_id': self.site_id,
- 'name': self.name,
- 'description': self.description,
- 'tag': self.tag,
- 'file_path': self.file_path,
- 'size_bytes': self.size_bytes,
- 'size_human': self._format_size(self.size_bytes),
- 'compressed': self.compressed,
- 'commit_sha': self.commit_sha,
- 'commit_message': self.commit_message,
- 'tables_included': json.loads(self.tables_included) if self.tables_included else None,
- 'row_count': self.row_count,
- 'status': self.status,
- 'error_message': self.error_message,
- 'created_at': self.created_at.isoformat() if self.created_at else None,
- 'expires_at': self.expires_at.isoformat() if self.expires_at else None,
- }
-
- @staticmethod
- def _format_size(size_bytes):
- if not size_bytes:
- return '0 B'
- for unit in ['B', 'KB', 'MB', 'GB']:
- if size_bytes < 1024:
- return f'{size_bytes:.1f} {unit}'
- size_bytes /= 1024
- return f'{size_bytes:.1f} TB'
-
- def __repr__(self):
- return f'<DatabaseSnapshot {self.id} "{self.name}">'
-
-
-class SyncJob(db.Model):
- """Scheduled database synchronization jobs between WordPress environments."""
-
- __tablename__ = 'sync_jobs'
-
- id = db.Column(db.Integer, primary_key=True)
-
- # Source and target sites
- source_site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
- target_site_id = db.Column(db.Integer, db.ForeignKey('wordpress_sites.id'), nullable=False)
-
- # Job name
- name = db.Column(db.String(200))
-
- # Schedule (cron expression)
- schedule = db.Column(db.String(100)) # e.g., "0 3 * * 0" = Sunday 3 AM
- enabled = db.Column(db.Boolean, default=True)
-
- # Configuration (JSON)
- config = db.Column(db.Text) # search_replace, anonymize, exclude_tables, truncate_tables
-
- # Execution tracking
- last_run = db.Column(db.DateTime)
- last_run_status = db.Column(db.String(20)) # success, failed, running
- last_run_duration = db.Column(db.Integer) # seconds
- last_run_error = db.Column(db.Text)
- next_run = db.Column(db.DateTime)
- run_count = db.Column(db.Integer, default=0)
-
- # Timestamps
- created_at = db.Column(db.DateTime, default=datetime.utcnow)
- updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
-
- def to_dict(self):
- return {
- 'id': self.id,
- 'name': self.name,
- 'source_site_id': self.source_site_id,
- 'target_site_id': self.target_site_id,
- 'schedule': self.schedule,
- 'enabled': self.enabled,
- 'config': json.loads(self.config) if self.config else None,
- 'last_run': self.last_run.isoformat() if self.last_run else None,
- 'last_run_status': self.last_run_status,
- 'last_run_duration': self.last_run_duration,
- 'last_run_error': self.last_run_error,
- 'next_run': self.next_run.isoformat() if self.next_run else None,
- 'run_count': self.run_count,
- 'created_at': self.created_at.isoformat() if self.created_at else None,
- 'updated_at': self.updated_at.isoformat() if self.updated_at else None,
- 'source_site': self.source_site.to_dict() if self.source_site else None,
- 'target_site': self.target_site.to_dict() if self.target_site else None,
- }
-
- def __repr__(self):
- return f'<SyncJob {self.id} {self.source_site_id}->{self.target_site_id}>'
-
-
class WordPressVulnerability(db.Model):
"""A known vulnerability found in a WordPress site's plugin, theme, or core."""
diff --git a/backend/tests/test_db_snapshot_relocation.py b/backend/tests/test_db_snapshot_relocation.py
new file mode 100644
index 00000000..c34546bb
--- /dev/null
+++ b/backend/tests/test_db_snapshot_relocation.py
@@ -0,0 +1,27 @@
+"""DatabaseSnapshot + SyncJob relocation (plan 52 Phase 1).
+
+The two models moved from ``app.models.wordpress_site`` to a neutral
+``app.models.db_snapshot`` module. This locks the import-compatibility contract
+so the ~6 existing ``from app.models.wordpress_site import DatabaseSnapshot``
+callers (apps API, db_sync/environment services, jobs) keep working, and that the
+move was pure — same classes, same tables.
+"""
+from app.models import db_snapshot, wordpress_site
+from app.models.db_snapshot import DatabaseSnapshot, SyncJob
+
+
+def test_models_live_in_db_snapshot_module():
+ assert DatabaseSnapshot.__module__ == 'app.models.db_snapshot'
+ assert SyncJob.__module__ == 'app.models.db_snapshot'
+
+
+def test_wordpress_site_reexports_same_classes():
+ # Back-compat: the old import path resolves to the exact same class objects.
+ assert wordpress_site.DatabaseSnapshot is DatabaseSnapshot
+ assert wordpress_site.SyncJob is SyncJob
+
+
+def test_tables_unchanged():
+ # Pure module move — table names must not have shifted.
+ assert db_snapshot.DatabaseSnapshot.__tablename__ == 'database_snapshots'
+ assert db_snapshot.SyncJob.__tablename__ == 'sync_jobs'