File size: 51,863 Bytes
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53c2ab
61d6e2a
 
 
2e79f82
61d6e2a
 
2e79f82
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109b8ca
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a4cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65651b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849f335
 
 
 
 
2e79f82
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
849f335
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c53c2ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de342dc
 
 
 
 
 
 
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
2e79f82
65651b1
 
2e79f82
 
65651b1
2e79f82
65651b1
2e79f82
 
65651b1
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65651b1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53e3df0
 
 
61d6e2a
 
 
 
 
 
 
 
b2a4cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e79f82
b2a4cfc
 
 
 
 
 
2e79f82
65651b1
2e79f82
 
65651b1
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2a4cfc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c71e4ac
 
 
 
 
 
 
2e79f82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61d6e2a
 
 
 
 
 
 
 
65651b1
61d6e2a
 
 
 
 
 
65651b1
61d6e2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
"""Database layer (SQLAlchemy 2.0 async). PostgreSQL in production, SQLite for local tests."""
from __future__ import annotations

import asyncio
import logging
import os
from datetime import datetime, timezone
from typing import Any, Optional
from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit

from sqlalchemy import (
    JSON,
    BigInteger,
    Boolean,
    DateTime,
    ForeignKey,
    Integer,
    String,
    Text,
    UniqueConstraint,
    delete,
    desc,
    func,
    select,
    update,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.exc import IntegrityError
from sqlalchemy.ext.asyncio import async_sessionmaker, create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.pool import NullPool

log = logging.getLogger(__name__)

JSONType = JSON().with_variant(JSONB(), "postgresql")


def utcnow() -> datetime:
    return datetime.now(timezone.utc)


class Base(DeclarativeBase):
    pass


class Channel(Base):
    __tablename__ = "channels"

    id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=False)
    access_hash: Mapped[int] = mapped_column(BigInteger, default=0)
    title: Mapped[str] = mapped_column(String(256), default="")
    username: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    active: Mapped[bool] = mapped_column(Boolean, default=True)
    added_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class Post(Base):
    __tablename__ = "posts"
    __table_args__ = (UniqueConstraint("channel_id", "message_id", name="uq_post_channel_msg"),)

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    channel_id: Mapped[int] = mapped_column(BigInteger, ForeignKey("channels.id"), index=True)
    message_id: Mapped[Optional[int]] = mapped_column(BigInteger, nullable=True)
    status: Mapped[str] = mapped_column(String(16), default="draft", index=True)  # draft | sent
    source: Mapped[str] = mapped_column(String(16), default="bot")  # bot | adopted
    text: Mapped[str] = mapped_column(Text, default="")
    entities: Mapped[list] = mapped_column(JSONType, default=list)
    media_kind: Mapped[Optional[str]] = mapped_column(String(16), nullable=True)
    media_file_id: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
    buttons: Mapped[list] = mapped_column(JSONType, default=list)
    link_preview: Mapped[bool] = mapped_column(Boolean, default=False)
    created_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
    updated_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
    sent_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)

    def as_dict(self) -> dict:
        return {c.name: getattr(self, c.name) for c in self.__table__.columns}


class Batch(Base):
    """One /swap run (so it can be undone)."""

    __tablename__ = "batches"

    id: Mapped[str] = mapped_column(String(32), primary_key=True)
    old_username: Mapped[str] = mapped_column(String(64))
    new_username: Mapped[str] = mapped_column(String(64))
    created_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
    undone: Mapped[bool] = mapped_column(Boolean, default=False)


class Change(Base):
    __tablename__ = "changes"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    batch_id: Mapped[str] = mapped_column(String(32), ForeignKey("batches.id"), index=True)
    channel_id: Mapped[int] = mapped_column(BigInteger)
    message_id: Mapped[int] = mapped_column(BigInteger)
    old_state: Mapped[dict] = mapped_column(JSONType, default=dict)
    new_state: Mapped[dict] = mapped_column(JSONType, default=dict)
    reverted: Mapped[bool] = mapped_column(Boolean, default=False)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class Migration(Base):
    """One /repost run: a channel copied post by post, so it can be continued, undone or finished."""

    __tablename__ = "migrations"

    id: Mapped[str] = mapped_column(String(32), primary_key=True)
    channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    old_username: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    new_username: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    include_typed: Mapped[bool] = mapped_column(Boolean, default=True)
    include_posts: Mapped[bool] = mapped_column(Boolean, default=False)
    first_id: Mapped[int] = mapped_column(BigInteger)
    last_id: Mapped[int] = mapped_column(BigInteger)
    partial: Mapped[bool] = mapped_column(Boolean, default=False)
    # copying | stopped | incomplete | copied | old_deleted | copies_deleted
    status: Mapped[str] = mapped_column(String(16), default="copying")
    created_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class MigrationItem(Base):
    __tablename__ = "migration_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    migration_id: Mapped[str] = mapped_column(String(32), ForeignKey("migrations.id"), index=True)
    old_id: Mapped[int] = mapped_column(BigInteger)
    new_id: Mapped[int] = mapped_column(BigInteger)
    old_deleted: Mapped[bool] = mapped_column(Boolean, default=False)
    new_deleted: Mapped[bool] = mapped_column(Boolean, default=False)


