Digitale bierlijst

seed.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """
  2. Provides functions to manage the database while the server is offline.
  3. """
  4. import argparse
  5. import csv
  6. import os
  7. from piket_server import db, Person, Settlement, ConsumptionType, Consumption
  8. def main():
  9. """ Main entry point. """
  10. parser = argparse.ArgumentParser()
  11. subparsers = parser.add_subparsers()
  12. # Clear command
  13. parser_clear = subparsers.add_parser("clear", help="Clear the database.")
  14. parser_clear.set_defaults(func=cmd_clear)
  15. parser_clear.add_argument("--removemydata", action="store_true")
  16. # Parser load_seeds
  17. # TODO
  18. args = parser.parse_args()
  19. args.func(args)
  20. def cmd_clear(args) -> None:
  21. """ Entry point for 'clear' subcommand. """
  22. if not args.removemydata:
  23. print("WARNING! This command will delete all contents in your database!")
  24. print("Type 'removemydata' to continue, anything else or CTRL-C to abort.")
  25. confirmation = input("> ")
  26. do_wipe = confirmation == "removemydata"
  27. else:
  28. do_wipe = True
  29. if do_wipe:
  30. print("Dropping all tables...")
  31. db.drop_all()
  32. print("All data removed. Recreating database...")
  33. db.create_all()
  34. from alembic.config import Config
  35. from alembic import command
  36. alembic_cfg = Config(os.path.join(os.path.dirname(__file__), "alembic.ini"))
  37. command.stamp(alembic_cfg, "head")
  38. print("Done.")
  39. return
  40. print("Aborting.")
  41. if __name__ == "__main__":
  42. main()