|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from sqlalchemy import ( |
| 4 | + JSON, |
| 5 | + BigInteger, |
| 6 | + Column, |
| 7 | + DateTime, |
| 8 | + ForeignKey, |
| 9 | + Index, |
| 10 | + Integer, |
| 11 | + String, |
| 12 | + Text, |
| 13 | + UniqueConstraint, |
| 14 | + func, |
| 15 | +) |
| 16 | +from sqlalchemy.orm import DeclarativeBase, relationship |
| 17 | + |
| 18 | + |
| 19 | +class Base(DeclarativeBase): |
| 20 | + pass |
| 21 | + |
| 22 | + |
| 23 | +class Dataset(Base): |
| 24 | + __tablename__ = "datasets" |
| 25 | + |
| 26 | + id = Column(Integer, primary_key=True, autoincrement=True) |
| 27 | + org = Column(String(255), nullable=False, index=True) |
| 28 | + name = Column(String(255), nullable=False) |
| 29 | + source = Column(String(128), default="", server_default="") |
| 30 | + description = Column(Text, default="") |
| 31 | + tags = Column(JSON, default=list) |
| 32 | + owner = Column(String(255), default="") |
| 33 | + homepage = Column(String(512), nullable=True) |
| 34 | + repo = Column(String(512), nullable=True) |
| 35 | + paper = Column(String(512), nullable=True) |
| 36 | + leaderboard = Column(String(512), nullable=True) |
| 37 | + logo_url = Column(String(512), nullable=True) |
| 38 | + os = Column(String(64), nullable=True) |
| 39 | + version = Column(String(64), nullable=True) |
| 40 | + created_at = Column(DateTime, server_default=func.now()) |
| 41 | + updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) |
| 42 | + |
| 43 | + instances = relationship("Instance", back_populates="dataset", cascade="all, delete-orphan") |
| 44 | + permissions = relationship("DatasetPermission", back_populates="dataset", cascade="all, delete-orphan") |
| 45 | + splits_rel = relationship("Split", back_populates="dataset", cascade="all, delete-orphan") |
| 46 | + |
| 47 | + __table_args__ = (UniqueConstraint("org", "name", name="uq_dataset_org_name"),) |
| 48 | + |
| 49 | + @property |
| 50 | + def full_name(self) -> str: |
| 51 | + return f"{self.org}/{self.name}" |
| 52 | + |
| 53 | + def __repr__(self): |
| 54 | + return f"<Dataset(id={self.id}, name='{self.full_name}')>" |
| 55 | + |
| 56 | + |
| 57 | +class Instance(Base): |
| 58 | + __tablename__ = "instances" |
| 59 | + |
| 60 | + id = Column(Integer, primary_key=True, autoincrement=True) |
| 61 | + dataset_id = Column(Integer, ForeignKey("datasets.id", ondelete="CASCADE"), nullable=False) |
| 62 | + split = Column(String(255), nullable=False) |
| 63 | + name = Column(String(255), nullable=False) |
| 64 | + description = Column(Text, default="") |
| 65 | + type = Column(String(16), default="directory") |
| 66 | + size = Column(BigInteger, nullable=True) |
| 67 | + file_count = Column(Integer, nullable=True) |
| 68 | + etag = Column(String(255), nullable=True) |
| 69 | + format = Column(String(64), nullable=True, index=True) |
| 70 | + repo = Column(String(512), nullable=True) |
| 71 | + language = Column(String(64), nullable=True, index=True) |
| 72 | + difficulty = Column(String(64), nullable=True) |
| 73 | + base_commit = Column(String(64), nullable=True) |
| 74 | + image_uris = Column(JSON, nullable=True) |
| 75 | + tags = Column(JSON, default=list) |
| 76 | + raw = Column(Text, nullable=True) |
| 77 | + source_revision = Column(String(128), nullable=True) |
| 78 | + imported_from = Column(String(512), nullable=True) |
| 79 | + created_by = Column(String(255), nullable=True) |
| 80 | + created_at = Column(DateTime, server_default=func.now()) |
| 81 | + updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) |
| 82 | + |
| 83 | + dataset = relationship("Dataset", back_populates="instances") |
| 84 | + |
| 85 | + __table_args__ = ( |
| 86 | + UniqueConstraint("dataset_id", "split", "name", name="uq_instance_dataset_split_name"), |
| 87 | + Index("ix_instance_dataset_split", "dataset_id", "split"), |
| 88 | + ) |
| 89 | + |
| 90 | + def __repr__(self): |
| 91 | + return f"<Instance(id={self.id}, name='{self.name}', split='{self.split}')>" |
| 92 | + |
| 93 | + |
| 94 | +class Split(Base): |
| 95 | + __tablename__ = "splits" |
| 96 | + |
| 97 | + id = Column(Integer, primary_key=True, autoincrement=True) |
| 98 | + dataset_id = Column(Integer, ForeignKey("datasets.id", ondelete="CASCADE"), nullable=False) |
| 99 | + name = Column(String(255), nullable=False) |
| 100 | + task_count = Column(Integer, nullable=False, default=0) |
| 101 | + created_by = Column(String(255), nullable=True) |
| 102 | + created_at = Column(DateTime, server_default=func.now()) |
| 103 | + updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) |
| 104 | + |
| 105 | + dataset = relationship("Dataset", back_populates="splits_rel") |
| 106 | + |
| 107 | + __table_args__ = ( |
| 108 | + UniqueConstraint("dataset_id", "name", name="uq_split_dataset_name"), |
| 109 | + Index("ix_split_dataset_name", "dataset_id", "name"), |
| 110 | + ) |
| 111 | + |
| 112 | + def __repr__(self): |
| 113 | + return f"<Split(id={self.id}, dataset_id={self.dataset_id}, name='{self.name}', task_count={self.task_count})>" |
| 114 | + |
| 115 | + |
| 116 | +class Image(Base): |
| 117 | + __tablename__ = "images" |
| 118 | + |
| 119 | + source_image_uri = Column(String(512), primary_key=True) |
| 120 | + image_uri_sg = Column(String(512), nullable=True) |
| 121 | + image_uri_sh = Column(String(512), nullable=True) |
| 122 | + image_hash = Column(String(71), nullable=True) |
| 123 | + status = Column(String(32), nullable=False, default="pending", index=True) |
| 124 | + last_error = Column(Text, nullable=True) |
| 125 | + last_job_id = Column(String(64), nullable=True) |
| 126 | + created_by = Column(String(255), nullable=False, default="system") |
| 127 | + created_at = Column(DateTime, server_default=func.now()) |
| 128 | + updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) |
| 129 | + |
| 130 | + def __repr__(self): |
| 131 | + return f"<Image(source_image_uri='{self.source_image_uri}', status='{self.status}')>" |
| 132 | + |
| 133 | + |
| 134 | +class DatasetPermission(Base): |
| 135 | + __tablename__ = "dataset_permissions" |
| 136 | + |
| 137 | + id = Column(Integer, primary_key=True, autoincrement=True) |
| 138 | + dataset_id = Column(Integer, ForeignKey("datasets.id", ondelete="CASCADE"), nullable=False) |
| 139 | + user_id = Column(String(255), nullable=False) |
| 140 | + role = Column(String(32), nullable=False, default="viewer") |
| 141 | + granted_by = Column(String(255), nullable=True) |
| 142 | + created_at = Column(DateTime, server_default=func.now()) |
| 143 | + updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) |
| 144 | + |
| 145 | + dataset = relationship("Dataset", back_populates="permissions") |
| 146 | + |
| 147 | + __table_args__ = ( |
| 148 | + UniqueConstraint("dataset_id", "user_id", name="uq_permission_dataset_user"), |
| 149 | + Index("ix_permission_user", "user_id"), |
| 150 | + ) |
| 151 | + |
| 152 | + def __repr__(self): |
| 153 | + return f"<DatasetPermission(dataset_id={self.dataset_id}, user_id='{self.user_id}', role='{self.role}')>" |
| 154 | + |
| 155 | + |
| 156 | +class AuditEvent(Base): |
| 157 | + __tablename__ = "audit_events" |
| 158 | + |
| 159 | + id = Column(Integer, primary_key=True, autoincrement=True) |
| 160 | + target_type = Column(String(32), nullable=False, index=True) |
| 161 | + target_id = Column(String(512), nullable=False) |
| 162 | + event_type = Column(String(64), nullable=False, index=True) |
| 163 | + operator = Column(String(255), nullable=False) |
| 164 | + changes = Column(JSON, nullable=True) |
| 165 | + created_at = Column(DateTime, server_default=func.now()) |
| 166 | + |
| 167 | + __table_args__ = (Index("ix_audit_target", "target_type", "target_id"),) |
| 168 | + |
| 169 | + def __repr__(self): |
| 170 | + return f"<AuditEvent(id={self.id}, target_type='{self.target_type}', event_type='{self.event_type}')>" |
0 commit comments