This file is indexed.

/usr/share/gforge/bin/db-upgrade.pl is in gforge-db-postgresql 5.1.1-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
#!/usr/bin/perl -w
#
# $Id$
#
# Debian-specific script to upgrade the database between releases
# Roland Mas <lolando@debian.org>

use strict ;
use diagnostics ;

use DBI ;
use MIME::Base64 ;
use HTML::Entities ;
use Digest::MD5 ;

use vars qw/$dbh @reqlist $query/ ;
use vars qw/$sys_default_domain $sys_scm_host $sys_download_host
    $sys_shell_host $sys_users_host $sys_docs_host $sys_lists_host
    $sys_dns1_host $sys_dns2_host $FTPINCOMING_DIR $FTPFILES_DIR
    $sys_urlroot $sf_cache_dir $sys_name $sys_themeroot
    $sys_news_group $sys_dbhost $sys_dbname $sys_dbuser $sys_dbpasswd
    $sys_ldap_base_dn $sys_ldap_host $admin_password
    $server_admin $domain_name $newsadmin_groupid $statsadmin_groupid
    $libdir $sqldir/ ;

require ("/etc/gforge/local.pl") ; 
$libdir="/usr/share/gforge/lib";
$sqldir="/usr/share/gforge/db";
require ("$libdir/sqlparser.pm") ; # Our magic SQL parser
require ("$libdir/sqlhelper.pm") ; # Our SQL functions
require ("$libdir/include.pl");  # Some other functions

&db_connect ;

