Skip to content

Sql interface lfs#21

Merged
eloise-nebula merged 12 commits intomainfrom
sql-interface-lfs
Apr 1, 2026
Merged

Sql interface lfs#21
eloise-nebula merged 12 commits intomainfrom
sql-interface-lfs

Conversation

@eloise-nebula
Copy link
Copy Markdown
Collaborator

Added Oracle and AIDA DBs along with ways to generate them.

Database structure is defined as follows:

slac-db/slac_db/oracle.py

Lines 106 to 126 in de7a98f

def _init_db(uri=None):
global _meta
if uri is None:
uri = _oracle_uri()
uri = _db_type_prefix(uri)
schema = {
"elements": {
"Area": "str 64 nullable",
"Element": "str 64 primary_key",
"Control System Name": "str 64 nullable",
"Keyword": "str 64 nullable",
"Beampath": "str 64 nullable",
"SumL (m)": "float 64 nullable",
"Effective Length (m)": "float 64 nullable",
"Rf Frequency (MHz)": "float 64 nullable"
}
}
_meta = pykern.sql_db.Meta(
uri=uri,
schema=schema
)

uri defines the access method and location of the database.
schema defines the database columns and their data types. This is not a complete list for the Oracle db.

An interesting thing to note here is the global _meta variable defined at the top of the module. I'd never seen this pattern before looking through the slicops db implementation.

First, note that _meta is contained in the module's namespace. Code that imports slac_db.oracle can only access _meta with slac_db.oracle._meta. _meta tells SQLAlchemy how to access the database, and creates a session. The programmer using slac_db.oracle doesn't need to know about the _meta object, so we can hide it as an attribute of the module. Now we don't need a database_reader class to manage database location, format, access method, etc. Everything is defined at the module level.

_meta is only defined once we need a session. It's bad practice to give a lot of overhead to loading a module, particularly when there's a connection to an external resource involved. We only want to encounter a possible connection error once the programmer expects a connection to exist.

This particular session concept is from a wrapper in pykern. I thought about writing my own implementation, but the one Radiasoft provided is very convenient.

Copied from #20

@eloise-nebula eloise-nebula self-assigned this Mar 9, 2026
Copy link
Copy Markdown
Member

@nneveu nneveu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I want to look at the oracle and aida py files a little more, but nice work. I left some minor comments. Also, sorry to nag, maybe add a few comments.

Comment thread slac_db/aida.py Outdated
Comment thread slac_db/aida.py Outdated
Comment thread tests/test_data/expected_areas.csv
Comment thread tests/test_data/expected_beampaths.csv
Comment thread slac_db/create/meme_names.py
import slac_db.config
import slac_db.oracle

def to_oracle_db(csv_source=None):
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some docstrings would be helpful to indicate that this function is where the Oracle db is made, and defaults to lcls_elements.csv

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name to_sqlite_db?

@kabanaty
Copy link
Copy Markdown
Collaborator

This is really great so far. I think docstrings and comments would be very helpful in interpreting the data flow and persistent session mechanics that are happening in this module.

Copy link
Copy Markdown
Collaborator

@shamin-slac shamin-slac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good so far! I just had a couple minor comments, and I'll echo the request for more comments/docstrings.

p = _Parser(csv_source=csv_source)
return slac_db.oracle.recreate(p)

class _Parser():
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are the parentheses here to suggest that this takes an argument?


def recreate(parser):
assert not _meta
assert parser.addresses
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these assert statements here for debugging? If not, I think it's better to not use assert statements for these kinds of checks.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the discussion in the workshop, should these be AssertError instead?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from slicops already

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eloise will add an error message

Comment thread slac_db/oracle.py Outdated

def recreate(parser):
assert not _meta
assert parser.rows
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as earlier

Copy link
Copy Markdown
Member

@nneveu nneveu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yay, thanks!

import slac_db.config
import slac_db.oracle

def to_oracle_db(csv_source=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe name to_sqlite_db?

Comment thread slac_db/create/meme_names.py Outdated
@@ -0,0 +1,15 @@
import slac_db.aida

def to_aida_db():
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to 'directory service'.

Comment thread slac_db/directory_service.py
Comment thread slac_db/aida.py Outdated

def recreate(parser):
assert not _meta
assert parser.addresses
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from slicops already


def recreate(parser):
assert not _meta
assert parser.addresses
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eloise will add an error message

Comment thread slac_db/aida.py Outdated
Comment thread slac_db/aida.py Outdated
Comment thread tests/aidalist_test.py Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename file

Comment thread tests/test_directory_service.py
@eloise-nebula eloise-nebula merged commit 3849dc6 into main Apr 1, 2026
6 checks passed
@nneveu nneveu deleted the sql-interface-lfs branch April 9, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants