Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[dev-v1.0] Fix init db command #151

Merged
merged 2 commits into from
Jul 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)