class Shift(Base):
    """One /shift run: posts of a source channel copied into another channel (so it can be continued or undone)."""

    __tablename__ = "shifts"

    id: Mapped[str] = mapped_column(String(32), primary_key=True)
    src_channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    src_access_hash: Mapped[int] = mapped_column(BigInteger, default=0)
    src_title: Mapped[str] = mapped_column(String(256), default="")
    src_username: Mapped[Optional[str]] = mapped_column(String(64), nullable=True)
    dst_channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    first_id: Mapped[int] = mapped_column(BigInteger)
    last_id: Mapped[int] = mapped_column(BigInteger)
    # copying | stopped | incomplete | copied | copies_deleted
    status: Mapped[str] = mapped_column(String(16), default="copying")
    created_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class ShiftItem(Base):
    __tablename__ = "shift_items"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    shift_id: Mapped[str] = mapped_column(String(32), ForeignKey("shifts.id"), index=True)
    old_id: Mapped[int] = mapped_column(BigInteger)  # message id in the source channel
    new_id: Mapped[int] = mapped_column(BigInteger)  # message id of the copy in the destination channel
    new_deleted: Mapped[bool] = mapped_column(Boolean, default=False)


class ScheduledDelete(Base):
    """A channel message the bot has to delete at a given time (a broadcast with an expiry)."""

    __tablename__ = "scheduled_deletes"

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    message_id: Mapped[int] = mapped_column(BigInteger)
    delete_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), index=True)
    group_id: Mapped[str] = mapped_column(String(32), default="")  # the broadcast it belongs to
    status: Mapped[str] = mapped_column(String(12), default="pending")  # pending | failed
    attempts: Mapped[int] = mapped_column(Integer, default=0)
    last_error: Mapped[Optional[str]] = mapped_column(String(160), nullable=True)
    created_by: Mapped[int] = mapped_column(BigInteger, default=0)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class ChannelSync(Base):
    """Where the last look at a channel stopped, so the next one only has to read what is new."""

    __tablename__ = "channel_sync"

    channel_id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=False)
    last_top: Mapped[int] = mapped_column(BigInteger, default=0)  # the newest message id that existed then
    last_pts: Mapped[int] = mapped_column(BigInteger, default=0)  # the channel's event counter then
    deferred: Mapped[int] = mapped_column(Integer, default=0)  # new messages left for the next look (too fresh then)
    last_run: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
    last_deep: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)  # last look at every id


class ChannelHistory(Base):
    """How far the older posts of a channel (the ones sent before the bot was there) have been read into My posts."""

    __tablename__ = "channel_history"

    channel_id: Mapped[int] = mapped_column(BigInteger, primary_key=True, autoincrement=False)
    top: Mapped[int] = mapped_column(BigInteger, default=0)  # the newest message id that counts as "older"
    next_id: Mapped[int] = mapped_column(BigInteger, default=1)  # where the reading goes on
    imported: Mapped[int] = mapped_column(Integer, default=0)  # older posts added to My posts so far
    done: Mapped[bool] = mapped_column(Boolean, default=False)
    started_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)
    finished_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)


class IgnoredPost(Base):
    """A post the owner made the bot forget: it stays in the channel, but the sync never takes it over again."""

    __tablename__ = "ignored_posts"
    __table_args__ = (UniqueConstraint("channel_id", "message_id", name="uq_ignored_channel_msg"),)

    id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
    channel_id: Mapped[int] = mapped_column(BigInteger, index=True)
    message_id: Mapped[int] = mapped_column(BigInteger)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class JobMark(Base):
    """A fact about a /repost or /shift run that has to outlive a restart, e.g. that links in other channels were
    pointed at its copies (so that Undo points them back), or which extras a shift was asked for."""

    __tablename__ = "job_marks"

    job_id: Mapped[str] = mapped_column(String(32), primary_key=True)
    mark: Mapped[str] = mapped_column(String(24), primary_key=True)
    created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), default=utcnow)


class Admin(Base):
    __tablename__ = "admins"
    user_id: Mapped[int] = mapped_column(BigInteger, primary_key=True)


def as_utc(dt: Optional[datetime]) -> Optional[datetime]:
    """A datetime from the database as an aware UTC one (SQLite hands back naive values)."""
    if dt is None:
        return None
    return dt.replace(tzinfo=timezone.utc) if dt.tzinfo is None else dt.astimezone(timezone.utc)


def normalize_db_url(url: str) -> tuple:
    """Turn a plain DATABASE_URL into a SQLAlchemy async URL + connect args."""
    connect_args: dict = {}
    if url.startswith("postgres://"):
        url = "postgresql://" + url[len("postgres://"):]
    if url.startswith("postgresql://"):
        url = "postgresql+asyncpg://" + url[len("postgresql://"):]
    if url.startswith("postgresql+asyncpg://"):
        parts = urlsplit(url)
        keep = []
        for k, v in parse_qsl(parts.query, keep_blank_values=True):
            if k == "sslmode":
                if v in ("require", "verify-ca", "verify-full"):
                    connect_args["ssl"] = True
            elif k in ("channel_binding", "options"):
                continue
            else:
                keep.append((k, v))
        host = parts.hostname or ""
        if "-pooler" in host or "pgbouncer" in host:
            # PgBouncer in transaction mode does not support prepared statements
            connect_args["statement_cache_size"] = 0
            keep.append(("prepared_statement_cache_size", "0"))
        url = urlunsplit(parts._replace(query=urlencode(keep)))
    elif url.startswith("sqlite:///"):
        url = "sqlite+aiosqlite:///" + url[len("sqlite:///"):]
    return url, connect_args


