Skip to content

Commit a91d033

Browse files
Migrate schema to match Bioconductor's BiocFileCache. (#12)
- Mostly changing some of the fields from varchar to text (#11) - Update contributor list - Update Changelog Co-authored-by: Khoroshevskyi <[email protected]>
1 parent 087837d commit a91d033

11 files changed

+100
-88
lines changed

AUTHORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
* Jayaram Kancherla <[email protected]>
44
* Max Hargreaves <[email protected]>
5+
* Oleksandr Khoroshevskyi <[email protected]>

CHANGELOG.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3-
## Version 0.1 (development)
3+
## Version 0.4 (development)
44

5-
- Initial release
5+
- Migrate the schema to match R/Bioconductor's BiocFileCache (Check out [this issue](https://github.com/BiocPy/pyBiocFileCache/issues/11)). Thanks to [@khoroshevskyi ](https://github.com/khoroshevskyi) for the PR.
6+
7+
## Version 0.1
8+
9+
- Initial release of the package, Setting up all the actions.

docs/conf.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,7 @@ def setup(app):
179179
# Theme options are theme-specific and customize the look and feel of a theme
180180
# further. For a list of options available for each theme, see the
181181
# documentation.
182-
html_theme_options = {
183-
"sidebar_width": "300px",
184-
"page_width": "1200px"
185-
}
182+
html_theme_options = {"sidebar_width": "300px", "page_width": "1200px"}
186183

187184
# Add any paths that contain custom themes here, relative to this directory.
188185
# html_theme_path = []
@@ -266,9 +263,7 @@ def setup(app):
266263

267264
# Grouping the document tree into LaTeX files. List of tuples
268265
# (source start file, target name, title, author, documentclass [howto/manual]).
269-
latex_documents = [
270-
("index", "user_guide.tex", "pyBiocFileCache Documentation", "jkanche", "manual")
271-
]
266+
latex_documents = [("index", "user_guide.tex", "pyBiocFileCache Documentation", "jkanche", "manual")]
272267

273268
# The name of an image file (relative to this directory) to place at the top of
274269
# the title page.
@@ -304,4 +299,4 @@ def setup(app):
304299
"pyscaffold": ("https://pyscaffold.org/en/stable", None),
305300
}
306301

307-
print(f"loading configurations for {project} {version} ...", file=sys.stderr)
302+
print(f"loading configurations for {project} {version} ...", file=sys.stderr)

src/pybiocfilecache/BiocFileCache.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
from sqlalchemy.orm import Session
1010

1111
from ._exceptions import NoFpathError, RnameExistsError, RpathTimeoutError
12-
from .db import create_schema
13-
from .db.schema import Resource
12+
from .db.db_config import create_schema, Resource
1413
from .utils import copy_or_move, create_tmp_dir, generate_id
1514

1615
__author__ = "Jayaram Kancherla"

src/pybiocfilecache/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
from .BiocFileCache import BiocFileCache as BiocFileCache
1919

20-
from .db.schema import Metadata as Metadata
21-
from .db.schema import Resource as Resource
20+
from .db.db_config import Metadata as Metadata
21+
from .db.db_config import Resource as Resource
2222

2323
from ._exceptions import NoFpathError as NoFpathError
2424
from ._exceptions import RnameExistsError as RnameExistsError

src/pybiocfilecache/const.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
SCHEMA_VERSION = "0.99.4"

src/pybiocfilecache/db/Base.py

-36
This file was deleted.

src/pybiocfilecache/db/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
__copyright__ = "jkanche"
33
__license__ = "MIT"
44

5-
from .Base import create_schema
5+
from .db_config import create_schema

src/pybiocfilecache/db/db_config.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from typing import Tuple
2+
3+
from sqlalchemy import create_engine, select, Column, Integer, Text, DateTime, func
4+
from sqlalchemy.engine import Engine
5+
from sqlalchemy.orm.session import Session
6+
7+
from sqlalchemy.orm import declarative_base, sessionmaker
8+
9+
from ..const import SCHEMA_VERSION
10+
11+
__author__ = "jkanche"
12+
__copyright__ = "jkanche"
13+
__license__ = "MIT"
14+
15+
Base = declarative_base()
16+
17+
18+
class Metadata(Base):
19+
__tablename__ = "metadata"
20+
key = Column(Text(), primary_key=True, index=True)
21+
value = Column(Text())
22+
23+
def __repr__(self):
24+
return "<Metadata(key='%s', valye='%s')>" % (self.key, self.value)
25+
26+
27+
class Resource(Base):
28+
__tablename__ = "resource"
29+
id = Column(Integer, primary_key=True, index=True, autoincrement=True)
30+
rid = Column(Text())
31+
rname = Column(Text())
32+
create_time = Column(DateTime, server_default=func.now())
33+
access_time = Column(DateTime, server_default=func.now())
34+
rpath = Column(Text())
35+
rtype = Column(Text())
36+
fpath = Column(Text())
37+
last_modified_time = Column(DateTime, onupdate=func.now())
38+
etag = Column(Text())
39+
expires = Column(DateTime)
40+
41+
def __repr__(self):
42+
return "<Resource(id='%s', rname='%s')>" % (self.id, self.rname)
43+
44+
45+
def add_metadata(key: str, value: str, engine: Engine) -> None:
46+
"""Add metadata to the database.
47+
48+
Args:
49+
key:
50+
Key of the metadata.
51+
value:
52+
Value of the metadata.
53+
engine:
54+
Engine
55+
"""
56+
with Session(engine) as session:
57+
if session.scalar(select(Metadata).where(Metadata.key == key)):
58+
pass
59+
else:
60+
new_metadata = Metadata(key=key, value=value)
61+
session.add(new_metadata)
62+
session.commit()
63+
64+
65+
def create_schema(cache_dir: str) -> Tuple[Engine, sessionmaker]:
66+
"""Create the schema in the sqlite database.
67+
68+
Args:
69+
cache_dir:
70+
Location where the cache directory.
71+
72+
Returns:
73+
A tuple of sqlalchemy engine and session maker.
74+
"""
75+
engine = create_engine(
76+
f"sqlite:///{cache_dir}", connect_args={"check_same_thread": False}
77+
)
78+
79+
Base.metadata.create_all(bind=engine, checkfirst=True)
80+
sessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
81+
82+
add_metadata("schema_version", SCHEMA_VERSION, engine)
83+
84+
return (engine, sessionLocal)

src/pybiocfilecache/db/schema.py

-35
This file was deleted.

tests/conftest.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
"""Dummy conftest.py for pybiocfilecache.
2-
32
If you don't know what this is for, just leave it empty.
43
Read more about conftest.py under:
54
- https://docs.pytest.org/en/stable/fixture.html
65
- https://docs.pytest.org/en/stable/writing_plugins.html
76
"""
87

9-
# import pytest
8+
# import pytest

0 commit comments

Comments
 (0)