Send patches - preferably formatted by git format-patch - to patches at archlinux32 dot org.
summaryrefslogtreecommitdiff
path: root/lib/mysql-functions
blob: 0c633c89f231032f3da8b6ba3a1ee5e858d97703 (plain)
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
#!/bin/sh

# contains functions used to access mysql db

# shellcheck disable=SC2016,SC2039,SC2119,SC2120

if [ -z "${base_dir}" ]; then
  # just to make shellcheck happy
  . '../lib/load-configuration'
fi

# base64_encode_each encode each line of stdin with base64

base64_encode_each() {
  local line

  while read -r line; do
    printf '%s' \
      "${line}" | \
      base64 -w0
    printf '\n'
  done
}

# mysql_run_query
# wrapper function to query mysql
mysql_run_query() {
  local query_stdin
  local query_stdout
  local query_stderr

  local file_name_extra
  local file
  local files
  local number

  file_name_extra=''
  if [ "x$1" = 'xunimportant' ]; then
    shift
    file_name_extra='unimportant_'
  elif [ -s "${work_dir}/build-master-sanity" ]; then
    file_name_extra='was_insane_'
  fi

  # we save the query in a file and delete that file if the query succeeded
  query_stdin=$(mktemp "${work_dir}/tmp.mysql-functions.${file_name_extra}query.$(date +'%Y-%m-%dT%T').XXXXXX.stdin")
  query_stdout="${query_stdin%.stdin}.stdout"
  query_stderr="${query_stdin%.stdin}.stderr"
  cat > "${query_stdin}"
  for _ in {1..10}; do
    if [ -f "${query_stdout}" ]; then
      wait_some_time 10 10
    fi
    if ${mysql_command} -N --raw --batch "$@" \
      < "${query_stdin}" \
      > "${query_stdout}" \
      2>> "${query_stderr}"; then
      if grep -qF 'DELETE' "${query_stdin}"; then
        sed -n '
          s/^.*INFILE "\(\S\+\)".*$/\1/
          T
          p
        ' "${query_stdin}" | \
          while read -r file; do
            cp "${file}" "${work_dir}/successful.${query_stdin##*/}.infile.${file##*/}"
          done
        mv "${query_stdin}" "${work_dir}/successful.${query_stdin##*/}"
      else
        rm "${query_stdin}"
      fi
      rm "${query_stderr}"
    fi
    if ! [ -f "${query_stdin}" ]; then
      # success!
      break
    fi
  done
  # a present query_file means there was an error
  if [ -f "${query_stdin}" ]; then
    >&2 printf 'I (%s) could not complete a mysql query:\n' \
      "$(hostname)"
    >&2 sed 's/^/mysql<  /' "${query_stdin}"
    >&2 sed 's/^/mysql>2 /' "${query_stderr}"
    files="${query_stdin} ${query_stdout} ${query_stderr}$(
      sed -n '
        s/^.*INFILE "\(\S\+\)".*$/\1/
        T
        p
      ' "${query_stdin}" | \
        sponge | \
        cat -n | \
        while read -r number file; do
          sed -i '
            s@"'"$(str_to_regex "${file}")"'"@"'"${query_stdin%.stdin}.infile.${number}"'"@g
          ' "${query_stdin}"
          cp "${file}" "${query_stdin%.stdin}.infile.${number}"
          printf ' %s' "${query_stdin%.stdin}.infile.${number}"
        done
    )"
    if ${i_am_the_master}; then
      if [ ! -s "${work_dir}/build-master-sanity" ] && \
        [ -z "${file_name_extra}" ]; then
        printf '\001ACTION failed to execute a mysql query - can you have a look at "%s"?.\001\n' \
          "${query_stdin##*/}" \
          | irc_say
      fi
      for file in ${files}; do
        cp "${file}" "${webserver_directory}/mysql-queries/${file##*/}.txt"
        chmod go+r "${webserver_directory}/mysql-queries/${file##*/}.txt"
      done
    fi
    if [ -z "${file_name_extra}" ]; then
      echo 'A mysql query failed.' > \
        "${work_dir}/build-master-sanity"
    else
      # shellcheck disable=SC2086
      rm -f ${files}
    fi
    return 2
  fi
  # shellcheck disable=SC2094
  {
    rm "${query_stdout}"
    cat
  } < \
    "${query_stdout}"
}

# mysql_add_package_source $pkgbase $git_revision $mod_git_revision $upstream_package_repository $srcinfo_file

# shellcheck disable=SC2086
mysql_add_package_source() {
  local pkgbase
  local git_revision
  local mod_git_revision
  local upstream_package_repository
  local srcinfo_file

  pkgbase="$1"
  git_revision="$2"
  mod_git_revision="$3"
  upstream_package_repository="$4"
  srcinfo_file="$5"

  local uses_upstream
  local uses_modification
  local repo
  local upstream_package_repository_id

  if grep -qx 'PKGBUILD_mod = \S\+' "${srcinfo_file}"; then
    uses_modification=1
  else
    uses_modification=0
  fi
  if grep -qx 'PKGBUILD = \S\+' "${srcinfo_file}"; then
    uses_upstream=1
  else
    uses_upstream=0
  fi

  upstream_package_repository_id=$(
    {
      printf 'SELECT `upstream_repositories`.`id`'
      printf ' FROM `upstream_repositories`'
      printf ' WHERE `upstream_repositories`.`name` = from_base64("%s")' \
        "$(
          printf '%s' "${upstream_package_repository}" | \
            base64 -w0
        )"
      printf ' LIMIT 1;\n'
    } | \
      mysql_run_query
  )

  if [ -z "${upstream_package_repository_id}" ]; then
    >&2 printf 'Cannot find upstream package repository "%s" in the database.\n' \
      "${upstream_package_repository}"
    exit 2
  fi

  {
    printf 'INSERT INTO `package_sources`'
    printf ' (`pkgbase`,`git_revision`,`mod_git_revision`,`upstream_package_repository`,`uses_upstream`,`uses_modification`)'
    printf ' VALUES ('
    printf 'from_base64("%s"),' \
      "$(
        printf '%s' "${pkgbase}" | \
          base64 -w0
      )" \
      "$(
        printf '%s' "${git_revision}" | \
          base64 -w0
      )" \
      "$(
        printf '%s' "${mod_git_revision}" | \
          base64 -w0
      )"
    printf '%s,' \
      "${upstream_package_repository_id}" \
      "${uses_upstream}" \
      "${uses_modification}" | \
      sed 's/,$/)/'
    printf ' ON DUPLICATE KEY UPDATE `id`=LAST_INSERT_ID(`package_sources`.`id`);\n'
    printf 'SELECT LAST_INSERT_ID();\n'
  } | \
    mysql_run_query
}

# mysql_generate_package_metadata $current_repository_id $package $git_revision $mod_git_revision $repository

# generate the meta data of a package (dependencies, built packages, ...) in the database

