Creation of new scripts.
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from .irida import import_irida
|
||||
from pathlib import Path
|
||||
import importlib
|
||||
|
||||
def hello(ctx):
|
||||
print("\n\nHello! Welcome to Robotics Submission Tracker.\n\n")
|
||||
|
||||
def goodbye(ctx):
|
||||
print("\n\nGoodbye. Thank you for using Robotics Submission Tracker.\n\n")
|
||||
p = Path(__file__).parent.absolute()
|
||||
subs = [item.stem for item in p.glob("*.py") if "__" not in item.stem]
|
||||
modules = {}
|
||||
for sub in subs:
|
||||
importlib.import_module(f"backend.scripts.{sub}")
|
||||
|
||||
45
src/submissions/backend/scripts/backup_database.py
Normal file
45
src/submissions/backend/scripts/backup_database.py
Normal file
@@ -0,0 +1,45 @@
|
||||
"""
|
||||
script meant to copy database data to new file. Currently for Sqlite only
|
||||
"""
|
||||
import logging, shutil
|
||||
from datetime import date
|
||||
from pathlib import Path
|
||||
from tools import Settings
|
||||
import pyodbc
|
||||
|
||||
logger = logging.getLogger(f"submissions.{__name__}")
|
||||
|
||||
|
||||
def script(ctx: Settings):
|
||||
"""
|
||||
Copies the database into the backup directory the first time it is opened every month.
|
||||
"""
|
||||
month = date.today().strftime("%Y-%m")
|
||||
current_month_bak = Path(ctx.backup_path).joinpath(f"submissions_backup-{month}").resolve()
|
||||
logger.info(f"Here is the db directory: {ctx.database_path}")
|
||||
logger.info(f"Here is the backup directory: {ctx.backup_path}")
|
||||
match ctx.database_schema:
|
||||
case "sqlite":
|
||||
db_path = ctx.database_path.joinpath(ctx.database_name).with_suffix(".db")
|
||||
current_month_bak = current_month_bak.with_suffix(".db")
|
||||
if not current_month_bak.exists() and "Archives" not in db_path.__str__():
|
||||
logger.info("No backup found for this month, backing up database.")
|
||||
try:
|
||||
shutil.copyfile(db_path, current_month_bak)
|
||||
except PermissionError as e:
|
||||
logger.error(f"Couldn't backup database due to: {e}")
|
||||
case "postgresql+psycopg2":
|
||||
logger.warning(f"Backup function not yet implemented for psql")
|
||||
current_month_bak = current_month_bak.with_suffix(".psql")
|
||||
case "mssql+pyodbc":
|
||||
logger.warning(f"{ctx.database_schema} backup is currently experiencing permission issues")
|
||||
current_month_bak = current_month_bak.with_suffix(".bak")
|
||||
return
|
||||
if not current_month_bak.exists():
|
||||
logger.info(f"No backup found for this month, backing up database to {current_month_bak}.")
|
||||
connection = pyodbc.connect(driver='{ODBC Driver 18 for SQL Server}',
|
||||
server=f'{ctx.database_path}', database=f'{ctx.database_name}',
|
||||
trusted_connection='yes', trustservercertificate="yes", autocommit=True)
|
||||
backup = f"BACKUP DATABASE [{ctx.database_name}] TO DISK = N'{current_month_bak}'"
|
||||
cursor = connection.cursor().execute(backup)
|
||||
connection.close()
|
||||
5
src/submissions/backend/scripts/goodbye.py
Normal file
5
src/submissions/backend/scripts/goodbye.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Test script for teardown_scripts
|
||||
"""
|
||||
def script(ctx):
|
||||
print("\n\nGoodbye. Thank you for using Robotics Submission Tracker.\n\n")
|
||||
5
src/submissions/backend/scripts/hello.py
Normal file
5
src/submissions/backend/scripts/hello.py
Normal file
@@ -0,0 +1,5 @@
|
||||
"""
|
||||
Test script for startup_scripts
|
||||
"""
|
||||
def script(ctx):
|
||||
print("\n\nHello! Welcome to Robotics Submission Tracker.\n\n")
|
||||
@@ -7,7 +7,8 @@ from backend.db import IridaControl, ControlType
|
||||
|
||||
logger = logging.getLogger(f"submissions.{__name__}")
|
||||
|
||||
def import_irida(ctx:Settings):
|
||||
|
||||
def script(ctx:Settings):
|
||||
"""
|
||||
Grabs Irida controls from secondary database.
|
||||
|
||||
Reference in New Issue
Block a user