mirror of
https://gitlab.com/crafty-controller/crafty-4.git
synced 2024-08-30 18:23:09 +00:00
Setup drop table migrations
This commit is contained in:
parent
038f275388
commit
3fac1a39a8
@ -95,9 +95,6 @@ class ManagementController:
|
||||
# **********************************************************************************
|
||||
# Audit_Log Methods
|
||||
# **********************************************************************************
|
||||
@staticmethod
|
||||
def get_activity_log():
|
||||
return HelpersManagement.get_activity_log()
|
||||
|
||||
def add_to_audit_log(self, user_id, log_msg, server_id=None, source_ip=None):
|
||||
return self.management_helper.add_to_audit_log(
|
||||
|
@ -23,24 +23,6 @@ logger = logging.getLogger(__name__)
|
||||
auth_logger = logging.getLogger("audit_log")
|
||||
|
||||
|
||||
# **********************************************************************************
|
||||
# Audit_Log Class
|
||||
# **********************************************************************************
|
||||
class AuditLog(BaseModel):
|
||||
audit_id = AutoField()
|
||||
created = DateTimeField(default=datetime.datetime.now)
|
||||
user_name = CharField(default="")
|
||||
user_id = IntegerField(default=0, index=True)
|
||||
source_ip = CharField(default="127.0.0.1")
|
||||
server_id = ForeignKeyField(
|
||||
Servers, backref="audit_server", null=True
|
||||
) # When auditing global events, use server ID null
|
||||
log_msg = TextField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "audit_log"
|
||||
|
||||
|
||||
# **********************************************************************************
|
||||
# Crafty Settings Class
|
||||
# **********************************************************************************
|
||||
@ -150,10 +132,6 @@ class HelpersManagement:
|
||||
# **********************************************************************************
|
||||
# Audit_Log Methods
|
||||
# **********************************************************************************
|
||||
@staticmethod
|
||||
def get_activity_log():
|
||||
query = AuditLog.select()
|
||||
return DatabaseShortcuts.return_db_rows(query)
|
||||
|
||||
def add_to_audit_log(self, user_id, log_msg, server_id=None, source_ip=None):
|
||||
logger.debug(f"Adding to audit log User:{user_id} - Message: {log_msg} ")
|
||||
|
@ -1503,8 +1503,6 @@ class PanelHandler(BaseHandler):
|
||||
template = "panel/panel_edit_role.html"
|
||||
|
||||
elif page == "activity_logs":
|
||||
page_data["audit_logs"] = self.controller.management.get_activity_log()
|
||||
|
||||
template = "panel/activity_logs.html"
|
||||
|
||||
elif page == "download_file":
|
||||
|
@ -6,7 +6,6 @@ import logging
|
||||
from app.classes.shared.console import Console
|
||||
from app.classes.shared.migration import Migrator, MigrateHistory
|
||||
from app.classes.models.management import (
|
||||
AuditLog,
|
||||
Webhooks,
|
||||
Schedules,
|
||||
Backups,
|
||||
@ -61,17 +60,6 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
|
||||
)
|
||||
|
||||
# Changes on Audit Log Table
|
||||
migrator.alter_column_type(
|
||||
AuditLog,
|
||||
"server_id",
|
||||
peewee.ForeignKeyField(
|
||||
Servers,
|
||||
backref="audit_server",
|
||||
null=True,
|
||||
field=peewee.CharField(primary_key=True, default=str(uuid.uuid4())),
|
||||
),
|
||||
)
|
||||
# Changes on Webhook Table
|
||||
migrator.alter_column_type(
|
||||
Webhooks,
|
||||
@ -109,13 +97,6 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
peewee.AutoField(),
|
||||
)
|
||||
|
||||
# Changes on Audit Log Table
|
||||
migrator.alter_column_type(
|
||||
AuditLog,
|
||||
"server_id",
|
||||
peewee.IntegerField(default=None, index=True),
|
||||
)
|
||||
|
||||
# Changes on Webhook Table
|
||||
migrator.alter_column_type(
|
||||
Webhooks,
|
||||
|
@ -6,7 +6,6 @@ import logging
|
||||
from app.classes.shared.console import Console
|
||||
from app.classes.shared.migration import Migrator, MigrateHistory
|
||||
from app.classes.models.management import (
|
||||
AuditLog,
|
||||
Webhooks,
|
||||
Schedules,
|
||||
Backups,
|
||||
@ -73,20 +72,6 @@ def migrate(migrator: Migrator, database, **kwargs):
|
||||
try:
|
||||
logger.info("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
Console.info("Migrating Data from Int to UUID (Foreign Keys)")
|
||||
# Changes on Audit Log Table
|
||||
for audit_log in AuditLog.select():
|
||||
old_server_id = audit_log.server_id_id
|
||||
if old_server_id == "0" or old_server_id is None:
|
||||
server_uuid = None
|
||||
else:
|
||||
try:
|
||||
server = Servers.get_by_id(old_server_id)
|
||||
server_uuid = server.server_uuid
|
||||
except:
|
||||
server_uuid = old_server_id
|
||||
AuditLog.update(server_id=server_uuid).where(
|
||||
AuditLog.audit_id == audit_log.audit_id
|
||||
).execute()
|
||||
|
||||
# Changes on Webhooks Log Table
|
||||
for webhook in Webhooks.select():
|
||||
@ -247,21 +232,6 @@ def rollback(migrator: Migrator, database, **kwargs):
|
||||
try:
|
||||
logger.info("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
Console.info("Migrating Data from UUID to Int (Foreign Keys)")
|
||||
# Changes on Audit Log Table
|
||||
for audit_log in AuditLog.select():
|
||||
old_server_id = audit_log.server_id_id
|
||||
if old_server_id is None:
|
||||
new_server_id = 0
|
||||
else:
|
||||
try:
|
||||
server = Servers.get_or_none(Servers.server_uuid == old_server_id)
|
||||
new_server_id = server.server_id
|
||||
except:
|
||||
new_server_id = old_server_id
|
||||
AuditLog.update(server_id=new_server_id).where(
|
||||
AuditLog.audit_id == audit_log.audit_id
|
||||
).execute()
|
||||
|
||||
# Changes on Webhooks Log Table
|
||||
for webhook in Webhooks.select():
|
||||
old_server_id = webhook.server_id_id
|
||||
|
34
app/migrations/20240420_audit_log_drop.py
Normal file
34
app/migrations/20240420_audit_log_drop.py
Normal file
@ -0,0 +1,34 @@
|
||||
import peewee
|
||||
import datetime
|
||||
from peewee import (
|
||||
AutoField,
|
||||
DateTimeField,
|
||||
CharField,
|
||||
IntegerField,
|
||||
ForeignKeyField,
|
||||
TextField,
|
||||
)
|
||||
|
||||
from app.classes.shared.server import Servers
|
||||
|
||||
|
||||
def migrate(migrator, db):
|
||||
migrator.drop_table("audit_log")
|
||||
|
||||
|
||||
def rollback(migrator, db):
|
||||
class AuditLog(peewee.Model):
|
||||
audit_id = AutoField()
|
||||
created = DateTimeField(default=datetime.datetime.now)
|
||||
user_name = CharField(default="")
|
||||
user_id = IntegerField(default=0, index=True)
|
||||
source_ip = CharField(default="127.0.0.1")
|
||||
server_id = ForeignKeyField(
|
||||
Servers, backref="audit_server", null=True
|
||||
) # When auditing global events, use server ID null
|
||||
log_msg = TextField(default="")
|
||||
|
||||
class Meta:
|
||||
table_name = "audit_log"
|
||||
|
||||
migrator.create_table(AuditLog)
|
Loading…
Reference in New Issue
Block a user