mysql_generate_package_metadata() {

  mysql_load_min_and_max_versions

  ( # new shell is intentional
    current_repository_id="$1"
    package="$2"
    git_revision="$3"
    mod_git_revision="$4"
    repository="$5"

    if [[ "${current_repository_id}" = *[!0-9]* ]]; then
      >&2 printf 'mysql_generate_package_metadata(): invalid current_repository_id="%s".\n' \
        "${current_repository_id}"
      exit 2
    fi

    temp_dir=$(mktemp -d 'tmp.mysql-functions.mysql_generate_package_metadata.XXXXXXXXXX' --tmpdir)
    trap 'rm -rf --one-file-system "${temp_dir}"' EXIT

    if ! make_source_info "${package}" "${repository}" "${git_revision}" "${mod_git_revision}" "${temp_dir}/SRCINFO"; then
      >&2 printf '"make_source_info %s %s %s %s %s" failed.\n' "${package}" "${repository}" "${git_revision}" "${mod_git_revision}" "${temp_dir}/SRCINFO"
      exit 2
    fi
    # remove empty lines and unsupported architectures
    sed -i '
      /^[^=]*=\s*$/d
      /^\s*arch = /{
        / \(i[46]86\|pentium4\|any\)$/!d
      }
    ' "${temp_dir}/SRCINFO"

    if [ ! -s "${temp_dir}/SRCINFO" ]; then
      >&2 printf '"make_source_info" had empty output - eh, what?\n'
      exit 2
    fi
    printf '\n\n' >> "${temp_dir}/SRCINFO"

    pkgbase=$(
      grep '^pkgbase = ' "${temp_dir}/SRCINFO" | \
        cut -d' ' -f3
    )
    if [ -z "${pkgbase}" ]; then
      >&2 printf '"make_source_info" did not return a "pkgbase" - eh, what?\n'
      exit 2
    fi

    # add the package source
    package_source_id=$(
      mysql_add_package_source "${pkgbase}" "${git_revision}" "${mod_git_revision}" "${repository}" "${temp_dir}/SRCINFO"
    )

    {
      sed -n '
        s/^\tarch = //
        T
        p
      ' "${temp_dir}/SRCINFO" | \
        sort -u | \
        grep -vxF 'any' || \
        echo 'any'
    } > \
      "${temp_dir}/architectures"

    # select any specific arch (which will be building the 'any' part of a split package)
    any_arch=$(
      tail -n1 "${temp_dir}/architectures"
    )

    # iterate over all pkgnames
    grep '^pkgname = ' "${temp_dir}/SRCINFO" | \
      cut -d' ' -f3 | \
      while read -r pkgname; do
        # iff this pkgname defines some arch, ...
        if sed -n '
          /^pkgname = '"$(str_to_regex "${pkgname}")"'$/,/^$/ p
        ' "${temp_dir}/SRCINFO" | \
          grep -q '^\sarch = '; then
          # ... it will override all archs defined in pkgbase
          sed '
            /^pkgbase = /,/^$/ {
              /^\sarch = /d
            }
          ' "${temp_dir}/SRCINFO"
        else
          cat "${temp_dir}/SRCINFO"
        fi | \
          sed -n '
            /^pkgbase = \|^pkgname = '"$(str_to_regex "${pkgname}")"'$/,/^$/ {
              s/^pkgname/\t\0/
              /^\S/d
              /^\s*$/d
              s/^\s*//
              p
            }
          ' > \
          "${temp_dir}/SRCINFO.tmp"

        epoch=$(
          {
            grep -m1 '^epoch = ' "${temp_dir}/SRCINFO.tmp" || \
              echo 'epoch = 0'
          } | \
            cut -d' ' -f3
        )
        pkgver=$(
          grep -m1 '^pkgver = ' "${temp_dir}/SRCINFO.tmp" | \
            cut -d' ' -f3
        )
        pkgrel=$(
          grep -m1 '^pkgrel = ' "${temp_dir}/SRCINFO.tmp" | \
            cut -d' ' -f3
        )

        # iterate over all archs
        grep '^arch = ' "${temp_dir}/SRCINFO.tmp" | \
          cut -d' ' -f3 | \
          while read -r arch; do
            build_arch=$(
              # this binary package is either built by the build_assignment
              # with the identical arch (if existent) or by the one
              # with arch=$any_arch
              grep -xF "${arch}" "${temp_dir}/architectures" || \
                echo "${any_arch}"
            )
            sed '
              s/^\(\S\+\)_'"${arch}"' = /\1 = /
              s/^pkgname = \(\S\+\)$/\0\nprovides = \1='"${epoch}"':'"${pkgver}"'\ndepends = base\nmakedepends = base-devel/
            ' "${temp_dir}/SRCINFO.tmp" | \
            sed '
              s/^arch = \S\+$/arch/
              t
              s/^provides = /provides\t/
              t mangle_version
              s/^groups = /groups\t/
              t mangle_version
              s/^makedepends = /makedepends\t/
              t mangle_version
              s/^checkdepends = /checkdepends\t/
              t mangle_version
              s/^depends = /rundepends\t/
              t mangle_version
              d
              :mangle_version
# TODO: we might want to keep the pkgrel part and only remove the
# sub_pkgrel part - but then we need to include pkgrels on the provide
# side, too(?)
              s/\(=\|<\|<=\|>=\|>\)\([^[:space:]-]\+\)\(-\S*\)\?$/\t\1\t\2/
              t split_epoch
              h
              s/$/\t>=\t'"${min_version}"'/
              s/\(\s[0-9]\+\):\(\S\+\)$/\1\t\2/
              p
              g
              s/$/\t<=\t'"${max_version}"'/
              /^\S\+depends\s/d
              :split_epoch
              s/\(\s[0-9]\+\):\(\S\+\)$/\1\t\2/
              t
              s/\s\S\+$/\t0\0/
            ' | \
            sed '
              s/^/'"$(
                printf '%s\t' \
                  "${pkgname}" \
                  "${arch}" \
                  "${epoch}" \
                  "${pkgver}" \
                  "${pkgrel}" \
                  "${build_arch}"
              )"'/
            '
          done
        rm "${temp_dir}/SRCINFO.tmp"
      done | \
      sort -u > \
      "${temp_dir}/database-input"

    {
      printf 'CREATE TEMPORARY TABLE `links`('
        printf '`pkgname` VARCHAR(64),'
        printf '`architecture` VARCHAR(16),'
        printf '`epoch` MEDIUMINT,'
        printf '`pkgver` VARCHAR(64),'
        printf '`pkgrel` MEDIUMINT,'
        printf '`build_architecture` VARCHAR(16),'
        printf '`type` VARCHAR(16),'
        printf '`install_target_name` VARCHAR(128),'
        printf '`version_relation` VARCHAR(2),'
        printf '`install_target_epoch` MEDIUMINT,'
        printf '`install_target_version` VARCHAR(64)'
      printf ');\n'
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `links`;\n' \
        "${temp_dir}/database-input"

      printf 'SET @sub_pkgrel=('
        printf 'SELECT IFNULL('
          printf 'MAX('
          printf '`binary_packages`.`sub_pkgrel`+'
          printf 'IF(`binary_packages_in_repositories`.`repository` IN (%s,%s),0,1)' \
            "${repository_ids__any_build_list}" \
            "${repository_ids__any_to_be_decided}"
          printf '),'
          printf '0'
        printf ')'
        printf ' FROM `links`'
        printf ' JOIN `architectures`'
        printf ' ON `architectures`.`name`=`links`.`architecture`'
        mysql_join_architectures_binary_packages
        printf ' AND `binary_packages`.`%s`=`links`.`%s`' \
          'pkgname' 'pkgname' \
          'epoch' 'epoch' \
          'pkgver' 'pkgver' \
          'pkgrel' 'pkgrel'
        printf ' LEFT'
        mysql_join_binary_packages_binary_packages_in_repositories
      printf ');\n'

      printf 'INSERT IGNORE INTO `build_assignments` ('
        printf '`package_source`,'
        printf '`architecture`'
      printf ') SELECT'
      printf ' %s,`architectures`.`id`' \
        "${package_source_id}"
      printf ' FROM `links`'
      printf ' JOIN `architectures`'
      printf ' ON `architectures`.`name`=`links`.`build_architecture`'
      printf ' WHERE `links`.`type`="arch";\n'

      # we delete unbuilt binary_packages of identical version prior to
      # inserting, so we can _update_ a build list entry (e.g. because
      # the source changed, but the version stays the same)
      printf 'DELETE `binary_packages`'
      printf ' FROM `links`'
      printf ' JOIN `architectures`'
      printf ' ON `links`.`architecture`=`architectures`.`name`'
      printf ' JOIN `architectures` AS `ba_a`'
      printf ' ON `ba_a`.`name`=`links`.`build_architecture`'
      mysql_join_architectures_build_assignments 'ba_a'
      mysql_join_build_assignments_binary_packages
      printf ' AND `binary_packages`.`%s`=`links`.`%s`' \
        'pkgname' 'pkgname' \
        'epoch' 'epoch' \
        'pkgver' 'pkgver' \
        'pkgrel' 'pkgrel'
      printf ' AND `binary_packages`.`sub_pkgrel`=@sub_pkgrel'
      printf ' AND `binary_packages`.`architecture`=`architectures`.`id`'
      printf ' WHERE `links`.`type`="arch"'
      # the below test should always give "true", but it is a useful
      # safeguard to not delete built packages from the database
      printf ' AND NOT EXISTS('
        printf 'SELECT 1'
        printf ' FROM `binary_packages_in_repositories`'
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE `repositories`.`is_on_master_mirror`'
        printf ' AND `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
      printf ');\n'

      printf 'INSERT IGNORE INTO `binary_packages` ('
        printf '`build_assignment`,'
        printf '`pkgname`,'
        printf '`epoch`,'
        printf '`pkgver`,'
        printf '`pkgrel`,'
        printf '`sub_pkgrel`,'
        printf '`architecture`,'
        printf '`compression`'
      printf ') SELECT'
      printf ' `build_assignments`.`id`,'
      printf '`links`.`%s`,' \
        'pkgname' \
        'epoch' \
        'pkgver' \
        'pkgrel'
      printf '@sub_pkgrel,'
      printf '`architectures`.`id`,'
      printf '%s' \
        "${compression_ids__tar_zst}"
      printf ' FROM `links`'
      printf ' JOIN `architectures`'
      printf ' ON `links`.`architecture`=`architectures`.`name`'
      printf ' JOIN `architectures` AS `ba_a`'
      printf ' ON `ba_a`.`name`=`links`.`build_architecture`'
      mysql_join_architectures_build_assignments 'ba_a'
      printf ' AND `build_assignments`.`package_source`=%s' \
        "${package_source_id}"
      printf ' WHERE `links`.`type`="arch";\n'

      printf 'INSERT IGNORE INTO `binary_packages_in_repositories` ('
      printf ' `package`,'
      printf '`repository`'
      printf ') SELECT'
      printf ' `binary_packages`.`id`,'
# TODO: we might want to move packages of blacklisted architectures to
# the deletion-list instead of the build-list
      printf '%s' \
        "${current_repository_id}"
      printf ' FROM `links`'
      printf ' JOIN `architectures`'
      printf ' ON `architectures`.`name`=`links`.`architecture`'
      mysql_join_architectures_binary_packages
      printf ' AND `binary_packages`.`sub_pkgrel`=@sub_pkgrel'
      printf ' AND `binary_packages`.`%s`=`links`.`%s`' \
        'pkgname' 'pkgname' \
        'epoch' 'epoch' \
        'pkgver' 'pkgver' \
        'pkgrel' 'pkgrel'
      printf ';\n'

      if [ "${current_repository_id}" -eq "${repository_ids__any_build_list}" ] || \
        [ "${current_repository_id}" -eq "${repository_ids__any_to_be_decided}" ]; then
        printf 'DELETE `binary_packages_in_repositories`'
        printf ' FROM `links`'
        printf ' JOIN `architectures`'
        printf ' ON `architectures`.`name`=`links`.`architecture`'
        mysql_join_architectures_binary_packages
        printf ' AND `binary_packages`.`pkgname`=`links`.`pkgname`'
        printf ' AND ('
          printf '`binary_packages`.`sub_pkgrel`!=@sub_pkgrel'
          printf ' OR `binary_packages`.`%s`!=`links`.`%s`' \
            'epoch' 'epoch' \
            'pkgver' 'pkgver' \
            'pkgrel' 'pkgrel'
        printf ')'
        mysql_join_binary_packages_binary_packages_in_repositories
        printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
          "${current_repository_id}"
        printf ';\n'

        printf 'DELETE `binary_packages`'
        printf ' FROM `links`'
        printf ' JOIN `architectures`'
        printf ' ON `architectures`.`name`=`links`.`architecture`'
        mysql_join_architectures_binary_packages
        printf ' AND `binary_packages`.`pkgname`=`links`.`pkgname`'
        printf ' AND ('
          printf '`binary_packages`.`sub_pkgrel`!=@sub_pkgrel'
          printf ' OR `binary_packages`.`%s`!=`links`.`%s`' \
            'epoch' 'epoch' \
            'pkgver' 'pkgver' \
            'pkgrel' 'pkgrel'
        printf ') LEFT'
        mysql_join_binary_packages_binary_packages_in_repositories
        printf ' WHERE `binary_packages_in_repositories`.`id` IS NULL'
        printf ' AND `binary_packages`.`sha512sum` IS NULL;\n'
      fi

      printf 'INSERT IGNORE INTO `install_targets` (`name`)'
      printf ' SELECT `links`.`install_target_name`'
      printf ' FROM `links`;\n'

      printf 'INSERT IGNORE INTO `versions` (`epoch`,`version`)'
      printf ' SELECT'
      printf ' `links`.`install_target_epoch`,'
      printf '`links`.`install_target_version`'
      printf ' FROM `links`'
      printf ' WHERE `links`.`install_target_epoch` IS NOT NULL'
      printf ' AND `links`.`install_target_version` IS NOT NULL'
      printf ';\n'

      for link in 'groups' 'provides' 'makedepends' 'checkdepends' 'rundepends'; do
        case "${link}" in
          'groups'|'provides')
            printf 'INSERT IGNORE INTO `install_target_providers` ('
              printf '`package`,'
              printf '`install_target`,'
              printf '`version`,'
              printf '`install_target_is_group`'
            printf ') SELECT'
            printf ' `binary_packages`.`id`,'
            printf '`install_targets`.`id`,'
            printf '`versions`.`id`,'
            if [ "${link}" = 'groups' ]; then
              printf '1'
            else
              printf '0'
            fi
          ;;
          'makedepends'|'checkdepends'|'rundepends')
            printf 'INSERT IGNORE INTO `dependencies` ('
              printf '`dependent`,'
              printf '`depending_on`,'
              printf '`dependency_type`,'
              printf '`version`,'
              printf '`version_relation`'
            printf ') SELECT'
            printf ' `binary_packages`.`id`,'
            printf '`install_targets`.`id`,'
            printf '`dependency_types`.`id`,'
            printf '`versions`.`id`,'
            printf '`links`.`version_relation`'
          ;;
        esac
        printf ' FROM `links`'
        printf ' JOIN `architectures`'
        printf ' ON `architectures`.`name`=`links`.`architecture`'
        mysql_join_architectures_binary_packages
        printf ' AND `binary_packages`.`sub_pkgrel`=@sub_pkgrel'
        printf ' AND `binary_packages`.`%s`=`links`.`%s`' \
          'pkgname' 'pkgname' \
          'epoch' 'epoch' \
          'pkgver' 'pkgver' \
          'pkgrel' 'pkgrel'
        printf ' JOIN `install_targets`'
        printf ' ON `install_targets`.`name`=`links`.`install_target_name`'
        printf ' JOIN `versions`'
        printf ' ON `versions`.`epoch`=`links`.`install_target_epoch`'
        printf ' AND `versions`.`version`=`links`.`install_target_version`'
        if [ "${link}" = 'makedepends' ] || \
          [ "${link}" = 'checkdepends' ] || \
          [ "${link}" = 'rundepends' ]; then
          printf ' JOIN `dependency_types`'
          printf ' ON `dependency_types`.`name`="%s"' \
            "${link%depends}"
        fi
        printf ' WHERE `links`.`type`="%s";\n' \
          "${link}"
      done
      printf 'DROP TEMPORARY TABLE `links`;\n'
    } | \
      mysql_run_query

  )
}