class Database:
    def __init__(self, url: str, pool: str = "null"):
        self.url, connect_args = normalize_db_url(url)
        kwargs: dict = {"connect_args": connect_args}
        if self.url.startswith("sqlite"):
            path = self.url.split(":///", 1)[-1]
            if path and path != ":memory:":
                os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
        elif pool == "queue":
            kwargs.update(pool_size=5, max_overflow=5, pool_pre_ping=True, pool_recycle=1800)
        else:
            # short-lived connections: lets serverless Postgres (e.g. Neon) go to sleep when idle
            kwargs["poolclass"] = NullPool
        self.engine = create_async_engine(self.url, **kwargs)
        self.Session = async_sessionmaker(self.engine, expire_on_commit=False)

    async def init(self, retries: int = 6) -> None:
        last: Optional[BaseException] = None
        for i in range(retries):
            try:
                async with self.engine.begin() as conn:
                    await conn.run_sync(Base.metadata.create_all)
                return
            except Exception as e:  # cold start of serverless DBs can fail once
                last = e
                log.warning("database not ready (%s) - retry %s/%s", e, i + 1, retries)
                await asyncio.sleep(min(2 ** i, 15))
        assert last is not None
        raise last

    async def close(self) -> None:
        await self.engine.dispose()

    async def list_admins(self) -> set[int]:
        async with self.Session() as s:
            return set((await s.execute(select(Admin.user_id))).scalars())

    async def add_admin(self, uid: int) -> bool:
        async with self.Session() as s:
            if await s.get(Admin, uid) is not None:
                return False
            s.add(Admin(user_id=uid))
            await s.commit()
            return True

    async def remove_admin(self, uid: int) -> bool:
        async with self.Session() as s:
            row = await s.get(Admin, uid)
            if row is None:
                return False
            await s.delete(row)
            await s.commit()
            return True

    # ------------------------------------------------------------------ channels
    async def list_channels(self, active_only: bool = True) -> list:
        async with self.Session() as s:
            q = select(Channel).order_by(Channel.title)
            if active_only:
                q = q.where(Channel.active.is_(True))
            return list((await s.execute(q)).scalars())

    async def get_channel(self, cid: int) -> Optional[Channel]:
        async with self.Session() as s:
            return await s.get(Channel, cid)

    async def save_channel(self, cid: int, access_hash: int, title: str, username: Optional[str], added_by: int) -> Channel:
        async with self.Session() as s:
            ch = await s.get(Channel, cid)
            if ch is None:
                ch = Channel(id=cid, access_hash=access_hash, title=title, username=username, added_by=added_by, active=True)
                s.add(ch)
            else:
                ch.access_hash = access_hash or ch.access_hash
                ch.title = title or ch.title
                ch.username = username
                ch.active = True
            await s.commit()
            return ch

    async def touch_channel_hash(self, cid: int, access_hash: int) -> None:
        async with self.Session() as s:
            ch = await s.get(Channel, cid)
            if ch is not None and access_hash and ch.access_hash != access_hash:
                ch.access_hash = access_hash
                await s.commit()

    async def set_channel_active(self, cid: int, active: bool) -> None:
        async with self.Session() as s:
            ch = await s.get(Channel, cid)
            if ch is not None:
                ch.active = active
                await s.commit()

    async def count_channel_posts(self, cid: int) -> int:
        """Every post of the channel in My posts - published ones and drafts."""
        async with self.Session() as s:
            return int((await s.execute(select(func.count()).select_from(Post).where(Post.channel_id == cid))).scalar_one())

    async def delete_channel_data(self, cid: int) -> int:
        """Everything My posts holds about a channel: its saved posts (published and drafts) and what the sync keeps
        about them - the posts forgotten on purpose, where the last look and the reading of the older posts stopped - so a
        channel that is added again starts from scratch. Nothing is touched in Telegram. The channel itself stays
        registered (switched off) because reposts and shifts that name it still need it, and so do timed deletions.
        Returns how many posts went."""
        async with self.Session() as s:
            n = (await s.execute(select(func.count()).select_from(Post).where(Post.channel_id == cid))).scalar_one()
            await s.execute(delete(Post).where(Post.channel_id == cid))
            for model in (IgnoredPost, ChannelSync, ChannelHistory):
                await s.execute(delete(model).where(model.channel_id == cid))
            await s.commit()
            return int(n)

    # --------------------------------------------------------------------- posts
    async def create_post(self, **fields: Any) -> Post:
        async with self.Session() as s:
            p = Post(**fields)
            s.add(p)
            await s.commit()
            return p

    async def get_post(self, pid: int) -> Optional[Post]:
        async with self.Session() as s:
            return await s.get(Post, pid)

    async def update_post(self, pid: int, **fields: Any) -> Optional[Post]:
        async with self.Session() as s:
            p = await s.get(Post, pid)
            if p is None:
                return None
            for k, v in fields.items():
                setattr(p, k, v)
            p.updated_at = utcnow()
            await s.commit()
            return p

    async def delete_post(self, pid: int) -> None:
        async with self.Session() as s:
            p = await s.get(Post, pid)
            if p is not None:
                await s.delete(p)
                await s.commit()

    async def list_posts(self, channel_id: Optional[int], status: Optional[str], offset: int, limit: int) -> tuple:
        cond = []
        if channel_id is not None:
            cond.append(Post.channel_id == channel_id)
        if status:
            cond.append(Post.status == status)
        async with self.Session() as s:
            total = (await s.execute(select(func.count()).select_from(Post).where(*cond))).scalar_one()
            q = (
                select(Post)
                .where(*cond)
                .order_by(Post.status.asc(), desc(Post.message_id), desc(Post.id))
                .offset(offset)
                .limit(limit)
            )
            rows = list((await s.execute(q)).scalars())
            return rows, total

    async def latest_sent(self, channel_id: int, limit: int = 30) -> list:
        async with self.Session() as s:
            q = (
                select(Post)
                .where(Post.channel_id == channel_id, Post.status == "sent", Post.message_id.is_not(None))
                .order_by(desc(Post.message_id))
                .limit(limit)
            )
            return list((await s.execute(q)).scalars())

    async def sent_posts(self, channel_id: int) -> list:
        """Every published post of a channel, oldest first."""
        async with self.Session() as s:
            q = (
                select(Post)
                .where(Post.channel_id == channel_id, Post.status == "sent", Post.message_id.is_not(None))
                .order_by(Post.message_id)
            )
            return list((await s.execute(q)).scalars())

    async def sent_posts_from(self, channel_id: int, first_message_id: int) -> list:
        """Published posts of a channel from message id `first_message_id` on, oldest first."""
        async with self.Session() as s:
            q = (
                select(Post)
                .where(
                    Post.channel_id == channel_id,
                    Post.status == "sent",
                    Post.message_id.is_not(None),
                    Post.message_id >= first_message_id,
                )
                .order_by(Post.message_id, Post.id)
            )
            return list((await s.execute(q)).scalars())

    async def mine_message_ids(self, channel_id: int) -> set:
        async with self.Session() as s:
            q = select(Post.message_id).where(
                Post.channel_id == channel_id, Post.source == "bot", Post.message_id.is_not(None)
            )
            return {r for r in (await s.execute(q)).scalars()}

    async def find_by_message(self, channel_id: int, message_id: int) -> Optional[Post]:
        async with self.Session() as s:
            q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
            return (await s.execute(q)).scalar_one_or_none()

    async def forget_posts(self, channel_id: int, message_ids: list) -> int:
        """Remove the saved posts of these channel messages (they were deleted in the channel) and what hangs on them:
        a timed deletion that is not needed any more, a "forgotten on purpose" mark. Returns how many posts went."""
        if not message_ids:
            return 0
        ids = list(message_ids)
        async with self.Session() as s:
            rows = (await s.execute(select(Post).where(Post.channel_id == channel_id, Post.message_id.in_(ids)))).scalars().all()
            for p in rows:
                await s.delete(p)
            for model in (ScheduledDelete, IgnoredPost):
                extra = await s.execute(select(model).where(model.channel_id == channel_id, model.message_id.in_(ids)))
                for r in extra.scalars().all():
                    await s.delete(r)
            await s.commit()
            return len(rows)

    async def posts_at(self, channel_id: int, message_ids: list) -> list:
        """The published posts that sit at these message ids."""
        if not message_ids:
            return []
        async with self.Session() as s:
            q = select(Post).where(
                Post.channel_id == channel_id, Post.status == "sent", Post.message_id.in_(list(message_ids))
            )
            return list((await s.execute(q)).scalars())

    async def release_slot(self, channel_id: int, message_id: int, keep_pid: Optional[int] = None) -> int:
        """Drop any other saved post at this message id (the sync may have picked the message up a moment earlier)."""
        async with self.Session() as s:
            q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
            if keep_pid is not None:
                q = q.where(Post.id != keep_pid)
            rows = list((await s.execute(q)).scalars())
            for p in rows:
                await s.delete(p)
            if rows:
                await s.commit()
            return len(rows)

    async def _drop_slots(self, s, channel_id: int, message_ids: list) -> None:
        """Inside a transaction: remove saved posts at these ids, so the posts about to be saved can take their place."""
        q = select(Post).where(Post.channel_id == channel_id, Post.message_id.in_(list(message_ids)))
        rows = list((await s.execute(q)).scalars())
        for p in rows:
            await s.delete(p)
        if rows:
            await s.flush()

    # ------------------------------------------------------- what happens outside the bot
    async def get_sync(self, channel_id: int) -> Optional[ChannelSync]:
        async with self.Session() as s:
            return await s.get(ChannelSync, channel_id)

    async def max_saved_id(self, channel_id: int) -> int:
        """The newest message id among the channel's published posts (0 if there is none)."""
        async with self.Session() as s:
            q = select(func.max(Post.message_id)).where(
                Post.channel_id == channel_id, Post.status == "sent", Post.message_id.is_not(None)
            )
            return int((await s.execute(q)).scalar_one_or_none() or 0)

    async def save_sync(
        self, channel_id: int, *, last_top: int, last_pts: int, deferred: int = 0, deep: bool = False
    ) -> None:
        async with self.Session() as s:
            row = await s.get(ChannelSync, channel_id)
            if row is None:
                row = ChannelSync(channel_id=channel_id)
                s.add(row)
            now = utcnow()
            row.last_top, row.last_pts, row.deferred, row.last_run = last_top, last_pts, deferred, now
            if deep:
                row.last_deep = now
            await s.commit()

    async def adopt_message(self, channel_id: int, message_id: int, fields: dict, sent_at: Optional[datetime] = None) -> tuple:
        """Save a post somebody else made in the channel. Returns (post, True), or (None, False) when the bot already
        has a post at that message id (also when another task saved it a moment ago)."""
        async with self.Session() as s:
            q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
            if (await s.execute(q)).scalar_one_or_none() is not None:
                return None, False
            p = Post(channel_id=channel_id, message_id=message_id, status="sent", source="adopted", created_by=0,
                     sent_at=sent_at or utcnow(), **fields)
            s.add(p)
            try:
                await s.commit()
            except IntegrityError:
                await s.rollback()
                return None, False
            return p, True

    async def adopt_many(self, channel_id: int, items: list) -> list:
        """items: [(message id, post fields, sent_at)] - save the posts somebody else made, many at once. Returns the
        message ids that were new (the bot already had a post at the others)."""
        if not items:
            return []
        ids = [i[0] for i in items]
        async with self.Session() as s:
            have = set(
                (await s.execute(select(Post.message_id).where(Post.channel_id == channel_id, Post.message_id.in_(ids)))).scalars()
            )
            fresh = [i for i in items if i[0] not in have]
            if not fresh:
                return []
            s.add_all(
                [
                    Post(channel_id=channel_id, message_id=mid, status="sent", source="adopted", created_by=0,
                         sent_at=at or utcnow(), **fields)
                    for mid, fields, at in fresh
                ]
            )
            try:
                await s.commit()
                return [i[0] for i in fresh]
            except IntegrityError:  # somebody saved one of them a moment ago: one by one then
                await s.rollback()
        made = []
        for mid, fields, at in fresh:
            _, created = await self.adopt_message(channel_id, mid, fields, sent_at=at)
            if created:
                made.append(mid)
        return made

    # ------------------------------------------------------------- the channel's older posts
    async def get_history(self, channel_id: int) -> Optional[ChannelHistory]:
        async with self.Session() as s:
            return await s.get(ChannelHistory, channel_id)

    async def start_history(self, channel_id: int, top: int) -> ChannelHistory:
        """Note that the older posts of a channel (message ids 1 to `top`) are to be read in. Does nothing when that
        has begun already."""
        async with self.Session() as s:
            row = await s.get(ChannelHistory, channel_id)
            if row is not None:
                return row
            row = ChannelHistory(channel_id=channel_id, top=max(0, top), next_id=1, imported=0)
            if top < 1:  # an empty channel: nothing to read
                row.done, row.finished_at = True, utcnow()
            s.add(row)
            try:
                await s.commit()
            except IntegrityError:
                await s.rollback()
                row = await s.get(ChannelHistory, channel_id)
            return row

    async def save_history(self, channel_id: int, *, next_id: int, imported: int = 0, done: bool = False) -> None:
        async with self.Session() as s:
            row = await s.get(ChannelHistory, channel_id)
            if row is None:
                return
            row.next_id = max(row.next_id, next_id)
            row.imported += imported
            if done and not row.done:
                row.done, row.finished_at = True, utcnow()
            await s.commit()

    async def reset_history(self, channel_id: int) -> None:
        """Read the channel's older posts again at the next look (the posts the bot already has are skipped)."""
        async with self.Session() as s:
            row = await s.get(ChannelHistory, channel_id)
            if row is not None:
                await s.delete(row)
                await s.commit()

    # ------------------------------------------------------------ posts forgotten on purpose
    async def ignore_post(self, channel_id: int, message_id: int) -> None:
        async with self.Session() as s:
            q = select(IgnoredPost).where(IgnoredPost.channel_id == channel_id, IgnoredPost.message_id == message_id)
            if (await s.execute(q)).scalar_one_or_none() is not None:
                return
            s.add(IgnoredPost(channel_id=channel_id, message_id=message_id))
            try:
                await s.commit()
            except IntegrityError:
                await s.rollback()

    async def ignored_ids(self, channel_id: int) -> set:
        async with self.Session() as s:
            q = select(IgnoredPost.message_id).where(IgnoredPost.channel_id == channel_id)
            return {int(r) for r in (await s.execute(q)).scalars()}

    # ------------------------------------------------------------ facts about a repost / shift run
    async def set_mark(self, job_id: str, mark: str) -> None:
        async with self.Session() as s:
            if await s.get(JobMark, (job_id, mark)) is not None:
                return
            s.add(JobMark(job_id=job_id, mark=mark))
            try:
                await s.commit()
            except IntegrityError:
                await s.rollback()

    async def has_mark(self, job_id: str, mark: str) -> bool:
        async with self.Session() as s:
            return await s.get(JobMark, (job_id, mark)) is not None

    async def marks_of(self, job_id: str) -> set:
        async with self.Session() as s:
            q = select(JobMark.mark).where(JobMark.job_id == job_id)
            return set((await s.execute(q)).scalars())

    async def sync_update_post(self, pid: int, expected: Optional[datetime], fields: dict) -> bool:
        """Save what the channel shows - but only if the saved post is still exactly as it was when it was read
        (`expected` is its updated_at then), so a change made through the bot meanwhile is never overwritten."""
        async with self.Session() as s:
            res = await s.execute(
                update(Post).where(Post.id == pid, Post.updated_at == expected).values(**fields, updated_at=utcnow())
            )
            await s.commit()
            return bool(res.rowcount)

    # ------------------------------------------------------------ timed deletions
    async def schedule_deletes(self, rows: list) -> None:
        """rows: dicts with channel_id, message_id, delete_at, group_id, created_by."""
        if not rows:
            return
        async with self.Session() as s:
            s.add_all([ScheduledDelete(**r) for r in rows])
            await s.commit()

    async def due_deletes(self, now: datetime, limit: int = 500) -> list:
        async with self.Session() as s:
            q = (
                select(ScheduledDelete)
                .where(ScheduledDelete.status == "pending", ScheduledDelete.delete_at <= now)
                .order_by(ScheduledDelete.delete_at, ScheduledDelete.id)
                .limit(limit)
            )
            return list((await s.execute(q)).scalars())

    async def next_delete_at(self) -> Optional[datetime]:
        async with self.Session() as s:
            q = select(func.min(ScheduledDelete.delete_at)).where(ScheduledDelete.status == "pending")
            return as_utc((await s.execute(q)).scalar_one_or_none())

    async def pending_deletes(self, group_id: Optional[str] = None, status: str = "pending") -> list:
        async with self.Session() as s:
            q = select(ScheduledDelete).where(ScheduledDelete.status == status).order_by(ScheduledDelete.id)
            if group_id:
                q = q.where(ScheduledDelete.group_id == group_id)
            return list((await s.execute(q)).scalars())

    async def finish_deletes(self, ids: list) -> None:
        if not ids:
            return
        async with self.Session() as s:
            for row in (await s.execute(select(ScheduledDelete).where(ScheduledDelete.id.in_(list(ids))))).scalars():
                await s.delete(row)
            await s.commit()

    async def retry_delete(self, sid: int, *, at: datetime, error: str) -> None:
        async with self.Session() as s:
            row = await s.get(ScheduledDelete, sid)
            if row is not None:
                row.attempts += 1
                row.delete_at = at
                row.last_error = error[:160]
                await s.commit()

    async def fail_delete(self, sid: int, error: str) -> None:
        async with self.Session() as s:
            row = await s.get(ScheduledDelete, sid)
            if row is not None:
                row.attempts += 1
                row.status = "failed"
                row.last_error = error[:160]
                await s.commit()

    # ------------------------------------------------------------- replace history
    async def create_batch(self, batch_id: str, old: str, new: str, user_id: int) -> None:
        async with self.Session() as s:
            s.add(Batch(id=batch_id, old_username=old, new_username=new, created_by=user_id))
            await s.commit()

    async def _adopt(self, s, channel_id: int, message_id: int, fields: dict, user_id: int) -> None:
        q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
        p = (await s.execute(q)).scalar_one_or_none()
        if p is not None:
            for k, v in fields.items():
                setattr(p, k, v)
            p.updated_at = utcnow()
        else:
            s.add(
                Post(
                    channel_id=channel_id,
                    message_id=message_id,
                    status="sent",
                    source="adopted",
                    created_by=user_id,
                    sent_at=utcnow(),
                    **fields,
                )
            )

    async def record_changes(self, batch_id: str, channel_id: int, records: list, user_id: int) -> None:
        """records: [{message_id, old_state, new_state, adopt: {post fields}}]"""
        if not records:
            return
        async with self.Session() as s:
            for r in records:
                s.add(
                    Change(
                        batch_id=batch_id,
                        channel_id=channel_id,
                        message_id=r["message_id"],
                        old_state=r["old_state"],
                        new_state=r["new_state"],
                    )
                )
                await self._adopt(s, channel_id, r["message_id"], r["adopt"], user_id)
            await s.commit()

    async def last_batch(self) -> Optional[Batch]:
        async with self.Session() as s:
            q = select(Batch).where(Batch.undone.is_(False)).order_by(desc(Batch.created_at)).limit(1)
            return (await s.execute(q)).scalar_one_or_none()

    async def get_batch(self, batch_id: str) -> Optional[Batch]:
        async with self.Session() as s:
            return await s.get(Batch, batch_id)

    async def batch_changes(self, batch_id: str) -> list:
        async with self.Session() as s:
            q = select(Change).where(Change.batch_id == batch_id, Change.reverted.is_(False)).order_by(Change.id)
            return list((await s.execute(q)).scalars())

    async def count_batch_changes(self, batch_id: str) -> int:
        async with self.Session() as s:
            q = select(func.count()).select_from(Change).where(Change.batch_id == batch_id, Change.reverted.is_(False))
            return (await s.execute(q)).scalar_one()

    async def finish_undo(self, batch_id: str, change_ids: list, restored: list, all_done: bool) -> None:
        """restored: [(channel_id, message_id, state)] -> refresh the saved post copies."""
        async with self.Session() as s:
            for cid in change_ids:
                c = await s.get(Change, cid)
                if c is not None:
                    c.reverted = True
            for channel_id, message_id, state in restored:
                q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
                p = (await s.execute(q)).scalar_one_or_none()
                if p is not None:
                    if not state.get("rich"):  # a rich message's text was never changed: it stays as saved
                        p.text = state.get("text", "")
                        p.entities = state.get("entities", [])
                    p.buttons = state.get("buttons", [])
                    p.updated_at = utcnow()
            if all_done:
                b = await s.get(Batch, batch_id)
                if b is not None:
                    b.undone = True
            await s.commit()


    # ------------------------------------------------------------------ /repost
    OPEN_STATES = ("copying", "stopped", "incomplete", "copied")

    async def create_migration(self, mid: str, channel_id: int, *, old, new, include_typed: bool, include_posts: bool,
                               first_id: int, last_id: int, partial: bool, user_id: int) -> None:
        async with self.Session() as s:
            s.add(Migration(id=mid, channel_id=channel_id, old_username=old, new_username=new,
                            include_typed=include_typed, include_posts=include_posts, first_id=first_id,
                            last_id=last_id, partial=partial, created_by=user_id))
            await s.commit()

    async def get_migration(self, mid: str) -> Optional[Migration]:
        async with self.Session() as s:
            return await s.get(Migration, mid)

    async def open_migration(self, channel_id: Optional[int] = None) -> Optional[Migration]:
        """The newest repost that is not finished yet (copies exist, old posts not deleted / copies not removed)."""
        async with self.Session() as s:
            q = select(Migration).where(Migration.status.in_(self.OPEN_STATES))
            if channel_id is not None:
                q = q.where(Migration.channel_id == channel_id)
            q = q.order_by(desc(Migration.created_at)).limit(1)
            return (await s.execute(q)).scalar_one_or_none()

    async def set_migration_status(self, mid: str, status: str) -> None:
        async with self.Session() as s:
            m = await s.get(Migration, mid)
            if m is not None:
                m.status = status
                await s.commit()

    async def migration_done_old_ids(self, mid: str) -> set:
        async with self.Session() as s:
            q = select(MigrationItem.old_id).where(MigrationItem.migration_id == mid)
            return set((await s.execute(q)).scalars())

    async def record_repost(self, mid: str, channel_id: int, rows: list, user_id: int) -> None:
        """rows: [{old_id, new_id, post: {Post fields}}] - saves the mapping and registers the new posts."""
        if not rows:
            return
        async with self.Session() as s:
            await self._drop_slots(s, channel_id, [r["new_id"] for r in rows])
            for r in rows:
                s.add(MigrationItem(migration_id=mid, old_id=r["old_id"], new_id=r["new_id"]))
                s.add(Post(channel_id=channel_id, message_id=r["new_id"], status="sent", source="bot",
                           created_by=user_id, sent_at=utcnow(), **r["post"]))
            await s.commit()

    async def migration_pairs(
        self, mid: Optional[str] = None, channel_id: Optional[int] = None, alive_only: bool = True
    ) -> list:
        """[(old id, new id)] of the copies of one repost (`mid`) or of every repost of a channel, oldest repost first.
        alive_only: leave out copies that were removed again (undo)."""
        async with self.Session() as s:
            q = select(MigrationItem.old_id, MigrationItem.new_id).join(Migration, Migration.id == MigrationItem.migration_id)
            if mid is not None:
                q = q.where(MigrationItem.migration_id == mid)
            if channel_id is not None:
                q = q.where(Migration.channel_id == channel_id)
            if alive_only:
                q = q.where(MigrationItem.new_deleted.is_(False))
            q = q.order_by(Migration.created_at, MigrationItem.id)
            return [(a, b) for a, b in (await s.execute(q)).all()]

    async def update_post_by_message(self, channel_id: int, message_id: int, **fields: Any) -> Optional[Post]:
        """Change the saved copy of the post that sits at `message_id` (None if the bot does not know that post)."""
        async with self.Session() as s:
            q = select(Post).where(Post.channel_id == channel_id, Post.message_id == message_id)
            p = (await s.execute(q)).scalar_one_or_none()
            if p is None:
                return None
            for k, v in fields.items():
                setattr(p, k, v)
            p.updated_at = utcnow()
            await s.commit()
            return p

    async def migration_pending_ids(self, mid: str, which: str, limit: int = 100) -> list:
        col, flag = (MigrationItem.old_id, MigrationItem.old_deleted) if which == "old" else (MigrationItem.new_id, MigrationItem.new_deleted)
        async with self.Session() as s:
            q = select(col).where(MigrationItem.migration_id == mid, flag.is_(False)).order_by(col).limit(limit)
            return list((await s.execute(q)).scalars())

    async def mark_migration_deleted(self, mid: str, channel_id: int, which: str, ids: list) -> None:
        """The messages are gone from Telegram: flag them and drop their saved post copies."""
        if not ids:
            return
        col, flag = ("old_id", "old_deleted") if which == "old" else ("new_id", "new_deleted")
        async with self.Session() as s:
            q = select(MigrationItem).where(MigrationItem.migration_id == mid, getattr(MigrationItem, col).in_(ids))
            for it in (await s.execute(q)).scalars():
                setattr(it, flag, True)
            pq = select(Post).where(Post.channel_id == channel_id, Post.message_id.in_(ids))
            for p in (await s.execute(pq)).scalars():
                await s.delete(p)
            await s.commit()

    async def migration_counts(self, mid: str) -> dict:
        async with self.Session() as s:
            total = (await s.execute(select(func.count()).select_from(MigrationItem).where(MigrationItem.migration_id == mid))).scalar_one()
            old_left = (await s.execute(select(func.count()).select_from(MigrationItem).where(
                MigrationItem.migration_id == mid, MigrationItem.old_deleted.is_(False)))).scalar_one()
            new_left = (await s.execute(select(func.count()).select_from(MigrationItem).where(
                MigrationItem.migration_id == mid, MigrationItem.new_deleted.is_(False)))).scalar_one()
            lo = (await s.execute(select(func.min(MigrationItem.new_id)).where(MigrationItem.migration_id == mid))).scalar_one()
            hi = (await s.execute(select(func.max(MigrationItem.new_id)).where(MigrationItem.migration_id == mid))).scalar_one()
            return {"copied": total, "old_left": old_left, "new_left": new_left, "first_new": lo, "last_new": hi}

    # ------------------------------------------------------------------- /shift
    SHIFT_OPEN = ("copying", "stopped", "incomplete")

    async def create_shift(self, sid: str, *, src, dst_channel_id: int, first_id: int, last_id: int, user_id: int) -> None:
        """`src`: any object with id, access_hash, title and username (the source channel need not be registered)."""
        async with self.Session() as s:
            s.add(Shift(id=sid, src_channel_id=src.id, src_access_hash=src.access_hash or 0, src_title=src.title or "",
                        src_username=src.username, dst_channel_id=dst_channel_id, first_id=first_id, last_id=last_id,
                        created_by=user_id))
            await s.commit()

    async def get_shift(self, sid: str) -> Optional[Shift]:
        async with self.Session() as s:
            return await s.get(Shift, sid)

    async def open_shift(self, src_id: Optional[int] = None, dst_id: Optional[int] = None) -> Optional[Shift]:
        """The newest shift that is not finished (still copying, stopped, or incomplete)."""
        async with self.Session() as s:
            q = select(Shift).where(Shift.status.in_(self.SHIFT_OPEN))
            if src_id is not None:
                q = q.where(Shift.src_channel_id == src_id)
            if dst_id is not None:
                q = q.where(Shift.dst_channel_id == dst_id)
            return (await s.execute(q.order_by(desc(Shift.created_at)).limit(1))).scalar_one_or_none()

    async def last_finished_shift(self) -> Optional[Shift]:
        """The newest finished shift that still has copies in its destination - so its Undo can be offered again."""
        async with self.Session() as s:
            alive = select(ShiftItem.id).where(ShiftItem.shift_id == Shift.id, ShiftItem.new_deleted.is_(False)).exists()
            q = select(Shift).where(Shift.status == "copied", alive).order_by(desc(Shift.created_at)).limit(1)
            return (await s.execute(q)).scalar_one_or_none()

    async def set_shift_status(self, sid: str, status: str) -> None:
        async with self.Session() as s:
            sh = await s.get(Shift, sid)
            if sh is not None:
                sh.status = status
                await s.commit()

    async def shift_done_ids(self, sid: str) -> set:
        async with self.Session() as s:
            q = select(ShiftItem.old_id).where(ShiftItem.shift_id == sid)
            return set((await s.execute(q)).scalars())

    async def shift_pairs(self, sid: str, alive_only: bool = True) -> list:
        """[(source id, id of the copy)] of one shift, in copy order."""
        async with self.Session() as s:
            q = select(ShiftItem.old_id, ShiftItem.new_id).where(ShiftItem.shift_id == sid)
            if alive_only:
                q = q.where(ShiftItem.new_deleted.is_(False))
            return [(a, b) for a, b in (await s.execute(q.order_by(ShiftItem.id))).all()]

    async def shifted_ids(self, src_id: int, dst_id: int, first: int, last: int) -> set:
        """Source ids in first..last that already have a live copy in `dst_id` from an earlier shift."""
        async with self.Session() as s:
            q = (
                select(ShiftItem.old_id)
                .join(Shift, Shift.id == ShiftItem.shift_id)
                .where(Shift.src_channel_id == src_id, Shift.dst_channel_id == dst_id, ShiftItem.new_deleted.is_(False),
                       ShiftItem.old_id >= first, ShiftItem.old_id <= last)
            )
            return set((await s.execute(q)).scalars())

    async def record_shift(self, sid: str, dst_channel_id: int, rows: list, user_id: int) -> None:
        """rows: [{old_id, new_id, post: {Post fields}}] - saves the mapping and registers the copies in My posts."""
        if not rows:
            return
        async with self.Session() as s:
            await self._drop_slots(s, dst_channel_id, [r["new_id"] for r in rows])
            for r in rows:
                s.add(ShiftItem(shift_id=sid, old_id=r["old_id"], new_id=r["new_id"]))
                s.add(Post(channel_id=dst_channel_id, message_id=r["new_id"], status="sent", source="bot",
                           created_by=user_id, sent_at=utcnow(), **r["post"]))
            await s.commit()

    async def shift_counts(self, sid: str) -> dict:
        async with self.Session() as s:
            base = ShiftItem.shift_id == sid
            total = (await s.execute(select(func.count()).select_from(ShiftItem).where(base))).scalar_one()
            left = (await s.execute(select(func.count()).select_from(ShiftItem).where(base, ShiftItem.new_deleted.is_(False)))).scalar_one()
            lo = (await s.execute(select(func.min(ShiftItem.new_id)).where(base))).scalar_one()
            hi = (await s.execute(select(func.max(ShiftItem.new_id)).where(base))).scalar_one()
            return {"copied": total, "new_left": left, "first_new": lo, "last_new": hi}

    async def shift_pending_new(self, sid: str, limit: int = 100) -> list:
        async with self.Session() as s:
            q = select(ShiftItem.new_id).where(ShiftItem.shift_id == sid, ShiftItem.new_deleted.is_(False)).order_by(ShiftItem.new_id).limit(limit)
            return list((await s.execute(q)).scalars())

    async def mark_shift_deleted(self, sid: str, dst_channel_id: int, ids: list) -> None:
        """The copies are gone from Telegram: flag them and drop their saved posts."""
        if not ids:
            return
        async with self.Session() as s:
            q = select(ShiftItem).where(ShiftItem.shift_id == sid, ShiftItem.new_id.in_(ids))
            for it in (await s.execute(q)).scalars():
                it.new_deleted = True
            pq = select(Post).where(Post.channel_id == dst_channel_id, Post.message_id.in_(ids))
            for p in (await s.execute(pq)).scalars():
                await s.delete(p)
            await s.commit()

    # -------------------------------------------------------------------- export
    async def export_all(self) -> dict:
        def clean(d: dict) -> dict:
            return {k: (v.isoformat() if isinstance(v, datetime) else v) for k, v in d.items()}

        async with self.Session() as s:
            chans = list((await s.execute(select(Channel))).scalars())
            posts = list((await s.execute(select(Post).order_by(Post.id))).scalars())
            marks = (await s.execute(select(IgnoredPost.channel_id, IgnoredPost.message_id).order_by(IgnoredPost.id))).all()
            return {
                "exported_at": utcnow().isoformat(),
                "channels": [
                    clean({c.name: getattr(ch, c.name) for c in ch.__table__.columns}) for ch in chans
                ],
                "posts": [clean(p.as_dict()) for p in posts],
                "ignored": [{"channel_id": int(c), "message_id": int(m)} for c, m in marks],  # posts forgotten on purpose
            }