Skip to content

Commit

Permalink
Merge pull request #151 from pdelboca/fix-init-db-command
Browse files Browse the repository at this point in the history
[dev-v1.0] Fix init db command
  • Loading branch information
tino097 authored Jul 6, 2024
2 parents a728d75 + cf26a5a commit cf34d53
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions ckanext/datapusher_plus/model/jobs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from datetime import datetime
from sqlalchemy import types, Column, Table, ForeignKey

from ckan.model import meta, DomainObject


__all__ = ["Jobs", "jobs_table", "Metadata", "metadata_table", "Logs", "logs_table"]
__all__ = [
"Jobs", "jobs_table", "Metadata", "metadata_table", "Logs", "logs_table"
]

"""Initialise the "jobs" table in the db."""
jobs_table = Table("jobs",
jobs_table = Table(
"jobs",
meta.metadata,
Column("job_id", types.UnicodeText, primary_key=True),
Column("job_type", types.UnicodeText),
Expand All @@ -24,21 +26,21 @@
Column("api_key", types.UnicodeText),
# Key to administer job:
Column("job_key", types.UnicodeText),
)
)

metadata_table = Table(
"metadata",
meta.metadata,
Column(
"job_id",
ForeignKey("jobs.job_id", ondelete="CASCADE"),
nullable=False,
primary_key=True,
),
Column("key", types.UnicodeText, primary_key=True),
Column("value", types.UnicodeText, index=True),
Column("type", types.UnicodeText),
)
"metadata",
meta.metadata,
Column(
"job_id",
ForeignKey("jobs.job_id", ondelete="CASCADE"),
nullable=False,
primary_key=True,
),
Column("key", types.UnicodeText, primary_key=True),
Column("value", types.UnicodeText, index=True),
Column("type", types.UnicodeText),
)

"""Initialise the "logs" table in the db."""
logs_table = Table(
Expand All @@ -58,6 +60,7 @@
Column("lineno", types.Integer),
)


class Jobs(DomainObject):
def __init__(self, job_id, job_type, status, data, error, requested_timestamp, finished_timestamp, sent_data, aps_job_id, result_url, api_key, job_key):
self.job_id = job_id
Expand Down Expand Up @@ -103,7 +106,6 @@ def get_by_job_key(cls, job_key):

return meta.Session.query(cls).filter(cls.job_key == job_key).first()


@classmethod
def get_by_status(cls, status):
if not status:
Expand All @@ -121,7 +123,7 @@ def update(cls, job_dict):
meta.Session.commit()
else:
raise Exception("Job not found")


class Metadata(DomainObject):
def __init__(self, job_id, key, value, type):
Expand All @@ -134,8 +136,11 @@ def __init__(self, job_id, key, value, type):
def get(cls, job_id, key):
if not job_id:
return None

return meta.Session.query(cls).filter(cls.job_id == job_id).filter(cls.key == key).first()
result = meta.Session.query(cls) \
.filter(cls.job_id == job_id) \
.filter(cls.key == key)\
.first()
return result


class Logs(DomainObject):
Expand All @@ -154,27 +159,34 @@ def get(cls, job_id):
return None

return meta.Session.query(cls).filter(cls.job_id == job_id).all()

# Return any logs for the given job_id from the logs table.
@classmethod
def get_logs(cls, job_id):
if not job_id:
return None

return meta.Session.query(cls).filter(cls.job_id == job_id).all()

@classmethod
def get_logs_by_limit(cls, job_id, limit):
if not job_id:
return None

return meta.Session.query(cls).filter(cls.job_id == job_id).order_by(cls.timestamp.desc()).limit(limit).all()

result = meta.Session.query(cls) \
.filter(cls.job_id == job_id) \
.order_by(cls.timestamp.desc()) \
.limit(limit) \
.all()
return result


meta.mapper(Jobs, jobs_table)
meta.mapper(Metadata, metadata_table)
meta.mapper(Logs, logs_table)


def init_tables():
meta.metadata.create_all(meta.engine)
jobs_table.create(meta.engine)
metadata_table.create(meta.engine)
logs_table.create(meta.engine)

0 comments on commit cf34d53

Please sign in to comment.