Skip to content

Commit

Permalink
Change concept.concept_id from int to str.
Browse files Browse the repository at this point in the history
Issue #87
  • Loading branch information
Wim-De-Clercq committed Jan 2, 2023
1 parent 52cc726 commit 80b8e4b
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 73 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
- DB="sqlite" SAURL="sqlite:///:memory:"
- DB="postgres" SAURL="postgresql://postgres:postgres@localhost/skosprovider_sqlalchemy"
install:
- pip install --upgrade pip setuptools importlib-metadata
- pip install -r requirements-dev.txt
- python setup.py -q develop
before_script:
Expand Down
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ A SQLAlchemy implementation of the skosprovider_ interface.
:target: https://coveralls.io/r/koenedaele/skosprovider_sqlalchemy


Migrating to skosprovider_sqlalchemy 2.0.0
------------------------------------------
A change in the models has been made which requires a database upgrade.
The "concept" table's "concept_id" column has changed from being an int to a string.

Existing databases will therefor require a small change to update table scheme.
Typically this will look like::

ALTER TABLE concept ALTER COLUMN concept_id TEXT NOT NULL;


Building the docs
-----------------

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

setup(
name='skosprovider_sqlalchemy',
version='1.0.0',
version='2.0.0',
description='A sqlAlchemy implementation of skosprovider.',
long_description=open('README.rst').read(),
long_description_content_type='text/x-rst',
Expand Down
2 changes: 1 addition & 1 deletion skosprovider_sqlalchemy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class Thing(Base):
id = Column(Integer, primary_key=True)
type = Column(String(30))
concept_id = Column(
Integer,
String,
nullable=False,
index=True
)
Expand Down
8 changes: 4 additions & 4 deletions skosprovider_sqlalchemy/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ def _from_thing(self, thing):
matches=matches
)

def get_by_id(self, id):
def get_by_id(self, concept_id):
try:
thing = self.session\
.query(Thing)\
.options(joinedload('labels'))\
.options(joinedload('notes'))\
.options(joinedload('sources'))\
.filter(
Thing.concept_id == int(id),
Thing.concept_id == str(concept_id),
Thing.conceptscheme_id == self.conceptscheme_id
).one()
except NoResultFound:
Expand Down Expand Up @@ -328,7 +328,7 @@ def expand(self, id):
thing = self.session\
.query(Thing)\
.filter(
Thing.concept_id == id,
Thing.concept_id == str(id),
Thing.conceptscheme_id == self.conceptscheme_id
).one()
except NoResultFound:
Expand Down Expand Up @@ -428,7 +428,7 @@ def get_children_display(self, id, **kwargs):
thing = self.session\
.query(Thing)\
.filter(
Thing.concept_id == int(id),
Thing.concept_id == str(id),
Thing.conceptscheme_id == self.conceptscheme_id
).one()
except NoResultFound:
Expand Down
12 changes: 6 additions & 6 deletions skosprovider_sqlalchemy/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ def import_provider(provider, conceptscheme, session):
if isinstance(c, Concept):
cm = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(c.id)) \
.filter(ConceptModel.concept_id == str(c.id)) \
.one()
if len(c.narrower) > 0:
for nc in c.narrower:
try:
nc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(nc)) \
.filter(ConceptModel.concept_id == str(nc)) \
.one()
cm.narrower_concepts.add(nc)
except NoResultFound:
Expand All @@ -94,7 +94,7 @@ def import_provider(provider, conceptscheme, session):
try:
sa = session.query(CollectionModel) \
.filter(CollectionModel.conceptscheme_id == conceptscheme.id) \
.filter(CollectionModel.concept_id == int(sa)) \
.filter(CollectionModel.concept_id == str(sa)) \
.one()
cm.narrower_collections.add(sa)
except NoResultFound:
Expand All @@ -106,7 +106,7 @@ def import_provider(provider, conceptscheme, session):
try:
rc = session.query(ConceptModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(rc)) \
.filter(ConceptModel.concept_id == str(rc)) \
.one()
cm.related_concepts.add(rc)
except NoResultFound:
Expand All @@ -116,13 +116,13 @@ def import_provider(provider, conceptscheme, session):
elif isinstance(c, Collection) and len(c.members) > 0:
cm = session.query(CollectionModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(c.id)) \
.filter(ConceptModel.concept_id == str(c.id)) \
.one()
for mc in c.members:
try:
mc = session.query(ThingModel) \
.filter(ConceptModel.conceptscheme_id == conceptscheme.id) \
.filter(ConceptModel.concept_id == int(mc)) \
.filter(ConceptModel.concept_id == str(mc)) \
.one()
cm.members.add(mc)
except NoResultFound:
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def pytest_addoption(parser):
parser.addoption(
'--sqlalchemy_url',
action='store',
default='sqlite:///:memory:',
default='postgresql://postgres:postgres@localhost/skosprovider_sqlalchemy',
help='SQLAlchemy connection url to database under test.'
)

Expand Down
Loading

0 comments on commit 80b8e4b

Please sign in to comment.