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

refact: update to SQLAlchemy 2.0 #102

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion chord_drs/db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from flask_sqlalchemy import SQLAlchemy

from .models import Base

__all__ = ["db"]

db = SQLAlchemy()
db = SQLAlchemy(model_class=Base)
45 changes: 27 additions & 18 deletions chord_drs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,47 @@
from flask import current_app
from hashlib import sha256
from pathlib import Path
from sqlalchemy import Boolean, Column, DateTime, ForeignKey, Integer, String
from sqlalchemy.sql import func
from sqlalchemy.orm import relationship
from sqlalchemy.orm import declarative_base, relationship
from werkzeug.utils import secure_filename
from urllib.parse import urlparse
from uuid import uuid4

from .backend import get_backend
from .backends.minio import MinioBackend
from .db import db
from .utils import drs_file_checksum

__all__ = [
"Base",
"DrsMixin",
"DrsBlob",
"DrsBundle",
]

Base = declarative_base()


class DrsMixin:
# IDs (PKs) must remain outside the mixin!
created = db.Column(db.DateTime, server_default=func.now())
checksum = db.Column(db.String(64), nullable=False)
size = db.Column(db.Integer, default=0)
name = db.Column(db.String(250), nullable=True)
description = db.Column(db.String(1000), nullable=True)
created = Column(DateTime, server_default=func.now())
checksum = Column(String(64), nullable=False)
size = Column(Integer, default=0)
name = Column(String(250), nullable=True)
description = Column(String(1000), nullable=True)
# Permissions/Bento-specific project & dataset tagging for DRS items
# TODO: Make some of these not nullable in the future:
project_id = db.Column(db.String(64), nullable=True) # Nullable for backwards-compatibility
dataset_id = db.Column(db.String(64), nullable=True) # Nullable for backwards-compatibility / project-only stuff?
data_type = db.Column(db.String(24), nullable=True) # NULL if multi-data type or something else
public = db.Column(db.Boolean, default=False, nullable=False) # If true, the object is accessible by anyone
project_id = Column(String(64), nullable=True) # Nullable for backwards-compatibility
dataset_id = Column(String(64), nullable=True) # Nullable for backwards-compatibility / project-only stuff?
data_type = Column(String(24), nullable=True) # NULL if multi-data type or something else
public = Column(Boolean, default=False, nullable=False) # If true, the object is accessible by anyone


class DrsBundle(db.Model, DrsMixin):
class DrsBundle(Base, DrsMixin):
__tablename__ = "drs_bundle"

id = db.Column(db.String, primary_key=True)
parent_bundle_id = db.Column(db.Integer, db.ForeignKey("drs_bundle.id"))
id = Column(String, primary_key=True)
parent_bundle_id = Column(Integer, ForeignKey("drs_bundle.id"))
parent_bundle = relationship("DrsBundle", remote_side=[id])
objects = relationship("DrsBlob", cascade="all, delete-orphan", backref="bundle")

Expand Down Expand Up @@ -62,12 +71,12 @@ def update_checksum_and_size(self):
self.size = total_size


class DrsBlob(db.Model, DrsMixin):
class DrsBlob(Base, DrsMixin):
__tablename__ = "drs_object"

id = db.Column(db.String, primary_key=True)
bundle_id = db.Column(db.Integer, db.ForeignKey(DrsBundle.id), nullable=True)
location = db.Column(db.String(500), nullable=False)
id = Column(String, primary_key=True)
bundle_id = Column(Integer, ForeignKey(DrsBundle.id), nullable=True)
location = Column(String(500), nullable=False)

def __init__(self, *args, **kwargs):
logger = current_app.logger
Expand Down
Loading