From 6092ed1f31de20791ba59d65b2707244cb4fe796 Mon Sep 17 00:00:00 2001 From: Kevin Chung Date: Mon, 27 Apr 2020 18:33:53 -0400 Subject: [PATCH] Clear caches after populate and enable foreign keys in SQLite --- CTFd/__init__.py | 11 +++++++++++ populate.py | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/CTFd/__init__.py b/CTFd/__init__.py index ae42dbc..0005486 100644 --- a/CTFd/__init__.py +++ b/CTFd/__init__.py @@ -181,6 +181,17 @@ def create_app(config="CTFd.config.Config"): if url.drivername.startswith("sqlite"): db.create_all() stamp_latest_revision() + + # Enable foreign keys for SQLite + from sqlalchemy.engine import Engine + from sqlalchemy import event + + @event.listens_for(Engine, "connect") + def set_sqlite_pragma(dbapi_connection, connection_record): + cursor = dbapi_connection.cursor() + cursor.execute("PRAGMA foreign_keys=ON") + cursor.close() + else: # This creates tables instead of db.create_all() # Allows migrations to happen properly diff --git a/populate.py b/populate.py index 033d69f..07cd639 100644 --- a/populate.py +++ b/populate.py @@ -7,6 +7,7 @@ import random import argparse from CTFd import create_app +from CTFd.cache import clear_config, clear_standings, clear_pages from CTFd.models import ( Users, Teams, @@ -338,3 +339,7 @@ if __name__ == "__main__": db.session.commit() db.session.close() + + clear_config() + clear_standings() + clear_pages()