# mysql_sanity_check
# do a sanity check on the mysql database

mysql_sanity_check() {
  {
    printf 'SELECT CONCAT('
      printf '"\\"",'
      printf '`ba_arch`.`name`,'
      printf '"\\" build-assignment building \\"",'
      printf '`bp_arch`.`name`,'
      printf '"\\" binary package: ",'
      printf '`binary_packages`.`pkgname`'
    printf ')'
    printf ' FROM `binary_packages`'
    mysql_join_binary_packages_build_assignments
    mysql_join_binary_packages_architectures '' 'bp_arch'
    mysql_join_build_assignments_architectures '' 'ba_arch'
    printf ' LEFT JOIN `architecture_compatibilities`'
    printf ' ON `architecture_compatibilities`.`runs_on`=`build_assignments`.`architecture`'
    printf ' AND `architecture_compatibilities`.`built_for`=`binary_packages`.`architecture`'
    printf ' AND `architecture_compatibilities`.`fully_compatible`'
    printf ' WHERE `architecture_compatibilities`.`id` IS NULL;\n'
    printf 'SELECT DISTINCT CONCAT("package multiple times in equally stable repositories: ",`a_ra`.`name`,"/{",`a_r`.`name`,",",`b_r`.`name`,"}/",`a`.`pkgname`)'
    printf ' FROM `binary_packages` AS `a`'
    mysql_join_binary_packages_binary_packages_in_repositories 'a' 'a_bir'
    mysql_join_binary_packages_in_repositories_repositories 'a_bir' 'a_r'
    mysql_join_repositories_architectures 'a_r' 'a_ra'
    printf ' JOIN `repositories` AS `b_r`'
    printf ' ON `a_r`.`stability`=`b_r`.`stability`'
    printf ' AND `a_r`.`architecture`=`b_r`.`architecture`'
    mysql_join_repositories_binary_packages_in_repositories 'b_r' 'b_bir'
    mysql_join_binary_packages_in_repositories_binary_packages 'b_bir' 'b'
    printf ' AND `a`.`pkgname`=`b`.`pkgname`'
    printf ' AND `a`.`id`!=`b`.`id`'
    printf ' AND ('
      printf '`a_r`.`architecture`!=%s' \
        "${architecture_ids__any}"
      printf ' OR `a`.`architecture`=`b`.`architecture`'
    printf ')'
    printf ' WHERE `a_r`.`stability` NOT IN (%s,%s)' \
      "${repository_stability_ids__forbidden}" \
      "${repository_stability_ids__virtual}"
    printf ' AND `b_r`.`stability` NOT IN (%s,%s);\n' \
      "${repository_stability_ids__forbidden}" \
      "${repository_stability_ids__virtual}"
    printf 'SELECT DISTINCT CONCAT("split-package with differing sub_pkgrels on the build-list: ",`a`.`pkgname`)'
    printf ' FROM `binary_packages` AS `a`'
    printf ' JOIN `binary_packages` AS `b`'
    printf ' ON `a`.`build_assignment`=`b`.`build_assignment`'
    mysql_join_binary_packages_binary_packages_in_repositories 'a' 'a_bir'
    mysql_join_binary_packages_binary_packages_in_repositories 'b' 'b_bir'
    printf ' WHERE `a`.`sub_pkgrel`!=`b`.`sub_pkgrel`'
    printf ' AND `%s_bir`.`repository`=%s' \
      'a' "${repository_ids__any_build_list}" \
      'b' "${repository_ids__any_build_list}"
    printf ';\n'
    printf 'SELECT DISTINCT CONCAT("non-virtual binary-package without checksum: ",'
    mysql_package_name_query
    printf ') FROM `binary_packages`'
    mysql_join_binary_packages_architectures
    printf ' LEFT'
    mysql_join_binary_packages_compressions
    mysql_join_binary_packages_binary_packages_in_repositories
    mysql_join_binary_packages_in_repositories_repositories
    printf ' WHERE `repositories`.`is_on_master_mirror`'
    printf ' AND `binary_packages`.`sha512sum` IS NULL;\n'
  } | \
    mysql_run_query | \
    sed '
      s,^,<font color="#FF0000">,
      s,$,</font>,
    '
  ( # new shell is intentional
    temp_dir=$(mktemp -d 'tmp.mysql-functions.mysql_sanity_check.XXXXXXXXXX' --tmpdir)
    trap 'rm -rf --one-file-system "${temp_dir}"' EXIT

    {
      printf 'SELECT DISTINCT `architectures`.`name`'
      printf ' FROM `architectures`'
      printf ' WHERE `architectures`.`id`!=%s;\n' \
        "${architecture_ids__any}"
    } | \
      mysql_run_query | \
      while read -r arch; do
        for dir in $(ls_master_mirror "${arch}"); do
          ls_master_mirror "${arch}/${dir}" | \
            sed '
              /\.pkg\.\('"${package_compression_suffix_regex}"'\)$/!d
              s|^|'"${arch}"'/'"${dir}"'/|
            '
        done
      done | \
      sort > \
      "${temp_dir}/master-mirror-listing"

    {
      printf 'SELECT CONCAT(`r_a`.`name`,"/",`repositories`.`name`,"/",'
      mysql_package_name_query
      printf ') FROM `binary_packages_in_repositories`'
      mysql_join_binary_packages_in_repositories_binary_packages
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      mysql_join_binary_packages_in_repositories_repositories
      mysql_join_repositories_architectures '' 'r_a'
      printf ' WHERE `repositories`.`is_on_master_mirror`'
    } | \
      mysql_run_query | \
      sed '
        s,\t,/,
        s,\t,/,
        s,\t,-,
        s,\t,:,
        s,\t,-,
        s,\t,.,
        s,\t,-,
      ' | \
      sort > \
      "${temp_dir}/mysql-packages"

    diff -u \
      "${temp_dir}/master-mirror-listing" \
      "${temp_dir}/mysql-packages"

    # shellcheck disable=SC2041
    ls_master_mirror 'pool' | \
      sed '
        /\.pkg\.\('"${package_compression_suffix_regex}"'\)\(\.sig\)\?$/ !d
      ' | \
      sort -u > \
      "${temp_dir}/master-mirror-pool"

    {
      printf 'SELECT '
      mysql_package_name_query
      printf ' FROM `binary_packages`'
      mysql_join_binary_packages_architectures
      printf ' LEFT'
      mysql_join_binary_packages_compressions
      printf ' WHERE NOT EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `binary_packages_in_repositories`'
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE NOT `repositories`.`is_on_master_mirror`'
        printf ' AND `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
      printf ') OR EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `binary_packages_in_repositories`'
        mysql_join_binary_packages_in_repositories_repositories
        printf ' WHERE `repositories`.`is_on_master_mirror`'
        printf ' AND `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
      printf ')'
    } | \
      mysql_run_query | \
      sed '
        s/^.*$/\0\n\0.sig/
      ' | \
      sort > \
      "${temp_dir}/mysql-packages-pool"

    diff -u \
      "${temp_dir}/master-mirror-pool" \
      "${temp_dir}/mysql-packages-pool"
  )
}

