prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  `clean_stale_db --force` kills demo databases newer than 3 days
  ## Description
  `clean_stale_db --force` is meant to only kill demo databases older than 3 days (by default), but that doesn't seem to be the case.
  
  ## Additional context
  
  https://github.com/centerofci/mathesar/blob/master/demo/management/commands/clean_stale_db.py
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `demo/management/commands/clean_stale_db.py`
  Content:
  ```
  1 from datetime import timedelta
  2 
  3 from django.conf import settings
  4 from django.core.management import BaseCommand
  5 from django.utils.timezone import now
  6 from sqlalchemy import text
  7 from sqlalchemy.exc import OperationalError
  8 
  9 from db import engine
  10 from db.metadata import get_empty_metadata
  11 from mathesar.models.base import Database
  12 from mathesar.state.django import reflect_db_objects
  13 
  14 
  15 class Command(BaseCommand):
  16     help = 'Cleans up the stale database created during live demo'
  17 
  18     def add_arguments(self, parser):
  19         parser.add_argument(
  20             '--force',
  21             action='store_true',
  22             help='Force delete a database even if it in use'
  23         )
  24         parser.add_argument(
  25             '--max-days',
  26             action='store',
  27             type=int,
  28             default=3,
  29             help='A database is considered for deletion if it has existed for more than --max-days',
  30         )
  31 
  32     def handle(self, *args, **options):
  33         drop_all_stale_databases(*args, **options)
  34 
  35 
  36 def drop_all_stale_databases(force=False, max_days=3, *args, **kwargs):
  37     excluded_databases = [
  38         settings.DATABASES["default"]["NAME"],
  39         settings.DATABASES["mathesar_tables"]["NAME"],
  40         getattr(settings, "MATHESAR_DEMO_TEMPLATE", None),
  41         # Exclude Postgres default databases
  42         'postgres',
  43         'template0',
  44         'template1'
  45     ]
  46     stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(minutes=max_days))
  47     deleted_databases = []
  48     for database in stale_databases:
  49         if database.name not in excluded_databases and database.deleted is False:
  50             dropped = drop_mathesar_database(
  51                 database.name,
  52                 username=settings.DATABASES["default"]["USER"],
  53                 password=settings.DATABASES["default"]["PASSWORD"],
  54                 hostname=settings.DATABASES["default"]["HOST"],
  55                 root_database=settings.DATABASES["default"]["NAME"],
  56                 port=settings.DATABASES["default"]["PORT"],
  57                 force=force
  58             )
  59             if dropped:
  60                 deleted_databases.append(database.name)
  61                 database.delete()
  62     reflect_db_objects(get_empty_metadata())
  63     return deleted_databases
  64 
  65 
  66 def drop_mathesar_database(
  67         user_database, username, password, hostname, root_database, port, force=False
  68 ):
  69     user_db_engine = engine.create_future_engine(
  70         username, password, hostname, user_database, port
  71     )
  72     try:
  73         user_db_engine.connect()
  74     except OperationalError:
  75         # Non existent db object
  76         user_db_engine.dispose()
  77         return True
  78     else:
  79         try:
  80             root_db_engine = engine.create_future_engine(
  81                 username, password, hostname, root_database, port,
  82             )
  83             with root_db_engine.connect() as conn:
  84                 conn.execution_options(isolation_level="AUTOCOMMIT")
  85                 delete_stmt = f"DROP DATABASE {user_database} {'WITH (FORCE)' if force else ''}"
  86                 conn.execute(text(delete_stmt))
  87                 # This database is not created using a config file,
  88                 # so their objects can be safety deleted
  89                 # as they won't be created again during reflection
  90                 return True
  91         except OperationalError:
  92             # Database is in use, ignore
  93             pass
  94     return False
  95
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/demo/management/commands/clean_stale_db.py b/demo/management/commands/clean_stale_db.py
  --- a/demo/management/commands/clean_stale_db.py
  +++ b/demo/management/commands/clean_stale_db.py
  @@ -43,7 +43,7 @@
           'template0',
           'template1'
       ]
  -    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(minutes=max_days))
  +    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(days=max_days))
       deleted_databases = []
       for database in stale_databases:
           if database.name not in excluded_databases and database.deleted is False:
  
verification_info: {"golden_diff": "diff --git a/demo/management/commands/clean_stale_db.py b/demo/management/commands/clean_stale_db.py\n--- a/demo/management/commands/clean_stale_db.py\n+++ b/demo/management/commands/clean_stale_db.py\n@@ -43,7 +43,7 @@\n         'template0',\n         'template1'\n     ]\n-    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(minutes=max_days))\n+    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(days=max_days))\n     deleted_databases = []\n     for database in stale_databases:\n         if database.name not in excluded_databases and database.deleted is False:\n", "issue": "`clean_stale_db --force` kills demo databases newer than 3 days\n## Description\r\n`clean_stale_db --force` is meant to only kill demo databases older than 3 days (by default), but that doesn't seem to be the case.\r\n\r\n## Additional context\r\n\r\nhttps://github.com/centerofci/mathesar/blob/master/demo/management/commands/clean_stale_db.py\n", "before_files": [{"content": "from datetime import timedelta\n\nfrom django.conf import settings\nfrom django.core.management import BaseCommand\nfrom django.utils.timezone import now\nfrom sqlalchemy import text\nfrom sqlalchemy.exc import OperationalError\n\nfrom db import engine\nfrom db.metadata import get_empty_metadata\nfrom mathesar.models.base import Database\nfrom mathesar.state.django import reflect_db_objects\n\n\nclass Command(BaseCommand):\n    help = 'Cleans up the stale database created during live demo'\n\n    def add_arguments(self, parser):\n        parser.add_argument(\n            '--force',\n            action='store_true',\n            help='Force delete a database even if it in use'\n        )\n        parser.add_argument(\n            '--max-days',\n            action='store',\n            type=int,\n            default=3,\n            help='A database is considered for deletion if it has existed for more than --max-days',\n        )\n\n    def handle(self, *args, **options):\n        drop_all_stale_databases(*args, **options)\n\n\ndef drop_all_stale_databases(force=False, max_days=3, *args, **kwargs):\n    excluded_databases = [\n        settings.DATABASES[\"default\"][\"NAME\"],\n        settings.DATABASES[\"mathesar_tables\"][\"NAME\"],\n        getattr(settings, \"MATHESAR_DEMO_TEMPLATE\", None),\n        # Exclude Postgres default databases\n        'postgres',\n        'template0',\n        'template1'\n    ]\n    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(minutes=max_days))\n    deleted_databases = []\n    for database in stale_databases:\n        if database.name not in excluded_databases and database.deleted is False:\n            dropped = drop_mathesar_database(\n                database.name,\n                username=settings.DATABASES[\"default\"][\"USER\"],\n                password=settings.DATABASES[\"default\"][\"PASSWORD\"],\n                hostname=settings.DATABASES[\"default\"][\"HOST\"],\n                root_database=settings.DATABASES[\"default\"][\"NAME\"],\n                port=settings.DATABASES[\"default\"][\"PORT\"],\n                force=force\n            )\n            if dropped:\n                deleted_databases.append(database.name)\n                database.delete()\n    reflect_db_objects(get_empty_metadata())\n    return deleted_databases\n\n\ndef drop_mathesar_database(\n        user_database, username, password, hostname, root_database, port, force=False\n):\n    user_db_engine = engine.create_future_engine(\n        username, password, hostname, user_database, port\n    )\n    try:\n        user_db_engine.connect()\n    except OperationalError:\n        # Non existent db object\n        user_db_engine.dispose()\n        return True\n    else:\n        try:\n            root_db_engine = engine.create_future_engine(\n                username, password, hostname, root_database, port,\n            )\n            with root_db_engine.connect() as conn:\n                conn.execution_options(isolation_level=\"AUTOCOMMIT\")\n                delete_stmt = f\"DROP DATABASE {user_database} {'WITH (FORCE)' if force else ''}\"\n                conn.execute(text(delete_stmt))\n                # This database is not created using a config file,\n                # so their objects can be safety deleted\n                # as they won't be created again during reflection\n                return True\n        except OperationalError:\n            # Database is in use, ignore\n            pass\n    return False\n", "path": "demo/management/commands/clean_stale_db.py"}], "after_files": [{"content": "from datetime import timedelta\n\nfrom django.conf import settings\nfrom django.core.management import BaseCommand\nfrom django.utils.timezone import now\nfrom sqlalchemy import text\nfrom sqlalchemy.exc import OperationalError\n\nfrom db import engine\nfrom db.metadata import get_empty_metadata\nfrom mathesar.models.base import Database\nfrom mathesar.state.django import reflect_db_objects\n\n\nclass Command(BaseCommand):\n    help = 'Cleans up the stale database created during live demo'\n\n    def add_arguments(self, parser):\n        parser.add_argument(\n            '--force',\n            action='store_true',\n            help='Force delete a database even if it in use'\n        )\n        parser.add_argument(\n            '--max-days',\n            action='store',\n            type=int,\n            default=3,\n            help='A database is considered for deletion if it has existed for more than --max-days',\n        )\n\n    def handle(self, *args, **options):\n        drop_all_stale_databases(*args, **options)\n\n\ndef drop_all_stale_databases(force=False, max_days=3, *args, **kwargs):\n    excluded_databases = [\n        settings.DATABASES[\"default\"][\"NAME\"],\n        settings.DATABASES[\"mathesar_tables\"][\"NAME\"],\n        getattr(settings, \"MATHESAR_DEMO_TEMPLATE\", None),\n        # Exclude Postgres default databases\n        'postgres',\n        'template0',\n        'template1'\n    ]\n    stale_databases = Database.objects.filter(created_at__lt=now() - timedelta(days=max_days))\n    deleted_databases = []\n    for database in stale_databases:\n        if database.name not in excluded_databases and database.deleted is False:\n            dropped = drop_mathesar_database(\n                database.name,\n                username=settings.DATABASES[\"default\"][\"USER\"],\n                password=settings.DATABASES[\"default\"][\"PASSWORD\"],\n                hostname=settings.DATABASES[\"default\"][\"HOST\"],\n                root_database=settings.DATABASES[\"default\"][\"NAME\"],\n                port=settings.DATABASES[\"default\"][\"PORT\"],\n                force=force\n            )\n            if dropped:\n                deleted_databases.append(database.name)\n                database.delete()\n    reflect_db_objects(get_empty_metadata())\n    return deleted_databases\n\n\ndef drop_mathesar_database(\n        user_database, username, password, hostname, root_database, port, force=False\n):\n    user_db_engine = engine.create_future_engine(\n        username, password, hostname, user_database, port\n    )\n    try:\n        user_db_engine.connect()\n    except OperationalError:\n        # Non existent db object\n        user_db_engine.dispose()\n        return True\n    else:\n        try:\n            root_db_engine = engine.create_future_engine(\n                username, password, hostname, root_database, port,\n            )\n            with root_db_engine.connect() as conn:\n                conn.execution_options(isolation_level=\"AUTOCOMMIT\")\n                delete_stmt = f\"DROP DATABASE {user_database} {'WITH (FORCE)' if force else ''}\"\n                conn.execute(text(delete_stmt))\n                # This database is not created using a config file,\n                # so their objects can be safety deleted\n                # as they won't be created again during reflection\n                return True\n        except OperationalError:\n            # Database is in use, ignore\n            pass\n    return False\n", "path": "demo/management/commands/clean_stale_db.py"}]}