Skip to content

Commit

Permalink
Merge pull request #282 from dr-rodriguez/Versions_table
Browse files Browse the repository at this point in the history
Adding a Versions table; part of #263
  • Loading branch information
dr-rodriguez authored Aug 23, 2022
2 parents d8862bc + 74ca6c6 commit 174a4ee
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 7 deletions.
14 changes: 14 additions & 0 deletions data/Versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"version": "2022.1",
"start_date": "2020-06-22",
"end_date": "2022-08-23",
"description": "Initial release"
},
{
"version": "latest",
"start_date": "2022-08-23",
"end_date": null,
"description": "Version in development"
}
]
12 changes: 6 additions & 6 deletions scripts/ingests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class SimpleError(Exception):
# sys.tracebacklimit = default_value # revert changes


def load_simpledb(db_file, recreatedb=True):
def load_simpledb(db_file, recreatedb=True, **kwargs):
# Utility function to load the database

db_file_path = Path(db_file)
Expand All @@ -61,17 +61,17 @@ def load_simpledb(db_file, recreatedb=True):

if not db_file_path.exists():
try: # Use fancy in-memory database, if supported by astrodbkit2
db = Database('sqlite://') # creates and connects to a temporary in-memory database
db = Database('sqlite://', **kwargs) # creates and connects to a temporary in-memory database
db.load_database('data/') # loads the data from the data files into the database
db.dump_sqlite(db_file) # dump in-memory database to file
db = Database(db_connection_string) # replace database object with new file version
db.dump_sqlite(db_file) # dump in-memory database to file
db = Database(db_connection_string, **kwargs) # replace database object with new file version
except RuntimeError:
# use in-file database
create_database(db_connection_string) # creates empty database based on the simple schema
db = Database(db_connection_string) # connects to the empty database
db = Database(db_connection_string, **kwargs) # connects to the empty database
db.load_database('data/') # loads the data from the data files into the database
else:
db = Database(db_connection_string) # if database already exists, connects to .db file
db = Database(db_connection_string, **kwargs) # if database already exists, connects to .db file

return db

Expand Down
34 changes: 34 additions & 0 deletions scripts/updates/update_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Script to show how to update the version number

import logging
from astrodbkit2 import REFERENCE_TABLES
from scripts.ingests.utils import logger, load_simpledb

logger.setLevel(logging.INFO)

db = load_simpledb('SIMPLE.db', recreatedb=True, reference_tables=REFERENCE_TABLES+['Versions'])

# Check all versions
print(db.query(db.Versions).table())

# Add new version, add new entries as appropriate
# Note that start_date and end_date are strings of the date in format YYYY-MM-DD
data = [{'version': '2022.1', 'start_date': '2020-06-22',
'end_date': '2022-08-23', 'description': 'Initial release'}]
db.Versions.insert().execute(data)

# Fetch data of latest release
latest_date = db.query(db.Versions.c.end_date).order_by(db.Versions.c.end_date.desc()).limit(1).table()
latest_date = latest_date['end_date'].value[0]

latest = db.query(db.Versions).filter(db.Versions.c.version == 'latest').count()
if latest == 0:
# Add latest if not present
data = [{'version': 'latest', 'start_date': latest_date, 'description': 'Version in development'}]
db.Versions.insert().execute(data)
else:
# Update if present
db.Versions.update().where(db.Versions.c.version == 'latest').values(start_date=latest_date).execute()

# Save the database
db.save_database('data/')
12 changes: 12 additions & 0 deletions simple/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ class PhotometryFilters(Base):
width = Column(Float)


class Versions(Base):
"""
ORM for Versions table
This stores the version numbers for the database
"""
__tablename__ = 'Versions'
version = Column(String(30), primary_key=True, nullable=False)
start_date = Column(String(30))
end_date = Column(String(30))
description = Column(String(1000))


# -------------------------------------------------------------------------------------------------------------------
# Hard-coded enumerations

Expand Down
2 changes: 1 addition & 1 deletion tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

# Used to overwrite AstrodbKit2 reference tables defaults
REFERENCE_TABLES = ['Publications', 'Telescopes', 'Instruments', 'Modes', 'PhotometryFilters']
REFERENCE_TABLES = ['Publications', 'Telescopes', 'Instruments', 'Modes', 'PhotometryFilters', 'Versions']

0 comments on commit 174a4ee

Please sign in to comment.