from . import db # tag to object relationship table image_tags = db.Table( "image_tags", db.Column( "image_id", db.Integer, db.ForeignKey("image_record.id", ondelete="CASCADE"), primary_key=True, ), db.Column( "tag_id", db.Integer, db.ForeignKey("tag.id", ondelete="CASCADE"), primary_key=True, ), db.UniqueConstraint("image_id", "tag_id", name="uq_image_tag"), ) class ImageRecord(db.Model): __tablename__ = "image_record" id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(255), nullable=False) filepath = db.Column(db.String(512), nullable=False) thumb_path = db.Column(db.String(512)) hash = db.Column(db.String(64), unique=True, nullable=False) # SHA256 perceptual_hash = db.Column(db.String(64), nullable=True) # hex of 256-bit hash file_size = db.Column(db.Integer, nullable=True) width = db.Column(db.Integer, nullable=True) height = db.Column(db.Integer, nullable=True) format = db.Column(db.String(10), nullable=True) camera_model = db.Column(db.String(255), nullable=True) taken_at = db.Column(db.DateTime, nullable=True) imported_at = db.Column(db.DateTime, default=db.func.now()) is_thumbnail = db.Column(db.Boolean, default=False) tags = db.relationship( "Tag", secondary=image_tags, back_populates="images", passive_deletes=True, ) class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(255), unique=True, nullable=False) kind = db.Column(db.String(64), nullable=True) images = db.relationship( "ImageRecord", secondary=image_tags, back_populates="tags", passive_deletes=True, ) class ArchiveRecord(db.Model): """ Tracks archives we've processed so we can skip re-importing the same large file. Also holds a Tag that marks images that came from this archive. """ __tablename__ = "archive_record" id = db.Column(db.Integer, primary_key=True) filename = db.Column(db.String(512), nullable=False) file_size = db.Column(db.BigInteger, nullable=False) hash = db.Column(db.String(64), unique=True, nullable=False) # SHA256 of the archive file imported_at = db.Column(db.DateTime, default=db.func.now()) artist = db.Column(db.String(255), nullable=True) tag_id = db.Column(db.Integer, db.ForeignKey("tag.id", ondelete="SET NULL"), nullable=True) tag = db.relationship("Tag") class AppSettings(db.Model): """ Key-value store for application settings and version tracking. Used to persist configuration and trigger data migrations on version changes. """ __tablename__ = "app_settings" id = db.Column(db.Integer, primary_key=True) key = db.Column(db.String(64), unique=True, nullable=False) value = db.Column(db.Text, nullable=True) updated_at = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now()) class ImportBatch(db.Model): """ Groups related import tasks for batch tracking and reporting. One batch per scan operation. """ __tablename__ = "import_batch" id = db.Column(db.Integer, primary_key=True) source_directory = db.Column(db.String(1024), nullable=False) status = db.Column(db.String(32), default='running', index=True) # running, complete, failed, cancelled # Statistics total_files = db.Column(db.Integer, default=0) processed_files = db.Column(db.Integer, default=0) imported_count = db.Column(db.Integer, default=0) skipped_count = db.Column(db.Integer, default=0) failed_count = db.Column(db.Integer, default=0) # Timestamps started_at = db.Column(db.DateTime, default=db.func.now()) completed_at = db.Column(db.DateTime, nullable=True) class ImportTask(db.Model): """ Tracks the state of each file being processed through the import pipeline. Enables resume capability and parallel processing. """ __tablename__ = "import_task" id = db.Column(db.Integer, primary_key=True) # File identification source_path = db.Column(db.String(1024), nullable=False, index=True) file_hash = db.Column(db.String(64), nullable=True, index=True) # SHA256, computed during processing file_size = db.Column(db.BigInteger, nullable=True) # Classification # Values: 'scan', 'import_image', 'import_archive', 'thumbnail', 'sidecar' task_type = db.Column(db.String(32), nullable=False, index=True) # State tracking # Values: 'pending', 'queued', 'processing', 'complete', 'failed', 'skipped' status = db.Column(db.String(32), nullable=False, default='pending', index=True) # Celery task tracking celery_task_id = db.Column(db.String(64), nullable=True, index=True) # Context/metadata (JSON) # For images: {"artist": "...", "dest_dir": "..."} # For archives: {"artist": "...", "dest_dir": "...", "archive_tag": "..."} # For sidecars: {"image_id": 123, "sidecar_path": "..."} context = db.Column(db.JSON, nullable=True) # Batch relationship batch_id = db.Column(db.Integer, db.ForeignKey('import_batch.id', ondelete='SET NULL'), nullable=True) batch = db.relationship('ImportBatch', backref=db.backref('tasks', lazy='dynamic')) # Result tracking result_image_id = db.Column(db.Integer, db.ForeignKey('image_record.id', ondelete='SET NULL'), nullable=True) result_image = db.relationship('ImageRecord', foreign_keys=[result_image_id]) error_message = db.Column(db.Text, nullable=True) retry_count = db.Column(db.Integer, default=0) # Timestamps created_at = db.Column(db.DateTime, default=db.func.now(), index=True) started_at = db.Column(db.DateTime, nullable=True) completed_at = db.Column(db.DateTime, nullable=True) # Unique constraint: prevent duplicate tasks for same file+type combination # Note: file_hash can be NULL initially (computed during processing) __table_args__ = ( db.Index('ix_import_task_status_type', 'status', 'task_type'), db.Index('ix_import_task_batch_status', 'batch_id', 'status'), )