|
2 | 2 | import pathlib |
3 | 3 | from typing import Optional, Dict, List |
4 | 4 |
|
5 | | -from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ForeignKey, BigInteger |
| 5 | +from sqlalchemy import Column, Integer, String, Boolean, JSON, Date, ForeignKey, BigInteger, Index, text |
6 | 6 | from sqlalchemy.orm import relationship, Session, deferred |
7 | 7 | from sqlalchemy.orm.collections import InstrumentedList |
8 | 8 |
|
|
28 | 28 |
|
29 | 29 | class Video(ModelHelper, Base): |
30 | 30 | __tablename__ = 'video' |
| 31 | + __table_args__ = ( |
| 32 | + Index('video_channel_id_idx', 'channel_id'), |
| 33 | + Index('video_source_id_idx', 'source_id'), |
| 34 | + Index('video_view_count_idx', 'view_count'), |
| 35 | + ) |
31 | 36 | id = Column(Integer, primary_key=True) |
32 | 37 |
|
33 | 38 | source_id = Column(String) # The id from yt-dlp |
@@ -530,18 +535,25 @@ def replace_info_json(self, info_json: dict, clean: bool = True, format_: bool = |
530 | 535 |
|
531 | 536 | class Channel(ModelHelper, Base): |
532 | 537 | __tablename__ = 'channel' |
| 538 | + __table_args__ = ( |
| 539 | + Index('channel_minimum_frequency_idx', 'minimum_frequency'), |
| 540 | + Index('channel_source_id', 'source_id'), |
| 541 | + Index('channel_total_size_idx', 'total_size'), |
| 542 | + Index('channel_video_count_idx', 'video_count'), |
| 543 | + Index('channel_url_key', 'url', unique=True, postgresql_where=text('url IS NOT NULL')), |
| 544 | + ) |
533 | 545 | id = Column(Integer, primary_key=True) |
534 | 546 | # name and directory are stored in Collection, accessed via properties |
535 | | - url = Column(String, unique=True) # will only be downloaded if related Download exists. |
| 547 | + url = Column(String) # will only be downloaded if related Download exists. Partial unique index on non-NULL values. |
536 | 548 | generate_posters = Column(Boolean, default=False) # generating posters may delete files, and can be slow. |
537 | 549 | calculate_duration = Column(Boolean, default=True) # use ffmpeg to extract duration (slower than info json). |
538 | 550 | download_missing_data = Column(Boolean, default=True) # fetch missing data like `source_id` and video comments. |
539 | 551 | source_id = Column(String) # the ID from the source website. |
540 | 552 | refreshed = Column(Boolean, default=False) # The files in the Channel have been refreshed. |
541 | 553 |
|
542 | 554 | # Columns updated by triggers |
543 | | - video_count = Column(Integer, default=0) # update_channel_video_count |
544 | | - total_size = Column(Integer, default=0) # update_channel_size |
| 555 | + video_count = Column(Integer, default=0, nullable=False) # update_channel_video_count |
| 556 | + total_size = Column(BigInteger, default=0, nullable=False) # update_channel_size |
545 | 557 | minimum_frequency = Column(Integer) # update_channel_minimum_frequency |
546 | 558 |
|
547 | 559 | info_json = deferred(Column(JSON)) |
|
0 commit comments