Skip to content

Commit

Permalink
Add command line tool for updating Murfey database tables (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
d-j-hatton authored Jan 2, 2024
1 parent d0e7420 commit c5b3555
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ console_scripts =
murfey.simulate = murfey.cli.dummy:run
murfey.transfer = murfey.cli.transfer:run
murfey.sessions = murfey.cli.db_sessions:run
murfey.create_db = murfey.cli.create_db:run

[options.extras_require]
server =
Expand Down
36 changes: 36 additions & 0 deletions src/murfey/cli/create_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import argparse
import os

from murfey.util.db import clear, setup


def run():
parser = argparse.ArgumentParser(
description="Generate the necessary tables for the Murfey database"
)

parser.add_argument(
"--no-clear",
dest="clear",
default=True,
action="store_false",
help="Do not clear current database tables before creating specified tables",
)
parser.add_argument(
"-m",
"--microscope",
dest="microscope",
type=str,
default="",
help="Microscope as specified in the Murfey machine configuration",
)

args = parser.parse_args()
if args.microscope:
os.environ["BEAMLINE"] = args.microscope

from murfey.server.murfey_db import url

if args.clear:
clear(url())
setup(url())
9 changes: 9 additions & 0 deletions src/murfey/util/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from typing import List, Optional

import sqlalchemy
from sqlmodel import Field, Relationship, SQLModel, create_engine


Expand Down Expand Up @@ -341,3 +342,11 @@ class Class3D(SQLModel, table=True): # type: ignore
def setup(url: str):
engine = create_engine(url)
SQLModel.metadata.create_all(engine)


def clear(url: str):
engine = create_engine(url)
metadata = sqlalchemy.MetaData(engine)
metadata.reflect()

metadata.drop_all(engine)

0 comments on commit c5b3555

Please sign in to comment.