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
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
|
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import datetime
import uuid
try:
import migrate
except ImportError:
# Apparently sqlalchemy-migrate is not installed, so we assume
# we must not need it
# TODO: Better error handling here, or require sqlalchemy-migrate
print("sqlalchemy-migrate not found... assuming we don't need it")
print("I hope you aren't running the legacy migrations!")
import pytz
import dateutil.tz
from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger,
Integer, Unicode, UnicodeText, DateTime,
ForeignKey, Date)
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import and_
from sqlalchemy.schema import UniqueConstraint
from mediagoblin import oauth
from mediagoblin.tools import crypto
from mediagoblin.db.extratypes import JSONEncoded, MutationDict
from mediagoblin.db.migration_tools import (
RegisterMigration, inspect_table, replace_table_hack, model_iteration_hack)
from mediagoblin.db.models import (
MediaEntry, Collection, Comment, User, Privilege, LocalUser, Location)
MIGRATIONS = {}
@RegisterMigration(1, MIGRATIONS)
def ogg_to_webm_audio(db_conn):
metadata = MetaData(bind=db_conn.bind)
file_keynames = Table('core__file_keynames', metadata, autoload=True,
autoload_with=db_conn.bind)
db_conn.execute(
file_keynames.update().where(file_keynames.c.name == 'ogg').
values(name='webm_audio')
)
db_conn.commit()
@RegisterMigration(2, MIGRATIONS)
def add_wants_notification_column(db_conn):
metadata = MetaData(bind=db_conn.bind)
users = Table('core__users', metadata, autoload=True,
autoload_with=db_conn.bind)
col = Column('wants_comment_notification', Boolean,
default=True, nullable=True)
col.create(users, populate_defaults=True)
db_conn.commit()
@RegisterMigration(3, MIGRATIONS)
def add_transcoding_progress(db_conn):
metadata = MetaData(bind=db_conn.bind)
media_entry = inspect_table(metadata, 'core__media_entries')
col = Column('transcoding_progress', SmallInteger)
col.create(media_entry)
db_conn.commit()
class Collection_v0(declarative_base()):
__tablename__ = "core__collections"
id = Column(Integer, primary_key=True)
title = Column(Unicode, nullable=False)
slug = Column(Unicode)
created = Column(DateTime, nullable=False, default=datetime.datetime.now,
index=True)
description = Column(UnicodeText)
creator = Column(Integer, ForeignKey(User.id), nullable=False)
items = Column(Integer, default=0)
class CollectionItem_v0(declarative_base()):
__tablename__ = "core__collection_items"
id = Column(Integer, primary_key=True)
media_entry = Column(
Integer, ForeignKey(MediaEntry.id), nullable=False, index=True)
collection = Column(Integer, ForeignKey(Collection.id), nullable=False)
note = Column(UnicodeText, nullable=True)
added = Column(DateTime, nullable=False, default=datetime.datetime.now)
position = Column(Integer)
## This should be activated, normally.
## But this would change the way the next migration used to work.
## So it's commented for now.
__table_args__ = (
UniqueConstraint('collection', 'media_entry'),
{})
collectionitem_unique_constraint_done = False
@RegisterMigration(4, MIGRATIONS)
def add_collection_tables(db_conn):
Collection_v0.__table__.create(db_conn.bind)
CollectionItem_v0.__table__.create(db_conn.bind)
global collectionitem_unique_constraint_done
collectionitem_unique_constraint_done = True
db_conn.commit()
@RegisterMigration(5, MIGRATIONS)
def add_mediaentry_collected(db_conn):
metadata = MetaData(bind=db_conn.bind)
media_entry = inspect_table(metadata, 'core__media_entries')
col = Column('collected', Integer, default=0)
col.create(media_entry)
db_conn.commit()
class ProcessingMetaData_v0(declarative_base()):
__tablename__ = 'core__processing_metadata'
id = Column(Integer, primary_key=True)
media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False,
index=True)
callback_url = Column(Unicode)
@RegisterMigration(6, MIGRATIONS)
def create_processing_metadata_table(db):
ProcessingMetaData_v0.__table__.create(db.bind)
db.commit()
# Okay, problem being:
# Migration #4 forgot to add the uniqueconstraint for the
# new tables. While creating the tables from scratch had
# the constraint enabled.
#
# So we have four situations that should end up at the same
# db layout:
#
# 1. Fresh install.
# Well, easy. Just uses the tables in models.py
# 2. Fresh install using a git version just before this migration
# The tables are all there, the unique constraint is also there.
# This migration should do nothing.
# But as we can't detect the uniqueconstraint easily,
# this migration just adds the constraint again.
# And possibly fails very loud. But ignores the failure.
# 3. old install, not using git, just releases.
# This one will get the new tables in #4 (now with constraint!)
# And this migration is just skipped silently.
# 4. old install, always on latest git.
# This one has the tables, but lacks the constraint.
# So this migration adds the constraint.
@RegisterMigration(7, MIGRATIONS)
def fix_CollectionItem_v0_constraint(db_conn):
"""Add the forgotten Constraint on CollectionItem"""
global collectionitem_unique_constraint_done
if collectionitem_unique_constraint_done:
# Reset it. Maybe the whole thing gets run again
# For a different db?
collectionitem_unique_constraint_done = False
return
metadata = MetaData(bind=db_conn.bind)
CollectionItem_table = inspect_table(metadata, 'core__collection_items')
constraint = UniqueConstraint('collection', 'media_entry',
name='core__collection_items_collection_media_entry_key',
table=CollectionItem_table)
try:
constraint.create()
except ProgrammingError:
# User probably has an install that was run since the
# collection tables were added, so we don't need to run this migration.
pass
db_conn.commit()
@RegisterMigration(8, MIGRATIONS)
def add_license_preference(db):
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, 'core__users')
col = Column('license_preference', Unicode)
col.create(user_table)
db.commit()
@RegisterMigration(9, MIGRATIONS)
def mediaentry_new_slug_era(db):
"""
Update for the new era for media type slugs.
Entries without slugs now display differently in the url like:
/u/cwebber/m/id=251/
... because of this, we should back-convert:
- entries without slugs should be converted to use the id, if possible, to
make old urls still work
- slugs with = (or also : which is now also not allowed) to have those
stripped out (small possibility of breakage here sadly)
"""
def slug_and_user_combo_exists(slug, uploader):
return db.execute(
media_table.select(
and_(media_table.c.uploader==uploader,
media_table.c.slug==slug))).first() is not None
def append_garbage_till_unique(row, new_slug):
"""
Attach junk to this row until it's unique, then save it
"""
if slug_and_user_combo_exists(new_slug, row.uploader):
# okay, still no success;
# let's whack junk on there till it's unique.
new_slug += '-' + uuid.uuid4().hex[:4]
# keep going if necessary!
while slug_and_user_combo_exists(new_slug, row.uploader):
new_slug += uuid.uuid4().hex[:4]
db.execute(
media_table.update(). \
where(media_table.c.id==row.id). \
values(slug=new_slug))
metadata = MetaData(bind=db.bind)
media_table = inspect_table(metadata, 'core__media_entries')
for row in db.execute(media_table.select()):
# no slug, try setting to an id
if not row.slug:
append_garbage_till_unique(row, str(row.id))
# has "=" or ":" in it... we're getting rid of those
elif "=" in row.slug or ":" in row.slug:
append_garbage_till_unique(
row, row.slug.replace("=", "-").replace(":", "-"))
db.commit()
@RegisterMigration(10, MIGRATIONS)
def unique_collections_slug(db):
"""Add unique constraint to collection slug"""
metadata = MetaData(bind=db.bind)
collection_table = inspect_table(metadata, "core__collections")
existing_slugs = {}
slugs_to_change = []
for row in db.execute(collection_table.select()):
# if duplicate slug, generate a unique slug
if row.creator in existing_slugs and row.slug in \
existing_slugs[row.creator]:
slugs_to_change.append(row.id)
else:
if not row.creator in existing_slugs:
existing_slugs[row.creator] = [row.slug]
else:
existing_slugs[row.creator].append(row.slug)
for row_id in slugs_to_change:
new_slug = str(uuid.uuid4())
db.execute(collection_table.update().
where(collection_table.c.id == row_id).
values(slug=new_slug))
# sqlite does not like to change the schema when a transaction(update) is
# not yet completed
db.commit()
constraint = UniqueConstraint('creator', 'slug',
name='core__collection_creator_slug_key',
table=collection_table)
constraint.create()
db.commit()
@RegisterMigration(11, MIGRATIONS)
def drop_token_related_User_columns(db):
"""
Drop unneeded columns from the User table after switching to using
itsdangerous tokens for email and forgot password verification.
"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, 'core__users')
verification_key = user_table.columns['verification_key']
fp_verification_key = user_table.columns['fp_verification_key']
fp_token_expire = user_table.columns['fp_token_expire']
verification_key.drop()
fp_verification_key.drop()
fp_token_expire.drop()
db.commit()
class CommentSubscription_v0(declarative_base()):
__tablename__ = 'core__comment_subscriptions'
id = Column(Integer, primary_key=True)
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False)
user_id = Column(Integer, ForeignKey(User.id), nullable=False)
notify = Column(Boolean, nullable=False, default=True)
send_email = Column(Boolean, nullable=False, default=True)
class Notification_v0(declarative_base()):
__tablename__ = 'core__notifications'
id = Column(Integer, primary_key=True)
type = Column(Unicode)
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
user_id = Column(Integer, ForeignKey(User.id), nullable=False,
index=True)
seen = Column(Boolean, default=lambda: False, index=True)
class CommentNotification_v0(Notification_v0):
__tablename__ = 'core__comment_notifications'
id = Column(Integer, ForeignKey(Notification_v0.id), primary_key=True)
subject_id = Column(Integer, ForeignKey(Comment.id))
class ProcessingNotification_v0(Notification_v0):
__tablename__ = 'core__processing_notifications'
id = Column(Integer, ForeignKey(Notification_v0.id), primary_key=True)
subject_id = Column(Integer, ForeignKey(MediaEntry.id))
@RegisterMigration(12, MIGRATIONS)
def add_new_notification_tables(db):
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, 'core__users')
mediaentry_table = inspect_table(metadata, 'core__media_entries')
mediacomment_table = inspect_table(metadata, 'core__media_comments')
CommentSubscription_v0.__table__.create(db.bind)
Notification_v0.__table__.create(db.bind)
CommentNotification_v0.__table__.create(db.bind)
ProcessingNotification_v0.__table__.create(db.bind)
db.commit()
@RegisterMigration(13, MIGRATIONS)
def pw_hash_nullable(db):
"""Make pw_hash column nullable"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
user_table.c.pw_hash.alter(nullable=True)
# sqlite+sqlalchemy seems to drop this constraint during the
# migration, so we add it back here for now a bit manually.
if db.bind.url.drivername == 'sqlite':
constraint = UniqueConstraint('username', table=user_table)
constraint.create()
db.commit()
# oauth1 migrations
class Client_v0(declarative_base()):
"""
Model representing a client - Used for API Auth
"""
__tablename__ = "core__clients"
id = Column(Unicode, nullable=True, primary_key=True)
secret = Column(Unicode, nullable=False)
expirey = Column(DateTime, nullable=True)
application_type = Column(Unicode, nullable=False)
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
# optional stuff
redirect_uri = Column(JSONEncoded, nullable=True)
logo_url = Column(Unicode, nullable=True)
application_name = Column(Unicode, nullable=True)
contacts = Column(JSONEncoded, nullable=True)
def __repr__(self):
if self.application_name:
return f"<Client {self.application_name} - {self.id}>"
else:
return f"<Client {self.id}>"
class RequestToken_v0(declarative_base()):
"""
Model for representing the request tokens
"""
__tablename__ = "core__request_tokens"
token = Column(Unicode, primary_key=True)
secret = Column(Unicode, nullable=False)
client = Column(Unicode, ForeignKey(Client_v0.id))
user = Column(Integer, ForeignKey(User.id), nullable=True)
used = Column(Boolean, default=False)
authenticated = Column(Boolean, default=False)
verifier = Column(Unicode, nullable=True)
callback = Column(Unicode, nullable=False, default="oob")
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
class AccessToken_v0(declarative_base()):
"""
Model for representing the access tokens
"""
__tablename__ = "core__access_tokens"
token = Column(Unicode, nullable=False, primary_key=True)
secret = Column(Unicode, nullable=False)
user = Column(Integer, ForeignKey(User.id))
request_token = Column(Unicode, ForeignKey(RequestToken_v0.token))
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
class NonceTimestamp_v0(declarative_base()):
"""
A place the timestamp and nonce can be stored - this is for OAuth1
"""
__tablename__ = "core__nonce_timestamps"
nonce = Column(Unicode, nullable=False, primary_key=True)
timestamp = Column(DateTime, nullable=False, primary_key=True)
@RegisterMigration(14, MIGRATIONS)
def create_oauth1_tables(db):
""" Creates the OAuth1 tables """
Client_v0.__table__.create(db.bind)
RequestToken_v0.__table__.create(db.bind)
AccessToken_v0.__table__.create(db.bind)
NonceTimestamp_v0.__table__.create(db.bind)
db.commit()
@RegisterMigration(15, MIGRATIONS)
def wants_notifications(db):
"""Add a wants_notifications field to User model"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
col = Column('wants_notifications', Boolean, default=True)
col.create(user_table)
db.commit()
@RegisterMigration(16, MIGRATIONS)
def upload_limits(db):
"""Add user upload limit columns"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, 'core__users')
media_entry_table = inspect_table(metadata, 'core__media_entries')
col = Column('uploaded', Integer, default=0)
col.create(user_table)
col = Column('upload_limit', Integer)
col.create(user_table)
col = Column('file_size', Integer, default=0)
col.create(media_entry_table)
db.commit()
@RegisterMigration(17, MIGRATIONS)
def add_file_metadata(db):
"""Add file_metadata to MediaFile"""
metadata = MetaData(bind=db.bind)
media_file_table = inspect_table(metadata, "core__mediafiles")
col = Column('file_metadata', MutationDict.as_mutable(JSONEncoded))
col.create(media_file_table)
db.commit()
###################
# Moderation tables
###################
class ReportBase_v0(declarative_base()):
__tablename__ = 'core__reports'
id = Column(Integer, primary_key=True)
reporter_id = Column(Integer, ForeignKey(User.id), nullable=False)
report_content = Column(UnicodeText)
reported_user_id = Column(Integer, ForeignKey(User.id), nullable=False)
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
discriminator = Column('type', Unicode(50))
resolver_id = Column(Integer, ForeignKey(User.id))
resolved = Column(DateTime)
result = Column(UnicodeText)
__mapper_args__ = {'polymorphic_on': discriminator}
class CommentReport_v0(ReportBase_v0):
__tablename__ = 'core__reports_on_comments'
__mapper_args__ = {'polymorphic_identity': 'comment_report'}
id = Column('id',Integer, ForeignKey('core__reports.id'),
primary_key=True)
comment_id = Column(Integer, ForeignKey(Comment.id), nullable=True)
class MediaReport_v0(ReportBase_v0):
__tablename__ = 'core__reports_on_media'
__mapper_args__ = {'polymorphic_identity': 'media_report'}
id = Column('id',Integer, ForeignKey('core__reports.id'), primary_key=True)
media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=True)
class UserBan_v0(declarative_base()):
__tablename__ = 'core__user_bans'
user_id = Column(Integer, ForeignKey(User.id), nullable=False,
primary_key=True)
expiration_date = Column(Date)
reason = Column(UnicodeText, nullable=False)
class Privilege_v0(declarative_base()):
__tablename__ = 'core__privileges'
id = Column(Integer, nullable=False, primary_key=True, unique=True)
privilege_name = Column(Unicode, nullable=False, unique=True)
class PrivilegeUserAssociation_v0(declarative_base()):
__tablename__ = 'core__privileges_users'
privilege_id = Column(
'core__privilege_id',
Integer,
ForeignKey(User.id),
primary_key=True)
user_id = Column(
'core__user_id',
Integer,
ForeignKey(Privilege.id),
primary_key=True)
PRIVILEGE_FOUNDATIONS_v0 = [{'privilege_name':'admin'},
{'privilege_name':'moderator'},
{'privilege_name':'uploader'},
{'privilege_name':'reporter'},
{'privilege_name':'commenter'},
{'privilege_name':'active'}]
# vR1 stands for "version Rename 1". This only exists because we need
# to deal with dropping some booleans and it's otherwise impossible
# with sqlite.
class User_vR1(declarative_base()):
__tablename__ = 'rename__users'
id = Column(Integer, primary_key=True)
username = Column(Unicode, nullable=False, unique=True)
email = Column(Unicode, nullable=False)
pw_hash = Column(Unicode)
created = Column(DateTime, nullable=False, default=datetime.datetime.now)
wants_comment_notification = Column(Boolean, default=True)
wants_notifications = Column(Boolean, default=True)
license_preference = Column(Unicode)
url = Column(Unicode)
bio = Column(UnicodeText) # ??
uploaded = Column(Integer, default=0)
upload_limit = Column(Integer)
@RegisterMigration(18, MIGRATIONS)
def create_moderation_tables(db):
# First, we will create the new tables in the database.
#--------------------------------------------------------------------------
ReportBase_v0.__table__.create(db.bind)
CommentReport_v0.__table__.create(db.bind)
MediaReport_v0.__table__.create(db.bind)
UserBan_v0.__table__.create(db.bind)
Privilege_v0.__table__.create(db.bind)
PrivilegeUserAssociation_v0.__table__.create(db.bind)
db.commit()
# Then initialize the tables that we will later use
#--------------------------------------------------------------------------
metadata = MetaData(bind=db.bind)
privileges_table= inspect_table(metadata, "core__privileges")
user_table = inspect_table(metadata, 'core__users')
user_privilege_assoc = inspect_table(
metadata, 'core__privileges_users')
# This section initializes the default Privilege foundations, that
# would be created through the FOUNDATIONS system in a new instance
#--------------------------------------------------------------------------
for parameters in PRIVILEGE_FOUNDATIONS_v0:
db.execute(privileges_table.insert().values(**parameters))
db.commit()
# This next section takes the information from the old is_admin and status
# columns and converts those to the new privilege system
#--------------------------------------------------------------------------
admin_users_ids, active_users_ids, inactive_users_ids = (
db.execute(
user_table.select().where(
user_table.c.is_admin==True)).fetchall(),
db.execute(
user_table.select().where(
user_table.c.is_admin==False).where(
user_table.c.status=="active")).fetchall(),
db.execute(
user_table.select().where(
user_table.c.is_admin==False).where(
user_table.c.status!="active")).fetchall())
# Get the ids for each of the privileges so we can reference them ~~~~~~~~~
(admin_privilege_id, uploader_privilege_id,
reporter_privilege_id, commenter_privilege_id,
active_privilege_id) = [
db.execute(privileges_table.select().where(
privileges_table.c.privilege_name==privilege_name)).first()['id']
for privilege_name in
["admin","uploader","reporter","commenter","active"]
]
# Give each user the appopriate privileges depending whether they are an
# admin, an active user or an inactive user ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for admin_user in admin_users_ids:
admin_user_id = admin_user['id']
for privilege_id in [admin_privilege_id, uploader_privilege_id,
reporter_privilege_id, commenter_privilege_id,
active_privilege_id]:
db.execute(user_privilege_assoc.insert().values(
core__privilege_id=admin_user_id,
core__user_id=privilege_id))
for active_user in active_users_ids:
active_user_id = active_user['id']
for privilege_id in [uploader_privilege_id, reporter_privilege_id,
commenter_privilege_id, active_privilege_id]:
db.execute(user_privilege_assoc.insert().values(
core__privilege_id=active_user_id,
core__user_id=privilege_id))
for inactive_user in inactive_users_ids:
inactive_user_id = inactive_user['id']
for privilege_id in [uploader_privilege_id, reporter_privilege_id,
commenter_privilege_id]:
db.execute(user_privilege_assoc.insert().values(
core__privilege_id=inactive_user_id,
core__user_id=privilege_id))
db.commit()
# And then, once the information is taken from is_admin & status columns
# we drop all of the vestigial columns from the User table.
#--------------------------------------------------------------------------
if db.bind.url.drivername == 'sqlite':
# SQLite has some issues that make it *impossible* to drop boolean
# columns. So, the following code is a very hacky workaround which
# makes it possible. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
User_vR1.__table__.create(db.bind)
db.commit()
new_user_table = inspect_table(metadata, 'rename__users')
replace_table_hack(db, user_table, new_user_table)
else:
# If the db is not run using SQLite, this process is much simpler ~~~~~
status = user_table.columns['status']
email_verified = user_table.columns['email_verified']
is_admin = user_table.columns['is_admin']
status.drop()
email_verified.drop()
is_admin.drop()
db.commit()
@RegisterMigration(19, MIGRATIONS)
def drop_MediaEntry_collected(db):
"""
Drop unused MediaEntry.collected column
"""
metadata = MetaData(bind=db.bind)
media_collected= inspect_table(metadata, 'core__media_entries')
media_collected = media_collected.columns['collected']
media_collected.drop()
db.commit()
@RegisterMigration(20, MIGRATIONS)
def add_metadata_column(db):
metadata = MetaData(bind=db.bind)
media_entry = inspect_table(metadata, 'core__media_entries')
col = Column('media_metadata', MutationDict.as_mutable(JSONEncoded),
default=MutationDict())
col.create(media_entry)
db.commit()
class PrivilegeUserAssociation_R1(declarative_base()):
__tablename__ = 'rename__privileges_users'
user = Column(
"user",
Integer,
ForeignKey(User.id),
primary_key=True)
privilege = Column(
"privilege",
Integer,
ForeignKey(Privilege.id),
primary_key=True)
@RegisterMigration(21, MIGRATIONS)
def fix_privilege_user_association_table(db):
"""
There was an error in the PrivilegeUserAssociation table that allowed for a
dangerous sql error. We need to the change the name of the columns to be
unique, and properly referenced.
"""
metadata = MetaData(bind=db.bind)
privilege_user_assoc = inspect_table(
metadata, 'core__privileges_users')
# This whole process is more complex if we're dealing with sqlite
if db.bind.url.drivername == 'sqlite':
PrivilegeUserAssociation_R1.__table__.create(db.bind)
db.commit()
new_privilege_user_assoc = inspect_table(
metadata, 'rename__privileges_users')
result = db.execute(privilege_user_assoc.select())
for row in result:
# The columns were improperly named before, so we switch the columns
user_id, priv_id = row['core__privilege_id'], row['core__user_id']
db.execute(new_privilege_user_assoc.insert().values(
user=user_id,
privilege=priv_id))
db.commit()
privilege_user_assoc.drop()
new_privilege_user_assoc.rename('core__privileges_users')
# much simpler if postgres though!
else:
privilege_user_assoc.c.core__user_id.alter(name="privilege")
privilege_user_assoc.c.core__privilege_id.alter(name="user")
db.commit()
@RegisterMigration(22, MIGRATIONS)
def add_index_username_field(db):
"""
This migration has been found to be doing the wrong thing. See
the documentation in migration 23 (revert_username_index) below
which undoes this for those databases that did run this migration.
Old description:
This indexes the User.username field which is frequently queried
for example a user logging in. This solves the issue #894
"""
## This code is left commented out *on purpose!*
##
## We do not normally allow commented out code like this in
## MediaGoblin but this is a special case: since this migration has
## been nullified but with great work to set things back below,
## this is commented out for historical clarity.
#
# metadata = MetaData(bind=db.bind)
# user_table = inspect_table(metadata, "core__users")
#
# new_index = Index("ix_core__users_uploader", user_table.c.username)
# new_index.create()
#
# db.commit()
pass
@RegisterMigration(23, MIGRATIONS)
def revert_username_index(db):
"""
Revert the stuff we did in migration 22 above.
There were a couple of problems with what we did:
- There was never a need for this migration! The unique
constraint had an implicit b-tree index, so it wasn't really
needed. (This is my (Chris Webber's) fault for suggesting it
needed to happen without knowing what's going on... my bad!)
- On top of that, databases created after the models.py was
changed weren't the same as those that had been run through
migration 22 above.
As such, we're setting things back to the way they were before,
but as it turns out, that's tricky to do!
"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
indexes = {
index.name: index for index in user_table.indexes}
# index from unnecessary migration
users_uploader_index = indexes.get('ix_core__users_uploader')
# index created from models.py after (unique=True, index=True)
# was set in models.py
users_username_index = indexes.get('ix_core__users_username')
if users_uploader_index is None and users_username_index is None:
# We don't need to do anything.
# The database isn't in a state where it needs fixing
#
# (ie, either went through the previous borked migration or
# was initialized with a models.py where core__users was both
# unique=True and index=True)
return
if db.bind.url.drivername == 'sqlite':
# Again, sqlite has problems. So this is tricky.
# Yes, this is correct to use User_vR1! Nothing has changed
# between the *correct* version of this table and migration 18.
User_vR1.__table__.create(db.bind)
db.commit()
new_user_table = inspect_table(metadata, 'rename__users')
replace_table_hack(db, user_table, new_user_table)
else:
# If the db is not run using SQLite, we don't need to do crazy
# table copying.
# Remove whichever of the not-used indexes are in place
if users_uploader_index is not None:
users_uploader_index.drop()
if users_username_index is not None:
users_username_index.drop()
# Given we're removing indexes then adding a unique constraint
# which *we know might fail*, thus probably rolling back the
# session, let's commit here.
db.commit()
try:
# Add the unique constraint
constraint = UniqueConstraint(
'username', table=user_table)
constraint.create()
except ProgrammingError:
# constraint already exists, no need to add
db.rollback()
db.commit()
class Generator_R0(declarative_base()):
__tablename__ = "core__generators"
id = Column(Integer, primary_key=True)
name = Column(Unicode, nullable=False)
published = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
object_type = Column(Unicode, nullable=False)
class ActivityIntermediator_R0(declarative_base()):
__tablename__ = "core__activity_intermediators"
id = Column(Integer, primary_key=True)
type = Column(Unicode, nullable=False)
# These are needed for migration 29
TABLENAMES = {
"user": "core__users",
"media": "core__media_entries",
"comment": "core__media_comments",
"collection": "core__collections",
}
class Activity_R0(declarative_base()):
__tablename__ = "core__activities"
id = Column(Integer, primary_key=True)
actor = Column(Integer, ForeignKey(User.id), nullable=False)
published = Column(DateTime, nullable=False, default=datetime.datetime.now)
updated = Column(DateTime, nullable=False, default=datetime.datetime.now)
verb = Column(Unicode, nullable=False)
content = Column(Unicode, nullable=True)
title = Column(Unicode, nullable=True)
generator = Column(Integer, ForeignKey(Generator_R0.id), nullable=True)
object = Column(Integer,
ForeignKey(ActivityIntermediator_R0.id),
nullable=False)
target = Column(Integer,
ForeignKey(ActivityIntermediator_R0.id),
nullable=True)
@RegisterMigration(24, MIGRATIONS)
def activity_migration(db):
"""
Creates everything to create activities in GMG
- Adds Activity, ActivityIntermediator and Generator table
- Creates GMG service generator for activities produced by the server
- Adds the activity_as_object and activity_as_target to objects/targets
- Retroactively adds activities for what we can acurately work out
"""
# Set constants we'll use later
FOREIGN_KEY = "core__activity_intermediators.id"
ACTIVITY_COLUMN = "activity"
# Create the new tables.
ActivityIntermediator_R0.__table__.create(db.bind)
Generator_R0.__table__.create(db.bind)
Activity_R0.__table__.create(db.bind)
db.commit()
# Initiate the tables we want to use later
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
activity_table = inspect_table(metadata, "core__activities")
generator_table = inspect_table(metadata, "core__generators")
collection_table = inspect_table(metadata, "core__collections")
media_entry_table = inspect_table(metadata, "core__media_entries")
media_comments_table = inspect_table(metadata, "core__media_comments")
ai_table = inspect_table(metadata, "core__activity_intermediators")
# Create the foundations for Generator
db.execute(generator_table.insert().values(
name="GNU Mediagoblin",
object_type="service",
published=datetime.datetime.now(),
updated=datetime.datetime.now()
))
db.commit()
# Get the ID of that generator
gmg_generator = db.execute(generator_table.select(
generator_table.c.name=="GNU Mediagoblin")).first()
# Now we want to modify the tables which MAY have an activity at some point
media_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
media_col.create(media_entry_table)
user_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
user_col.create(user_table)
comments_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
comments_col.create(media_comments_table)
collection_col = Column(ACTIVITY_COLUMN, Integer, ForeignKey(FOREIGN_KEY))
collection_col.create(collection_table)
db.commit()
# Now we want to retroactively add what activities we can
# first we'll add activities when people uploaded media.
# these can't have content as it's not fesible to get the
# correct content strings.
for media in db.execute(media_entry_table.select()):
# Now we want to create the intermedaitory
db_ai = db.execute(ai_table.insert().values(
type="media",
))
db_ai = db.execute(ai_table.select(
ai_table.c.id==db_ai.inserted_primary_key[0]
)).first()
# Add the activity
activity = {
"verb": "create",
"actor": media.uploader,
"published": media.created,
"updated": media.created,
"generator": gmg_generator.id,
"object": db_ai.id
}
db.execute(activity_table.insert().values(**activity))
# Add the AI to the media.
db.execute(media_entry_table.update().values(
activity=db_ai.id
).where(media_entry_table.c.id==media.id))
# Now we want to add all the comments people made
for comment in db.execute(media_comments_table.select()):
# Get the MediaEntry for the comment
media_entry = db.execute(
media_entry_table.select(
media_entry_table.c.id==comment.media_entry
)).first()
# Create an AI for target
db_ai_media = db.execute(ai_table.select(
ai_table.c.id==media_entry.activity
)).first().id
db.execute(
media_comments_table.update().values(
activity=db_ai_media
).where(media_comments_table.c.id==media_entry.id))
# Now create the AI for the comment
db_ai_comment = db.execute(ai_table.insert().values(
type="comment"
)).inserted_primary_key[0]
activity = {
"verb": "comment",
"actor": comment.author,
"published": comment.created,
"updated": comment.created,
"generator": gmg_generator.id,
"object": db_ai_comment,
"target": db_ai_media,
}
# Now add the comment object
db.execute(activity_table.insert().values(**activity))
# Now add activity to comment
db.execute(media_comments_table.update().values(
activity=db_ai_comment
).where(media_comments_table.c.id==comment.id))
# Create 'create' activities for all collections
for collection in db.execute(collection_table.select()):
# create AI
db_ai = db.execute(ai_table.insert().values(
type="collection"
))
db_ai = db.execute(ai_table.select(
ai_table.c.id==db_ai.inserted_primary_key[0]
)).first()
# Now add link the collection to the AI
db.execute(collection_table.update().values(
activity=db_ai.id
).where(collection_table.c.id==collection.id))
activity = {
"verb": "create",
"actor": collection.creator,
"published": collection.created,
"updated": collection.created,
"generator": gmg_generator.id,
"object": db_ai.id,
}
db.execute(activity_table.insert().values(**activity))
# Now add the activity to the collection
db.execute(collection_table.update().values(
activity=db_ai.id
).where(collection_table.c.id==collection.id))
db.commit()
class Location_V0(declarative_base()):
__tablename__ = "core__locations"
id = Column(Integer, primary_key=True)
name = Column(Unicode)
position = Column(MutationDict.as_mutable(JSONEncoded))
address = Column(MutationDict.as_mutable(JSONEncoded))
@RegisterMigration(25, MIGRATIONS)
def add_location_model(db):
""" Add location model """
metadata = MetaData(bind=db.bind)
# Create location table
Location_V0.__table__.create(db.bind)
db.commit()
# Inspect the tables we need
user = inspect_table(metadata, "core__users")
collections = inspect_table(metadata, "core__collections")
media_entry = inspect_table(metadata, "core__media_entries")
media_comments = inspect_table(metadata, "core__media_comments")
# Now add location support to the various models
col = Column("location", Integer, ForeignKey(Location_V0.id))
col.create(user)
col = Column("location", Integer, ForeignKey(Location_V0.id))
col.create(collections)
col = Column("location", Integer, ForeignKey(Location_V0.id))
col.create(media_entry)
col = Column("location", Integer, ForeignKey(Location_V0.id))
col.create(media_comments)
db.commit()
@RegisterMigration(26, MIGRATIONS)
def datetime_to_utc(db):
""" Convert datetime stamps to UTC """
# Get the server's timezone, this is what the database has stored
server_timezone = dateutil.tz.tzlocal()
##
# Look up all the timestamps and convert them to UTC
##
metadata = MetaData(bind=db.bind)
def dt_to_utc(dt):
# Add the current timezone
dt = dt.replace(tzinfo=server_timezone)
# Convert to UTC
return dt.astimezone(pytz.UTC)
# Convert the User model
user_table = inspect_table(metadata, "core__users")
for user in db.execute(user_table.select()):
db.execute(user_table.update().values(
created=dt_to_utc(user.created)
).where(user_table.c.id==user.id))
# Convert Client
client_table = inspect_table(metadata, "core__clients")
for client in db.execute(client_table.select()):
db.execute(client_table.update().values(
created=dt_to_utc(client.created),
updated=dt_to_utc(client.updated)
).where(client_table.c.id==client.id))
# Convert RequestToken
rt_table = inspect_table(metadata, "core__request_tokens")
for request_token in db.execute(rt_table.select()):
db.execute(rt_table.update().values(
created=dt_to_utc(request_token.created),
updated=dt_to_utc(request_token.updated)
).where(rt_table.c.token==request_token.token))
# Convert AccessToken
at_table = inspect_table(metadata, "core__access_tokens")
for access_token in db.execute(at_table.select()):
db.execute(at_table.update().values(
created=dt_to_utc(access_token.created),
updated=dt_to_utc(access_token.updated)
).where(at_table.c.token==access_token.token))
# Convert MediaEntry
media_table = inspect_table(metadata, "core__media_entries")
for media in db.execute(media_table.select()):
db.execute(media_table.update().values(
created=dt_to_utc(media.created)
).where(media_table.c.id==media.id))
# Convert Media Attachment File
media_attachment_table = inspect_table(metadata, "core__attachment_files")
for ma in db.execute(media_attachment_table.select()):
db.execute(media_attachment_table.update().values(
created=dt_to_utc(ma.created)
).where(media_attachment_table.c.id==ma.id))
# Convert MediaComment
comment_table = inspect_table(metadata, "core__media_comments")
for comment in db.execute(comment_table.select()):
db.execute(comment_table.update().values(
created=dt_to_utc(comment.created)
).where(comment_table.c.id==comment.id))
# Convert Collection
collection_table = inspect_table(metadata, "core__collections")
for collection in db.execute(collection_table.select()):
db.execute(collection_table.update().values(
created=dt_to_utc(collection.created)
).where(collection_table.c.id==collection.id))
# Convert Collection Item
collection_item_table = inspect_table(metadata, "core__collection_items")
for ci in db.execute(collection_item_table.select()):
db.execute(collection_item_table.update().values(
added=dt_to_utc(ci.added)
).where(collection_item_table.c.id==ci.id))
# Convert Comment subscription
comment_sub = inspect_table(metadata, "core__comment_subscriptions")
for sub in db.execute(comment_sub.select()):
db.execute(comment_sub.update().values(
created=dt_to_utc(sub.created)
).where(comment_sub.c.id==sub.id))
# Convert Notification
notification_table = inspect_table(metadata, "core__notifications")
for notification in db.execute(notification_table.select()):
db.execute(notification_table.update().values(
created=dt_to_utc(notification.created)
).where(notification_table.c.id==notification.id))
# Convert ReportBase
reportbase_table = inspect_table(metadata, "core__reports")
for report in db.execute(reportbase_table.select()):
db.execute(reportbase_table.update().values(
created=dt_to_utc(report.created)
).where(reportbase_table.c.id==report.id))
# Convert Generator
generator_table = inspect_table(metadata, "core__generators")
for generator in db.execute(generator_table.select()):
db.execute(generator_table.update().values(
published=dt_to_utc(generator.published),
updated=dt_to_utc(generator.updated)
).where(generator_table.c.id==generator.id))
# Convert Activity
activity_table = inspect_table(metadata, "core__activities")
for activity in db.execute(activity_table.select()):
db.execute(activity_table.update().values(
published=dt_to_utc(activity.published),
updated=dt_to_utc(activity.updated)
).where(activity_table.c.id==activity.id))
# Commit this to the database
db.commit()
##
# Migrations to handle migrating from activity specific foreign key to the
# new GenericForeignKey implementations. They have been split up to improve
# readability and minimise errors
##
class GenericModelReference_V0(declarative_base()):
__tablename__ = "core__generic_model_reference"
id = Column(Integer, primary_key=True)
obj_pk = Column(Integer, nullable=False)
model_type = Column(Unicode, nullable=False)
@RegisterMigration(27, MIGRATIONS)
def create_generic_model_reference(db):
""" Creates the Generic Model Reference table """
GenericModelReference_V0.__table__.create(db.bind)
db.commit()
@RegisterMigration(28, MIGRATIONS)
def add_foreign_key_fields(db):
"""
Add the fields for GenericForeignKey to the model under temporary name,
this is so that later a data migration can occur. They will be renamed to
the origional names.
"""
metadata = MetaData(bind=db.bind)
activity_table = inspect_table(metadata, "core__activities")
# Create column and add to model.
object_column = Column("temp_object", Integer, ForeignKey(GenericModelReference_V0.id))
object_column.create(activity_table)
target_column = Column("temp_target", Integer, ForeignKey(GenericModelReference_V0.id))
target_column.create(activity_table)
# Commit this to the database
db.commit()
@RegisterMigration(29, MIGRATIONS)
def migrate_data_foreign_keys(db):
"""
This will migrate the data from the old object and target attributes which
use the old ActivityIntermediator to the new temparay fields which use the
new GenericForeignKey.
"""
metadata = MetaData(bind=db.bind)
activity_table = inspect_table(metadata, "core__activities")
ai_table = inspect_table(metadata, "core__activity_intermediators")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
# Iterate through all activities doing the migration per activity.
for activity in model_iteration_hack(db, activity_table.select()):
# First do the "Activity.object" migration to "Activity.temp_object"
# I need to get the object from the Activity, I can't use the old
# Activity.get_object as we're in a migration.
object_ai = db.execute(ai_table.select(
ai_table.c.id==activity.object
)).first()
object_ai_type = ActivityIntermediator_R0.TABLENAMES[object_ai.type]
object_ai_table = inspect_table(metadata, object_ai_type)
activity_object = db.execute(object_ai_table.select(
object_ai_table.c.activity==object_ai.id
)).first()
# If the object the activity is referecing doesn't revolve, then we
# should skip it, it should be deleted when the AI table is deleted.
if activity_object is None:
continue
# now we need to create the GenericModelReference
object_gmr = db.execute(gmr_table.insert().values(
obj_pk=activity_object.id,
model_type=object_ai_type
))
# Now set the ID of the GenericModelReference in the GenericForignKey
db.execute(activity_table.update().values(
temp_object=object_gmr.inserted_primary_key[0]
))
# Now do same process for "Activity.target" to "Activity.temp_target"
# not all Activities have a target so if it doesn't just skip the rest
# of this.
if activity.target is None:
continue
# Now get the target for the activity.
target_ai = db.execute(ai_table.select(
ai_table.c.id==activity.target
)).first()
target_ai_type = ActivityIntermediator_R0.TABLENAMES[target_ai.type]
target_ai_table = inspect_table(metadata, target_ai_type)
activity_target = db.execute(target_ai_table.select(
target_ai_table.c.activity==target_ai.id
)).first()
# It's quite possible that the target, alike the object could also have
# been deleted, if so we should just skip it
if activity_target is None:
continue
# We now want to create the new target GenericModelReference
target_gmr = db.execute(gmr_table.insert().values(
obj_pk=activity_target.id,
model_type=target_ai_type
))
# Now set the ID of the GenericModelReference in the GenericForignKey
db.execute(activity_table.update().values(
temp_object=target_gmr.inserted_primary_key[0]
))
# Commit to the database. We're doing it here rather than outside the
# loop because if the server has a lot of data this can cause problems
db.commit()
@RegisterMigration(30, MIGRATIONS)
def rename_and_remove_object_and_target(db):
"""
Renames the new Activity.object and Activity.target fields and removes the
old ones.
"""
metadata = MetaData(bind=db.bind)
activity_table = inspect_table(metadata, "core__activities")
# Firstly lets remove the old fields.
old_object_column = activity_table.columns["object"]
old_target_column = activity_table.columns["target"]
# Drop the tables.
old_object_column.drop()
old_target_column.drop()
# Now get the new columns.
new_object_column = activity_table.columns["temp_object"]
new_target_column = activity_table.columns["temp_target"]
# rename them to the old names.
new_object_column.alter(name="object_id")
new_target_column.alter(name="target_id")
# Commit the changes to the database.
db.commit()
@RegisterMigration(31, MIGRATIONS)
def remove_activityintermediator(db):
"""
This removes the old specific ActivityIntermediator model which has been
superseeded by the GenericForeignKey field.
"""
metadata = MetaData(bind=db.bind)
# Remove the columns which reference the AI
collection_table = inspect_table(metadata, "core__collections")
collection_ai_column = collection_table.columns["activity"]
collection_ai_column.drop()
media_entry_table = inspect_table(metadata, "core__media_entries")
media_entry_ai_column = media_entry_table.columns["activity"]
media_entry_ai_column.drop()
comments_table = inspect_table(metadata, "core__media_comments")
comments_ai_column = comments_table.columns["activity"]
comments_ai_column.drop()
user_table = inspect_table(metadata, "core__users")
user_ai_column = user_table.columns["activity"]
user_ai_column.drop()
# Drop the table
ai_table = inspect_table(metadata, "core__activity_intermediators")
ai_table.drop()
# Commit the changes
db.commit()
##
# Migrations for converting the User model into a Local and Remote User
# setup.
##
class LocalUser_V0(declarative_base()):
__tablename__ = "core__local_users"
id = Column(Integer, ForeignKey(User.id), primary_key=True)
username = Column(Unicode, nullable=False, unique=True)
email = Column(Unicode, nullable=False)
pw_hash = Column(Unicode)
wants_comment_notification = Column(Boolean, default=True)
wants_notifications = Column(Boolean, default=True)
license_preference = Column(Unicode)
uploaded = Column(Integer, default=0)
upload_limit = Column(Integer)
class RemoteUser_V0(declarative_base()):
__tablename__ = "core__remote_users"
id = Column(Integer, ForeignKey(User.id), primary_key=True)
webfinger = Column(Unicode, unique=True)
@RegisterMigration(32, MIGRATIONS)
def federation_user_create_tables(db):
"""
Create all the tables
"""
# Create tables needed
LocalUser_V0.__table__.create(db.bind)
RemoteUser_V0.__table__.create(db.bind)
db.commit()
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
# Create the fields
updated_column = Column(
"updated",
DateTime,
default=datetime.datetime.utcnow
)
updated_column.create(user_table)
type_column = Column(
"type",
Unicode
)
type_column.create(user_table)
name_column = Column(
"name",
Unicode
)
name_column.create(user_table)
db.commit()
@RegisterMigration(33, MIGRATIONS)
def federation_user_migrate_data(db):
"""
Migrate the data over to the new user models
"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
local_user_table = inspect_table(metadata, "core__local_users")
for user in model_iteration_hack(db, user_table.select()):
db.execute(local_user_table.insert().values(
id=user.id,
username=user.username,
email=user.email,
pw_hash=user.pw_hash,
wants_comment_notification=user.wants_comment_notification,
wants_notifications=user.wants_notifications,
license_preference=user.license_preference,
uploaded=user.uploaded,
upload_limit=user.upload_limit
))
db.execute(user_table.update().where(user_table.c.id==user.id).values(
updated=user.created,
type=LocalUser.__mapper_args__["polymorphic_identity"]
))
db.commit()
class User_vR2(declarative_base()):
__tablename__ = "rename__users"
id = Column(Integer, primary_key=True)
url = Column(Unicode)
bio = Column(UnicodeText)
name = Column(Unicode)
type = Column(Unicode)
created = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
updated = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
location = Column(Integer, ForeignKey(Location.id))
@RegisterMigration(34, MIGRATIONS)
def federation_remove_fields(db):
"""
This removes the fields from User model which aren't shared
"""
metadata = MetaData(bind=db.bind)
user_table = inspect_table(metadata, "core__users")
# Remove the columns moved to LocalUser from User
username_column = user_table.columns["username"]
username_column.drop()
email_column = user_table.columns["email"]
email_column.drop()
pw_hash_column = user_table.columns["pw_hash"]
pw_hash_column.drop()
license_preference_column = user_table.columns["license_preference"]
license_preference_column.drop()
uploaded_column = user_table.columns["uploaded"]
uploaded_column.drop()
upload_limit_column = user_table.columns["upload_limit"]
upload_limit_column.drop()
# SQLLite can't drop booleans -.-
if db.bind.url.drivername == 'sqlite':
# Create the new hacky table
User_vR2.__table__.create(db.bind)
db.commit()
new_user_table = inspect_table(metadata, "rename__users")
replace_table_hack(db, user_table, new_user_table)
else:
wcn_column = user_table.columns["wants_comment_notification"]
wcn_column.drop()
wants_notifications_column = user_table.columns["wants_notifications"]
wants_notifications_column.drop()
db.commit()
@RegisterMigration(35, MIGRATIONS)
def federation_media_entry(db):
metadata = MetaData(bind=db.bind)
media_entry_table = inspect_table(metadata, "core__media_entries")
# Add new fields
public_id_column = Column(
"public_id",
Unicode,
unique=True,
nullable=True
)
public_id_column.create(
media_entry_table,
unique_name="media_public_id"
)
remote_column = Column(
"remote",
Boolean,
default=False
)
remote_column.create(media_entry_table)
updated_column = Column(
"updated",
DateTime,
default=datetime.datetime.utcnow,
)
updated_column.create(media_entry_table)
db.commit()
# Data migration
for entry in model_iteration_hack(db, media_entry_table.select()):
db.execute(media_entry_table.update().values(
updated=entry.created,
remote=False
))
db.commit()
@RegisterMigration(36, MIGRATIONS)
def create_oauth1_dummies(db):
"""
Creates a dummy client, request and access tokens.
This is used when invalid data is submitted but real clients and
access tokens. The use of dummy objects prevents timing attacks.
"""
metadata = MetaData(bind=db.bind)
client_table = inspect_table(metadata, "core__clients")
request_token_table = inspect_table(metadata, "core__request_tokens")
access_token_table = inspect_table(metadata, "core__access_tokens")
# Whilst we don't rely on the secret key being unique or unknown to prevent
# unauthorized clients from using it to authenticate, we still as an extra
# layer of protection created a cryptographically secure key individual to
# each instance that should never be able to be known.
client_secret = crypto.random_string(50)
request_token_secret = crypto.random_string(50)
request_token_verifier = crypto.random_string(50)
access_token_secret = crypto.random_string(50)
# Dummy created/updated datetime object
epoc_datetime = datetime.datetime.fromtimestamp(0)
# Create the dummy Client
db.execute(client_table.insert().values(
id=oauth.DUMMY_CLIENT_ID,
secret=client_secret,
application_type="dummy",
created=epoc_datetime,
updated=epoc_datetime
))
# Create the dummy RequestToken
db.execute(request_token_table.insert().values(
token=oauth.DUMMY_REQUEST_TOKEN,
secret=request_token_secret,
client=oauth.DUMMY_CLIENT_ID,
verifier=request_token_verifier,
created=epoc_datetime,
updated=epoc_datetime,
callback="oob"
))
# Create the dummy AccessToken
db.execute(access_token_table.insert().values(
token=oauth.DUMMY_ACCESS_TOKEN,
secret=access_token_secret,
request_token=oauth.DUMMY_REQUEST_TOKEN,
created=epoc_datetime,
updated=epoc_datetime
))
# Commit the changes
db.commit()
@RegisterMigration(37, MIGRATIONS)
def federation_collection_schema(db):
""" Converts the Collection and CollectionItem """
metadata = MetaData(bind=db.bind)
collection_table = inspect_table(metadata, "core__collections")
collection_items_table = inspect_table(metadata, "core__collection_items")
media_entry_table = inspect_table(metadata, "core__media_entries")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
##
# Collection Table
##
# Add the fields onto the Collection model, we need to set these as
# not null to avoid DB integreity errors. We will add the not null
# constraint later.
public_id_column = Column(
"public_id",
Unicode,
unique=True
)
public_id_column.create(
collection_table,
unique_name="collection_public_id")
updated_column = Column(
"updated",
DateTime,
default=datetime.datetime.utcnow
)
updated_column.create(collection_table)
type_column = Column(
"type",
Unicode,
)
type_column.create(collection_table)
db.commit()
# Iterate over the items and set the updated and type fields
for collection in db.execute(collection_table.select()):
db.execute(collection_table.update().where(
collection_table.c.id==collection.id
).values(
updated=collection.created,
type="core-user-defined"
))
db.commit()
# Add the not null constraint onto the fields
updated_column = collection_table.columns["updated"]
updated_column.alter(nullable=False)
type_column = collection_table.columns["type"]
type_column.alter(nullable=False)
db.commit()
# Rename the "items" to "num_items" as per the TODO
num_items_field = collection_table.columns["items"]
num_items_field.alter(name="num_items")
db.commit()
##
# CollectionItem
##
# Adding the object ID column, this again will have not null added later.
object_id = Column(
"object_id",
Integer,
ForeignKey(GenericModelReference_V0.id),
)
object_id.create(
collection_items_table,
)
db.commit()
# Iterate through and convert the Media reference to object_id
for item in db.execute(collection_items_table.select()):
# Check if there is a GMR for the MediaEntry
object_gmr = db.execute(gmr_table.select(
and_(
gmr_table.c.obj_pk == item.media_entry,
gmr_table.c.model_type == "core__media_entries"
)
)).first()
if object_gmr:
object_gmr = object_gmr[0]
else:
# Create a GenericModelReference
object_gmr = db.execute(gmr_table.insert().values(
obj_pk=item.media_entry,
model_type="core__media_entries"
)).inserted_primary_key[0]
# Now set the object_id column to the ID of the GMR
db.execute(collection_items_table.update().where(
collection_items_table.c.id==item.id
).values(
object_id=object_gmr
))
db.commit()
# Add not null constraint
object_id = collection_items_table.columns["object_id"]
object_id.alter(nullable=False)
db.commit()
# Now remove the old media_entry column
media_entry_column = collection_items_table.columns["media_entry"]
media_entry_column.drop()
db.commit()
@RegisterMigration(38, MIGRATIONS)
def federation_actor(db):
""" Renames refereces to the user to actor """
metadata = MetaData(bind=db.bind)
# RequestToken: user -> actor
request_token_table = inspect_table(metadata, "core__request_tokens")
rt_user_column = request_token_table.columns["user"]
rt_user_column.alter(name="actor")
# AccessToken: user -> actor
access_token_table = inspect_table(metadata, "core__access_tokens")
at_user_column = access_token_table.columns["user"]
at_user_column.alter(name="actor")
# MediaEntry: uploader -> actor
media_entry_table = inspect_table(metadata, "core__media_entries")
me_user_column = media_entry_table.columns["uploader"]
me_user_column.alter(name="actor")
# MediaComment: author -> actor
media_comment_table = inspect_table(metadata, "core__media_comments")
mc_user_column = media_comment_table.columns["author"]
mc_user_column.alter(name="actor")
# Collection: creator -> actor
collection_table = inspect_table(metadata, "core__collections")
mc_user_column = collection_table.columns["creator"]
mc_user_column.alter(name="actor")
# commit changes to db.
db.commit()
class Graveyard_V0(declarative_base()):
""" Where models come to die """
__tablename__ = "core__graveyard"
id = Column(Integer, primary_key=True)
public_id = Column(Unicode, nullable=True, unique=True)
deleted = Column(DateTime, nullable=False)
object_type = Column(Unicode, nullable=False)
actor_id = Column(Integer, ForeignKey(GenericModelReference_V0.id))
@RegisterMigration(39, MIGRATIONS)
def federation_graveyard(db):
""" Introduces soft deletion to models
This adds a Graveyard model which is used to copy (soft-)deleted models to.
"""
metadata = MetaData(bind=db.bind)
# Create the graveyard table
Graveyard_V0.__table__.create(db.bind)
# Commit changes to the db
db.commit()
@RegisterMigration(40, MIGRATIONS)
def add_public_id(db):
metadata = MetaData(bind=db.bind)
# Get the table
activity_table = inspect_table(metadata, "core__activities")
activity_public_id = Column(
"public_id",
Unicode,
unique=True,
nullable=True
)
activity_public_id.create(
activity_table,
unique_name="activity_public_id"
)
# Commit this.
db.commit()
class Comment_V0(declarative_base()):
__tablename__ = "core__comment_links"
id = Column(Integer, primary_key=True)
target_id = Column(
Integer,
ForeignKey(GenericModelReference_V0.id),
nullable=False
)
comment_id = Column(
Integer,
ForeignKey(GenericModelReference_V0.id),
nullable=False
)
added = Column(DateTime, nullable=False, default=datetime.datetime.utcnow)
@RegisterMigration(41, MIGRATIONS)
def federation_comments(db):
"""
This reworks the MediaComent to be a more generic Comment model.
"""
metadata = MetaData(bind=db.bind)
textcomment_table = inspect_table(metadata, "core__media_comments")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
# First of all add the public_id field to the TextComment table
comment_public_id_column = Column(
"public_id",
Unicode,
unique=True
)
comment_public_id_column.create(
textcomment_table,
unique_name="public_id_unique"
)
comment_updated_column = Column(
"updated",
DateTime,
)
comment_updated_column.create(textcomment_table)
# First create the Comment link table.
Comment_V0.__table__.create(db.bind)
db.commit()
# now look up the comment table
comment_table = inspect_table(metadata, "core__comment_links")
# Itierate over all the comments and add them to the link table.
for comment in db.execute(textcomment_table.select()):
# Check if there is a GMR to the comment.
comment_gmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == comment.id,
gmr_table.c.model_type == "core__media_comments"
))).first()
if comment_gmr:
comment_gmr = comment_gmr[0]
else:
comment_gmr = db.execute(gmr_table.insert().values(
obj_pk=comment.id,
model_type="core__media_comments"
)).inserted_primary_key[0]
# Get or create the GMR for the media entry
entry_gmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == comment.media_entry,
gmr_table.c.model_type == "core__media_entries"
))).first()
if entry_gmr:
entry_gmr = entry_gmr[0]
else:
entry_gmr = db.execute(gmr_table.insert().values(
obj_pk=comment.media_entry,
model_type="core__media_entries"
)).inserted_primary_key[0]
# Add the comment link.
db.execute(comment_table.insert().values(
target_id=entry_gmr,
comment_id=comment_gmr,
added=datetime.datetime.utcnow()
))
# Add the data to the updated field
db.execute(textcomment_table.update().where(
textcomment_table.c.id == comment.id
).values(
updated=comment.created
))
db.commit()
# Add not null constraint
textcomment_update_column = textcomment_table.columns["updated"]
textcomment_update_column.alter(nullable=False)
# Remove the unused fields on the TextComment model
comment_media_entry_column = textcomment_table.columns["media_entry"]
comment_media_entry_column.drop()
db.commit()
@RegisterMigration(42, MIGRATIONS)
def consolidate_reports(db):
""" Consolidates the report tables into just one """
metadata = MetaData(bind=db.bind)
report_table = inspect_table(metadata, "core__reports")
comment_report_table = inspect_table(metadata, "core__reports_on_comments")
media_report_table = inspect_table(metadata, "core__reports_on_media")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
# Add the GMR object field onto the base report table
report_object_id_column = Column(
"object_id",
Integer,
ForeignKey(GenericModelReference_V0.id),
nullable=True,
)
report_object_id_column.create(report_table)
db.commit()
# Iterate through the reports in the comment table and merge them in.
for comment_report in db.execute(comment_report_table.select()):
# If the comment is None it's been deleted, we should skip
if comment_report.comment_id is None:
continue
# Find a GMR for this if one exists.
crgmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == comment_report.comment_id,
gmr_table.c.model_type == "core__media_comments"
))).first()
if crgmr:
crgmr = crgmr[0]
else:
crgmr = db.execute(gmr_table.insert().values(
obj_pk=comment_report.comment_id,
model_type="core__media_comments"
)).inserted_primary_key[0]
# Great now we can save this back onto the (base) report.
db.execute(report_table.update().where(
report_table.c.id == comment_report.id
).values(
object_id=crgmr
))
# Iterate through the Media Reports and do the save as above.
for media_report in db.execute(media_report_table.select()):
# If the media report is None then it's been deleted nd we should skip
if media_report.media_entry_id is None:
continue
# Find Mr. GMR :)
mrgmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == media_report.media_entry_id,
gmr_table.c.model_type == "core__media_entries"
))).first()
if mrgmr:
mrgmr = mrgmr[0]
else:
mrgmr = db.execute(gmr_table.insert().values(
obj_pk=media_report.media_entry_id,
model_type="core__media_entries"
)).inserted_primary_key[0]
# Save back on to the base.
db.execute(report_table.update().where(
report_table.c.id == media_report.id
).values(
object_id=mrgmr
))
db.commit()
# Now we can remove the fields we don't need anymore
report_type = report_table.columns["type"]
report_type.drop()
# Drop both MediaReports and CommentTable.
comment_report_table.drop()
media_report_table.drop()
# Commit we're done.
db.commit()
@RegisterMigration(43, MIGRATIONS)
def consolidate_notification(db):
""" Consolidates the notification models into one """
metadata = MetaData(bind=db.bind)
notification_table = inspect_table(metadata, "core__notifications")
cn_table = inspect_table(metadata, "core__comment_notifications")
cp_table = inspect_table(metadata, "core__processing_notifications")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
# Add fields needed
notification_object_id_column = Column(
"object_id",
Integer,
ForeignKey(GenericModelReference_V0.id)
)
notification_object_id_column.create(notification_table)
db.commit()
# Iterate over comments and move to notification base table.
for comment_notification in db.execute(cn_table.select()):
# Find the GMR.
cngmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == comment_notification.subject_id,
gmr_table.c.model_type == "core__media_comments"
))).first()
if cngmr:
cngmr = cngmr[0]
else:
cngmr = db.execute(gmr_table.insert().values(
obj_pk=comment_notification.subject_id,
model_type="core__media_comments"
)).inserted_primary_key[0]
# Save back on notification
db.execute(notification_table.update().where(
notification_table.c.id == comment_notification.id
).values(
object_id=cngmr
))
db.commit()
# Do the same for processing notifications
for processing_notification in db.execute(cp_table.select()):
cpgmr = db.execute(gmr_table.select().where(and_(
gmr_table.c.obj_pk == processing_notification.subject_id,
gmr_table.c.model_type == "core__processing_notifications"
))).first()
if cpgmr:
cpgmr = cpgmr[0]
else:
cpgmr = db.execute(gmr_table.insert().values(
obj_pk=processing_notification.subject_id,
model_type="core__processing_notifications"
)).inserted_primary_key[0]
db.execute(notification_table.update().where(
notification_table.c.id == processing_notification.id
).values(
object_id=cpgmr
))
db.commit()
# Add the not null constraint
notification_object_id = notification_table.columns["object_id"]
notification_object_id.alter(nullable=False)
# Now drop the fields we don't need
notification_type_column = notification_table.columns["type"]
notification_type_column.drop()
# Drop the tables we no longer need
cp_table.drop()
cn_table.drop()
db.commit()
@RegisterMigration(44, MIGRATIONS)
def activity_cleanup(db):
"""
This cleans up activities which are broken and have no graveyard object as
well as removing the not null constraint on Report.object_id as that can
be null when action has been taken to delete the reported content.
Some of this has been changed in previous migrations so we need to check
if there is anything to be done, there might not be. It was fixed as part
of the #5369 fix. Some past migrations could have broken on some people so
needed to be fixed then however for some they would have run fine.
"""
metadata = MetaData(bind=db.bind)
report_table = inspect_table(metadata, "core__reports")
activity_table = inspect_table(metadata, "core__activities")
gmr_table = inspect_table(metadata, "core__generic_model_reference")
# Remove not null on Report.object_id
object_id_column = report_table.columns["object_id"]
if not object_id_column.nullable:
object_id_column.alter(nullable=False)
db.commit()
# Go through each activity and verify the object and if a target is
# specified both exist.
for activity in db.execute(activity_table.select()):
# Get the GMR
obj_gmr = db.execute(gmr_table.select().where(
gmr_table.c.id == activity.object_id,
)).first()
# Get the object the GMR points to, might be null.
obj_table = inspect_table(metadata, obj_gmr.model_type)
obj = db.execute(obj_table.select().where(
obj_table.c.id == obj_gmr.obj_pk
)).first()
if obj is None:
# Okay we need to delete the activity and move to the next
db.execute(activity_table.delete().where(
activity_table.c.id == activity.id
))
continue
# If there is a target then check that too, if not that's fine
if activity.target_id == None:
continue
# Okay check the target is valid
target_gmr = db.execute(gmr_table.select().where(
gmr_table.c.id == activity.target_id
)).first()
target_table = inspect_table(metadata, target_gmr.model_type)
target = db.execute(target_table.select().where(
target_table.c.id == target_gmr.obj_pk
)).first()
# If it doesn't exist, delete the activity.
if target is None:
db.execute(activity_table.delete().where(
activity_table.c.id == activity.id
))
|