# mysql_find_build_assignment_loops
# recreate the `build_dependency_loops` table
mysql_find_build_assignment_loops() {
  ( # new shell is intentional
    tmp_file=$(mktemp 'tmp.mysql-functions.mysql_find_build_assignment_loops.XXXXXXXXXX' --tmpdir)
    trap 'rm "${tmp_file}"' EXIT
    {
      printf 'SELECT DISTINCT `architectures`.`id`'
      printf ' FROM `architectures`'
      printf ' WHERE `architectures`.`id`!=%s;\n' \
        "${architecture_ids__any}"
    } | \
      mysql_run_query | \
      while read -r arch_id; do
        {
          printf 'SELECT DISTINCT'
          printf ' `ncy_ba`.`id`,'
          printf '`nt_ba`.`id`'
          printf ' FROM `dependencies`'
          mysql_join_dependencies_dependency_types
          mysql_join_dependencies_install_target_providers_with_versions
          mysql_join_install_target_providers_binary_packages '' 'ncy_bp'
          mysql_join_dependencies_binary_packages '' 'nt_bp'
          for which in 'ncy' 'nt'; do
            mysql_join_binary_packages_binary_packages_in_repositories "${which}"'_bp' "${which}"'_bpir'
            mysql_join_binary_packages_build_assignments "${which}"'_bp' "${which}"'_ba'
            printf ' JOIN `architecture_compatibilities` AS `%s_ac`' \
              "${which}"
            printf ' ON `%s_ac`.`built_for`=`%s_ba`.`architecture`' \
              "${which}" "${which}"
            printf ' AND `%s_ac`.`runs_on`=%s' \
              "${which}" "${arch_id}"
            printf ' AND `%s_ac`.`fully_compatible`' \
              "${which}"
          done
          printf ' WHERE `nt_bpir`.`repository`=%s' \
            "${repository_ids__any_build_list}"
          printf ' AND `ncy_bpir`.`repository`=%s' \
            "${repository_ids__any_build_list}"
          printf ' AND `dependency_types`.`relevant_for_building`'
          printf ' AND ('
            printf '`dependency_types`.`relevant_for_binary_packages`'
            printf ' OR NOT EXISTS ('
              printf 'SELECT 1'
              printf ' FROM `binary_packages_in_repositories` AS `subst_bpir`'
              mysql_join_binary_packages_in_repositories_repositories 'subst_bpir' 'subst_r'
              printf ' AND `subst_r`.`is_on_master_mirror`'
              mysql_join_binary_packages_in_repositories_binary_packages 'subst_bpir' 'subst_bp'
              mysql_join_binary_packages_build_assignments 'subst_bp' 'subst_ba'
              printf ' JOIN `architecture_compatibilities` AS `subst_ac`'
              printf ' ON `subst_ac`.`built_for`=`subst_ba`.`architecture`'
              printf ' AND `subst_ac`.`runs_on`=%s' \
                "${arch_id}"
              printf ' AND `subst_ac`.`fully_compatible`'
              mysql_join_binary_packages_in_repositories_install_target_providers 'subst_bpir' 'subst_itp'
              mysql_join_install_target_providers_versions 'subst_itp' 'subst_itp_v'
              printf ' WHERE `subst_itp`.`install_target`=`dependencies`.`depending_on`'
              printf ' AND '
              mysql_query_ordering_correct \
                '`dependencies_versions`.`order`' \
                '`subst_itp_v`.`order`' \
                '`dependencies`.`version_relation`'
            printf ')'
          printf ');\n'
        } | \
          mysql_run_query | \
          tr '\t' ' ' | \
          tsort 2>&1 >/dev/null | \
          sed 's/^tsort:\s*//'
      done | \
      awk '
        BEGIN {
          i=0
        };
        /^-: input contains a loop:$/ {
          i=i+1
        };
        !/^-: input contains a loop:$/ {
          print i "\t" $1
        }
      ' > \
      "${tmp_file}"
    {
      printf 'DELETE FROM `build_dependency_loops`;\n'
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `build_dependency_loops` (`loop`,`build_assignment`);\n' \
        "${tmp_file}"
    } | \
      mysql_run_query
  )
}

# mysql_cleanup [dry]
# clean up left overs from mysql database
mysql_cleanup() {
  local operator
  if [ "$#" = '0' ]; then
    operator='DELETE'
  elif [ "$#" = '1' ] && [ "x$1" = 'xdry' ]; then
    operator='SELECT COUNT(1)'
  else
    >&2 echo 'Unknown parameter'
    >&2 echo 'Call "mysql_clean_up" or "mysql_clean_up dry".'
    exit 2
  fi
  {
    # remove to-be-decided binary_packages_in_repositories
    printf '%s ' \
      "${operator}"
    if [ "${operator}" = 'DELETE' ]; then
      printf '`binary_packages_in_repositories` '
    fi
    printf 'FROM `binary_packages`'
    mysql_join_binary_packages_binary_packages_in_repositories
    printf ' WHERE `binary_packages_in_repositories`.`repository`=%s;\n' \
      "${repository_ids__any_to_be_decided}"
    # remove virtual binary_packages w/o binary_packages_in_repositories
    printf '%s ' \
      "${operator}"
    if [ "${operator}" = 'DELETE' ]; then
      printf '`binary_packages` '
    fi
    printf 'FROM `binary_packages`'
    printf ' WHERE `binary_packages`.`sha512sum` IS NULL'
    printf ' AND NOT EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `binary_packages_in_repositories` '
      printf 'WHERE `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
    printf ');\n'
    # remove build_assignments w/o binary_package
    printf '%s FROM `build_assignments` ' \
      "${operator}"
    printf 'WHERE NOT EXISTS '
    printf '('
      printf 'SELECT 1'
      printf ' FROM `binary_packages` '
      printf 'WHERE `binary_packages`.`build_assignment`=`build_assignments`.`id`'
    printf ');\n'
    # remove failed_builds with unbroken build_assignments
    printf '%s ' \
      "${operator}"
    if [ "${operator}" = 'DELETE' ]; then
      printf '`failed_builds` '
    fi
    printf 'FROM `failed_builds` '
    mysql_join_failed_builds_build_assignments
    printf 'WHERE NOT `build_assignments`.`is_broken`'
    printf ';\n'
    # remove package_sources w/o build_assignment
    printf '%s FROM `package_sources` ' \
      "${operator}"
    printf 'WHERE NOT EXISTS '
    printf '('
      printf 'SELECT 1'
      printf ' FROM `build_assignments` '
      printf 'WHERE `build_assignments`.`package_source`=`package_sources`.`id`'
    printf ');\n'
    # remove jobs from build slaves that are not on the build-list
    if [ "${operator}" = 'DELETE' ]; then
      printf 'UPDATE `binary_packages_in_repositories`'
    else
      printf 'SELECT COUNT(DISTINCT `build_slaves`.`id`)'
      printf ' FROM `binary_packages_in_repositories`'
    fi
    mysql_join_binary_packages_in_repositories_binary_packages
    printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
      "${repository_ids__any_build_list}"
    printf ' RIGHT'
    mysql_join_binary_packages_build_slaves
    if [ "${operator}" = 'DELETE' ]; then
      printf ' SET `build_slaves`.`currently_building`=NULL'
    fi
    printf ' WHERE `build_slaves`.`currently_building` IS NOT NULL'
    printf ' AND `binary_packages_in_repositories`.`id` IS NULL;\n'
    # remove build orders from build slaves which have not connected within 1h
    if [ "${operator}" = 'DELETE' ]; then
      printf 'UPDATE `build_slaves`'
      printf ' SET `build_slaves`.`currently_building`=NULL'
    else
      printf 'SELECT COUNT(1)'
      printf ' FROM `build_slaves`'
    fi
    printf ' WHERE `build_slaves`.`currently_building` IS NOT NULL'
    printf ' AND TIMEDIFF(NOW(),('
      printf 'SELECT MAX(`ssh_log`.`date`)'
      printf ' FROM `ssh_log`'
      printf ' WHERE `ssh_log`.`build_slave`=`build_slaves`.`id`'
    printf ')) > "1:00:00";\n'
    # remove `last_action`, `trials` and `logged_lines` from buildslaves without an assignment
    if [ "${operator}" = 'DELETE' ]; then
      printf 'UPDATE `build_slaves`'
      printf ' SET `build_slaves`.`last_action`=NULL,'
      printf ' `build_slaves`.`logged_lines`=NULL,'
      printf ' `build_slaves`.`trials`=NULL'
    else
      printf 'SELECT COUNT(1)'
      printf ' FROM `build_slaves`'
    fi
    printf ' WHERE `build_slaves`.`currently_building` IS NULL'
    printf ' AND (`build_slaves`.`last_action` IS NOT NULL'
    printf ' OR `build_slaves`.`logged_lines` IS NOT NULL'
    printf ' OR `build_slaves`.`trials` IS NOT NULL);\n'
    # shrink the priorities
    printf 'CREATE TEMPORARY TABLE `ps%s`(`old_p` SMALLINT, `new_p` SMALLINT, UNIQUE KEY `old_p`(`old_p`));\n' \
      '' '_2' '_3'
    printf 'INSERT INTO `ps`(`old_p`) VALUES (0);\n'
    printf 'INSERT IGNORE INTO `ps`(`old_p`)'
    printf ' SELECT `build_assignments`.`priority`'
    printf ' FROM `build_assignments`;\n'
    for copy in '2' '3'; do
      printf 'INSERT INTO `ps_%s`(`old_p`)' \
        "${copy}"
      printf ' SELECT `ps`.`old_p`'
      printf ' FROM `ps`;\n'
    done
    printf 'UPDATE `ps`'
    printf ' LEFT JOIN ('
      printf 'SELECT'
      printf ' `ps_2`.`old_p` AS `limit_p`,'
      printf 'COUNT(`ps_3`.`old_p`) AS `count_p`'
      printf ' FROM `ps_2`'
      printf ' JOIN `ps_3`'
      printf ' ON `ps_3`.`old_p`<`ps_2`.`old_p`'
      printf ' GROUP BY `ps_2`.`old_p`'
    printf ') AS `ps_q`'
    printf ' ON `ps_q`.`limit_p`=`ps`.`old_p`'
    printf ' SET `ps`.`new_p`=IFNULL(`ps_q`.`count_p`,0)'
    printf ';\n'
    printf 'UPDATE `build_assignments`'
    printf ' JOIN `ps`'
    printf ' ON `build_assignments`.`priority`=`ps`.`old_p`'
    printf ' SET `build_assignments`.`priority`=`ps`.`new_p`;\n'
    printf 'DROP TEMPORARY TABLE `ps%s`;\n' \
      '' '_2' '_3'
  } | \
    mysql_run_query 'unimportant'
}

