2
0
mirror of https://expo.survex.com/repositories/troggle/.git synced 2024-11-22 07:11:52 +00:00

Fix un-cleared db error (partial)

This commit is contained in:
Philip Sargent 2020-06-07 16:16:35 +01:00
parent 75bac01f3a
commit fcfda644d3
2 changed files with 93 additions and 10 deletions

View File

@ -39,7 +39,7 @@ if os.geteuid() == 0:
expouser=settings.EXPOUSER expouser=settings.EXPOUSER
expouserpass=settings.EXPOUSERPASS expouserpass=settings.EXPOUSERPASS
expouseremail=settings.EXPOUSER_EMAIL expouseremail=settings.EXPOUSER_EMAIL
print(" - SETTINGS: {} ({:.5}...) <{}> on module loading".format(expouser, expouserpass, expouseremail)) print(" - settings: {} ({:.5}...) <{}> on module loading".format(expouser, expouserpass, expouseremail))
@ -47,12 +47,16 @@ def reinit_db():
"""Rebuild database from scratch. Deletes the file first if sqlite is used, """Rebuild database from scratch. Deletes the file first if sqlite is used,
otherwise it drops the database and creates it. otherwise it drops the database and creates it.
""" """
django.db.close_old_connections() # wipes an in-memory sqlite db
currentdbname = settings.DATABASES['default']['NAME'] currentdbname = settings.DATABASES['default']['NAME']
if settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3': if settings.DATABASES['default']['NAME'] == ':memory:':
pass
elif settings.DATABASES['default']['ENGINE'] == 'django.db.backends.sqlite3':
try: try:
os.remove(currentdbname) os.remove(currentdbname)
except OSError: except OSError:
pass print(" ! OSError on removing: " + currentdbname + " (Is the file open in another app?\n")
raise
else: else:
cursor = connection.cursor() cursor = connection.cursor()
cursor.execute("DROP DATABASE %s" % currentdbname) cursor.execute("DROP DATABASE %s" % currentdbname)
@ -65,12 +69,17 @@ def reinit_db():
def syncuser(): def syncuser():
"""Sync user - needed after reload """Sync user - needed after reload
""" """
print("Synchronizing user") print(" - Synchronizing user on: " + settings.DATABASES['default']['NAME'])
management.call_command('migrate', interactive=False) management.call_command('migrate', interactive=False)
user = User.objects.create_user(expouser, expouseremail, expouserpass) try:
user.is_staff = True user = User.objects.create_user(expouser, expouseremail, expouserpass)
user.is_superuser = True user.is_staff = True
user.save() user.is_superuser = True
user.save()
except:
print(" ! INTEGRITY ERROR user on: " + settings.DATABASES['default']['NAME'])
print(" ! You probably have not got a clean db when you thought you had.\n")
raise
def dirsredirect(): def dirsredirect():
"""Make directories that troggle requires and sets up page redirects """Make directories that troggle requires and sets up page redirects
@ -438,7 +447,7 @@ if __name__ == "__main__":
runlabel=None runlabel=None
jq = JobQueue(runlabel) jq = JobQueue(runlabel)
jq.run_now_django_tests(1) #jq.run_now_django_tests(1)
if len(sys.argv)==1: if len(sys.argv)==1:
usage() usage()
@ -446,7 +455,7 @@ if __name__ == "__main__":
elif "test" in sys.argv: elif "test" in sys.argv:
jq.enq("caves",import_caves) jq.enq("caves",import_caves)
jq.enq("people",import_people) jq.enq("people",import_people)
#jq.run_now_django_tests(2) jq.run_now_django_tests(2)
elif "caves" in sys.argv: elif "caves" in sys.argv:
jq.enq("caves",import_caves) jq.enq("caves",import_caves)
elif "logbooks" in sys.argv: elif "logbooks" in sys.argv:

74
reset-django.py Normal file
View File

@ -0,0 +1,74 @@
import os
import shutil
from pprint import pprint
# from https://groups.google.com/forum/#!topic/django-users/C8Q7CTpcChc
# Just put it in the folder where manage.py file is and run it.
folders = []
base_dir = os.path.dirname(os.path.realpath(__file__))
def get_directory_list():
global folders
global base_dir
for root, d_names, f_names in os.walk(base_dir):
for name in d_names:
folders.append(os.path.join(root, name))
folders = sorted(folders)
return folders
def delete_pycache():
global folders
for folder in folders:
if folder.endswith("__pycache__"):
shutil.rmtree(folder)
print("All __pycache__ files deleted.")
return None
def delete_migrations():
global folders
for folder in folders:
if folder.endswith("migrations"):
for item in os.listdir(folder):
if not item.endswith("__init__.py"):
os.remove(os.path.join(folder, item))
print("All migration files deleted.")
return None
def delete_sqlite3():
global base_dir
db_file = os.path.join(base_dir, "troggle.sqlite3")
if os.path.exists(db_file):
os.remove(db_file)
def main():
global folders
try:
get_directory_list()
delete_pycache()
delete_migrations()
delete_sqlite3()
print("All cleanup operations performed successfully.")
except Exception as e:
print("There was some error! Aaargh. \n")
raise
if __name__ == "__main__":
main()