summaryrefslogtreecommitdiff
path: root/bin/bootstrap-mysql
blob: 351971b393fe804b886eaf828a1c118b184ad900 (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
#!/bin/sh

# shellcheck disable=SC2119,SC2120

# shellcheck source=../lib/load-configuration
. "${0%/*}/../lib/load-configuration"

exec 9> "${build_list_lock_file}"
if ! verbose_flock -n 9; then
  >&2 echo 'Cannot get build-list lock.'
  exit 1
fi
exec 8> "${sanity_check_lock_file}"
if ! verbose_flock -s -n 9; then
  >&2 echo 'Cannot get sanity-check lock.'
  exit 1
fi
exec 7> "${package_database_lock_file}"
if ! verbose_flock -s -n 7; then
  >&2 echo 'Cannot get package-database lock.'
  exit 1
fi

#shellcheck disable=SC2016
{
  # calculate_maximal_moveable_set
  # stores results in `moveable_bpir` and `replaced_bpir`
  # Give a maximal list of packages to be moved, while implementing the
  # condition from db-update:

  # Every package which is replaced[1], must have its provided install_targets:
  #   a) provided by another moved or not-replaced package or
  #   b) not required by any not-replaced package.

  # Every package being moved needs to have all dependencies
  # installable in the target repository.

  printf 'DELIMITER //\n'
  printf 'DROP PROCEDURE IF EXISTS `calculate_maximal_moveable_set`//\n'
  printf 'CREATE PROCEDURE `calculate_maximal_moveable_set`(IN `from_stability` MEDIUMINT)\n'
  printf 'BEGIN\n'
  # variable to store count of changed rows
  printf 'DECLARE row_count_saved INT DEFAULT 0;\n'
  for copy in '' '_copy' '_copy2'; do
    printf 'DROP TEMPORARY TABLE IF EXISTS `%s_bpir%s`;\n' \
      'moveable' "${copy}" \
      'replaced' "${copy}"
    printf 'CREATE TEMPORARY TABLE `replaced_bpir%s` (`id` BIGINT, `replaced_by` BIGINT, UNIQUE KEY (`id`));\n' \
      "${copy}"
    if [ "${copy}" = '_copy2' ]; then
      continue
    fi
    printf 'CREATE TEMPORARY TABLE `moveable_bpir%s` (`id` BIGINT, `to_repository` MEDIUMINT, UNIQUE KEY (`id`));\n' \
      "${copy}"
  done
  printf 'DROP TEMPORARY TABLE IF EXISTS `package_blobs`;\n'
  printf 'CREATE TEMPORARY TABLE `package_blobs` (`ps_a` BIGINT, `ps_b` BIGINT, UNIQUE KEY `content` (`ps_a`,`ps_b`));\n'
  # all packages being vaguely relevant enter `package_blobs`, because
  # they might block any other package from moving
  printf 'INSERT IGNORE INTO `package_blobs` (`ps_a`,`ps_b`)'
  printf ' SELECT `a_ps`.`id`,`b_ps`.`id`'
  printf ' FROM `package_sources` AS `a_ps`'
  printf ' JOIN `package_sources` AS `b_ps`'
  printf ' ON UNIX_TIMESTAMP(`a_ps`.`commit_time`) - UNIX_TIMESTAMP(`b_ps`.`commit_time`) BETWEEN -10 AND 10'
  mysql_join_package_sources_build_assignments 'a_ps' 'a_ba'
  mysql_join_package_sources_build_assignments 'b_ps' 'b_ba'
  mysql_join_build_assignments_binary_packages 'a_ba' 'a_bp'
  mysql_join_build_assignments_binary_packages 'b_ba' 'b_bp'
  mysql_join_binary_packages_binary_packages_in_repositories 'a_bp' 'a_bpir'
  mysql_join_binary_packages_in_repositories_repositories 'a_bpir' 'a_r'
  mysql_join_binary_packages_binary_packages_in_repositories 'b_bp' 'b_bpir'
  mysql_join_binary_packages_in_repositories_repositories 'b_bpir' 'b_r'
  printf ' WHERE `a_r`.`stability` = `from_stability`'
  printf ' AND `b_r`.`stability` = `from_stability`;\n'
  # these packages are considered for moving:
  printf 'INSERT IGNORE INTO `moveable_bpir` (`id`,`to_repository`)'
  printf ' SELECT `binary_packages_in_repositories`.`id`,`repository_moves`.`to_repository`'
  printf ' FROM `binary_packages_in_repositories`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_in_repositories_repositories
  mysql_join_repositories_repository_stabilities
  mysql_join_binary_packages_build_assignments
  mysql_join_build_assignments_package_sources
  mysql_join_package_sources_upstream_repositories
  mysql_join_upstream_repositories_repository_moves
  printf ' AND `repository_moves`.`from_repository`=`binary_packages_in_repositories`.`repository`'
  # correct stability: "testing"/"staging" - as chosen
  printf ' WHERE `repository_stabilities`.`id`=`from_stability`'
  printf ' AND ('
    # this should not be replaced by ${repository_stability_ids__staging},
    # because it may change
    printf '`repository_stabilities`.`name`="staging"'
    # "testing" packages must be tested
    printf ' OR `binary_packages`.`is_tested`'
  printf ')'
  # no open issues
  printf ' AND NOT `binary_packages`.`has_issues`'
  printf ';\n'

  # these packages are considered for being replaced:
  # for each moved package
  printf 'INSERT IGNORE INTO `replaced_bpir` (`id`,`replaced_by`)'
  printf ' SELECT `r_bpir`.`id`,`m_bpir`.`id`'
  printf ' FROM `moveable_bpir`'
  printf ' JOIN `binary_packages_in_repositories` AS `m_bpir` ON `m_bpir`.`id`=`moveable_bpir`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages 'm_bpir' 'm_bp'
  # in all repositories more stable than its target repository
  printf ' JOIN `repositories` AS `m_to_r` ON `moveable_bpir`.`to_repository`=`m_to_r`.`id`'
  printf ' JOIN `repository_stability_relations` AS `rsr`'
  printf ' ON `rsr`.`less_stable`=`m_to_r`.`stability`'
  printf ' JOIN `repositories` AS `r_r`'
  printf ' ON `rsr`.`more_stable`=`r_r`.`stability`'
  printf ' AND `r_r`.`architecture`=`m_to_r`.`architecture`'
  mysql_join_repositories_binary_packages_in_repositories 'r_r' 'r_bpir'
  mysql_join_binary_packages_in_repositories_binary_packages 'r_bpir' 'r_bp'
  # all packages with identical names
  printf ' AND `r_bp`.`pkgname`=`m_bp`.`pkgname`;\n'

  # create copies of our temporary tables *yuck*
  for table in 'replaced' 'moveable'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DELETE FROM `%s_bpir_copy%s`;\n' "${table}" "${copy}"
      printf 'INSERT IGNORE INTO `%s_bpir_copy%s`' "${table}" "${copy}"
      printf ' SELECT `%s_bpir`.*' "${table}"
      printf ' FROM `%s_bpir`;\n' "${table}"
    done
  done

  # only the least stable of equally named to-be-replaced packages is
  # kept
  printf 'DELETE `replaced_bpir` FROM `replaced_bpir`'
  printf ' JOIN `binary_packages_in_repositories` AS `not_r_bpir`'
  printf ' ON `not_r_bpir`.`id`=`replaced_bpir`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages 'not_r_bpir' 'not_r_bp'
  mysql_join_binary_packages_in_repositories_repositories 'not_r_bpir' 'not_r_r'
  printf ' JOIN `binary_packages` AS `r_bp`'
  printf ' ON `r_bp`.`pkgname`=`not_r_bp`.`pkgname`'
  printf ' AND `r_bp`.`id`!=`not_r_bp`.`id`'
  mysql_join_binary_packages_binary_packages_in_repositories 'r_bp' 'r_bpir'
  printf ' JOIN `replaced_bpir_copy`'
  printf ' ON `r_bpir`.`id`=`replaced_bpir_copy`.`id`'
  mysql_join_binary_packages_in_repositories_repositories 'r_bpir' 'r_r'
  printf ' AND `not_r_r`.`architecture`=`r_r`.`architecture`'
  printf ' JOIN `repository_stability_relations` AS `rsr`'
  printf ' ON `rsr`.`less_stable`=`r_r`.`stability`'
  printf ' AND `rsr`.`more_stable`=`not_r_r`.`stability`;\n'

  # now we delete all unreplaceable and unmoveable packages from the respective
  # list until no further changes are required
  printf 'REPEAT\n'
  printf 'SET row_count_saved = 0;\n'

  # create copies of our temporary tables *yuck*
  for table in 'replaced' 'moveable'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DELETE FROM `%s_bpir_copy%s`;\n' "${table}" "${copy}"
      printf 'INSERT IGNORE INTO `%s_bpir_copy%s`' "${table}" "${copy}"
      printf ' SELECT `%s_bpir`.*' "${table}"
      printf ' FROM `%s_bpir`;\n' "${table}"
    done
  done
  # a package is not moveable if its dependencies are not provided ...
  printf 'DELETE `replaced_bpir`,`moveable_bpir`'
  printf ' FROM `replaced_bpir`'
  printf ' RIGHT JOIN `moveable_bpir`'
  printf ' ON `moveable_bpir`.`id`=`replaced_bpir`.`replaced_by`'
  printf ' JOIN `binary_packages_in_repositories` ON `binary_packages_in_repositories`.`id`=`moveable_bpir`.`id`'
  printf ' JOIN `repositories` AS `target_repositories` ON `moveable_bpir`.`to_repository`=`target_repositories`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_dependencies
  mysql_join_dependencies_dependency_types
  printf ' AND `dependency_types`.`relevant_for_binary_packages`'
  # ... by a not-deleted, "more stable" package already in place or ...
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1 FROM `install_target_providers`'
    mysql_join_install_target_providers_binary_packages '' 'prov_bp'
    mysql_join_binary_packages_binary_packages_in_repositories 'prov_bp' 'prov_bpir'
    mysql_join_binary_packages_in_repositories_repositories 'prov_bpir' 'prov_r'
    printf ' JOIN `repository_stability_relations` ON `prov_r`.`stability`=`repository_stability_relations`.`more_stable`'
    printf ' WHERE `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
    printf ' AND `target_repositories`.`stability`=`repository_stability_relations`.`less_stable`'
    printf ' AND NOT EXISTS ('
      printf 'SELECT 1 FROM `replaced_bpir_copy`'
      printf ' WHERE `replaced_bpir_copy`.`id`=`prov_bpir`.`id`'
    printf ')'
  # ... by a moved package
  printf ') AND NOT EXISTS ('
    printf 'SELECT 1 FROM `install_target_providers`'
    mysql_join_install_target_providers_binary_packages_in_repositories '' 'itp_bpir'
    printf ' JOIN `moveable_bpir_copy` ON `moveable_bpir_copy`.`id`=`itp_bpir`.`id`'
    printf ' WHERE `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
  printf ');\n'
  printf 'SET row_count_saved = row_count_saved + ROW_COUNT();\n'

  # refresh copies of our temporary tables *yuck*
  for table in 'replaced' 'moveable'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DELETE FROM `%s_bpir_copy%s`;\n' "${table}" "${copy}"
      printf 'INSERT IGNORE INTO `%s_bpir_copy%s`' "${table}" "${copy}"
      printf ' SELECT `%s_bpir`.*' "${table}"
      printf ' FROM `%s_bpir`;\n' "${table}"
    done
  done

  # packages which should not be replaced: ones providing something that is:
  # a) still needed by a not-replaced package x "less stable" than the target repository and
  # b) not provided by:
  #   1) a moved package or
  #   2) any current, not-replaced package in a repository more stable than x's repository
  #
  # Note, that this is not 100% clean from an academic point of view:
  # It might require _downgrading_ of a package to keep fulfilling a dependency
  # and it might require to do so _without_ any chance for the user to notice,
  # because there may be more "dependencies" in the database than in the package files.
  #
  # However, in practice both should not happen.
  #
  printf 'DELETE `replaced_bpir`,`moveable_bpir` FROM `replaced_bpir`'
  printf ' JOIN `moveable_bpir` ON `replaced_bpir`.`replaced_by`=`moveable_bpir`.`id`'
  printf ' JOIN `binary_packages_in_repositories` AS `repl_bpir` ON `repl_bpir`.`id`=`replaced_bpir`.`id`'
  mysql_join_binary_packages_in_repositories_install_target_providers 'repl_bpir'
  mysql_join_binary_packages_in_repositories_repositories 'repl_bpir' 'repl_r'
  mysql_join_install_target_providers_dependencies
  printf ' AND NOT EXISTS ('
    # dependencies of replaced packages don't matter
    printf 'SELECT 1 FROM `replaced_bpir_copy`'
    printf ' JOIN `binary_packages_in_repositories` AS `repl_bpir_copy` ON `repl_bpir_copy`.`id`=`replaced_bpir_copy`.`id`'
    mysql_join_binary_packages_in_repositories_repositories 'repl_bpir_copy' 'repl_r_copy'
    printf ' WHERE `repl_bpir_copy`.`package`=`dependencies`.`dependent`'
    printf ' AND `repl_r_copy`.`architecture`=`repl_r`.`architecture`'
  printf ')'
  mysql_join_dependencies_dependency_types
  # consider only runtime dependencies
  printf ' AND `dependency_types`.`relevant_for_binary_packages`'
  mysql_join_dependencies_binary_packages '' 'req_bp'
  # we need to check whether req_bp's dependency is (un)critical
  mysql_join_binary_packages_binary_packages_in_repositories 'req_bp' 'req_bpir'
  mysql_join_binary_packages_in_repositories_repositories 'req_bpir' 'req_r'
  printf ' AND `repl_r`.`architecture`=`req_r`.`architecture`'
  # dependent package is "less stable" than dependency
  printf ' JOIN `repository_stability_relations` AS `repl_rr`'
  printf ' ON `repl_rr`.`more_stable`=`repl_r`.`stability`'
  printf ' AND `repl_rr`.`less_stable`=`req_r`.`stability`'
  # a) ^
  printf ' WHERE NOT EXISTS ('
    # no moved package ...
    printf 'SELECT 1'
    printf ' FROM `moveable_bpir_copy`'
    printf ' JOIN `binary_packages_in_repositories` AS `subst_bpir` ON `subst_bpir`.`id`=`moveable_bpir_copy`.`id`'
    mysql_join_binary_packages_in_repositories_install_target_providers 'subst_bpir' 'subst_itp'
    mysql_join_binary_packages_in_repositories_repositories 'subst_bpir' 'subst_r'
    # ... provides the same
    printf ' WHERE `subst_itp`.`install_target`=`install_target_providers`.`install_target`'
    printf ' AND `subst_r`.`architecture`=`repl_r`.`architecture`'
  # b) 1) ^
  printf ') AND NOT EXISTS ('
    # no current package ...
    printf 'SELECT 1'
    printf ' FROM `binary_packages_in_repositories` AS `subst_bpir`'
    mysql_join_binary_packages_in_repositories_install_target_providers 'subst_bpir' 'subst_itp'
    # ... in a repository ...
    mysql_join_binary_packages_in_repositories_repositories 'subst_bpir' 'subst_r'
    # ... more stable ...
    printf ' JOIN `repository_stability_relations` AS `subst_rr`'
    printf ' ON `subst_rr`.`more_stable`=`subst_r`.`stability`'
    # ... than x's repository ...
    printf ' WHERE `subst_rr`.`less_stable`=`repl_r`.`stability`'
    # (architecture check is not necessary, as repositories of different
    # architectures are not less or more stable than each other)
    printf ' AND NOT EXISTS ('
    # ... and which is not replaced ...
      printf 'SELECT 1 FROM `replaced_bpir_copy2`'
      printf ' WHERE `replaced_bpir_copy2`.`id`=`subst_bpir`.`id`'
    printf ')'
    # ... and provides the same
    printf ' AND `subst_itp`.`install_target`=`install_target_providers`.`install_target`'
    # b) 2) ^
  printf ');\n'
  printf 'SET row_count_saved = row_count_saved + ROW_COUNT();\n'

  # refresh copies of our temporary tables *yuck*
  for table in 'replaced' 'moveable'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DELETE FROM `%s_bpir_copy%s`;\n' "${table}" "${copy}"
      printf 'INSERT IGNORE INTO `%s_bpir_copy%s`' "${table}" "${copy}"
      printf ' SELECT `%s_bpir`.*' "${table}"
      printf ' FROM `%s_bpir`;\n' "${table}"
    done
  done

  # remove all packages of a blob, where some part cannot be moved
  printf 'DELETE `replaced_bpir`,`moveable_bpir` FROM `replaced_bpir`'
  printf ' RIGHT JOIN `moveable_bpir`'
  printf ' ON `replaced_bpir`.`replaced_by`=`moveable_bpir`.`id`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `binary_packages_in_repositories`.`id`=`moveable_bpir`.`id`'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_build_assignments
  printf ' JOIN `package_blobs`'
  printf ' ON `build_assignments`.`package_source`=`package_blobs`.`ps_a`'
  printf ' JOIN `build_assignments` AS `bl_ba`'
  printf ' ON `bl_ba`.`package_source`=`package_blobs`.`ps_b`'
  mysql_join_build_assignments_binary_packages 'bl_ba' 'bl_bp'
  mysql_join_binary_packages_binary_packages_in_repositories 'bl_bp' 'bl_bpir'
  printf ' WHERE NOT EXISTS ('
    printf 'SELECT 1 FROM `moveable_bpir_copy`'
    printf ' WHERE `moveable_bpir_copy`.`id`=`bl_bpir`.`id`'
  printf ');\n'
  printf 'SET row_count_saved = row_count_saved + ROW_COUNT();\n'

  printf 'UNTIL row_count_saved=0\n'
  printf 'END REPEAT;\n'
  # we now need to remove the replaced_bpir which are not actually to be
  # replaced, but simply hidden

  # refresh copies of our temporary tables *yuck*
  for table in 'replaced' 'moveable'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DELETE FROM `%s_bpir_copy%s`;\n' "${table}" "${copy}"
      printf 'INSERT IGNORE INTO `%s_bpir_copy%s`' "${table}" "${copy}"
      printf ' SELECT `%s_bpir`.*' "${table}"
      printf ' FROM `%s_bpir`;\n' "${table}"
    done
  done

  printf 'DELETE `replaced_bpir` FROM `replaced_bpir`'
  printf ' JOIN `moveable_bpir`'
  printf ' ON `replaced_bpir`.`replaced_by`=`moveable_bpir`.`id`'
  printf ' JOIN `binary_packages_in_repositories` AS `r_bpir`'
  printf ' ON `replaced_bpir`.`id`=`r_bpir`.`id`'
  mysql_join_binary_packages_in_repositories_repositories 'r_bpir' 'r_r'
  printf ' JOIN `repositories` AS `m_to_r`'
  printf ' ON `m_to_r`.`id`=`moveable_bpir`.`to_repository`'
  printf ' WHERE `m_to_r`.`stability`!=`r_r`.`stability`;\n'

  for table in 'moveable' 'replaced'; do
    for copy in '' '2'; do
      if [ "${table}" = 'moveable' ] && \
        [ "${copy}" = '2' ]; then
        continue
      fi
      printf 'DROP TEMPORARY TABLE `%s_bpir_copy%s`;\n' \
        "${table}" "${copy}"
    done
  done
  printf 'END\n'
  printf '//\n'


  printf 'DROP PROCEDURE IF EXISTS `find_the_culprit`//\n'
  printf 'CREATE PROCEDURE `find_the_culprit`(IN `knot_id` BIGINT)\n'
  printf 'find_the_culprit:BEGIN\n'
  printf 'DECLARE max_relevance_level INT DEFAULT 0;\n'
  printf 'DECLARE min_edges_count INT DEFAULT 0;\n'

  printf 'CREATE TEMPORARY TABLE `todo_knots` ('
  printf '`knot_id` BIGINT NOT NULL,'
  printf '`edges_count` BIGINT,'
  printf '`irrelevant` BIT,'
  printf 'UNIQUE KEY `knot_id` (`knot_id`)'
  printf ');\n'

  printf 'UPDATE `knots`'
  printf ' SET `knots`.`reason_length`=NULL,'
  printf ' `knots`.`relevance_level`=NULL,'
  printf ' `knots`.`active`=1;\n'
  printf 'DELETE FROM `edges_copy`;\n'
  printf 'INSERT INTO `edges_copy` (`cause`,`impact`,`invert`)'
  printf ' SELECT `edges`.`cause`,'
  printf '`edges`.`impact`,'
  printf '`edges`.`invert`'
  printf ' FROM `edges`;\n'

  printf 'UPDATE `knots`'
  printf ' SET `knots`.`reason_length`=0,'
  printf '`knots`.`active`=1'
  printf ' WHERE `knots`.`id`=`knot_id`;\n'

  printf 'trial_loop: REPEAT\n'


# about which relevane_level should we care?

    # test here for contradictions: "exists" & "foreach" conditions
    printf 'SELECT max_relevance_level:=MIN(`i_k`.`relevance_level`)'
    printf ' FROM `knots` AS `i_k`'
    printf ' JOIN `edges`'
    printf ' ON `edges`.`impact`=`i_k`.`id`'
    printf ' JOIN `knots_copy` AS `c_k`'
    printf ' ON `edges`.`cause`=`c_k`.`id`'
    printf ' WHERE `i_k`.`reason_length` IS NOT NULL'
    printf ' AND `i_k`.'


    printf ''

    # start again if something was done
    printf 'IF (ROW_COUNT() != 0)'
      printf ' ITERATE `trial_loop`\n'
    printf 'END IF;\n'

    # if the proof is done, all the steps below will find nothing which
    # is still to be proven, and the loop will end

    printf 'UPDATE `knots_copy`'
    printf ' JOIN `knots`'
    printf ' ON `knots_copy`.`id`=`knots`.`id`'
    printf ' SET `knots_copy`.`reason_length`=`knots`.`reason_length`,'
    printf '`knots_copy`.`active`=`knots`.`active`,'
    printf '`knots_copy`.`relevance_level`=`knots`.`relevance_level`;\n'

    # set cause-knots of and = 1 / or = 0 (which is unambiguous => fast)
    printf 'SET row_count_saved = 0;\n'

    printf 'UPDATE `knots` AS `c_k`'
    printf ' JOIN `edges`'
    printf ' ON `edges`.`cause`=`c_k`.`id`'
    printf ' JOIN `knots_copy` AS `i_k`'
    printf ' ON `edges`.`impact`=`i_k`.`id`'
    printf ' SET `c_k`.`active`=(`i_k`.`active` XOR `edges`.`invert`),'
    printf '`c_k`.`reason_length`=`i_k`.`reason_length`+1,'
    printf '`c_k`.`relevance_level`=`i_k`.`relevance_level`'
    printf ' WHERE `c_k`.`reason_length` IS NULL'
    printf ' AND `i_k`.`reason_length` IS NOT NULL'
    printf ' AND (`i_k`.`and`=`i_k`.`active`);\n'

    # start again if something was done
    printf 'IF (ROW_COUNT() != 0)'
      printf ' ITERATE `trial_loop`\n'
    printf 'END IF;\n'

    printf 'SELECT max_relevance_level:=IFNULL(MAX(`knots`.`relevance_level`),0)'
    printf ' FROM `knots`'
    printf ' WHERE `knots`.`reason_length` IS NOT NULL;\n'

    # try through ambiguous knots:
    #   and = 0 with all reasoned causes = 1
    #   or = 1 with all reasoned causes = 0
    printf 'DELETE FROM `todo_knots`;\n'
    printf 'INSERT IGNORE INTO `todo_knots` (`knot_id`,`edges_count`,`irrelevant`)'
    printf ' SELECT'
    printf ' MIN('
      printf 'IF('
        printf '`c_k`.`reason_length` IS NULL,'
        printf '`c_k`.`id`,'
        printf 'NULL'
      printf ')'
    printf '),'
    printf 'SUM('
      printf 'IF('
        printf '`c_k`.`reason_length` IS NULL,'
        printf '1,'
        printf '0'
      printf ')'
    printf '),'
    printf 'IFNULL('
      printf 'MAX('
        printf 'IF('
          # is this knot already proven?
          printf '`c_k`.`reason_length` IS NOT NULL'
          printf ' AND (`c_k`.`active` XOR `edges`.`invert` = `i_k`.`active`)'
          printf '1,'
          printf '0'
        printf ')'
      printf '),'
      printf '0'
    printf ')'
    printf ' FROM `knots` AS `c_k`'
    printf ' JOIN `edges`'
    printf ' ON `edges`.`cause`=`c_k`.`id`'
    printf ' JOIN `knots_copy` AS `i_k`'
    printf ' ON `edges`.`impact`=`i_k`.`id`'
    printf ' WHERE `i_k`.`reason_length` IS NOT NULL'
    printf ' AND (`i_k`.`and` XOR `i_k`.`active`)'
    printf ' GROUP BY `i_k`.`id`;\n'

    printf 'SELECT min_edges_count:=MIN(`todo_knots`.`edges_count`)'
    printf ' FROM `todo_knots`'
    printf ' WHERE NOT `todo_knots`.`irrelevant`;\n'

    printf 'DELETE FROM `todo_knots`'
    printf ' WHERE `todo_knots`.`irrelevant`'
    printf ' OR `todo_knots`.`edges_count`!=min_edges_count;\n'

    printf 'UPDATE `knots` AS `c_k`'
    printf ' JOIN `todo_knots`'
    printf ' ON `todo_knots`.`knot_id`=`c_k`.`id`'
    printf ' JOIN `edges`'
    printf ' ON `edges`.`cause`=`c_k`.`id`'
    printf ' JOIN `knots_copy` AS `i_k`'
    printf ' ON `edges`.`impact`=`i_k`.`id`'
    printf ' SET `c_k`.`active`=(`i_k`.`active` XOR `edges`.`invert`),'
    printf '`c_k`.`reason_length`=`i_k`.`reason_length`+1,'
    printf '`c_k`.`relevance_level`=max_relevance_level+1+`c_k`.`id`'
    printf ';\n'

  # start again if something was done
  printf 'UNTIL ROW_COUNT() = 0\n'
  printf 'END REPEAT trial_loop;\n'

  printf 'DROP TEMPORARY TABLE `knots_copy`;\n'

  for copy in '' '_copy'; do
    printf 'CREATE TEMPORARY TABLE `knot_names%s` (' \
      "${copy}"
    printf '`id` BIGINT NOT NULL,'
    printf ' `name` VARCHAR (128),'
    printf ' `and` BIT,'
    printf ' `active` BIT,'
    printf ' `reason_length` MEDIUMINT,'
    printf ' UNIQUE KEY (`id`)'
    printf ');\n'
  done

  printf 'INSERT INTO `knot_names` (`id`,`name`,`reason_length`,`and`,`active`)'
  printf ' SELECT `knots`.`id`,'
  printf ' `install_targets`.`name`,'
  printf ' `knots`.`reason_length`,'
  printf ' `knots`.`and`,'
  printf ' `knots`.`active`'
  printf ' FROM `knots`'
  printf ' JOIN `install_targets`'
  printf ' ON `knots`.`content_id`=`install_targets`.`id`'
  printf ' AND `knots`.`content_type`="it";\n'

  printf 'INSERT INTO `knot_names` (`id`,`name`,`reason_length`,`and`,`active`)'
  printf ' SELECT `knots`.`id`,'
  printf ' CONCAT('
    printf '`r_a`.`name`,"/",'
    printf '`repositories`.`name`,"/",'
    mysql_package_name_query
  printf ') AS `name`,'
  printf ' `knots`.`reason_length`,'
  printf ' `knots`.`and`,'
  printf ' `knots`.`active`'
  printf ' FROM `knots`'
  printf ' JOIN `binary_packages_in_repositories`'
  printf ' ON `knots`.`content_id`=`binary_packages_in_repositories`.`id`'
  printf ' AND `knots`.`content_type`="bpir"'
  mysql_join_binary_packages_in_repositories_binary_packages
  mysql_join_binary_packages_in_repositories_repositories
  mysql_join_repositories_architectures '' 'r_a'
  mysql_join_binary_packages_architectures
  printf ';\n'

  printf 'INSERT INTO `knot_names_copy`'
  printf ' SELECT * FROM `knot_names`;\n'

  printf 'SELECT CONCAT('
    printf 'IF(`impact`.`and`,"(and) ","(or) "),'
    printf '`impact`.`name`,'
    printf '" ",IF(`impact`.`active`,"1","0"),'
    printf 'IF(`edges`.`invert`," -NOT-> "," --> "),'
    printf '`cause`.`name`,'
    printf '" ",IF(`cause`.`active`,"1","0")'
  printf ') FROM `edges`'
  printf ' JOIN `knot_names` AS `cause`'
  printf ' ON `cause`.`id`=`edges`.`cause`'
  printf ' JOIN `knot_names_copy` AS `impact`'
  printf ' ON `impact`.`id`=`edges`.`impact`'
  printf ' WHERE `cause`.`reason_length` IS NOT NULL'
  printf ' AND `impact`.`reason_length` IS NOT NULL;\n'

  printf 'DROP TEMPORARY TABLE `knot_names`;\n'

  printf 'END\n'
  printf '//\n'


  printf 'DROP PROCEDURE IF EXISTS `blacklist_packages`//\n'
  printf 'CREATE PROCEDURE `blacklist_packages`()\n'
  printf 'blacklist_packages:BEGIN\n'
  printf 'DECLARE row_count_saved INT DEFAULT 0;\n'

  for suffix in '' '_copy'; do
    printf 'CREATE TEMPORARY TABLE `bl%s` (' \
      "${suffix}"
      printf '`arch` SMALLINT,'
      printf '`pkgbase` VARCHAR(64),'
      printf 'UNIQUE KEY `content`(`arch`,`pkgbase`)'
    printf ');\n'
  done
  printf 'INSERT IGNORE INTO `bl`(`arch`,`pkgbase`)'
  printf ' SELECT'
  printf ' `architectures`.`id`,'
  printf '`blacklist`.`pkgbase`'
  printf ' FROM `blacklist`'
  printf ' JOIN `architectures` AS `bl_a`'
  printf ' ON `bl_a`.`name`=`blacklist`.`arch`'
  printf ' JOIN `architecture_compatibilities`'
  printf ' ON `architecture_compatibilities`.`runs_on`=`bl_a`.`id`'
  printf ' OR `bl_a`.`name`="any"'
  printf ' JOIN `architectures`'
  printf ' ON `architectures`.`id`=`architecture_compatibilities`.`built_for`'
  printf ' WHERE `architectures`.`name`!="any";\n'

  printf 'REPEAT\n'

    printf 'DELETE FROM `bl_copy`;\n'
    printf 'INSERT IGNORE INTO `bl_copy` (`arch`,`pkgbase`)'
    printf ' SELECT'
    printf ' `bl`.`arch`,'
    printf ' `bl`.`pkgbase`'
    printf ' FROM `bl`;\n'

    printf 'INSERT IGNORE INTO `bl` (`arch`,`pkgbase`)'
    printf ' SELECT'
    printf ' `a_r`.`architecture`,'
    printf '`a_ps`.`pkgbase`'
    printf ' FROM `package_sources` AS `a_ps`'
    mysql_join_package_sources_build_assignments 'a_ps' 'a_ba'
    mysql_join_build_assignments_binary_packages 'a_ba' 'a_bp'
    mysql_join_binary_packages_binary_packages_in_repositories 'a_bp' 'a_bpir'
    mysql_join_binary_packages_in_repositories_repositories 'a_bpir' 'a_r'
    mysql_join_repositories_architectures 'a_r' 'a_ra'
    mysql_join_binary_packages_dependencies 'a_bp'
    mysql_join_dependencies_dependency_types
    printf ' AND (`dependency_types`.`relevant_for_building`'
    printf ' OR `dependency_types`.`relevant_for_binary_packages`)'
    printf ' WHERE `a_ra`.`name`!="any"'
    # TODO: This should be corrected at the root: automatic install targets, which are bogus should
    #       not be added in the first place - but how do we detect that? -> add exceptions!
    printf ' AND EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `install_target_providers`'
      printf ' WHERE `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
    printf ') AND NOT EXISTS ('
      printf 'SELECT 1'
      printf ' FROM `install_target_providers`'
      printf ' WHERE NOT EXISTS ('
        printf 'SELECT 1'
        printf ' FROM `bl_copy`'
        printf ' JOIN `package_sources` AS `b_ps`'
        printf ' ON `bl_copy`.`pkgbase`=`b_ps`.`pkgbase`'
        mysql_join_package_sources_build_assignments 'b_ps' 'b_ba'
        mysql_join_build_assignments_binary_packages 'b_ba' 'b_bp'
        printf ' WHERE `install_target_providers`.`package`=`b_bp`.`id`'
      printf ')'
      printf ' AND `install_target_providers`.`install_target`=`dependencies`.`depending_on`'
    printf ');\n'

  printf 'UNTIL ROW_COUNT()=0\n'
  printf 'END REPEAT;\n'

  printf 'SELECT'
  printf ' `architectures`.`name`,'
  printf '`bl`.`pkgbase`'
  printf ' FROM `bl`'
  printf ' JOIN `architectures`'
  printf ' ON `architectures`.`id`=`bl`.`arch`'
  printf ';\n'

  printf 'DROP TEMPORARY TABLE `bl`;\n'

  printf 'END\n'
  printf '//\n'

  printf 'DELIMITER ;\n'

  if [ "x$1" != 'x-no-privileges' ]; then
    printf 'GRANT %s ON %s TO '"'"'buildmaster'"'"'@'"'"'localhost'"'"';\n' \
      'CREATE ROUTINE' 'buildmaster.*' \
      'CREATE TEMPORARY TABLES' 'buildmaster.*' \
      'EXECUTE' 'buildmaster.*' \
      'RELOAD' '*.*' \
      'SELECT' 'buildmaster.*' \
      'SELECT' 'mysql.proc' \
      'SHOW VIEW' 'buildmaster.*' \
      'UPDATE' 'buildmaster.*'
    printf 'GRANT %s ON %s TO '"'"'webserver'"'"'@'"'"'localhost'"'"';\n' \
      'CREATE TEMPORARY TABLES' 'buildmaster.*' \
      'EXECUTE' 'buildmaster.*' \
      'SELECT' 'buildmaster.*' \
      'SHOW VIEW' 'buildmaster.*'
    printf 'FLUSH PRIVILEGES;\n'
  fi
} | \
  mysql_run_query -u root -p