# mysql_query_has_pending_dependencies builder-architecture.id `build_assignment`.`id`
# print a mysql query giving whether dependencies are pending (unsuccessfully built
# dependencies do not count after 1 day)
mysql_query_has_pending_dependencies() {
  # we have pending dependencies ...
  printf 'EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `binary_packages` AS `todos`'
    mysql_join_binary_packages_binary_packages_in_repositories 'todos' 'todo_bpirs'
    printf ' AND `todo_bpirs`.`repository`=%s' \
      "${repository_ids__any_build_list}"
  # ... if any dependency ...
    mysql_join_binary_packages_dependencies 'todos' 'l_deps'
    mysql_join_dependencies_versions 'l_deps' 'l_dep_vs'
    mysql_join_dependencies_dependency_types 'l_deps' 'l_dep_ts'
    mysql_join_dependencies_install_targets 'l_deps'
  # ... is relevant for building ...
    printf ' AND `l_dep_ts`.`relevant_for_building`'
    printf ' WHERE `todos`.`build_assignment`=%s' \
      "$2"
    printf ' AND IF('
  # ... and if (a) relevant for binary_packages ...
      printf '`l_dep_ts`.`relevant_for_binary_packages`,'
  # ... has some unbuilt provider ...
      printf 'EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `install_target_providers` AS `l_itps`'
        mysql_join_install_target_providers_versions 'l_itps' 'l_itp_vs'
        mysql_join_install_target_providers_binary_packages 'l_itps' 'dep_bp'
        mysql_join_binary_packages_binary_packages_in_repositories 'dep_bp' 'dep_bpir'
        printf ' AND `dep_bpir`.`repository`=%s' \
          "${repository_ids__any_build_list}"
        mysql_join_binary_packages_build_assignments 'dep_bp' 'dep_ba'
        # ignore dependencies which are broken >=1 day
        printf ' AND ('
          printf 'NOT EXISTS ('
            printf 'SELECT 1'
            printf ' FROM `failed_builds`'
            printf ' WHERE `failed_builds`.`date`<=ADDTIME(NOW(),"-1 00:00:00")'
            printf ' AND `failed_builds`.`build_assignment`=`dep_ba`.`id`'
          printf ')'
          # haskell-* is a hopeless case in this regard
          printf ' OR `dep_bp`.`pkgname` LIKE "haskell-%%"'
        printf ')'
        printf ' JOIN `architecture_compatibilities`'
        printf ' ON `architecture_compatibilities`.`built_for`=`dep_bp`.`architecture`'
        printf ' AND `architecture_compatibilities`.`fully_compatible`'
        printf ' WHERE `l_itps`.`install_target`=`l_deps`.`depending_on`'
        printf ' AND `dep_bp`.`build_assignment`!=%s' \
          "$2"
        printf ' AND `architecture_compatibilities`.`runs_on`=%s' \
          "$1"
        # version is irelevant: either the package is built and we don't
        # care or the package is not built and we don't know
      printf '),'
  # ... or (b) not relevant for binary_packages (e.g. a makedepends) has
  # no built provider
# TODO: only consider installable providers (at least for haskell-*, but
# it would be probably best to do so for all packages)
      printf 'NOT EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `install_target_providers` AS `l_itps`'
        mysql_join_install_target_providers_versions 'l_itps' 'l_itp_vs'
        mysql_join_install_target_providers_binary_packages 'l_itps' 'dep_bp'
        mysql_join_binary_packages_binary_packages_in_repositories 'dep_bp' 'dep_bpir'
        mysql_join_binary_packages_in_repositories_repositories 'dep_bpir' 'dep_r'
        printf ' WHERE `l_itps`.`install_target`=`l_deps`.`depending_on`'
        printf ' AND ('
          printf '`dep_r`.`is_on_master_mirror`'
          printf ' OR `dep_bp`.`build_assignment`=%s' \
            "$2"
        printf ') AND `dep_r`.`architecture`=%s' \
          "$1"
        printf ' AND '
        mysql_query_ordering_correct \
          '`l_dep_vs`.`order`' \
          '`l_itp_vs`.`order`' \
          '`l_deps`.`version_relation`'
      printf ')'
    printf ')'
  printf ')'
}