$dbh->{AutoCommit} = 0;
$dbh->{RaiseError} = 1;
eval {
    my ($sth, @array, $version, $path, $target) ;

    # Do we have at least the basic schema?
    # Create Sourceforge database
    if (! &table_exists ($dbh, 'groups')) {	# No 'groups' table
	# Installing SF 2.6 from scratch
	&debug ("Creating initial Sourceforge database from files.") ;

	&create_metadata_table ("2.5.9999") ;

	$query = "SELECT count(*) from debian_meta_data where key = 'current-path'";
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	@array = $sth->fetchrow_array () ;
	$sth->finish () ;
	if ($array[0] == 0) {
	    $query = "INSERT INTO debian_meta_data (key, value) VALUES ('current-path', 'scratch-to-2.6')" ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	&debug ("...OK.") ;
	$dbh->commit () ;

    } else {			# A 'groups' table exists
	if (! &table_exists ($dbh, 'debian_meta_data')) {	# No 'debian_meta_data' table
	    # If we're here, we're upgrading from 2.5-7 or earlier
	    # We therefore need to create the table
	    &create_metadata_table ("2.5-7+just+before+8") ;
	}

	$version = &get_db_version ;
	if (&is_lesser ($version, "2.5.9999")) {
	    &debug ("Found an old (2.5) database, will upgrade to 2.6") ;

	    $query = "SELECT count(*) from debian_meta_data where key = 'current-path'";
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    @array = $sth->fetchrow_array () ;
	    $sth->finish () ;

	    if ($array[0] == 0) {
		$query = "INSERT INTO debian_meta_data (key, value) VALUES ('current-path', '2.5-to-2.6')" ;
		$sth = $dbh->prepare ($query) ;
		$sth->execute () ;
		$sth->finish () ;
		$dbh->commit () ;
	    }
	}
    }

    $query = "SELECT count(*) from debian_meta_data where key = 'current-path'";
    $sth = $dbh->prepare ($query) ;
    $sth->execute () ;
    @array = $sth->fetchrow_array () ;
    $sth->finish () ;

    if ($array[0] == 0) {
	$path = "" ;
    } else {
	$query = "SELECT value from debian_meta_data where key = 'current-path'";
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	@array = $sth->fetchrow_array () ;
	$sth->finish () ;

	$path = $array[0] ;
    }

  PATH_SWITCH: {
      ($path eq 'scratch-to-2.6') && do {
	  &update_with_sql ("sf-2.6-complete", "2.5.9999.1+global+data+done") ;

	  $version = &get_db_version ;
	  $target = "2.5.9999.2+local+data+done" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Adding local data.") ;

	      do "/etc/gforge/local.pl" or die "Cannot read /etc/gforge/local.pl" ;

	      my ($login, $md5pwd, $unixpwd, $email, $noreplymail, $date) ;

	      $login = 'admin' ;
	      $md5pwd = 'INVALID' ;
	      $unixpwd = 'INVALID' ;
	      $email = $server_admin ;
	      $noreplymail="noreply\@$domain_name" ;
	      $date = time () ;

	      @reqlist = (
			  "UPDATE groups SET homepage = '$domain_name/admin/' where group_id = 1",
			  "UPDATE groups SET homepage = '$domain_name/news/' where group_id = 2",
			  "UPDATE groups SET homepage = '$domain_name/stats/' where group_id = 3",
			  "UPDATE groups SET homepage = '$domain_name/peerrating/' where group_id = 4",
			  "UPDATE users SET email = '$noreplymail' where user_id = 100",
			  "INSERT INTO users VALUES (101,'$login','$email','$md5pwd','Sourceforge admin','A','/bin/bash','$unixpwd','N',2000,'shell',$date,'',1,0,NULL,NULL,0,'','GMT', 1, 0)", 
			  "SELECT setval ('\"users_pk_seq\"', 102, 'f')",
			  "INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (101, 1, 'A')",
			  "INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (101, 2, 'A')",
			  "INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (101, 3, 'A')",
			  "INSERT INTO user_group (user_id, group_id, admin_flags) VALUES (101, 4, 'A')"
			  ) ;

	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }
	      @reqlist = () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5.9999.3+skills+done" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Inserting skills.") ;

	      foreach my $skill (split m/;/, "Ada;C;C++;HTML;LISP;Perl;PHP;Python;SQL") {
		  push @reqlist, "INSERT INTO people_skill (name) VALUES ('$skill')" ;
	      }

	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }
	      @reqlist = () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.6-0+checkpoint+1" ;
	  if (&is_lesser ($version, $target)) {
	      $query = "DELETE FROM debian_meta_data WHERE key = 'current-path'" ;
	      # debug $query ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  last PATH_SWITCH ;
      } ;

      ($path eq '2.5-to-2.6') && do {

	  $version = &get_db_version ;
	  $target = "2.5-8" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Adding row to people_job_category.") ;
	      $query = "INSERT INTO people_job_category VALUES (100, 'Undefined', 0)" ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5-25" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Adding row to supported_languages.") ;
	      $query = "INSERT INTO supported_languages VALUES (15, 'Korean', 'Korean.class', 'Korean', 'kr')" ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5-27" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Fixing unix_box entries.") ;

	      $query = "update groups set unix_box = 'shell'" ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      $query = "update users set unix_box = 'shell'" ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &debug ("Also fixing a few sequences.") ;

	      &bump_sequence_to ($dbh, "bug_pk_seq", 100) ;
	      &bump_sequence_to ($dbh, "project_task_pk_seq", 100) ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

 	  $version = &get_db_version ;
 	  $target = "2.5-30" ;
 	  if (&is_lesser ($version, $target)) {
 	      &debug ("Adding rows to supported_languages.") ;
	      @reqlist = (
			  "INSERT INTO supported_languages VALUES (16,'Bulgarian','Bulgarian.class','Bulgarian','bg')",
			  "INSERT INTO supported_languages VALUES (17,'Greek','Greek.class','Greek','el')",
			  "INSERT INTO supported_languages VALUES (18,'Indonesian','Indonesian.class','Indonesian','id')",
			  "INSERT INTO supported_languages VALUES (19,'Portuguese (Brazillian)','PortugueseBrazillian.class','PortugueseBrazillian', 'br')",
			  "INSERT INTO supported_languages VALUES (20,'Polish','Polish.class','Polish','pl')",
			  "INSERT INTO supported_languages VALUES (21,'Portuguese','Portuguese.class','Portuguese', 'pt')",
			  "INSERT INTO supported_languages VALUES (22,'Russian','Russian.class','Russian','ru')"
			  ) ;

	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }
	      @reqlist = () ;

 	      &update_db_version ($target) ;
 	      &debug ("...OK.") ;
 	      $dbh->commit () ;
 	  }

	  $version = &get_db_version ;
	  $target = "2.5-32" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Fixing unix_uid entries.") ;

	      $query = "UPDATE users SET unix_uid = nextval ('unix_uid_seq') WHERE unix_status != 'N' AND status != 'P' AND unix_uid = 0" ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5.9999.1+temp+data+dropped" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Preparing to upgrade your database - dropping temporary tables") ;

	      my @tables = qw/ user_metric_tmp1_1 user_metric_tmp1_2
		  user_metric_tmp1_3 user_metric_tmp1_4
		  user_metric_tmp1_5 user_metric_tmp1_6
		  user_metric_tmp1_7 user_metric_tmp1_8 user_metric1
		  user_metric2 user_metric3 user_metric4 user_metric5
		  user_metric6 user_metric7 user_metric8
		  project_counts_tmp project_metric_tmp
		  project_metric_tmp1 project_counts_weekly_tmp
		  project_metric_weekly_tmp project_metric_weekly_tmp1
		  / ;

	      my @sequences = qw/ user_metric1_ranking_seq
		  user_metric2_ranking_seq user_metric3_ranking_seq
		  user_metric4_ranking_seq user_metric5_ranking_seq
		  user_metric6_ranking_seq user_metric7_ranking_seq
		  user_metric8_ranking_seq project_metric_weekly_seq
		  trove_treesum_trove_treesum_seq
		  project_metric_tmp1_pk_seq / ;

	      my @indexes = qw/ idx_project_metric_group
		  idx_project_metric_weekly_group
		  user_metric_history_date_userid / ;

	      foreach my $table (@tables) {
		  &drop_table_if_exists ($dbh, $table) ;
	      }

	      foreach my $sequence (@sequences) {
		  &drop_sequence_if_exists ($dbh, $sequence) ;
	      }

	      foreach my $index (@indexes) {
		  &drop_index_if_exists ($dbh, $index) ;
	      }

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5.9999.2+data+upgraded" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Upgrading your database scheme from 2.5") ;

	      @reqlist = (
		  "ALTER TABLE groups DROP CONSTRAINT groups_pkey",
		  "ALTER TABLE users DROP CONSTRAINT users_pkey",
		  ) ;
	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }

	      @reqlist = @{ &parse_sql_file ("$sqldir/sf2.5-to-sf2.6.sql") } ;
	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }
	      @reqlist = () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5.9999.3+artifact+transcoded" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Transcoding the artifact data fields") ;

	      $query = "SELECT id,bin_data FROM artifact_file ORDER BY id ASC" ;
	      # debug $query ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      while (@array = $sth->fetchrow_array) {
		  my $query2 = "UPDATE artifact_file SET bin_data='" ;
		  $query2 .= encode_base64 (decode_entities ($array [1])) ;
		  $query2 .= "' WHERE id=" ;
		  $query2 .= $array [0] ;
		  $query2 .= "" ;
		  # debug $query2 ;
		  my $sth2 =$dbh->prepare ($query2) ;
		  $sth2->execute () ;
		  $sth2->finish () ;
	      }
	      $sth->finish () ;

	      @reqlist = () ;
	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.5.9999.4+groups+inserted" ;
	  if (&is_lesser ($version, $target)) {
	      &debug ("Inserting missing groups") ;

	      @reqlist = (
			  "INSERT INTO groups (group_name, homepage,
                           is_public, status, unix_group_name,
                           unix_box, http_domain, short_description,
                           cvs_box, license, register_purpose,
                           license_other, register_time, rand_hash,
                           use_mail, use_survey, use_forum, use_pm,
                           use_cvs, use_news, type, use_docman,
                           new_task_address, send_all_tasks,
                           use_pm_depend_box)
       	                   VALUES ('Stats', '$domain_name/top/', 0,
       	    	           'A', 'stats', 'shell', NULL, NULL, 'cvs',
       	    	           'website', NULL, NULL, 0, NULL, 1, 0, 0, 0, 0,
       	    	           1, 1, 1, '', 0, 0)",
			  "INSERT INTO groups (group_name, homepage,
                           is_public, status, unix_group_name,
                           unix_box, http_domain, short_description,
                           cvs_box, license, register_purpose,
                           license_other, register_time, rand_hash,
                           use_mail, use_survey, use_forum, use_pm,
                           use_cvs, use_news, type, use_docman,
                           new_task_address, send_all_tasks,
                           use_pm_depend_box)
                           VALUES ('Peer Ratings', '$domain_name/people/', 0,
                           'A', 'peerrating', 'shell', NULL, NULL, 'cvs1',
                           'website', NULL, NULL, 0, NULL, 1, 0, 0, 0, 0,
                           1, 1, 0, '', 0, 0)"
			  ) ;

	      foreach my $s (@reqlist) {
		  $query = $s ;
		  # debug $query ;
		  $sth = $dbh->prepare ($query) ;
		  $sth->execute () ;
		  $sth->finish () ;
	      }
	      @reqlist = () ;
	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  $version = &get_db_version ;
	  $target = "2.6-0+checkpoint+1" ;
	  if (&is_lesser ($version, $target)) {
	      $query = "DELETE FROM debian_meta_data WHERE key = 'current-path'" ;
	      # debug $query ;
	      $sth = $dbh->prepare ($query) ;
	      $sth->execute () ;
	      $sth->finish () ;

	      &update_db_version ($target) ;
	      &debug ("...OK.") ;
	      $dbh->commit () ;
	  }

	  last PATH_SWITCH ;
      } ;
  } # PATH_SWITCH

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+2" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Updating permissions on system groups.") ;
	$query = "UPDATE groups SET group_name='Site Admin', is_public=1 WHERE group_id=1" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;
	$query = "UPDATE groups SET group_name='Site News Admin', is_public=1 WHERE group_id=$sys_news_group" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+3" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Creating table group_cvs_history.") ;
	$query = "CREATE TABLE group_cvs_history (
            id integer DEFAULT nextval('group_cvs_history_pk_seq'::text) NOT NULL,
            group_id integer DEFAULT '0' NOT NULL,
            user_name character varying(80) DEFAULT '' NOT NULL,
            cvs_commits integer DEFAULT '0' NOT NULL,
            cvs_commits_wk integer DEFAULT '0' NOT NULL,
            cvs_adds integer DEFAULT '0' NOT NULL,
            cvs_adds_wk integer DEFAULT '0' NOT NULL,
            PRIMARY KEY (id))";
    	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+4" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Registering Savannah themes.") ;

	$query = "SELECT max(theme_id) FROM themes" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	@array = $sth->fetchrow_array () ;
	$sth->finish () ;
	my $maxid = $array [0] ;

	&bump_sequence_to ($dbh, "themes_pk_seq", $maxid) ;

	@reqlist = (
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_codex', 'Savannah CodeX')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_forest', 'Savannah Forest')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_reverse', 'Savannah Reverse')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_sad', 'Savannah Sad')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_savannah', 'Savannah Original')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_slashd', 'Savannah SlashDot')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_startrek', 'Savannah StarTrek')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_transparent', 'Savannah Transparent')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_water', 'Savannah Water')",
		    "INSERT INTO themes (dirname, fullname) VALUES ('savannah_www.gnu.org', 'Savannah www.gnu.org')"
		    ) ;
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+5" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Registering yet another Savannah theme.") ;

	$query = "INSERT INTO themes (dirname, fullname) VALUES ('savannah_darkslate', 'Savannah Dark Slate')";
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+6" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Updating language codes.") ;

	@reqlist = (
		    "UPDATE supported_languages SET language_code='en' where classname='English'",
		    "UPDATE supported_languages SET language_code='ja' where classname='Japanese'",
		    "UPDATE supported_languages SET language_code='iw' where classname='Hebrew'",
		    "UPDATE supported_languages SET language_code='es' where classname='Spanish'",
		    "UPDATE supported_languages SET language_code='th' where classname='Thai'",
		    "UPDATE supported_languages SET language_code='de' where classname='German'",
		    "UPDATE supported_languages SET language_code='it' where classname='Italian'",
		    "UPDATE supported_languages SET language_code='no' where classname='Norwegian'",
		    "UPDATE supported_languages SET language_code='sv' where classname='Swedish'",
		    "UPDATE supported_languages SET language_code='zh' where classname='Chinese'",
		    "UPDATE supported_languages SET language_code='nl' where classname='Dutch'",
		    "UPDATE supported_languages SET language_code='eo' where classname='Esperanto'",
		    "UPDATE supported_languages SET language_code='ca' where classname='Catalan'",
		    "UPDATE supported_languages SET language_code='ko' where classname='Korean'",
		    "UPDATE supported_languages SET language_code='bg' where classname='Bulgarian'",
		    "UPDATE supported_languages SET language_code='el' where classname='Greek'",
		    "UPDATE supported_languages SET language_code='id' where classname='Indonesian'",
		    "UPDATE supported_languages SET language_code='pt' where classname='Portuguese (Brazillian)'",
		    "UPDATE supported_languages SET language_code='pl' where classname='Polish'",
		    "UPDATE supported_languages SET language_code='pt' where classname='Portuguese'",
		    "UPDATE supported_languages SET language_code='ru' where classname='Russian'",
		    "UPDATE supported_languages SET language_code='fr' where classname='French'"
		    ) ;
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;
	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+7" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Fixing artifact-related views.") ;

	&drop_view_if_exists ($dbh, "artifact_file_user_vw") ;
	&drop_view_if_exists ($dbh, "artifact_history_user_vw") ;
	&drop_view_if_exists ($dbh, "artifact_message_user_vw") ;
	&drop_view_if_exists ($dbh, "artifactperm_artgrouplist_vw") ;
	&drop_view_if_exists ($dbh, "artifactperm_user_vw") ;
	&drop_view_if_exists ($dbh, "artifact_vw") ;

	@reqlist = (
		    "CREATE VIEW artifact_file_user_vw as SELECT af.id, af.artifact_id, af.description, af.bin_data, af.filename, af.filesize, af.filetype, af.adddate, af.submitted_by, users.user_name, users.realname FROM artifact_file af, users WHERE (af.submitted_by = users.user_id)",
		    "CREATE VIEW artifact_history_user_vw as SELECT ah.id, ah.artifact_id, ah.field_name, ah.old_value, ah.entrydate, users.user_name FROM artifact_history ah, users WHERE (ah.mod_by = users.user_id)",
		    "CREATE VIEW artifact_message_user_vw as SELECT am.id, am.artifact_id, am.from_email, am.body, am.adddate, users.user_id, users.email, users.user_name, users.realname FROM artifact_message am, users WHERE (am.submitted_by = users.user_id)",
		    "CREATE VIEW artifactperm_artgrouplist_vw as SELECT agl.group_artifact_id, agl.name, agl.description, agl.group_id, ap.user_id, ap.perm_level FROM artifact_perm ap, artifact_group_list agl WHERE (ap.group_artifact_id = agl.group_artifact_id)",
		    "CREATE VIEW artifactperm_user_vw as SELECT ap.id, ap.group_artifact_id, ap.user_id, ap.perm_level, users.user_name, users.realname FROM artifact_perm ap, users WHERE (users.user_id = ap.user_id)",
		    "CREATE VIEW artifact_vw as SELECT artifact.artifact_id, artifact.group_artifact_id, artifact.status_id, artifact.category_id, artifact.artifact_group_id, artifact.resolution_id, artifact.priority, artifact.submitted_by, artifact.assigned_to, artifact.open_date, artifact.close_date, artifact.summary, artifact.details, u.user_name AS assigned_unixname, u.realname AS assigned_realname, u.email AS assigned_email, u2.user_name AS submitted_unixname, u2.realname AS submitted_realname, u2.email AS submitted_email, artifact_status.status_name, artifact_category.category_name, artifact_group.group_name, artifact_resolution.resolution_name FROM users u, users u2, artifact, artifact_status, artifact_category, artifact_group, artifact_resolution WHERE ((((((artifact.assigned_to = u.user_id) AND (artifact.submitted_by = u2.user_id)) AND (artifact.status_id = artifact_status.id)) AND (artifact.category_id = artifact_category.id)) AND (artifact.artifact_group_id = artifact_group.id)) AND (artifact.resolution_id = artifact_resolution.id))"
		    ) ;
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;
	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+8" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Adding integrity constraints between the Trove map tables.") ;

	@reqlist = (
		    "ALTER TABLE trove_group_link ADD CONSTRAINT tgl_group_id_fk FOREIGN KEY (group_id) REFERENCES groups(group_id) MATCH FULL",
		    "ALTER TABLE trove_group_link ADD CONSTRAINT tgl_cat_id_fk FOREIGN KEY (trove_cat_id) REFERENCES trove_cat(trove_cat_id) MATCH FULL",
		    "ALTER TABLE trove_agg ADD CONSTRAINT trove_agg_cat_id_fk FOREIGN KEY (trove_cat_id) REFERENCES trove_cat(trove_cat_id) MATCH FULL",
		    "ALTER TABLE trove_agg ADD CONSTRAINT trove_agg_group_id_fk FOREIGN KEY (group_id) REFERENCES groups(group_id) MATCH FULL",
		    "DELETE FROM trove_treesums WHERE trove_cat_id NOT IN (SELECT trove_cat_id FROM trove_cat)",
		    "ALTER TABLE trove_treesums ADD CONSTRAINT trove_treesums_cat_id_fk FOREIGN KEY (trove_cat_id) REFERENCES trove_cat(trove_cat_id) MATCH FULL",
		    ) ;
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;
	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+9" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Adding extra fields to the groups table.") ;

	@reqlist = (
		    "ALTER TABLE groups ADD COLUMN use_ftp integer",
		    "ALTER TABLE groups ALTER COLUMN use_ftp SET DEFAULT 1",
		    "UPDATE groups SET use_ftp = 1",
		    "ALTER TABLE groups ADD COLUMN use_tracker integer",
		    "ALTER TABLE groups ALTER COLUMN use_tracker SET DEFAULT 1",
		    "UPDATE groups SET use_tracker = 1",
		    "ALTER TABLE groups ADD COLUMN use_frs integer",
		    "ALTER TABLE groups ALTER COLUMN use_frs SET DEFAULT 1",
		    "UPDATE groups SET use_frs = 1",
		    "ALTER TABLE groups ADD COLUMN use_stats integer",
		    "ALTER TABLE groups ALTER COLUMN use_stats SET DEFAULT 1",
		    "UPDATE groups SET use_stats = 1",
		    "ALTER TABLE groups ADD COLUMN enable_pserver integer",
		    "ALTER TABLE groups ALTER COLUMN enable_pserver SET DEFAULT 1",
		    "UPDATE groups SET enable_pserver = 1",
		    "ALTER TABLE groups ADD COLUMN enable_anoncvs integer",
		    "ALTER TABLE groups ALTER COLUMN enable_anoncvs SET DEFAULT 1",
		    "UPDATE groups SET enable_anoncvs = 1",
		    ) ;
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;
	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+10" ;
    if (&is_lesser ($version, $target)) {
 	&debug ("Updating supported_languages table.") ;
	
	@reqlist = (
	    "ALTER TABLE supported_languages RENAME COLUMN language_code TO language_code_old",
	    "ALTER TABLE supported_languages ADD COLUMN language_code character(5)",
	    "UPDATE supported_languages SET language_code = language_code_old",
	    "ALTER TABLE supported_languages DROP COLUMN language_code_old",
	    "UPDATE supported_languages SET language_code='pt_BR', classname='PortugueseBrazilian', name='Pt. Brazilian', filename='PortugueseBrazilian.class' where classname='PortugueseBrazillian'",
	    ) ;
 	foreach my $s (@reqlist) {
 	    $query = $s ;
 	    # debug $query ;
 	    $sth = $dbh->prepare ($query) ;
 	    $sth->execute () ;
 	    $sth->finish () ;
 	}
 	@reqlist = () ;
 	&update_db_version ($target) ;
 	&debug ("...OK.") ;
 	$dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+11" ;
    if (&is_lesser ($version, $target)) {
 	&debug ("Adding tables for the plugin subsystem.") ;

 	@reqlist = (
		    "CREATE SEQUENCE plugins_pk_seq",
		    "CREATE TABLE plugins (plugin_id integer DEFAULT nextval('plugins_pk_seq'::text) NOT NULL, plugin_name varchar(32) UNIQUE NOT NULL, plugin_desc text, CONSTRAINT plugins_pkey PRIMARY KEY (plugin_id))",
		    "CREATE SEQUENCE group_plugin_pk_seq",
		    "CREATE TABLE group_plugin (group_plugin_id integer DEFAULT nextval('group_plugin_pk_seq'::text) NOT NULL, group_id integer, plugin_id integer, CONSTRAINT group_plugin_pkey PRIMARY KEY (group_plugin_id), CONSTRAINT group_plugin_group_id_fk FOREIGN KEY (group_id) REFERENCES groups(group_id) MATCH FULL, CONSTRAINT group_plugin_plugin_id_fk FOREIGN KEY (plugin_id) REFERENCES plugins(plugin_id) MATCH FULL)",
		    "CREATE SEQUENCE user_plugin_pk_seq",
		    "CREATE TABLE user_plugin (user_plugin_id integer DEFAULT nextval('user_plugin_pk_seq'::text) NOT NULL, user_id integer, plugin_id integer, CONSTRAINT user_plugin_pkey PRIMARY KEY (user_plugin_id), CONSTRAINT user_plugin_user_id_fk FOREIGN KEY (user_id) REFERENCES users(user_id) MATCH FULL, CONSTRAINT user_plugin_plugin_id_fk FOREIGN KEY (plugin_id) REFERENCES plugins(plugin_id) MATCH FULL)",
 		    ) ;
 	foreach my $s (@reqlist) {
 	    $query = $s ;
 	    # debug $query ;
 	    $sth = $dbh->prepare ($query) ;
 	    $sth->execute () ;
 	    $sth->finish () ;
 	}
 	@reqlist = () ;
 	&update_db_version ($target) ;
 	&debug ("...OK.") ;
 	$dbh->commit () ;
    }

    &update_with_sql("20021125", "2.6-0+checkpoint+12") ;
    &update_with_sql("20021212", "2.6-0+checkpoint+13") ;
    &update_with_sql("20021213-1", "2.6-0+checkpoint+14") ;

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+15" ;
    if (&is_lesser ($version, $target)) {
      &debug ("Transcoding documentation data fields") ;
      $query = "SELECT docid,data FROM doc_data ORDER BY docid ASC" ;
      # debug $query ;
      $sth = $dbh->prepare ($query) ;
      $sth->execute () ;
      while (@array = $sth->fetchrow_array) {
	  my $query2 = "UPDATE doc_data SET data='" ;
	  $query2 .= encode_base64 (decode_entities ($array [1])) ;
	  $query2 .= "', filename='file".$array [0].".html'";
	  $query2 .= ", filetype='text/html'"; 
	  $query2 .= " WHERE docid=" ;
	  $query2 .= $array [0] ;
	  $query2 .= "" ;
	  # debug $query2 ;
	  my $sth2 =$dbh->prepare ($query2) ;
	  $sth2->execute () ;
	  $sth2->finish () ;
      }
      $sth->finish () ;

      @reqlist = () ;
      &update_db_version ($target) ;
      &debug ("...OK.") ;
      $dbh->commit () ;
    }

    &update_with_sql("20021214", "2.6-0+checkpoint+16") ;
    &update_with_sql("20021215", "2.6-0+checkpoint+17") ;
    &update_with_sql("20021216", "2.6-0+checkpoint+18") ;
    &update_with_sql("20021223-2", "2.6-0+checkpoint+19") ;
    &update_with_sql("20030102-2", "2.6-0+checkpoint+20") ;
    &update_with_sql("20030105", "2.6-0+checkpoint+21") ;
    &update_with_sql("20030107", "2.6-0+checkpoint+22") ;
    &update_with_sql("20030109", "2.6-0+checkpoint+23") ;

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+24" ;
    if (&is_lesser ($version, $target)) {

      &debug ("Adjusting language sequences") ;

	$query = "SELECT max(language_id) FROM supported_languages" ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	@array = $sth->fetchrow_array () ;
	$sth->finish () ;
	my $maxid = $array [0] ;
	&bump_sequence_to ($dbh, "supported_languages_pk_seq", $maxid) ;

      &debug ("Upgrading with 20030112.sql") ;

      @reqlist = @{ &parse_sql_file ("$sqldir/20030112.sql") } ;
      foreach my $s (@reqlist) {
	  $query = $s ;
	  # debug $query ;
	  $sth = $dbh->prepare ($query) ;
	  $sth->execute () ;
	  $sth->finish () ;
      }
      @reqlist = () ;

      &update_db_version ($target) ;
      &debug ("...OK.") ;
      $dbh->commit () ;
    }

    &update_with_sql("20030113-2", "2.6-0+checkpoint+25") ;
    &update_with_sql("20030131", "2.6-0+checkpoint+26") ;
    &update_with_sql("20030209", "2.6-0+checkpoint+27") ;
    &update_with_sql("20030312", "2.6-0+checkpoint+28") ;

    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+29" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Registering KDE theme.") ;

	$query = "INSERT INTO themes (dirname, fullname) VALUES ('kde', 'KDE')";
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }


    $version = &get_db_version ;
    $target = "2.6-0+checkpoint+30" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Registering Dark Aqua theme.") ;

	$query = "INSERT INTO themes (dirname, fullname) VALUES ('darkaqua', 'Dark Aqua')";
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }


    &update_with_sql("20030513", "2.6-0+checkpoint+31") ;

    $version = &get_db_version ;
    $target = "3.0-1" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Database schema is now version 3.0-1.") ;

	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    &update_with_sql("20030822", "3.0-7") ;
    &update_with_sql("20031105", "3.1-0+1") ;
    &update_with_sql("20031124", "3.1-0+1.1") ;
    &update_with_sql("20031129", "3.1-0+2") ;
    &update_with_sql("20031126", "3.1-0+3") ;
    &update_with_sql("20031205", "3.2.1-0+2") ;
    &update_with_sql("20040130", "3.2.1-0+3") ;
    &update_with_sql("20040204", "3.2.1-0+4") ;
    &update_with_sql("20040315", "3.2.1-0+5") ;
    &update_with_sql("200403251", "3.3.0-0+0") ;
    &update_with_sql("200403252", "3.3.0-0+1") ;
    &update_with_sql("20040507", "3.3.0-0+3") ;
    &update_with_sql("20040722", "3.3.0-0+4") ;
    &update_with_sql("20040804", "3.3.0-0+6") ;
    &update_with_sql("20040826", "3.3.0-0+7") ;

    $version = &get_db_version ;
    $target = "3.3.0-2+1" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Migrating forum names") ;
	
	$query = "SELECT group_forum_id,forum_name FROM forum_group_list" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $forumid = $array[0] ;
	    my $oldname = $array[1] ;
	    
	    my $newname = lc $oldname ;
	    $newname =~ s/[^_.0-9a-z-]/-/g ;
	    
	    my $query2 = "UPDATE forum_group_list SET forum_name='$newname' WHERE group_forum_id=$forumid" ;
	    # &debug ($query2) ;
	    my $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    $sth2->finish () ;
	}
	$sth->finish () ;
	
        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "3.3.0-2+2" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Migrating permissions to RBAC") ;
	
	my $defaultroles = {
	    'Admin'	       => { 'projectadmin'=>'A', 'frs'=>'1', 'scm'=>'1', 'docman'=>'1', 'forumadmin'=>'2', 'forum'=>'2', 'trackeradmin'=>'2', 'tracker'=>'2', 'pmadmin'=>'2', 'pm'=>'2' },
	    'Senior Developer' => { 'projectadmin'=>'0', 'frs'=>'1', 'scm'=>'1', 'docman'=>'1', 'forumadmin'=>'2', 'forum'=>'2', 'trackeradmin'=>'2', 'tracker'=>'2', 'pmadmin'=>'2', 'pm'=>'2' },
	    'Junior Developer' => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'1', 'docman'=>'0', 'forumadmin'=>'0', 'forum'=>'1', 'trackeradmin'=>'0', 'tracker'=>'1', 'pmadmin'=>'0', 'pm'=>'1' },
	    'Doc Writer'       => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'0', 'docman'=>'1', 'forumadmin'=>'0', 'forum'=>'1', 'trackeradmin'=>'0', 'tracker'=>'0', 'pmadmin'=>'0', 'pm'=>'0' },
	    'Support Tech'     => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'0', 'docman'=>'1', 'forumadmin'=>'0', 'forum'=>'1', 'trackeradmin'=>'0', 'tracker'=>'2', 'pmadmin'=>'0', 'pm'=>'0' }
	} ;
	
	$query = "SELECT group_id FROM groups where status != 'P'" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $group_id = $array[0] ;

	    my ($query2, $sth2, @array2, $admin_rid, $jd_rid, %roledata) ;
	    foreach my $rname (keys %$defaultroles) {
		$query2 = "SELECT nextval('role_role_id_seq'::text)" ;
		# &debug ($query2) ;
		$sth2 =$dbh->prepare ($query2) ;
		$sth2->execute () ;
		@array2 = $sth2->fetchrow_array ;
		my $rid = $array2[0] ;
		$sth2->finish () ;
		if ($rname eq 'Admin') {
		    $admin_rid = $rid ;
		} elsif ($rname eq 'Junior Developer') {
		    $jd_rid = $rid ;
		}

		$query2 = "INSERT INTO role (role_id, group_id, role_name)
                           VALUES ($rid, $group_id, '$rname')" ;
		# &debug ($query2) ;
		$sth2 =$dbh->prepare ($query2) ;
		$sth2->execute () ;
		$sth2->finish () ;

		foreach my $section (keys %{$defaultroles->{$rname}}) {
		    if ($section eq 'forum') {
			$query2 = "SELECT group_forum_id 
                               FROM forum_group_list 
                               WHERE group_id = $group_id" ;
			# &debug ($query2) ;
			$sth2 =$dbh->prepare ($query2) ;
			$sth2->execute () ;
			while (@array2 = $sth2->fetchrow_array) {
			    $roledata{'forum'}{$array2[0]} = $defaultroles->{$rname}{'forum'} ;
			}
			$sth2->finish () ;
		    } elsif ($section eq 'pm') {
			$query2 = "SELECT group_project_id 
                               FROM project_group_list 
                               WHERE group_id = $group_id" ;
			# &debug ($query2) ;
			$sth2 =$dbh->prepare ($query2) ;
			$sth2->execute () ;
			while (@array2 = $sth2->fetchrow_array) {
			    $roledata{'pm'}{$array2[0]} = $defaultroles->{$rname}{'pm'} ;
			}
			$sth2->finish () ;
		    } elsif ($section eq 'tracker') {
			$query2 = "SELECT group_artifact_id 
                               FROM artifact_group_list 
                               WHERE group_id = $group_id" ;
			# &debug ($query2) ;
			$sth2 =$dbh->prepare ($query2) ;
			$sth2->execute () ;
			while (@array2 = $sth2->fetchrow_array) {
			    $roledata{'tracker'}{$array2[0]} = $defaultroles->{$rname}{'tracker'} ;
			}
			$sth2->finish () ;
		    } else {
			$roledata{$section}{0} = $defaultroles->{$rname}{$section} ;
		    }
		    
		    foreach my $rd_it (keys %{$roledata{$section}}) {
			$query2 = "INSERT INTO role_setting (role_id, section_name, ref_id, value)
                                   VALUES ($rid, '$section', $rd_it, '$roledata{$section}{$rd_it}')" ;
			# &debug ($query2) ;
			$sth2 =$dbh->prepare ($query2) ;
			$sth2->execute () ;
			$sth2->finish () ;
		    }
		    
		}
		
	    }
	    
	    #   affecter le rôle Admin aux admins, JD aux autres
	    $query2 = "SELECT user_id, admin_flags FROM user_group WHERE group_id = $group_id" ;
	    # &debug ($query2) ;
	    $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    while (@array2 = $sth2->fetchrow_array) {
		my $uid        = $array2[0] ;
		my $adminflags = $array2[1] ;
		my ($rid, $rname) ;

		$adminflags =~ s/\s//g ;
		if ($adminflags eq 'A') {
		    $rid = $admin_rid ;
		    $rname = 'Admin' ;
		} else {
		    $rid = $jd_rid ;
		    $rname = 'Junior Developer' ;
		}
		my @reqlist3 = (
				"UPDATE user_group
                                 SET role_id = $rid,
			         admin_flags    = '$defaultroles->{$rname}{'projectadmin'}',
			         forum_flags    = '$defaultroles->{$rname}{'forumadmin'}',
			         project_flags  = '$defaultroles->{$rname}{'pmadmin'}',
			         doc_flags      = '$defaultroles->{$rname}{'docman'}',
			         cvs_flags      = '$defaultroles->{$rname}{'scm'}',
			         release_flags  = '$defaultroles->{$rname}{'frs'}',
			         artifact_flags = '$defaultroles->{$rname}{'trackeradmin'}'
                                 WHERE user_id = $uid AND group_id = $group_id" ,
				"UPDATE forum_perm
				 SET perm_level=$defaultroles->{$rname}{'forum'}
				 WHERE group_forum_id IN (
                                    SELECT group_forum_id
                                    FROM forum_group_list
                                    WHERE group_id=$group_id)
                                 AND user_id=$uid" ,
				"UPDATE project_perm
				 SET perm_level=$defaultroles->{$rname}{'pm'}
				 WHERE group_project_id IN (
                                    SELECT group_project_id
                                    FROM project_group_list
                                    WHERE group_id=$group_id)
                                 AND user_id=$uid" ,
				"UPDATE artifact_perm
				 SET perm_level=$defaultroles->{$rname}{'tracker'}
				 WHERE group_artifact_id IN (
                                    SELECT group_artifact_id
                                    FROM artifact_group_list
                                    WHERE group_id=$group_id)
                                 AND user_id=$uid" ,
				) ;
		foreach my $query3 (@reqlist3) {
		    # &debug ($query3) ;
		    my $sth3 = $dbh->prepare ($query3) ;
		    $sth3->execute () ;
		    $sth3->finish () ;
		}
	    }
	    $sth2->finish () ;
	}
	$sth->finish () ;
	
        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20040914", "3.3.0-2+4") ;
    &update_with_sql("20041001", "3.3.0-2+4+1") ;
    &update_with_sql("20041005", "3.3.0-2+5") ;
    &update_with_sql("20041006", "3.3.0-2+6") ;
    &update_with_sql("20041014", "3.3.0-3") ;
    &update_with_sql("20041020", "3.3.0-4") ;
    &update_with_sql("20040729", "4.0.0-0") ;

    $version = &get_db_version ;
    $target = "4.0.0-0+1" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Granting read access permissions to NSS") ;

        @reqlist = ( "GRANT SELECT ON nss_passwd TO ${sys_dbuser}_nss",
		     "GRANT SELECT ON nss_groups TO ${sys_dbuser}_nss",
		     "GRANT SELECT ON nss_usergroups TO ${sys_dbuser}_nss",
		    ) ;
        foreach my $s (@reqlist) {
            $query = $s ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "4.0.0-0+2" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Upgrading with 20041031.sql") ;

        @reqlist = @{ &parse_sql_file ("$sqldir/20041031.sql") } ;
        foreach my $s (@reqlist) {
            $query = $s ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &debug ("Granting read access permissions to NSS") ;

        @reqlist = ( "GRANT SELECT ON mta_users TO ${sys_dbuser}_mta",
		     "GRANT SELECT ON mta_lists TO ${sys_dbuser}_mta",
		    ) ;
        foreach my $s (@reqlist) {
            $query = $s ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20041104", "4.0.0-0+3") ;
    &update_with_sql("20041108", "4.0.0-0+4") ;
    &update_with_sql("20041124", "4.0.2-0+0") ;

    $version = &get_db_version ;
    $target = "4.0.2-0+1" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Creating automatic commit notification mailing-lists") ;
	

	$query = "SELECT group_id, unix_group_name FROM groups WHERE status='A' ORDER BY group_id" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $group_id   = $array[0] ;
	    my $group_name = $array[1] ;

	    my $query2 = "SELECT count(*) FROM mail_group_list 
   			  WHERE group_id = $group_id 
			  AND list_name = '".$group_name."-commits'" ;
	    # &debug ($query2) ;
	    my $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    my @array2 = $sth2->fetchrow_array ;
	    $sth2->finish () ;
	    if ($array2[0] == 0) {
		my $listname = $group_name."-commits" ;
		my $listpw = substr (Digest::MD5::md5_base64 ($listname . rand(1)), 0, 16) ;
		
		
		$query2 = "SELECT user_id FROM user_group 
			   WHERE admin_flags = 'A' 
			   AND group_id = $group_id" ;
		# &debug ($query2) ;
		$sth2 =$dbh->prepare ($query2) ;
		$sth2->execute () ;
		my $group_admin = -1 ;
		if (@array2 = $sth2->fetchrow_array) {
		    $group_admin = $array2[0] ;
		}
		$sth2->finish () ;

		$query2 = "INSERT INTO mail_group_list (group_id, list_name, is_public, password, list_admin, status, description)
                           VALUES ($group_id, '$listname', 1, '$listpw', $group_admin, 1, 'commits')" ;
		# &debug ($query2) ;
		$sth2 =$dbh->prepare ($query2) ;
		$sth2->execute () ;
		$sth2->finish () ;
	    }
	}
	$sth->finish () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20050115", "4.0.2-0+3") ;
#
# We got this at upgrade
#
#DBD::Pg::st execute failed: ERREUR:  la relation avec l'OID 387345 n'existe pas at /usr/share/gforge/bin/db-upgrade.pl line 1970.
#Transaction aborted because DBD::Pg::st execute failed: ERREUR:  la relation avec l'OID 387345 n'existe pas at /usr/share/gforge/bin/db-upgrade.pl line 1970.
#Transaction aborted because DBD::Pg::st execute failed: ERREUR:  la relation avec l'OID 387345 n'existe pas at /usr/share/gforge/bin/db-upgrade.pl line 1970.
#Last SQL query was:
#update project_task SET last_modified_date=EXTRACT(EPOCH FROM now())::integer;
#(end of query)
#Your database schema is at version 4.0.2-0+5
#
# This is a hack to disconnect and reconnect the DB and solve the problem
#
    $dbh->rollback ;
    &db_disconnect ;
    &db_connect ;

    $dbh->{AutoCommit} = 0;
    $dbh->{RaiseError} = 1;

    &update_with_sql("20050130", "4.0.2-0+5") ;
    &update_with_sql("20050212", "4.0.2-0+6") ;

    $version = &get_db_version ;
    $target = "4.0.2-0+7" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Upgrading with 20050214-nss.sql") ;

        @reqlist = @{ &parse_sql_file ("$sqldir/20050214-nss.sql") } ;
        foreach my $s (@reqlist) {
            $query = $s ;
            $query =~ s/TO gforge_nss;/TO ${sys_dbuser}_nss;/ ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20050224-2", "4.1-0") ;

    $version = &get_db_version ;
    $target = "4.1-1" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Upgrading with 20050225-nsssetup.sql") ;

        @reqlist = @{ &parse_sql_file ("$sqldir/20050225-nsssetup.sql") } ;
        foreach my $s (@reqlist) {
            $query = $s ;
            $query =~ s/TO gforge_nss;/TO ${sys_dbuser}_nss;/ ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20050311", "4.1-2") ;
    &update_with_sql("20050315", "4.1-3") ;
    &update_with_sql("20050325-2", "4.1-4") ;

    $version = &get_db_version ;
    $target = "4.1-5" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Converting trackers to use their extra fields") ;
	
	$query = "SELECT group_id,group_artifact_id,use_resolution FROM artifact_group_list" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $group_id = $array[0] ;
	    my $gaid 	 = $array[1] ;
	    my $ur 	 = $array[2] ;

	    # Ajout du champ Category
	    my $query2 = "SELECT nextval('artifact_extra_field_list_extra_field_id_seq'::text)" ;
	    # &debug ($query2) ;
	    my $sth2 = $dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    my @array2 = $sth2->fetchrow_array ;
	    $sth2->finish () ;
	    my $aefid = $array2[0] ;
	    
	    $query2 = "INSERT INTO artifact_extra_field_list (extra_field_id, group_artifact_id,field_name,field_type) 
                       VALUES ($aefid, $gaid, 'Category', 1)" ;
	    # &debug ($query2) ;
	    $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;

	    $query2 = "SELECT id, category_name FROM artifact_category WHERE group_artifact_id=$gaid" ;
	    # &debug ($query2) ;
	    $sth2 = $dbh->prepare ($query2) ;
	    $sth2->execute () ;

	    while (@array2 = $sth2->fetchrow_array) {
		my $cat_id = $array2[0] ;
		my $catname = $array2[1] ;

		if ($catname eq '') { $catname = '[empty]' ; }
		
		my $query3 = "SELECT nextval('artifact_extra_field_elements_element_id_seq'::text)" ;
		# &debug ($query3) ;
		my $sth3 = $dbh->prepare ($query3) ;
		$sth3->execute () ;
		my @array3 = $sth3->fetchrow_array ;
		$sth3->finish () ;
		my $efeid = $array3[0] ;

		$query3 = "INSERT INTO artifact_extra_field_elements (element_id, extra_field_id, element_name, status_id) 
                              VALUES ($efeid, $aefid, ?, 0)" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute ($catname) ;
		$sth3->finish () ;

		$query3 = "INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
                           SELECT artifact_id,$efeid,$aefid FROM artifact 
                           WHERE category_id=$cat_id" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute () ;
		$sth3->finish () ;

		$query3 = "UPDATE artifact_history SET old_value=?,field_name='Category'
			   WHERE old_value='$cat_id' AND field_name='category_id'" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute ($catname) ;
		$sth3->finish () ;
	    }
	    $sth2->finish () ;
	    
	    # Ajout du champ Group
	    $query2 = "SELECT nextval('artifact_extra_field_list_extra_field_id_seq'::text)" ;
	    # &debug ($query2) ;
	    $sth2 = $dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    @array2 = $sth2->fetchrow_array ;
	    $sth2->finish () ;
	    $aefid = $array2[0] ;
	    
	    $query2 = "INSERT INTO artifact_extra_field_list (extra_field_id, group_artifact_id,field_name,field_type) 
                       VALUES ($aefid, $gaid, 'Group', 1)" ;
	    # &debug ($query2) ;
	    $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;

	    $query2 = "SELECT id, group_name FROM artifact_group WHERE group_artifact_id=$gaid" ;
	    # &debug ($query2) ;
	    $sth2 = $dbh->prepare ($query2) ;
	    $sth2->execute () ;

	    while (@array2 = $sth2->fetchrow_array) {
		my $grp_id = $array2[0] ;
		my $grpname = $array2[1] ;

		if ($grpname eq '') { $grpname = '[empty]' ; }
		
		my $query3 = "SELECT nextval('artifact_extra_field_elements_element_id_seq'::text)" ;
		# &debug ($query3) ;
		my $sth3 = $dbh->prepare ($query3) ;
		$sth3->execute () ;
		my @array3 = $sth3->fetchrow_array ;
		$sth3->finish () ;
		my $efeid = $array3[0] ;

		$query3 = "INSERT INTO artifact_extra_field_elements (element_id, extra_field_id, element_name, status_id) 
                              VALUES ($efeid, $aefid, ?, 0)" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute ($grpname) ;
		$sth3->finish () ;

		$query3 = "INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
                           SELECT artifact_id,$efeid,$aefid FROM artifact 
                           WHERE artifact_group_id=$grp_id" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute () ;
		$sth3->finish () ;

		$query3 = "UPDATE artifact_history SET old_value=?,field_name='Group'
			   WHERE old_value='$grp_id' AND field_name='artifact_group_id'" ;
		# &debug ($query3) ;
		$sth3 =$dbh->prepare ($query3) ;
		$sth3->execute ($grpname) ;
		$sth3->finish () ;
	    }
	    $sth2->finish () ;

	    # Ajout du champ Resolution (s'il existe, cf. $ur)
	    if ($ur) {
		$query2 = "SELECT nextval('artifact_extra_field_list_extra_field_id_seq'::text)" ;
		# &debug ($query2) ;
		$sth2 = $dbh->prepare ($query2) ;
		$sth2->execute () ;
		@array2 = $sth2->fetchrow_array ;
		$sth2->finish () ;
		$aefid = $array2[0] ;
		
		$query2 = "INSERT INTO artifact_extra_field_list (extra_field_id, group_artifact_id,field_name,field_type) 
                       VALUES ($aefid, $gaid, 'Resolution', 1)" ;
		# &debug ($query2) ;
		$sth2 =$dbh->prepare ($query2) ;
		$sth2->execute () ;

		$query2 = "SELECT id, resolution_name FROM artifact_resolution" ;
		# &debug ($query2) ;
		$sth2 = $dbh->prepare ($query2) ;
		$sth2->execute () ;

		while (@array2 = $sth2->fetchrow_array) {
		    my $res_id = $array2[0] ;
		    my $resname = $array2[1] ;

		    if ($resname eq '') { $resname = '[empty]' ; }
		    
		    my $query3 = "SELECT nextval('artifact_extra_field_elements_element_id_seq'::text)" ;
		    # &debug ($query3) ;
		    my $sth3 = $dbh->prepare ($query3) ;
		    $sth3->execute () ;
		    my @array3 = $sth3->fetchrow_array ;
		    $sth3->finish () ;
		    my $efeid = $array3[0] ;

		    $query3 = "INSERT INTO artifact_extra_field_elements (element_id, extra_field_id, element_name, status_id) 
                               VALUES ($efeid, $aefid, ?, 0)" ;
		    # &debug ($query3) ;
		    $sth3 =$dbh->prepare ($query3) ;
		    $sth3->execute ($resname) ;
		    $sth3->finish () ;

		    $query3 = "INSERT INTO artifact_extra_field_data (artifact_id,field_data,extra_field_id)
                               SELECT artifact_id,$efeid,$aefid FROM artifact 
                               WHERE resolution_id=$res_id and group_artifact_id=$gaid" ;
		    # &debug ($query3) ;
		    $sth3 =$dbh->prepare ($query3) ;
		    $sth3->execute () ;
		    $sth3->finish () ;

		    $query3 = "UPDATE artifact_history SET old_value=?,field_name='Resolution'
			       WHERE old_value='$res_id' AND field_name='resolution_id'" ;
		    # &debug ($query3) ;
		    $sth3 =$dbh->prepare ($query3) ;
		    $sth3->execute ($resname) ;
		    $sth3->finish () ;
		}
		$sth2->finish () ;
	    }
	}
	
        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20050325-5", "4.1-6") ;
    &update_with_sql("20050605", "4.1-7") ;

    $version = &get_db_version ;
    $target = "4.1-8" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Creating aliases for the extra fields") ;

	$query = "ALTER TABLE artifact_extra_field_list ADD COLUMN alias TEXT" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;

	my %reserved_alias = (
	    "project" => 1,
	    "type" => 1,
	    "priority" => 1,
	    "assigned_to" => 1,
	    "summary" => 1,
	    "details" => 1,
	) ;

	$query = "SELECT field_name, alias, group_artifact_id, extra_field_id FROM artifact_extra_field_list" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $name = $array[0] ;
	    my $alias = $array[1] ;
	    my $gaid = $array[2] ;
	    my $efid = $array[3] ;

	    if (! $alias) {
		my $newalias = lc $name ;
		$newalias =~ s/\s/_/g ;
		$newalias =~ s/[^_a-z]//g ;
		
		if ($newalias ne "") {
		    if ($reserved_alias{$newalias}) {
			$newalias = "extra_" . $newalias ;
		    }
		    
		    my $candidate ;
		    my $conflict = 0 ;
		    my $count = 0 ;
		    do {
			$candidate = $newalias ;
			$candidate .= $count if ($count > 0) ;
			my $query2 = "SELECT count(*) FROM artifact_extra_field_list WHERE group_artifact_id=$gaid AND LOWER(alias)='$candidate' AND extra_field_id <> $efid" ;
			# &debug ($query2) ;
			my $sth2 =$dbh->prepare ($query2) ;
			$sth2->execute () ;
			my @array2 = $sth2->fetchrow_array ;
			if ($array2[0] == 0) {
			    $conflict = 0 ;
			} else {
			    $conflict = 1 ;
			    $count++ ;
			}
			$sth2->finish () ;
		    } until ($conflict == 0) ;
			
		    my $query2 = "UPDATE artifact_extra_field_list SET alias='$candidate' WHERE extra_field_id=$efid" ;
		    # &debug ($query2) ;
		    my $sth2 =$dbh->prepare ($query2) ;
		    $sth2->execute () ;
		    $sth2->finish () ;
		}
	    }

	}
	$sth->finish () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20050628", "4.1-9") ;
    &update_with_sql("20050711", "4.5-1") ;
    &update_with_sql("20050906","4.5-2"); 
    &update_with_sql("20050804-1","4.5-3"); 

    $version = &get_db_version ;
    $target = "4.5-4" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Updating document sizes") ;

	$query = "SELECT docid, data FROM doc_data" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $docid = $array[0] ;
	    my $b64data = $array[1] ;
	    my $data = decode_base64 ($b64data) ;
	    my $size = length ($data) ;

	    my $query2 = "UPDATE doc_data SET filesize=$size WHERE docid=$docid" ;
	    # &debug ($query2) ;
	    my $sth2 =$dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    $sth2->finish () ;
	}
	$sth->finish () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "4.5.14-3" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Setting up time tracking") ;

	if (&table_exists ($dbh, "rep_time_category")) {
	    &debug ("...already set up.") ;
	} else {
	    &drop_table_if_exists ($dbh, "rep_time_category") ;
	    &drop_sequence_if_exists ($dbh, "rep_time_category_time_code_seq") ;
	    &drop_table_if_exists ($dbh, "rep_time_tracking") ;
	    &drop_table_if_exists ($dbh, "rep_users_added_daily") ;
	    &drop_table_if_exists ($dbh, "rep_users_added_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_users_added_monthly") ;
	    &drop_table_if_exists ($dbh, "rep_users_cum_daily") ;
	    &drop_table_if_exists ($dbh, "rep_users_cum_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_users_cum_monthly") ;
	    &drop_table_if_exists ($dbh, "rep_groups_added_daily") ;
	    &drop_table_if_exists ($dbh, "rep_groups_added_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_groups_added_monthly") ;
	    &drop_table_if_exists ($dbh, "rep_groups_cum_daily") ;
	    &drop_table_if_exists ($dbh, "rep_groups_cum_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_groups_cum_monthly") ;
	    &drop_view_if_exists ($dbh, "rep_group_act_oa_vw") ;
	    &drop_view_if_exists ($dbh, "rep_user_act_oa_vw") ;
	    &drop_view_if_exists ($dbh, "rep_site_act_daily_vw") ;
	    &drop_view_if_exists ($dbh, "rep_site_act_weekly_vw") ;
	    &drop_view_if_exists ($dbh, "rep_site_act_monthly_vw") ;
	    &drop_table_if_exists ($dbh, "rep_user_act_daily") ;
	    &drop_table_if_exists ($dbh, "rep_user_act_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_user_act_monthly") ;
	    &drop_table_if_exists ($dbh, "rep_group_act_daily") ;
	    &drop_index_if_exists ($dbh, "repgroupactdaily_daily") ;
	    &drop_table_if_exists ($dbh, "rep_group_act_weekly") ;
	    &drop_index_if_exists ($dbh, "repgroupactweekly_weekly") ;
	    &drop_table_if_exists ($dbh, "rep_group_act_monthly") ;
	    &drop_index_if_exists ($dbh, "repgroupactmonthly_monthly") ;

	    @reqlist = @{ &parse_sql_file ("$sqldir/timetracking-init.sql") } ;
	    foreach my $s (@reqlist) {
		$query = $s ;
		# debug $query ;
		$sth = $dbh->prepare ($query) ;
		$sth->execute () ;
		$sth->finish () ;
	    }
	    @reqlist = () ;
	}
	
	&update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    # I had to increase versions from 4.5.14 to 4.5.15
    # The activity view is created by 20060216-nocommit
    # If the view doesn't exists apply 
    if (! &view_exists ($dbh, 'activity_vw')) {
        &update_with_sql("20050812","4.5.15-10merge"); 
        &update_with_sql("20050822-2","4.5.15-11merge"); 
        &update_with_sql("20050823","4.5.15-12merge"); 
        &update_with_sql("20050824","4.5.15-13merge"); 
        &update_with_sql("20050831","4.5.15-14merge"); 

        &update_with_sql("20060113","4.5.15-15"); 
        &update_with_sql("20060214","4.5.15-16"); 
        &update_with_sql("20060216-2-debian-nocommit","4.5.15-17"); 
    }

    $version = &get_db_version ;
    $target = "4.5.15-21" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Fixing past mistakes in role naming") ;

	my $defaultroles_restricted = {
	    'Admin'	       => { 'projectadmin'=>'A', 'frs'=>'1', 'scm'=>'1', 'docman'=>'1', 'forumadmin'=>'2', 'trackeradmin'=>'2', 'pmadmin'=>'2' },
	    'Senior Developer' => { 'projectadmin'=>'0', 'frs'=>'1', 'scm'=>'1', 'docman'=>'1', 'forumadmin'=>'2', 'trackeradmin'=>'2', 'pmadmin'=>'2' },
	    'Junior Developer' => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'1', 'docman'=>'0', 'forumadmin'=>'0', 'trackeradmin'=>'0', 'pmadmin'=>'0' },
	    'Doc Writer'       => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'0', 'docman'=>'1', 'forumadmin'=>'0', 'trackeradmin'=>'0', 'pmadmin'=>'0' },
	    'Support Tech'     => { 'projectadmin'=>'0', 'frs'=>'0', 'scm'=>'0', 'docman'=>'1', 'forumadmin'=>'0', 'trackeradmin'=>'0', 'pmadmin'=>'0' }
	} ;

	foreach my $drname (keys %{$defaultroles_restricted}) {
	    $query = "UPDATE role SET role_name='$drname' WHERE role_id IN (SELECT role.role_id" ;
	    my $from = "" ;
	    my $where = "" ;
	    my $setting = "" ;
	    my $value = 0 ;
	    foreach my $setting (keys %{$defaultroles_restricted->{$drname}}) {
		$value = $defaultroles_restricted->{$drname}->{$setting} ;
		$from .= ", role_setting rs_$setting" ;
		$where .= "role.role_id = rs_$setting.role_id AND rs_$setting.section_name='$setting' AND " ;
		$where .= "rs_$setting.value = '$value' \nAND " ;
	    }
	    $query .= "\nFROM role$from" ;
	    $query .= "\nWHERE $where role.role_name='rname')";
	    push @reqlist, $query;
	}
	
	foreach my $s (@reqlist) {
	    $query = $s ;
	    # debug $query ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

      &update_with_sql("20051103_transiciel_motscle_document","4.6-1");
	
      &update_with_sql("20070924-forum-perm","4.6.99-1");
      &update_with_sql("20070924-project-perm","4.6.99-2");
      &update_with_sql("20070924-artifact-perm","4.6.99-3");
	
    $version = &get_db_version ;
    $target = "4.6.99-4" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Dropping old translations table") ;

	&drop_table_if_exists ($dbh, "tmp_lang") ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "4.6.99-5" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Updating available themes") ;

	my @obsolete_themes = qw/ classic debian savannah
                                  savannah_codex savannah_forest
                                  savannah_reverse savannah_sad
                                  savannah_savannah savannah_slashd
                                  savannah_startrek
                                  savannah_transparent savannah_water
                                  savannah_www.gnu.org
                                  savannah_darkslate forged kde
                                  darkaqua / ;

	my $otids = join (',', map { "'$_'" } @obsolete_themes) ;
	
	$query = "UPDATE users SET theme_id=1 WHERE theme_id IN
                     (SELECT theme_id FROM themes WHERE dirname IN ($otids))" ;
	push @reqlist, $query;
	
	$query = "DELETE FROM themes WHERE dirname IN ($otids)" ;
	push @reqlist, $query;

	my %new_themes = (
	    'gforge-classic'      => 'GForge classic',
	    'gforge-simple-theme' => 'GForge simple',
	    'lite'                => 'GForge lite'
	    ) ;

	foreach my $dir (sort keys %new_themes) {
	    $query = "INSERT INTO themes (dirname, fullname) VALUES ('$dir', '$new_themes{$dir}')" ;
	    push @reqlist, $query;
	}

	foreach my $s (@reqlist) {
	    $query = $s ;
	    # &debug ($query) ;
	    $sth = $dbh->prepare ($query) ;
	    $sth->execute () ;
	    $sth->finish () ;
	}
	@reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    $version = &get_db_version ;
    $target = "4.6.99-6" ;
    if (&is_lesser ($version, $target)) {
      &debug ("DROP UNIQUE INDEX never UNIQUE") ;
      &drop_index_if_exists ($dbh, "statsaggsitebygrp_oid") ;
      &drop_index_if_exists ($dbh, "statsprojectmetric_oid") ;
      &drop_index_if_exists ($dbh, "statsagglogobygrp_oid") ;
      &drop_index_if_exists ($dbh, "statsprojectdevelop_oid") ;
      &drop_index_if_exists ($dbh, "statssubdpages_oid") ;
      &drop_index_if_exists ($dbh, "statscvsgrp_oid") ;
      &drop_index_if_exists ($dbh, "statsproject_oid") ;
      &drop_index_if_exists ($dbh, "statssite_oid") ;
      &drop_index_if_exists ($dbh, "statssitepgsbyday_oid") ;
      &update_db_version ($target) ;
      &debug ("...OK.") ;
      $dbh->commit () ;
    }
    
    &update_with_sql("20090327_create_table_project_tags","4.6.99-7");
    &update_with_sql("20090402-add-projecttags-constraints","4.7.99-1");
    &update_with_sql("20090402-forum-attachment-types","4.7.99-2");

    &update_with_sql("20090507-add_artifact_workflow","4.8.99-1");
    &update_with_sql("20090507-add_element_pos","4.8.99-2");
    &update_with_sql("20090507-add_project_query","4.8.99-3");
    &update_with_sql("20090507-browse_list","4.8.99-4");

    $version = &get_db_version ;
    $target = "4.8.99-5" ;
    if (&is_lesser ($version, $target)) {
	&debug ("Initialising tracker workflows") ;

	
	$query = "SELECT group_id, artifact_group_list.group_artifact_id, element_id, artifact_extra_field_elements.extra_field_id
		FROM artifact_extra_field_list, artifact_extra_field_elements, artifact_group_list
		WHERE artifact_extra_field_list.extra_field_id=artifact_extra_field_elements.extra_field_id
		AND artifact_group_list.group_artifact_id = artifact_extra_field_list.group_artifact_id
		AND field_type=7" ;
	# &debug ($query) ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	while (@array = $sth->fetchrow_array) {
	    my $gid = $array[0];
	    my $gaid = $array[1];
	    my $eid = $array[2];

	    my $query2 = "SELECT extra_field_id
				FROM artifact_extra_field_list 
				WHERE group_artifact_id=$gaid
                                AND field_type = 7
				ORDER BY field_type ASC" ;
	    my $sth2 = $dbh->prepare ($query2) ;
	    $sth2->execute () ;
	    
	    if (my @array2 = $sth2->fetchrow_array) {
		my $efid = $array2[0];
		$sth2->finish () ;

		$query2 = "SELECT element_id,element_name,status_id
				FROM artifact_extra_field_elements
				WHERE extra_field_id = $efid
				ORDER BY element_pos ASC, element_id ASC" ;
		# debug $query2;
		$sth2 = $dbh->prepare ($query2) ;
		$sth2->execute () ;
		while (@array2 = $sth2->fetchrow_array) {
		    my $eid2 = $array2[0];
		    if ($eid2 != $eid) {
			my $query3 = "INSERT INTO artifact_workflow_event
				(group_artifact_id, field_id, from_value_id, to_value_id)
				VALUES ($gaid, $efid, $eid, $eid2)";
			# debug $query3;
			my $sth3 = $dbh->prepare ($query3) ;
			$sth3->execute () ;
			$sth3->finish () ;
			$query3 = "INSERT INTO artifact_workflow_event
				(group_artifact_id, field_id, from_value_id, to_value_id)
				VALUES ($gaid, $efid, $eid2, $eid)";
			# debug $query3;
			$sth3 = $dbh->prepare ($query3) ;
			$sth3->execute () ;
			$sth3->finish () ;
		    }
		}
		$sth2->finish () ;
		my $query3 = "INSERT INTO artifact_workflow_event
				(group_artifact_id, field_id, from_value_id, to_value_id)
				VALUES ($gaid, $efid, 100, $eid)";
		# debug $query3;
		my $sth3 = $dbh->prepare ($query3) ;
		$sth3->execute () ;
		$sth3->finish () ;
	    }
	}
	$sth->finish () ;

	@reqlist = () ;
	&update_db_version ($target) ;
	&debug ("...OK.") ;
	$dbh->commit () ;
    }

    &update_with_sql("20100308-forum-attachment-types","4.8.99-6");

    $version = &get_db_version ;
    $target = "4.8.99-7" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Granting read access permissions to NSS and MTA") ;

        @reqlist = ( "GRANT SELECT ON nss_passwd TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON nss_groups TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON nss_usergroups TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON mta_users TO ${sys_dbuser}_mta",
                    "GRANT SELECT ON mta_lists TO ${sys_dbuser}_mta",
                   ) ;
        foreach my $s (@reqlist) {
            $query = $s ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20100330-add-system-event","5.0.0-1");
    &update_with_sql("20100331-alter-system-event","5.0.0-2");
    &update_with_sql("20100505-alter-user-preference","5.0.1-1");
    &update_with_sql("20100506-add-widgets","5.0.1-2");
    &update_with_sql("20100517-add-project-widgets","5.0.1-3");
    &update_with_sql("20100518-pfo-rbac","5.0.1-4");
    &update_with_sql("20100524-pfo-rbac","5.0.1-5");
    &update_with_sql("20100606-clean-perm-views","5.0.1-6");
    &update_with_sql("20100610-pfo-rbac","5.0.1-7");
    &update_with_sql("20100730-docman","5.0.1-8");
    &update_with_sql("20100924-theme","5.0.1-9");
    &update_with_sql("20100926-pfo-rbac","5.0.1-10");
    &update_with_sql("20100927-pfo-rbac","5.0.1-11");
    &update_with_sql("20101012-docman-webdav","5.0.51-1");
    &update_with_sql("20101021-pfo-rbac","5.0.51-2");
    &update_with_sql("20101025-ipv6","5.0.51-3");

    $version = &get_db_version ;
    $target = "5.0.51-4" ;
    if (&is_lesser ($version, $target)) {
        &debug ("Granting read access permissions to NSS and MTA") ;

        @reqlist = ( "GRANT SELECT ON nss_passwd TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON nss_groups TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON nss_usergroups TO ${sys_dbuser}_nss",
                    "GRANT SELECT ON mta_users TO ${sys_dbuser}_mta",
                    "GRANT SELECT ON mta_lists TO ${sys_dbuser}_mta",
                   ) ;
        foreach my $s (@reqlist) {
            $query = $s ;
            # debug $query ;
            $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }

    &update_with_sql("20101027-docman-lock","5.0.51-5");
    &update_with_sql("20101105-pfo-rbac","5.0.51-6");
    &update_with_sql("20101029-docman-monitoring","5.0.51-7");
    &update_with_sql("20100402_add_query_options","5.0.51-8");
    &update_with_sql("20101024-docman-createonline","5.0.51-9");
    &update_with_sql("20101213-project-template","5.0.51-10");
    &update_with_sql("20110110-pw-size","5.0.51-11");
    &update_with_sql("20110405-forum_attachment_fix-bug284","5.0.51-12");
    &update_with_sql("20110408-anonymous-read-news","5.0.51-13");
    &update_with_sql("20110414-move-news-forums-to-own-project","5.0.51-14");
    &update_with_sql("20110701-gforge-5.1","5.1-1");
    &update_with_sql("20110728-fix-mta-lists-view","5.1-2");
    &update_with_sql("FTI","5.1-3");
    &update_with_sql("FTI-20050315","5.1-4");
    &update_with_sql("FTI-20050401","5.1-5");
    &update_with_sql("FTI-20050530","5.1-6");
    &update_with_sql("FTI-20060130","5.1-7");
    &update_with_sql("FTI-20061025","5.1-8");
    &update_with_sql("20111007-fti-aggregate-functions","5.1-9");
    &update_with_sql("20111007-add-fti-for-task-messages","5.1-10");
    &update_with_sql("20111009-string-aggregate-function","5.1-11");

    ########################### INSERT HERE #################################

    # There should be a commit at the end of every block above.
    # If there is not, then it might be symptomatic of a problem.
    # For safety, we roll back.
    $dbh->rollback ();
};

if ($@) {
    warn "Transaction aborted because $@" ;
    &debug ("Transaction aborted because $@") ;
    &debug ("Last SQL query was:\n$query\n(end of query)") ;
    $dbh->rollback ;
    my $version = &get_db_version ; 
    if ($version) {
	&debug ("Your database schema is at version $version") ;
    } else {
	&debug ("Couldn't get your database schema version.") ;
    }
    &debug ("Please report this bug on the Debian bug-tracking system.") ;
    &debug ("Please include the previous messages as well to help debugging.") ;
    &debug ("You should not worry too much about this,") ;
    &debug ("your DB is still in a consistent state and should be usable.") ;
    exit 1 ;
}

$dbh->rollback ;
&db_disconnect ;

sub get_pg_version () {
    my $command;
    if (-x '/usr/bin/pg_lsclusters' ) {
    	$command = q(/usr/bin/pg_lsclusters | grep 5432 | grep online | cut -d' ' -f1) ;
    } else {
    	$command = q(dpkg -s postgresql | awk '/^Version: / { print $2 }') ;
    }
    my $version = qx($command) ;
    chomp $version ;
    return $version ;
}

sub create_metadata_table ( $ ) {
    my $v = shift || "2.5-7+just+before+8" ;

    my ($query, $sth, @array) ;

    # Let's create this table if we have it not
    if (! &table_exists ($dbh, 'debian_meta_data')) {
	&debug ("Creating debian_meta_data table.") ;
	$query = "CREATE TABLE debian_meta_data (key varchar primary key, value text not null)" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;
    }

    $query = "SELECT count(*) FROM debian_meta_data WHERE key = 'db-version'";
    # debug $query ;
    $sth = $dbh->prepare ($query) ;
    $sth->execute () ;
    @array = $sth->fetchrow_array () ;
    $sth->finish () ;

    # Empty table?  We'll have to fill it up a bit

    if ($array [0] == 0) {
	&debug ("Inserting first data into debian_meta_data table.") ;
	$query = "INSERT INTO debian_meta_data (key, value) VALUES ('db-version', '$v')" ;
	# debug $query ;
	$sth = $dbh->prepare ($query) ;
	$sth->execute () ;
	$sth->finish () ;
    }
}

sub update_db_version ( $ ) {
    my $v = shift or die "Not enough arguments" ;

    $query = "UPDATE debian_meta_data SET value = '$v' WHERE key = 'db-version'" ;
    my $sth = $dbh->prepare ($query) ;
    $sth->execute () ;
    $sth->finish () ;
}

sub get_db_version () {
    $query = "SELECT value FROM debian_meta_data WHERE key = 'db-version'" ;
    # debug $query ;
    my $sth = $dbh->prepare ($query) ;
    $sth->execute () ;
    my @array = $sth->fetchrow_array () ;
    $sth->finish () ;

    my $version = $array [0] ;

    return $version ;
}

sub update_with_sql ( $$ ) {
    my $sqlfile = shift or die "Not enough arguments" ;
    my $target = shift or die "Not enough arguments" ;
    my $version = &get_db_version ;
    if (&is_lesser ($version, $target)) {
        &debug ("Upgrading database with $sqlfile.sql") ;

        @reqlist = @{ &parse_sql_file ("$sqldir/$sqlfile.sql") } ;
        foreach my $s (@reqlist) {
            my $query = $s ;
            # debug $query ;
            my $sth = $dbh->prepare ($query) ;
            $sth->execute () ;
            $sth->finish () ;
        }
        @reqlist = () ;

        &update_db_version ($target) ;
        &debug ("...OK.") ;
        $dbh->commit () ;
    }
}