# mysql_query_generate_packages_with_pending_dependencies
# print the query to generate the temporary table
# `packages_with_pending_dependencies`
mysql_query_generate_packages_with_pending_dependencies() {
  printf 'CREATE TEMPORARY TABLE `install_target_statuses`('
    printf '`id` BIGINT NOT NULL,'
    printf '`version_relation` VARCHAR(2) NOT NULL,'
    printf '`version_order` BIGINT NOT NULL,'
    printf '`builder_architecture` SMALLINT NOT NULL,'
    printf '`exists_built` BIT NOT NULL,'
    printf '`exists_unbuilt` BIT NOT NULL,'
    printf 'UNIQUE KEY `content`(`id`,`builder_architecture`),'
    printf 'KEY(`id`),'
    printf 'KEY(`builder_architecture`)'
  printf ');\n'

  printf 'INSERT IGNORE INTO `install_target_statuses`('
    printf '`id`,'
    printf '`version_relation`,'
    printf '`version_order`,'
    printf '`builder_architecture`,'
    printf '`exists_built`,'
    printf '`exists_unbuilt`'
  printf ') SELECT '
  printf '`dependencies`.`%s`,' \
    'depending_on' \
    'version_relation'
  printf '`versions`.`order`,'
  printf '`architectures`.`id`,0,0'
  printf ' FROM `dependencies`'
  mysql_join_dependencies_versions
  printf ' JOIN `architectures`;\n'

  printf 'UPDATE `install_target_statuses`'
  mysql_join_install_targets_install_target_providers 'install_target_statuses'
  mysql_join_install_target_providers_versions
  printf ' AND'
  mysql_query_ordering_correct \
    '`install_target_statuses`.`version_order`' \
    '`versions`.`order`' \
    '`install_target_statuses`.`version_relation`'
  mysql_join_install_target_providers_binary_packages
  printf ' AND ('
    printf '`binary_packages`.`architecture`=`install_target_statuses`.`builder_architecture`'
    printf ' OR `binary_packages`.`architecture`=%s' \
      "${architecture_ids__any}"
    printf ' OR `install_target_statuses`.`builder_architecture`=%s' \
      "${architecture_ids__any}"
  printf ')'
  mysql_join_binary_packages_binary_packages_in_repositories
  printf ' SET `install_target_statuses`.`exists_unbuilt`=1'
  printf ' WHERE `binary_packages_in_repositories`.`repository`=%s;\n' \
    "${repository_ids__any_build_list}"

  printf 'UPDATE `install_target_statuses`'
  mysql_join_install_targets_install_target_providers 'install_target_statuses'
  mysql_join_install_target_providers_versions
  printf ' AND'
  mysql_query_ordering_correct \
    '`install_target_statuses`.`version_order`' \
    '`versions`.`order`' \
    '`install_target_statuses`.`version_relation`'
  mysql_join_install_target_providers_binary_packages
  printf ' AND ('
    printf '`binary_packages`.`architecture`=`install_target_statuses`.`builder_architecture`'
    printf ' OR `binary_packages`.`architecture`=%s' \
      "${architecture_ids__any}"
    printf ' OR `install_target_statuses`.`builder_architecture`=%s' \
      "${architecture_ids__any}"
  printf ')'
  mysql_join_binary_packages_binary_packages_in_repositories
  mysql_join_binary_packages_in_repositories_repositories
  printf ' SET `install_target_statuses`.`exists_built`=1'
  printf ' WHERE `repositories`.`is_on_master_mirror`;\n'

  printf 'CREATE TEMPORARY TABLE `packages_with_pending_dependencies`('
    printf '`builder_architecture` SMALLINT NOT NULL,'
    printf '`build_assignment` BIGINT NOT NULL,'
    printf 'UNIQUE KEY `content`(`builder_architecture`,`build_assignment`),'
    printf 'KEY(`builder_architecture`),'
    printf 'KEY(`build_assignment`)'
  printf ');\n'

  printf 'INSERT IGNORE INTO `packages_with_pending_dependencies`('
    printf '`builder_architecture`,'
    printf '`build_assignment`'
  printf ') SELECT'
  printf ' `install_target_statuses`.`builder_architecture`,'
  printf '`build_assignments`.`id`'
  printf ' FROM `install_target_statuses`'
  printf ' JOIN `install_targets`'
  printf ' ON `install_targets`.`id`=`install_target_statuses`.`id`'
  mysql_join_install_targets_dependencies
  printf ' AND `dependencies`.`version_relation`=`install_target_statuses`.`version_relation`'
  mysql_join_dependencies_versions
  printf ' AND `versions`.`order`=`install_target_statuses`.`version_order`'
  mysql_join_dependencies_dependency_types
  printf ' AND `dependency_types`.`relevant_for_building`'
  printf ' AND ('
    printf 'NOT `install_target_statuses`.`exists_built`'
    printf 'OR ('
      printf '`install_target_statuses`.`exists_unbuilt`'
      printf ' AND `dependency_types`.`relevant_for_binary_packages`'
    printf ')'
  printf ')'
  mysql_join_dependencies_binary_packages
  mysql_join_binary_packages_binary_packages_in_repositories
  printf ' AND `binary_packages_in_repositories`.`repository`=%s' \
    "${repository_ids__any_build_list}"
  mysql_join_binary_packages_build_assignments
  printf ' WHERE `build_assignments`.`architecture`=`install_target_statuses`.`builder_architecture`'
  printf ' OR `build_assignments`.`architecture`=%s' \
    "${architecture_ids__any}"
  printf ' OR `install_target_statuses`.`builder_architecture`=%s' \
    "${architecture_ids__any}"
  printf ';\n'
  printf 'DROP TEMPORARY TABLE `install_target_statuses`;\n'
}

# mysql_query_is_part_of_loop `build_assignment`.`id`
# print a mysql query giving whether the package is part of a loop
mysql_query_is_part_of_loop() {
  printf 'EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `build_dependency_loops`'
    printf ' WHERE `build_dependency_loops`.`build_assignment`=%s' \
      "$1"
  printf ')'
}

# mysql_query_select_pkgbase_and_revision
# print the part of a mysql query giving:
# pkgbase git_revision mod_git_revision upstream_package_repository
mysql_query_select_pkgbase_and_revision() {
  printf '`package_sources`.`%s`,' \
    'pkgbase' \
    'git_revision' \
    'mod_git_revision'
  printf '`upstream_repositories`.`name`'
  printf ' FROM `build_assignments`'
  mysql_join_build_assignments_package_sources
  mysql_join_package_sources_upstream_repositories
}

# mysql_package_name_query [binary_packages] [architectures] [compressions]
# print a mysql query of the full name of a package file

mysql_package_name_query() {
  local bp_name="${1:-binary_packages}"
  local a_name="${2:-architectures}"
  local c_name="${3:-compressions}"
  printf 'CONCAT('
  printf '`%s`.`pkgname`,"-",' "${bp_name}"
  printf 'IF(`%s`.`epoch`=0,"",CONCAT(`%s`.`epoch`,":")),' "${bp_name}" "${bp_name}"
  printf '`%s`.`pkgver`,"-",' "${bp_name}"
  printf '`%s`.`pkgrel`,' "${bp_name}"
  printf 'IF(`%s`.`sub_pkgrel_omitted`,"",CONCAT(".",`%s`.`sub_pkgrel`)),"-",' "${bp_name}" "${bp_name}"
  printf '`%s`.`name`,".pkg.",' "${a_name}"
  printf 'IF('
    printf '`%s`.`suffix` IS NULL,' "${c_name}"
    printf '"tar.zst",'
    printf '`%s`.`suffix`' "${c_name}"
  printf ')'
  printf ')'
}

# mysql_join_*_*
# print 'JOIN' part of mysql query to connect the respective tables
# these functions take 2 optional arguments, acting as aliases for
# the tables

# mysql_join__generic $table_a $column_a $table_b $column_b
# create mysql_join_${table_a}_${table_b}() function

mysql_join__generic() {
  eval "$(
    printf 'mysql_join_%s_%s() {\n' "$1" "$3"
    printf '  printf '"'"' JOIN `%s`'"'"'\n' "$3"
    printf '  if [ -n "$2" ]; then\n'
    printf '    printf '"'"' AS `%%s`'"'"' "$2"\n'
    printf '  fi\n'
    printf '  if [ -n "$1" ]; then\n'
    printf '    printf '"'"' ON `%%s`.`%s`='"'"' "$1"\n' "$2"
    printf '  else\n'
    printf '    printf '"'"' ON `%s`.`%s`='"'"'\n' "$1" "$2"
    printf '  fi\n'
    printf '  if [ -n "$2" ]; then\n'
    printf '    printf '"'"'`%%s`.`%s`'"'"' "$2"\n' "$4"
    printf '  else\n'
    printf '    printf '"'"'`%s`.`%s`'"'"'\n' "$3" "$4"
    printf '  fi\n'
    printf '}\n'
  )"
}

for link in \
  'allowed_email_actions:action:email_actions' \
  'allowed_email_actions:gpg_key:gpg_keys' \
  \
  'binary_packages:architecture:architectures' \
  'binary_packages:build_assignment:build_assignments' \
  'binary_packages:compression:compressions' \
  'binary_packages:signing_key:gpg_keys' \
  \
  'binary_packages_in_repositories:package:binary_packages' \
  'binary_packages_in_repositories:repository:repositories' \
  \
  'build_assignments:architecture:architectures' \
  'build_assignments:package_source:package_sources' \
  \
  'build_dependency_loops:build_assignment:build_assignments' \
  'build_dependency_loops:build_assignment build_assignment:binary_packages' \
  \
  'build_slaves:currently_building:build_assignments' \
  'build_slaves:currently_building build_assignment:binary_packages' \
  'build_slaves:ssh_key:ssh_keys' \
  \
  'dependencies:depending_on:install_targets' \
  'dependencies:dependent:binary_packages' \
  'dependencies:dependent package:binary_packages_in_repositories' \
  'dependencies:dependency_type:dependency_types' \
  'dependencies:version:versions' \
  \
  'email_log:action:email_actions' \
  'email_log:gpg_key:gpg_keys' \
  \
  'failed_builds:reason:fail_reasons' \
  'failed_builds:build_assignment:build_assignments' \
  'failed_builds:build_slave:build_slaves' \
  \
  'gpg_keys:owner:persons' \
  \
  'install_target_providers:package:binary_packages' \
  'install_target_providers:package package:binary_packages_in_repositories' \
  'install_target_providers:install_target:install_targets' \
  'install_target_providers:install_target depending_on:dependencies' \
  'install_target_providers:version:versions' \
  \
  'package_sources:upstream_package_repository:upstream_repositories' \
  'package_sources:pkgbase pkgbase:toolchain_order' \
  \
  'repositories:stability:repository_stabilities' \
  'repositories:architecture:architectures' \
  \
  'repository_moves:upstream_package_repository:upstream_repositories' \
  'repository_moves:upstream_package_repository upstream_package_repository:package_sources' \
  \
  'ssh_keys:owner:persons' \
  \
  'ssh_log:build_slave:build_slaves' \
  \
  'upstream_repositories:git_repository:git_repositories'; do
# A join for these cannot be done, because it's not clear on what to join:
#  'repository_stability_relations:more_stable:repository_stabilities'
#  'repository_stability_relations:less_stable:repository_stabilities'

  table_b="${link##*:}"
  table_a="${link%:*}"
  column_b="${table_a##*:}"
  table_a="${table_a%:*}"
  column_a="${column_b% *}"
  if [ "${column_a}" = "${column_b}" ]; then
    column_b='id'
  else
    column_b="${column_b##* }"
  fi

  mysql_join__generic "${table_a}" "${column_a}" "${table_b}" "${column_b}"
  mysql_join__generic "${table_b}" "${column_b}" "${table_a}" "${column_a}"
done

# mysql_join_install_target_providers_dependencies_with_versions() and
# mysql_join_dependencies_install_target_providers_with_versions() also
# join via `versions`, optional 3rd argument cancels 1st version-join,
# optional 4th argument cancels 2nd version-join
mysql_join_install_target_providers_dependencies_with_versions() {
# TODO: needs a left-join variant, too (possibly a right-join, too?)
  mysql_join_install_target_providers_dependencies "$1" "$2"
  if [ -z "$3" ]; then
    mysql_join_install_target_providers_versions "$1" "${1:-install_target_providers}_versions"
  fi
  if [ -z "$4" ]; then
    mysql_join_dependencies_versions "$2" "${2:-dependencies}_versions"
  fi
  printf ' AND '
  mysql_query_ordering_correct \
    '`'"${4:-${2:-dependencies}_versions}"'`.`order`' \
    '`'"${3:-${1:-install_target_providers}_versions}"'`.`order`' \
    '`'"${2:-dependencies}"'`.`version_relation`'
}

mysql_join_dependencies_install_target_providers_with_versions() {
  local relation
  mysql_join_dependencies_install_target_providers "$1" "$2"
  if [ -z "$4" ]; then
    mysql_join_install_target_providers_versions "$2" "${2:-install_target_providers}_versions"
  fi
  if [ -z "$3" ]; then
    mysql_join_dependencies_versions "$1" "${1:-dependencies}_versions"
  fi
  printf ' AND '
  mysql_query_ordering_correct \
    '`'"${3:-${1:-dependencies}_versions}"'`.`order`' \
    '`'"${4:-${2:-install_target_providers}_versions}"'`.`order`' \
    '`'"${1:-dependencies}"'`.`version_relation`'
}

# mysql_retrieve_static_information
# retrieve some static information from the database:
# - names of architectures -> $architectures
# - ids of architectures -> $architecture_ids__$arch
# - ids of non-any architectures -> $non_any_architecture_ids
# - ids of package repositories -> $repository_ids__$arch_$repo
# - names of upstream_repositories -> $upstream_repository_names
# - names, paths and heads of git_repositories
# - ids of repository_stabilities -> $repository_stability_ids__$stability
# - ids of compressions -> $compression_ids__$compression
# - $package_compression_suffix_regex
mysql_retrieve_static_information() {
  eval "$(
    {
      printf 'SELECT CONCAT('
      printf '"architectures=\\"",'
      printf 'GROUP_CONCAT(`architectures`.`name` SEPARATOR " "),'
      printf '"\\"")'
      printf ' FROM `architectures`'
      printf ' GROUP BY "1";\n'
      printf 'SELECT REPLACE(CONCAT('
      printf '"architecture_ids__",'
      printf '`architectures`.`name`,"=",'
      printf '`architectures`.`id`'
      printf '),"-","_")'
      printf ' FROM `architectures`;\n'
      printf 'SELECT CONCAT('
      printf '"non_any_architecture_ids=",'
      printf 'GROUP_CONCAT('
      printf '`architectures`.`id`'
      printf ' ORDER BY `architectures`.`id`'
      printf '))'
      printf ' FROM `architectures`'
      printf ' WHERE `architectures`.`name`!="any";\n'
      printf 'SELECT REPLACE(CONCAT('
      printf '"repository_ids__",'
      printf '`architectures`.`name`,"_",'
      printf '`repositories`.`name`,"=",'
      printf '`repositories`.`id`'
      printf '),"-","_")'
      printf ' FROM `repositories`'
      mysql_join_repositories_architectures
      printf ';\n'
      printf 'SELECT CONCAT('
      printf '"upstream_repository_names=\\"",'
      printf 'GROUP_CONCAT(`upstream_repositories`.`name` SEPARATOR " "),'
      printf '"\\"")'
      printf ' FROM `upstream_repositories`'
      printf ' GROUP BY "1";\n'
      printf 'SELECT CONCAT('
      printf '"repo_names=\\"",'
      printf 'GROUP_CONCAT(`git_repositories`.`name` SEPARATOR " "),'
      printf '"\\"")'
      printf ' FROM `git_repositories`'
      printf ' GROUP BY "1";\n'
      printf 'SELECT CONCAT('
      printf '"repo_paths__",'
      printf '`git_repositories`.`name`,"=",'
      printf '"\\"",`git_repositories`.`directory`,"\\";'
      printf 'repo_heads__",'
      printf '`git_repositories`.`name`,"=",'
      printf '"\\"",`git_repositories`.`head`,"\\"")'
      printf ' FROM `git_repositories`;\n'
      printf 'SELECT CONCAT('
      printf '"repository_stability_ids__",'
      printf '`repository_stabilities`.`name`,"=",'
      printf '`repository_stabilities`.`id`)'
      printf ' FROM `repository_stabilities`;\n'
      printf 'SELECT REPLACE(CONCAT('
      printf '"compression_ids__",'
      printf 'REPLACE('
        printf '`compressions`.`suffix`,".","_"'
      printf '),"=",'
      printf '`compressions`.`id`'
      printf '),"-","_")'
      printf ' FROM `compressions`;\n'
      printf 'SELECT CONCAT('
      printf '"package_compression_suffix_regex=\\"",'
      printf 'GROUP_CONCAT('
        printf 'REPLACE('
          printf '`compressions`.`suffix`,'
          printf '".","\\\\."'
      printf ') SEPARATOR "\\\\|"),'
      printf '"\\""'
      printf ') FROM `compressions`;\n'
    } | \
      mysql_run_query
  )"
}

# mysql_query_and_delete_unneeded_binary_packages
# print a query which lists and deletes binary_packages which are not
# linked from binary_packages_in_repositories
mysql_query_and_delete_unneeded_binary_packages() {
  printf 'SELECT CONCAT("pool/",'
  mysql_package_name_query
  printf ') FROM `binary_packages`'
  mysql_join_binary_packages_architectures
  printf ' LEFT'
  mysql_join_binary_packages_compressions
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `binary_packages_in_repositories`'
    printf ' WHERE `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
  printf ');\n'
  printf 'DELETE `binary_packages`'
  printf ' FROM `binary_packages`'
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1'
    printf ' FROM `binary_packages_in_repositories`'
    printf ' WHERE `binary_packages_in_repositories`.`package`=`binary_packages`.`id`'
  printf ');\n'
}

# mysql_sort_versions [-f]
# sort the table of versions, iff versions without order exist or -f was
# given

mysql_sort_versions() {
  ( # new shell is intentional
    tmp_file=$(mktemp 'tmp.mysql_sort_versions.XXXXXXXXXX' --tmpdir)
    trap 'rm "${tmp_file}"' EXIT
    if [ "x$1" != 'x-f' ] && \
      {
        printf 'SELECT'
        printf ' COUNT(1)'
        printf ' FROM `versions`'
        printf ' WHERE `versions`.`order` IS NULL'
      } | \
        mysql_run_query | \
        grep -qxF '0'; then
      # nothing to do
      exit 0
    fi
    {
      printf 'SELECT '
      printf '`versions`.`%s`,' \
        'id' \
        'epoch' \
        'version' | \
        sed 's/,$//'
      printf ' FROM `versions`;\n'
    } | \
      mysql_run_query | \
      expand_version 3 | \
      sort -k2n,2 -k3V,3 | \
      cut -f1 | \
      cat -n | \
      awk '{print $1 " " $2}' | \
      tr ' ' '\t' > \
      "${tmp_file}"
    max_order=$(
      {
        printf 'SELECT'
        printf ' GREATEST(%s,' \
          "$(
            wc -l < "${tmp_file}"
          )"
        printf 'MAX(`versions`.`order`))'
        printf ' FROM `versions`;\n'
      } | \
        mysql_run_query
    )
    {
      printf 'CREATE TEMPORARY TABLE `vs`('
        printf '`id` BIGINT,'
        printf '`ord` BIGINT,'
        printf 'UNIQUE KEY(`id`),'
        printf 'UNIQUE KEY(`ord`)'
      printf ');\n'
      printf 'LOAD DATA LOCAL INFILE "%s" INTO TABLE `vs`(`ord`,`id`);\n' \
        "${tmp_file}"
      printf 'UPDATE `versions`'
      printf ' SET `versions`.`order`=`versions`.`order`+1+%s;\n' \
        "${max_order}"
      printf 'COMMIT;\n'
      printf 'UPDATE `versions`'
      printf ' JOIN `vs`'
      printf ' ON `vs`.`id`=`versions`.`id`'
      printf ' SET `versions`.`order`=`vs`.`ord`;\n'
    } | \
      mysql_run_query
  )
}

# mysql_load_min_and_max_versions
# load the minimal and maximal versions into min_version and max_version

mysql_load_min_and_max_versions() {
  if [ -z "${min_version}" ]; then
    min_version=$(
      {
        printf 'SELECT CONCAT(`versions`.`epoch`,":",`versions`.`version`)'
        printf ' FROM `versions`'
        printf ' WHERE `versions`.`order` IS NOT NULL'
        printf ' ORDER BY `versions`.`order`'
        printf ' LIMIT 1'
      } | \
      mysql_run_query
    )
  fi

  if [ -z "${max_version}" ]; then
    max_version=$(
      {
        printf 'SELECT CONCAT(`versions`.`epoch`,":",`versions`.`version`)'
        printf ' FROM `versions`'
        printf ' WHERE `versions`.`order` IS NOT NULL'
        printf ' ORDER BY `versions`.`order` DESC'
        printf ' LIMIT 1'
      } | \
      mysql_run_query
    )
  fi

  if [ -z "${min_version_id}" ]; then
    min_version_id=$(
      {
        printf 'SELECT `versions`.`id`'
        printf ' FROM `versions`'
        printf ' WHERE `versions`.`order` IS NOT NULL'
        printf ' ORDER BY `versions`.`order`'
        printf ' LIMIT 1'
      } | \
      mysql_run_query
    )
  fi

  if [ -z "${max_version_id}" ]; then
    max_version_id=$(
      {
        printf 'SELECT `versions`.`id`'
        printf ' FROM `versions`'
        printf ' WHERE `versions`.`order` IS NOT NULL'
        printf ' ORDER BY `versions`.`order` DESC'
        printf ' LIMIT 1'
      } | \
      mysql_run_query
    )
  fi
}

# mysql_query_remove_old_binary_packages_from_build_list
# print a query to remove old versions of packages from the build-list
mysql_query_remove_old_binary_packages_from_build_list() {
  printf 'DELETE `d_bpir`'
  printf ' FROM `binary_packages_in_repositories` AS `d_bpir`'
  mysql_join_binary_packages_in_repositories_binary_packages 'd_bpir' 'd_bp'
  printf ' JOIN `versions` as `d_v`'
  printf ' ON `d_bp`.`epoch`=`d_v`.`epoch`'
  printf ' AND `d_bp`.`pkgver`=`d_v`.`version`'
  printf ' JOIN `binary_packages` AS `n_bp`'
  printf ' ON `d_bp`.`pkgname`=`n_bp`.`pkgname`'
  printf ' JOIN `versions` as `n_v`'
  printf ' ON `n_bp`.`epoch`=`n_v`.`epoch`'
  printf ' AND `n_bp`.`pkgver`=`n_v`.`version`'
  mysql_join_binary_packages_binary_packages_in_repositories 'n_bp' 'n_bpir'
  printf ' WHERE `d_bpir`.`repository`=%s' \
    "${repository_ids__any_build_list}"
  printf ' AND `n_bpir`.`repository`=%s' \
    "${repository_ids__any_build_list}"
  printf ' AND ('
    printf '`n_v`.`order`>`d_v`.`order`'
    printf ' OR ('
      printf '`n_v`.`order`=`d_v`.`order`'
      printf ' AND ('
        printf '`n_bp`.`pkgrel`>`d_bp`.`pkgrel`'
        printf ' OR ('
          printf '`n_bp`.`pkgrel`=`d_bp`.`pkgrel`'
          printf ' AND `n_bp`.`sub_pkgrel`>`d_bp`.`sub_pkgrel`'
        printf ')'
      printf ')'
    printf ')'
  printf ');\n'

  printf 'COMMIT;\n'

  mysql_query_and_delete_unneeded_binary_packages | \
    grep -v '^SELECT '
}

# mysql_query_ordering_correct dependency.version.order install_target_provider.version.order dependency.version_relation
# print a query yielding wether the ordering is correct
mysql_query_ordering_correct() {
  local relation
  printf '('
    for relation in '<' '<=' '>' '>=' '='; do
      printf '('
        printf '%s="%s"' \
          "${3}" \
          "${relation}"
        printf ' AND %s%s%s' \
          "${2}" \
          "${relation}" \
          "${1}"
      printf ') OR '
    done | \
      sed '
        s/ OR $//
      '
  printf ')'
}

# mysql_determine_majority_build_slave_architecture_id
# retreive the `architectures`.`id` of the majority of the currently
# active build slaves
mysql_determine_majority_build_slave_architecture_id() {
  {
    printf 'SELECT'
    printf ' COUNT(DISTINCT `ssh_log`.`id`) AS `count`,'
    printf '`architectures`.`id`'
    printf ' FROM `ssh_log`'
    printf ' JOIN `architectures`'
    printf ' ON `ssh_log`.`parameters`=CONCAT(`architectures`.`name`," ")'
    printf ' WHERE `action`="get-assignment"'
    printf ' AND `ssh_log`.`date`>ADDTIME(NOW(),"-1 00:00:00")'
    printf ' GROUP BY `ssh_log`.`parameters`'
    printf ' ORDER BY `count` DESC'
    printf ' LIMIT 1'
  } | \
    mysql_run_query | \
    cut -f2
}

# mysql_query_delete_packages $package_sources_condition $repositories_condition

# print a mysql query for deleting:
#  - binary_packages_in_repositories matching $condition
#  - binary_packages linked to *only* deleted
#    binary_packages_in_repositories
#  - build_assignments linked to *only* deleted binary_packages
#  - package_sources linked to *only* deleted build_assignments
# This is done in reverse order to not loose the handle on the
# to-be-deleted binary_packages_in_repositories
mysql_query_delete_packages() {
  local depth package_sources_condition repositories_condition
  package_sources_condition="$1"
  repositories_condition="$2"
  for depth in 4 3 2 1; do
    printf 'DELETE '
    if [ ${depth} -ge 4 ]; then
      # try to delete package_sources, too
      printf '`package_sources`,'
    fi
    if [ ${depth} -ge 3 ]; then
      # try to delete build_assignments, too
      printf '`build_assignments`,'
    fi
    if [ ${depth} -ge 2 ]; then
      # try to delete binary_packages, too
      printf '`binary_packages`,'
    fi
    printf '`binary_packages_in_repositories`'
    printf ' FROM `repositories`'
    mysql_join_repositories_binary_packages_in_repositories
    printf ' AND %s' \
      "${repositories_condition}"
    mysql_join_binary_packages_in_repositories_binary_packages
    mysql_join_binary_packages_build_assignments
    mysql_join_build_assignments_package_sources
    printf ' AND %s' \
      "${package_sources_condition}"
    case "${depth}" in
      '2')
        printf ' LEFT JOIN ('
          printf '`binary_packages_in_repositories` AS `keep_bpir`'
          mysql_join_binary_packages_in_repositories_repositories 'keep_bpir' 'keep_r'
          printf ' AND %s' "${repositories_condition}" \
          | sed '
            s/`repositories`/`keep_r`/g
          '
        printf ') ON `keep_bpir`.`package`=`binary_packages`.`id`'
      ;;
      '3')
        printf ' LEFT JOIN ('
          printf '`binary_packages` AS `keep_bp`'
          mysql_join_binary_packages_binary_packages_in_repositories 'keep_bp' 'keep_bpir'
          mysql_join_binary_packages_in_repositories_repositories 'keep_bpir' 'keep_r'
          printf ' AND NOT (%s)' "${repositories_condition}" \
          | sed '
            s/`repositories`/`keep_r`/g
          '
        printf ') ON `keep_bp`.`build_assignment`=`build_assignments`.`id`'
      ;;
      '4')
        printf ' LEFT JOIN ('
          printf '`build_assignments` AS `keep_ba`'
          mysql_join_build_assignments_binary_packages 'keep_ba' 'keep_bp'
          mysql_join_binary_packages_binary_packages_in_repositories 'keep_bp' 'keep_bpir'
          mysql_join_binary_packages_in_repositories_repositories 'keep_bpir' 'keep_r'
          printf ' AND %s' "${repositories_condition}" \
          | sed '
            s/`repositories`/`keep_r`/g
          '
        printf ') ON `keep_ba`.`package_source`=`package_sources`.`id`'
      ;;
    esac
    if [ ${depth} -ge 2 ]; then
      # only if all packages unbuilt or on the deletion-list
      printf ' WHERE `keep_r`.`id` IS NULL'
    fi
    printf ';\n'
  done
}

# mysql_create_build_environment_packages
# create and populate the `build_environment_packages`(`binary_packages`.`id`,`architectures`.`id`) table
# which lists all the packages, that can be installed in the build environments
# (possibly ignoring dependencies)
mysql_create_build_environment_packages() {
  printf 'CREATE TEMPORARY TABLE `build_environment_packages`('
    printf '`package` BIGINT NOT NULL,'
    printf '`architecture` SMALLINT NOT NULL,'
    printf 'KEY `package`(`package`),'
    printf 'KEY `architecture`(`architecture`),'
    printf 'UNIQUE KEY `content`(`package`,`architecture`)'
  printf ');\n'
  printf 'INSERT IGNORE INTO `build_environment_packages`('
    printf '`package`,'
    printf '`architecture`'
  printf ') SELECT'
  printf ' `binary_packages`.`id`,'
  printf '`repositories`.`architecture`'
  printf ' FROM `binary_packages`'
  mysql_join_binary_packages_binary_packages_in_repositories
  mysql_join_binary_packages_in_repositories_repositories
  printf ' AND `repositories`.`is_on_master_mirror`'
  printf ' LEFT JOIN ('
    printf '`binary_packages` AS `newer_bp`'
    mysql_join_binary_packages_binary_packages_in_repositories 'newer_bp' 'newer_bpir'
    mysql_join_binary_packages_in_repositories_repositories 'newer_bpir' 'newer_r'
    printf ' AND `newer_r`.`is_on_master_mirror`'
    printf ' JOIN `repository_stability_relations`'
    printf ' ON `repository_stability_relations`.`less_stable`=`newer_r`.`stability`'
    printf ' AND `repository_stability_relations`.`less_stable`!=`repository_stability_relations`.`more_stable`'
  printf ')'
  printf ' ON `newer_bp`.`pkgname`=`binary_packages`.`pkgname`'
  printf ' AND `newer_r`.`architecture`=`repositories`.`architecture`'
  printf ' AND `repository_stability_relations`.`more_stable`=`repositories`.`stability`'
  printf ' WHERE `newer_bp`.`id` IS NULL;\n'
}