/usr/sbin/openvas-migrate-to-postgres is in openvas-manager 7.0.2-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 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 | #!/bin/sh
#
# migrate-to-postgres
# This script migrates an OpenVAS installation from SQLite to Postgres.
#
# Authors:
# Timo Pollmeier <timo.pollmeier@greenbone.net>
# Matthew Mundell <matthew.mundell@greenbone.net>
#
# Copyright:
# Copyright (C) 2014 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
VERSION=20140904
# SETTINGS
# ========
# SCRIPT_NAME is the name the script will use to identify itself and to mark
# log messages.
SCRIPT_NAME="openvas-migrate-to-postgres"
# LOG_CMD defines the command to use for logging. To have logger log to stderr
# as well as syslog, add "-s" here.
LOG_CMD="logger -s -t $SCRIPT_NAME"
SQLITE3="sqlite3 -noheader -bail"
PSQL="psql -v ON_ERROR_STOP=1 -q --pset pager=off -d tasks -t"
# Maximum number of retries if database is locked
if [ -z "$MAX_SQL_RETRIES" ]; then
MAX_SQL_RETRIES="1" # 0 : try only once
fi
# Delay between retries
if [ -z "$SQL_RETRY_DELAY" ]; then
SQL_RETRY_DELAY="10m" # allowed unit suffixes: see sleep command
fi
[ -r /etc/openvas/migrate-to-postgres.conf ] && . /etc/openvas/migrate-to-postgres.conf
if [ -z "$SQLITE_DB_DIR" ]; then
SQLITE_DB_DIR="/var/lib/openvas/mgr"
fi
if [ -z "$SQLITE_DB_FILE" ]; then
SQLITE_DB_FILE="tasks.db"
fi
if [ -z "$SQLITE_DB" ]; then
SQLITE_DB="$SQLITE_DB_DIR/$SQLITE_DB_FILE"
fi
SQLITE=`command -v sqlite3`
log_debug () {
$LOG_CMD -p daemon.debug $1
}
log_info () {
$LOG_CMD -p daemon.info $1
}
log_notice () {
$LOG_CMD -p daemon.notice $1
}
log_warning () {
$LOG_CMD -p daemon.warning $1
}
log_err () {
$LOG_CMD -p daemon.err $1
}
reset_sql_tries () {
try_sql=1
sql_retries=0
}
test_sql_exit () {
exit_code=$?
try_sql=0
if [ 0 -ne "$exit_code" ]
then
log_err "$1: sql command exited with code $exit_code."
exit 1
fi
}
sqlite () {
log_debug "SQLite: $1"
$SQLITE3 $SQLITE_DB "$1"
}
pg () {
log_debug " PG: $1"
$PSQL -c "$1"
exit_code=$?
if [ 0 -ne "$exit_code" ]
then
log_err "$1: psql exited with code $exit_code for sql: $1."
exit 1
fi
}
does_pg_db_exist () {
PG_DB_EXISTS=`pg "SELECT EXISTS (SELECT table_name
FROM information_schema.tables
WHERE table_catalog = 'tasks'
AND table_schema = 'public'
AND table_name = 'meta')
::integer;"`
return $PG_DB_EXISTS
}
is_db_broken () {
log_info "Checking SQLite3 database."
DB_BROKEN=0
if [ ! -e $SQLITE_DB ]
then
return
fi
# Index report_hosts_by_host may have been created with COLLATE collate_ip.
# collate_ip has been removed. So just recreate the index.
reset_sql_tries
until [ "$try_sql" -eq 0 ]
do
sqlite "DROP INDEX report_hosts_by_host;"
test_sql_exit "Could not drop index report_hosts_by_host"
done
reset_sql_tries
until [ "$try_sql" -eq 0 ]
do
sqlite "CREATE INDEX report_hosts_by_host ON report_hosts (host);"
test_sql_exit "Could not create index report_hosts_by_host"
done
reset_sql_tries
until [ "$try_sql" -eq 0 ]
do
DB_INTEGRITY=`sqlite "PRAGMA integrity_check;"`
test_sql_exit "Could not check CERT database integrity"
done
if [ "$DB_INTEGRITY" != "ok" ]
then
DB_BROKEN=1
log_warning "Database integrity check failed."
fi
}
do_help () {
echo "$0: Migrate Manager database from SQLite3 to Postgres"
echo " --help display this help"
echo " --selftest perform self-test"
echo " --version display version"
echo ""
exit 0
}
create_tables_133 () {
log_info "Creating tables."
pg "CREATE TABLE IF NOT EXISTS current_credentials
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL);";
pg "CREATE TABLE IF NOT EXISTS meta
(id SERIAL PRIMARY KEY,
name text UNIQUE NOT NULL,
value text);";
pg "CREATE TABLE IF NOT EXISTS users
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
password text,
timezone text,
hosts text,
hosts_allow integer,
ifaces text,
ifaces_allow integer,
method text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS agents
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
installer bytea,
installer_64 text,
installer_filename text,
installer_signature_64 text,
installer_trust integer,
installer_trust_time integer,
howto_install text,
howto_use text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS agents_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
installer bytea,
installer_64 text,
installer_filename text,
installer_signature_64 text,
installer_trust integer,
installer_trust_time integer,
howto_install text,
howto_use text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS alerts
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
event integer,
condition integer,
method integer,
filter integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS alerts_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
event integer,
condition integer,
method integer,
filter integer,
filter_location integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS alert_condition_data
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS alert_condition_data_trash
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts_trash (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS alert_event_data
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS alert_event_data_trash
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts_trash (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS alert_method_data
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS alert_method_data_trash
(id SERIAL PRIMARY KEY,
alert integer REFERENCES alerts_trash (id) ON DELETE RESTRICT,
name text,
data text);";
pg "CREATE TABLE IF NOT EXISTS filters
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
type text,
term text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS filters_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
type text,
term text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS groups
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS groups_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS group_users
(id SERIAL PRIMARY KEY,
\"group\" integer REFERENCES groups (id) ON DELETE RESTRICT,
\"user\" integer REFERENCES users (id) ON DELETE RESTRICT);";
pg "CREATE TABLE IF NOT EXISTS group_users_trash
(id SERIAL PRIMARY KEY,
\"group\" integer REFERENCES groups_trash (id) ON DELETE RESTRICT,
\"user\" integer REFERENCES users (id) ON DELETE RESTRICT);";
pg "CREATE TABLE IF NOT EXISTS roles
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS roles_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS role_users
(id SERIAL PRIMARY KEY,
role integer REFERENCES roles (id) ON DELETE RESTRICT,
\"user\" integer REFERENCES users (id) ON DELETE RESTRICT);";
pg "CREATE TABLE IF NOT EXISTS role_users_trash
(id SERIAL PRIMARY KEY,
role integer REFERENCES roles_trash (id) ON DELETE RESTRICT,
\"user\" integer REFERENCES users (id) ON DELETE RESTRICT);";
pg "CREATE TABLE IF NOT EXISTS nvt_selectors
(id SERIAL PRIMARY KEY,
name text,
exclude integer,
type integer,
family_or_nvt text,
family text);";
pg "CREATE TABLE IF NOT EXISTS port_lists
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS port_lists_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS port_ranges
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
port_list integer REFERENCES port_lists (id) ON DELETE RESTRICT,
type integer,
start integer,
\"end\" integer,
comment text,
exclude integer);";
pg "CREATE TABLE IF NOT EXISTS port_ranges_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
port_list integer REFERENCES port_lists_trash (id) ON DELETE RESTRICT,
type integer,
start integer,
\"end\" integer,
comment text,
exclude integer);";
pg "CREATE TABLE IF NOT EXISTS port_names
(id SERIAL PRIMARY KEY,
number integer,
protocol text,
name text,
UNIQUE (number, protocol));";
pg "CREATE TABLE IF NOT EXISTS lsc_credentials
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
login text,
password text,
comment text,
private_key text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS lsc_credentials_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
login text,
password text,
comment text,
private_key text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS targets
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
hosts text,
exclude_hosts text,
reverse_lookup_only integer,
reverse_lookup_unify integer,
comment text,
lsc_credential integer,
ssh_port text,
smb_lsc_credential integer,
port_range integer REFERENCES port_lists (id) ON DELETE RESTRICT,
alive_test integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS targets_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
hosts text,
exclude_hosts text,
reverse_lookup_only integer,
reverse_lookup_unify integer,
comment text,
lsc_credential integer,
ssh_port text,
smb_lsc_credential integer,
port_range integer,
ssh_location integer,
smb_location integer,
port_list_location integer,
alive_test integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS configs
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
nvt_selector text,
comment text,
family_count integer,
nvt_count integer,
families_growing integer,
nvts_growing integer,
type integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS configs_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
nvt_selector text,
comment text,
family_count integer,
nvt_count integer,
families_growing integer,
nvts_growing integer,
type integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS config_preferences
(id SERIAL PRIMARY KEY,
config integer REFERENCES configs (id) ON DELETE RESTRICT,
type text,
name text,
value text);";
pg "CREATE TABLE IF NOT EXISTS config_preferences_trash
(id SERIAL PRIMARY KEY,
config integer REFERENCES configs_trash (id) ON DELETE RESTRICT,
type text,
name text,
value text);";
pg "CREATE TABLE IF NOT EXISTS schedules
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
first_time integer,
period integer,
period_months integer,
duration integer,
timezone text,
initial_offset integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS schedules_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
first_time integer,
period integer,
period_months integer,
duration integer,
timezone text,
initial_offset integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS slaves
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
host text,
port text,
login text,
password text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS slaves_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
host text,
port text,
login text,
password text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS scanners
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text,
comment text,
host text,
port integer,
type integer,
ca_pub text,
key_pub text,
key_priv text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS scanners_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text,
comment text,
host text,
port integer,
type integer,
ca_pub text,
key_pub text,
key_priv text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS tasks
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text,
hidden integer,
comment text,
run_status integer,
start_time integer,
end_time integer,
config integer,
target integer,
schedule integer,
schedule_next_time integer,
slave integer,
scanner integer,
config_location integer,
target_location integer,
schedule_location integer,
slave_location integer,
upload_result_count integer,
hosts_ordering text,
alterable integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS task_files
(id SERIAL PRIMARY KEY,
task integer REFERENCES tasks (id) ON DELETE RESTRICT,
name text,
content text);";
pg "CREATE TABLE IF NOT EXISTS task_alerts
(id SERIAL PRIMARY KEY,
task integer REFERENCES tasks (id) ON DELETE RESTRICT,
alert integer,
alert_location integer);";
pg "CREATE TABLE IF NOT EXISTS task_preferences
(id SERIAL PRIMARY KEY,
task integer REFERENCES tasks (id) ON DELETE RESTRICT,
name text,
value text);";
pg "CREATE TABLE IF NOT EXISTS reports
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
hidden integer,
task integer REFERENCES tasks (id) ON DELETE RESTRICT,
date integer,
start_time integer,
end_time integer,
nbefile text,
comment text,
scan_run_status integer,
slave_progress integer,
slave_task_uuid text,
slave_uuid text,
slave_name text,
slave_host text,
slave_port integer,
source_iface text);";
pg "CREATE TABLE IF NOT EXISTS report_counts
(id SERIAL PRIMARY KEY,
report integer REFERENCES reports (id) ON DELETE RESTRICT,
\"user\" integer REFERENCES users (id) ON DELETE RESTRICT,
severity decimal,
count integer,
override integer,
end_time integer);";
pg "CREATE TABLE IF NOT EXISTS results
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
task integer REFERENCES tasks (id) ON DELETE RESTRICT,
host text,
port text,
nvt text,
type text,
description text,
report integer REFERENCES reports (id) ON DELETE RESTRICT,
nvt_version text,
severity real,
qod integer,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
date integer);";
pg "CREATE TABLE IF NOT EXISTS report_formats
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
extension text,
content_type text,
summary text,
description text,
signature text,
trust integer,
trust_time integer,
flags integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS report_formats_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
extension text,
content_type text,
summary text,
description text,
signature text,
trust integer,
trust_time integer,
flags integer,
original_uuid text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS report_format_params
(id SERIAL PRIMARY KEY,
report_format integer REFERENCES report_formats (id) ON DELETE RESTRICT,
name text,
type integer,
value text,
type_min bigint,
type_max bigint,
type_regex text,
fallback text);";
pg "CREATE TABLE IF NOT EXISTS report_format_params_trash
(id SERIAL PRIMARY KEY,
report_format integer REFERENCES report_formats_trash (id) ON DELETE RESTRICT,
name text,
type integer,
value text,
type_min bigint,
type_max bigint,
type_regex text,
fallback text);";
pg "CREATE TABLE IF NOT EXISTS report_format_param_options
(id SERIAL PRIMARY KEY,
report_format_param integer REFERENCES report_format_params (id) ON DELETE RESTRICT,
value text);";
pg "CREATE TABLE IF NOT EXISTS report_format_param_options_trash
(id SERIAL PRIMARY KEY,
report_format_param integer REFERENCES report_format_params_trash (id) ON DELETE RESTRICT,
value text);";
# attack_state has not been created by Manager since 2014-11-20 (and it is
# never migrated away by Manager), but create it here in case the SQLite
# database is older than the one of 2014-11-20.
pg "CREATE TABLE IF NOT EXISTS report_hosts
(id SERIAL PRIMARY KEY,
report integer REFERENCES reports (id) ON DELETE RESTRICT,
host text,
start_time integer,
end_time integer,
attack_state text,
current_port integer,
max_port integer);";
pg "CREATE TABLE IF NOT EXISTS report_host_details
(id SERIAL PRIMARY KEY,
report_host integer REFERENCES report_hosts (id) ON DELETE RESTRICT,
source_type text,
source_name text,
source_description text,
name text,
value text);";
pg "CREATE TABLE IF NOT EXISTS report_results
(id SERIAL PRIMARY KEY,
report integer REFERENCES reports (id) ON DELETE RESTRICT,
result integer REFERENCES results (id) ON DELETE RESTRICT);";
pg "CREATE TABLE IF NOT EXISTS nvt_preferences
(id SERIAL PRIMARY KEY,
name text UNIQUE NOT NULL,
value text);";
pg "CREATE TABLE IF NOT EXISTS nvts
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
oid text UNIQUE NOT NULL,
version text,
name text,
comment text,
summary text,
copyright text,
cve text,
bid text,
xref text,
tag text,
category text,
family text,
cvss_base text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS nvt_cves
(id SERIAL PRIMARY KEY,
nvt integer REFERENCES nvts (id) ON DELETE RESTRICT,
oid text,
cve_name text);";
pg "CREATE TABLE IF NOT EXISTS notes
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
nvt text NOT NULL,
creation_time integer,
modification_time integer,
text text,
hosts text,
port text,
severity double precision,
task integer,
result integer,
end_time integer);";
pg "CREATE TABLE IF NOT EXISTS notes_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
nvt text NOT NULL,
creation_time integer,
modification_time integer,
text text,
hosts text,
port text,
severity double precision,
task integer,
result integer,
end_time integer);";
pg "CREATE TABLE IF NOT EXISTS overrides
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
nvt text NOT NULL,
creation_time integer,
modification_time integer,
text text,
hosts text,
new_severity double precision,
port text,
severity double precision,
task integer,
result integer,
end_time integer);";
pg "CREATE TABLE IF NOT EXISTS overrides_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
nvt text NOT NULL,
creation_time integer,
modification_time integer,
text text,
hosts text,
new_severity double precision,
port text,
severity double precision,
task integer,
result integer,
end_time integer);";
pg "CREATE TABLE IF NOT EXISTS permissions
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
resource_type text,
resource integer,
resource_uuid text,
resource_location integer,
subject_type text,
subject integer,
subject_location integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS permissions_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
resource_type text,
resource integer,
resource_uuid text,
resource_location integer,
subject_type text,
subject integer,
subject_location integer,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS settings
(id SERIAL PRIMARY KEY,
uuid text NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
value text);";
pg "CREATE TABLE IF NOT EXISTS tags
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
resource_type text,
resource integer,
resource_uuid text,
resource_location integer,
active integer,
value text,
creation_time integer,
modification_time integer);";
pg "CREATE TABLE IF NOT EXISTS tags_trash
(id SERIAL PRIMARY KEY,
uuid text UNIQUE NOT NULL,
owner integer REFERENCES users (id) ON DELETE RESTRICT,
name text NOT NULL,
comment text,
resource_type text,
resource integer,
resource_uuid text,
resource_location integer,
active integer,
value text,
creation_time integer,
modification_time integer);";
}
cleanup_sqlite_db () {
# Remove stray port_ranges.
sqlite "DELETE FROM port_ranges WHERE port_list NOT IN (SELECT id FROM port_lists);"
test_sql_exit "Failed to clean up port_lists"
# Remove stray port_ranges_trash.
sqlite "DELETE FROM port_ranges_trash WHERE port_list NOT IN (SELECT id FROM port_lists_trash);"
test_sql_exit "Failed to clean up port_lists_trash"
# Remove stray role_users_trash.
sqlite "DELETE FROM role_users_trash WHERE role NOT IN (SELECT id FROM roles_trash);"
test_sql_exit "Failed to clean up role_users_trash"
# Remove stray task_alerts.
sqlite "DELETE FROM task_alerts WHERE alert_location = 0 AND alert NOT IN (SELECT id FROM alerts);"
test_sql_exit "Failed to clean up task_alerts"
sqlite "DELETE FROM task_alerts WHERE alert_location = 1 AND alert NOT IN (SELECT id FROM alerts_trash);"
test_sql_exit "Failed to clean up task_alerts (trash alerts)"
# Remove stray task_preferences.
sqlite "DELETE FROM task_preferences WHERE task NOT IN (SELECT id FROM tasks);"
test_sql_exit "Failed to clean up task_preferences"
# Remove stray report_format_params_trash.
sqlite "DELETE FROM report_format_params_trash WHERE report_format NOT IN (SELECT id FROM report_formats_trash);"
test_sql_exit "Failed to clean up report_format_params_trash"
# Remove stray report_format_param_options_trash.
sqlite "DELETE FROM report_format_param_options_trash WHERE report_format_param NOT IN (SELECT id FROM report_format_params_trash);"
test_sql_exit "Failed to clean up report_format_param_options_trash"
# Remove stray report_hosts. Not sure where these came from.
sqlite "DELETE FROM report_hosts WHERE report NOT IN (SELECT id FROM reports);"
test_sql_exit "Failed to clean up report_hosts"
# Remove stray report_host_details. Not sure where these came from.
sqlite "DELETE FROM report_host_details WHERE report_host NOT IN (SELECT id FROM report_hosts);"
test_sql_exit "Failed to clean up report_host_details"
# Make end_time in tasks consistent.
#
# Manager --migrate only does this in migrate_135_to_136, but Postgres needs
# the end_times to be a single type for import.
sqlite "UPDATE tasks SET end_time = (SELECT reports.end_time FROM reports WHERE task = tasks.id ORDER BY id DESC LIMIT 1) WHERE EXISTS (SELECT id FROM reports WHERE task = tasks.id);"
test_sql_exit "Failed to make end_time in tasks consistent"
# Make end_time in report_hosts consistent.
# 2011-11-03T12:41:34+02:00, 2011-11-08T19:57:06Z
sqlite "UPDATE report_hosts SET end_time = strftime ('%s', end_time) WHERE end_time GLOB '*-*-*T*:*:*[+-]*:*' OR end_time GLOB '*-*-*T*:*:*Z';"
test_sql_exit "Failed to make end_time in report_hosts consistent"
# Fri Sep 3 05:12:34 2010
#
# Assume that this time matches the current timezone, may change the
# time, but it seems these are only from very old reports anyway.
echo "BEGIN EXCLUSIVE;
CREATE TEMPORARY TABLE months (number, name);
INSERT INTO months (number, name) VALUES ('01', 'Jan');
INSERT INTO months (number, name) VALUES ('02', 'Feb');
INSERT INTO months (number, name) VALUES ('03', 'Mar');
INSERT INTO months (number, name) VALUES ('04', 'Apr');
INSERT INTO months (number, name) VALUES ('05', 'May');
INSERT INTO months (number, name) VALUES ('06', 'Jun');
INSERT INTO months (number, name) VALUES ('07', 'Jul');
INSERT INTO months (number, name) VALUES ('08', 'Aug');
INSERT INTO months (number, name) VALUES ('09', 'Sep');
INSERT INTO months (number, name) VALUES ('10', 'Oct');
INSERT INTO months (number, name) VALUES ('11', 'Nov');
INSERT INTO months (number, name) VALUES ('12', 'Dec');
UPDATE report_hosts SET end_time = strftime ('%s', substr (end_time, 21) || '-' || (select number from months where name = substr (end_time, 5, 3)) || '-' || replace (substr (end_time, 9, 2), ' ', '0') || 'T' || substr (end_time, 12, 9)) WHERE end_time GLOB '??? ??? ?? ??:??:?? ????';
COMMIT;" | $SQLITE3 -batch $SQLITE_DB
test_sql_exit "Failed to make end_time in report_hosts consistent"
# There was an error in the C code migrate_56_to_57 which left this
# table lying around.
sqlite "DROP TABLE IF EXISTS escalator_condition_data_trash;"
test_sql_exit "Failed to remove escalator_condition_data_trash"
}
# Replace the value of a column if the cast does not work
clean_column () {
table="$1"
column="$2"
type="$3"
replace="$4"
if [ -z "$type" ]
then
type="numeric"
fi
if [ -z "$replace" ]
then
replace="null"
fi
x=$(sqlite "SELECT \`$column\` FROM $table LIMIT 1")
if [ "$?" -eq 0 ]
then
sqlite "UPDATE $table SET \`$column\` = $replace WHERE (CAST (\`$column\` AS $type) IS NOT \`$column\`);"
fi
}
# Set all columns that do not fit the type expected by postgres to null
clean_columns () {
log_info "Cleaning columns."
psql tasks -Atc "SELECT DISTINCT table_name, column_name, data_type
FROM information_schema.columns
WHERE table_schema='public'
AND data_type != 'text'
AND data_type != 'bytea'" | while read -r line
do
table=`echo "$line" | cut -d '|' -f1`
column=`echo "$line" | cut -d '|' -f2`
type=`echo "$line" | cut -d '|' -f3`
if [ "$column" != "id" ]
then
clean_column "$table" "$column" "$type"
fi
done
}
clean_fkey_columns () {
log_info "Cleaning foreign key columns."
psql tasks -Atc "SELECT tc.table_name, kcu.column_name,
ccu.table_name, ccu.column_name
FROM information_schema.table_constraints AS tc
JOIN information_schema.key_column_usage AS kcu
ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage AS ccu
ON ccu.constraint_name = tc.constraint_name
WHERE constraint_type = 'FOREIGN KEY'
AND tc.constraint_schema = 'public'" | while read -r line
do
table=`echo "$line" | cut -d '|' -f1`
column=`echo "$line" | cut -d '|' -f2`
ftable=`echo "$line" | cut -d '|' -f3`
fcolumn=`echo "$line" | cut -d '|' -f4`
sqlite "UPDATE $table SET \`$column\` = null WHERE \`$column\` NOT IN (SELECT \`$fcolumn\` FROM $ftable);"
done
}
copy_data () {
if [ $# -ne 1 ]
then
log_notice "Internal Error: copy_data must have an arg"
exit 1
fi
log_info "Copying SQLite3 data:"
TEMP_DIR=`mktemp -d`
log_debug "TEMP_DIR: $TEMP_DIR"
# Tables must be ordered to respect foreign keys.
#
# These tables are left out: report_counts nvts nvt_cves
log_info "Copying SQLite3 data: checking that tables match"
TABLES=$1
log_debug "TABLES: $TABLES"
SQLITE_TABLES=`sqlite "SELECT tbl_name FROM sqlite_master WHERE type='table' and tbl_name not like 'sqlite_%';"`
test_sql_exit "Failed get table list"
log_debug "SQLITE_TABLES: $SQLITE_TABLES"
echo $SQLITE_TABLES | tr -s ' ' '\n' | sort - | grep --invert-match "^\(nvt_cves\|nvts\|report_counts\)" > $TEMP_DIR/sqlite_tables_sorted
echo $TABLES | tr -s ' ' '\n' | sort - | diff - $TEMP_DIR/sqlite_tables_sorted
if [ $? -ne 0 ]
then
log_notice "Internal Error: tables being copied do not match tables in SQLite db"
exit 1
fi
log_info "Copying SQLite3 data: copying tables"
for TABLE in $TABLES; do
# Get the Postgres columns because the import CSV must be in that order.
#
# This also works around extra columns that were hanging around in old
# database.
#
log_info "Copying SQLite3 data: copying table $TABLE"
COLS=`pg "SELECT string_agg ('\"' || column_name || '\"', ',') from (select column_name from information_schema.columns where table_schema = 'public' and table_name = '$TABLE' order by ordinal_position) as sub;"`
log_debug "COLS: $COLS"
# Pipe SQLite CSV output into Postgres COPY FROM.
$SQLITE3 -csv $SQLITE_DB "SELECT $COLS FROM $TABLE;" | pg "COPY $TABLE ($COLS) FROM STDIN DELIMITER ',' CSV";
test_sql_exit "Could not generate CSV for $TABLE"
done
# Force a --rebuild.
pg "DELETE FROM meta WHERE name = 'nvts_feed_version';"
pg "UPDATE configs SET family_count = 0, nvt_count = 0, families_growing = 0, nvts_growing = 0;"
log_info "Copying SQLite3 data: resetting Postgres sequences"
# Reset ID sequences, because the COPY specified the ID values explicitly.
pg "SELECT 'SELECT 1'
|| ' WHERE'
|| ' SETVAL'
|| ' (' || quote_literal (quote_ident (PGT.schemaname)
|| '.' || quote_ident (S.relname)) || ','
|| ' COALESCE'
|| ' ((SELECT MAX (' || quote_ident (C.attname) || ')'
|| ' FROM ' || quote_ident (PGT.schemaname)
|| '.' || quote_ident (T.relname) || '),'
|| ' 1))'
|| ' < 0;'
FROM pg_class AS S,
pg_depend AS D,
pg_class AS T,
pg_attribute AS C,
pg_tables AS PGT
WHERE S.relkind = 'S'
AND S.oid = D.objid
AND D.refobjid = T.oid
AND D.refobjid = C.attrelid
AND D.refobjsubid = C.attnum
AND T.relname = PGT.tablename
ORDER BY S.relname;" > $TEMP_DIR/reset.sql;
test_sql_exit "Could not generate reset SQL"
$PSQL -f $TEMP_DIR/reset.sql | sed '/^\s*$/d'
test_sql_exit "Could not run reset SQL"
rm -rf $TEMP_DIR
}
#### Schema Migrators
##
## NB These only need to migrate the schema.
##
TABLES_133="users agents agents_trash configs config_preferences meta nvt_preferences nvt_selectors lsc_credentials lsc_credentials_trash port_lists port_ranges targets schedules slaves tasks reports report_hosts report_formats report_format_params report_format_param_options results report_results task_files configs_trash config_preferences_trash report_formats_trash report_format_params_trash report_format_param_options_trash schedules_trash slaves_trash targets_trash report_host_details task_preferences port_lists_trash port_ranges_trash alerts_trash alert_event_data_trash alert_method_data_trash alerts alert_condition_data alert_event_data alert_method_data task_alerts alert_condition_data_trash groups group_users filters filters_trash port_names settings roles role_users overrides overrides_trash notes notes_trash groups_trash roles_trash tags_trash group_users_trash role_users_trash scanners scanners_trash permissions_trash permissions tags"
migrate_133_to_134 () {
pg "ALTER TABLE targets ADD COLUMN esxi_lsc_credential integer;"
pg "ALTER TABLE targets_trash ADD COLUMN esxi_lsc_credential integer;"
pg "ALTER TABLE targets_trash ADD COLUMN esxi_location integer;"
}
TABLES_134=$TABLES_133
migrate_134_to_135 () {
pg "DROP TABLE report_results;"
}
TABLES_135=`echo $TABLES_134 | sed -e "s/ report_results//g"`
migrate_135_to_136 () {
: # Schema stayed the same.
}
TABLES_136=$TABLES_135
migrate_136_to_137 () {
: # Schema stayed the same.
}
TABLES_137=$TABLES_136
migrate_137_to_138 () {
: # Schema stayed the same.
}
TABLES_138=$TABLES_137
migrate_138_to_139 () {
pg "ALTER TABLE nvts ADD COLUMN solution_type text;"
}
TABLES_139=$TABLES_138
migrate_139_to_140 () {
: # Schema stayed the same.
}
TABLES_140=$TABLES_139
migrate_140_to_141 () {
pg "ALTER TABLE config_preferences ADD COLUMN default_value text;"
pg "ALTER TABLE config_preferences_trash ADD COLUMN default_value text;"
}
TABLES_141=$TABLES_140
migrate_141_to_142 () {
: # Schema stayed the same.
}
TABLES_142=$TABLES_141
migrate_142_to_143 () {
: # Schema stayed the same.
}
TABLES_143=$TABLES_142
migrate_143_to_144 () {
pg "ALTER TABLE nvts ADD COLUMN qod INTEGER;"
pg "ALTER TABLE nvts ADD COLUMN qod_type TEXT;"
pg "ALTER TABLE results ADD COLUMN qod_type TEXT;"
}
TABLES_144=$TABLES_143
migrate_144_to_145 () {
pg "ALTER TABLE tasks ADD COLUMN schedule_periods INTEGER;"
}
TABLES_145=$TABLES_144
migrate_145_to_146 () {
pg "DROP VIEW IF EXISTS result_new_severities;"
pg "DROP VIEW IF EXISTS result_overrides;"
pg "CREATE TABLE IF NOT EXISTS auth_cache \
(id SERIAL PRIMARY KEY, \
username text NOT NULL, \
hash text, \
method integer, \
creation_time integer);"
}
TABLES_146="$TABLES_145 auth_cache"
migrate_146_to_147 () {
pg "ALTER TABLE report_counts ADD COLUMN min_qod INTEGER;"
}
TABLES_147=$TABLES_146
migrate_147_to_148 () {
: # Schema stayed the same.
}
TABLES_148=$TABLES_147
migrate_148_to_149 () {
pg "ALTER TABLE tasks ADD COLUMN scanner_location INTEGER;"
}
TABLES_149=$TABLES_148
migrate_149_to_150 () {
pg "DROP VIEW IF EXISTS result_new_severities;"
}
TABLES_150=$TABLES_149
migrate_150_to_151 () {
# Create new tables.
pg "CREATE TABLE IF NOT EXISTS hosts \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
creation_time integer, \
modification_time integer);"
pg "CREATE TABLE IF NOT EXISTS host_identifiers \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
host integer REFERENCES hosts (id) ON DELETE RESTRICT, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
value text NOT NULL, \
source_type text NOT NULL, \
source_id text NOT NULL, \
source_data text NOT NULL, \
creation_time integer, \
modification_time integer);"
pg "CREATE TABLE IF NOT EXISTS oss \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
creation_time integer, \
modification_time integer);"
pg "CREATE TABLE IF NOT EXISTS host_oss \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
host integer REFERENCES hosts (id) ON DELETE RESTRICT, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
os integer REFERENCES oss (id) ON DELETE RESTRICT, \
source_type text NOT NULL, \
source_id text NOT NULL, \
source_data text NOT NULL, \
creation_time integer, \
modification_time integer);"
pg "CREATE TABLE IF NOT EXISTS host_max_severities \
(id SERIAL PRIMARY KEY, \
host integer REFERENCES hosts (id) ON DELETE RESTRICT, \
severity real, \
source_type text NOT NULL, \
source_id text NOT NULL, \
creation_time integer);"
pg "CREATE TABLE IF NOT EXISTS host_details \
(id SERIAL PRIMARY KEY, \
host integer REFERENCES hosts (id) ON DELETE RESTRICT, \
source_type text NOT NULL, \
source_id text NOT NULL, \
detail_source_type text, \
detail_source_name text, \
detail_source_description text, \
name text, \
value text);"
}
# FIX host_details added between 150 and 151 (at 151 they have to be there, at 150 coping will fail if they're not there (if missing from sqlite then ignore))
TABLES_151="$TABLES_150 hosts host_identifiers oss host_oss host_max_severities host_details"
migrate_151_to_152 () {
: # Schema stayed the same.
}
TABLES_152=$TABLES_151
migrate_152_to_153 () {
: # Schema stayed the same.
}
TABLES_153=$TABLES_152
migrate_153_to_154 () {
pg "DROP TABLE lsc_credentials;"
pg "DROP TABLE lsc_credentials_trash;"
# Create new tables.
pg "CREATE TABLE credentials \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
creation_time integer, \
modification_time integer, \
type text);"
pg "CREATE TABLE credentials_trash \
(id SERIAL PRIMARY KEY, \
uuid text UNIQUE NOT NULL, \
owner integer REFERENCES users (id) ON DELETE RESTRICT, \
name text NOT NULL, \
comment text, \
creation_time integer, \
modification_time integer, \
type text);"
pg "CREATE TABLE credentials_data \
(id SERIAL PRIMARY KEY, \
credential INTEGER REFERENCES credentials (id) ON DELETE RESTRICT, \
type TEXT, \
value TEXT);"
pg "CREATE TABLE credentials_trash_data \
(id SERIAL PRIMARY KEY, \
credential INTEGER REFERENCES credentials_trash (id) ON DELETE RESTRICT, \
type TEXT, \
value TEXT);"
}
TABLES_153_NO_USERS=`echo $TABLES_153 | sed -e "s/^users //g"`
TABLES_154=`echo "users credentials credentials_trash credentials_data credentials_trash_data $TABLES_153_NO_USERS" | sed -e "s/\( lsc_credentials\| lsc_credentials_trash\)//g"`
migrate_154_to_155 () {
pg "ALTER TABLE reports ADD COLUMN flags INTEGER;"
}
TABLES_155=$TABLES_154
migrate_155_to_156 () {
pg "ALTER TABLE targets DROP COLUMN lsc_credential;"
pg "ALTER TABLE targets DROP COLUMN ssh_port;"
pg "ALTER TABLE targets DROP COLUMN smb_lsc_credential;"
pg "ALTER TABLE targets DROP COLUMN esxi_lsc_credential;"
pg "ALTER TABLE targets RENAME COLUMN port_range TO port_list;"
pg "ALTER TABLE targets_trash DROP COLUMN lsc_credential;"
pg "ALTER TABLE targets_trash DROP COLUMN ssh_location;"
pg "ALTER TABLE targets_trash DROP COLUMN ssh_port;"
pg "ALTER TABLE targets_trash DROP COLUMN smb_lsc_credential;"
pg "ALTER TABLE targets_trash DROP COLUMN smb_location;"
pg "ALTER TABLE targets_trash DROP COLUMN esxi_lsc_credential;"
pg "ALTER TABLE targets_trash DROP COLUMN esxi_location;"
pg "ALTER TABLE targets_trash RENAME COLUMN port_range TO port_list;"
# Create new tables.
pg "CREATE TABLE IF NOT EXISTS targets_login_data \
(id SERIAL PRIMARY KEY, \
target INTEGER REFERENCES targets (id), \
type TEXT, \
credential INTEGER REFERENCES credentials (id), \
port INTEGER);"
pg "CREATE TABLE IF NOT EXISTS targets_trash_login_data \
(id SERIAL PRIMARY KEY, \
target INTEGER REFERENCES targets_trash (id), \
type TEXT, \
credential INTEGER, \
port INTEGER, \
credential_location INTEGER);"
}
TABLES_156="$TABLES_155 targets_login_data targets_trash_login_data"
migrate_156_to_157 () {
pg "ALTER TABLE slaves ADD COLUMN credential INTEGER REFERENCES credentials (id) ON DELETE RESTRICT;"
pg "ALTER TABLE slaves_trash ADD COLUMN credential INTEGER;"
pg "ALTER TABLE slaves_trash ADD COLUMN credential_location INTEGER;"
pg "ALTER TABLE slaves DROP COLUMN login;"
pg "ALTER TABLE slaves DROP COLUMN password;"
pg "ALTER TABLE slaves_trash DROP COLUMN login;"
pg "ALTER TABLE slaves_trash DROP COLUMN password;"
}
TABLES_157=$TABLES_156
migrate_157_to_158 () {
pg "ALTER TABLE configs ADD COLUMN scanner INTEGER"
" REFERENCES scanners (id) ON DELETE RESTRICT;"
pg "ALTER TABLE configs_trash ADD COLUMN scanner INTEGER"
" REFERENCES scanners (id) ON DELETE RESTRICT;"
}
TABLES_158=$TABLES_157
migrate_158_to_159 () {
pg "ALTER TABLE scanners ADD COLUMN credential INTEGER REFERENCES credentials (id) ON DELETE RESTRICT;"
pg "ALTER TABLE scanners_trash ADD COLUMN credential INTEGER;"
pg "ALTER TABLE scanners_trash ADD COLUMN credential_location INTEGER;"
pg "ALTER TABLE scanners DROP COLUMN key_pub;"
pg "ALTER TABLE scanners DROP COLUMN key_priv;"
pg "ALTER TABLE scanners_trash DROP COLUMN key_pub;"
pg "ALTER TABLE scanners_trash DROP COLUMN key_priv;"
}
TABLES_159=$TABLES_158
migrate_159_to_160 () {
: # Schema stayed the same.
}
TABLES_160=$TABLES_159
migrate_160_to_161 () {
: # Schema stayed the same.
}
TABLES_161=$TABLES_160
migrate_161_to_162 () {
pg "ALTER TABLE credentials ADD COLUMN allow_insecure INTEGER;"
pg "ALTER TABLE credentials_trash ADD COLUMN allow_insecure INTEGER;"
}
TABLES_162=$TABLES_161
migrate_162_to_163 () {
: # Schema stayed the same.
}
TABLES_163=$TABLES_162
migrate_163_to_164 () {
: # Schema stayed the same.
}
TABLES_164=$TABLES_163
migrate_164_to_165 () {
pg "ALTER TABLE config_preferences ADD COLUMN hr_name TEXT;"
pg "ALTER TABLE config_preferences_trash ADD COLUMN hr_name TEXT;"
}
TABLES_165=$TABLES_164
migrate_165_to_166 () {
: # Schema stayed the same.
}
TABLES_166=$TABLES_165
migrate_166_to_167 () {
# Create new table.
pg "CREATE TABLE IF NOT EXISTS resources_predefined \
(id SERIAL PRIMARY KEY, resource_type text, resource INTEGER);"
}
TABLES_167="$TABLES_166 resources_predefined"
migrate_167_to_168 () {
: # Schema stayed the same.
}
TABLES_168=$TABLES_167
migrate_168_to_169 () {
: # Schema stayed the same.
}
TABLES_169=$TABLES_168
migrate_169_to_170 () {
: # Schema stayed the same.
}
TABLES_170=$TABLES_169
migrate_170_to_171 () {
: # Schema stayed the same.
}
TABLES_171=$TABLES_170
migrate_171_to_172 () {
: # Schema stayed the same.
}
TABLES_172=$TABLES_171
migrate_172_to_173 () {
pg "ALTER TABLE nvts DROP COLUMN summary;"
}
TABLES_173=$TABLES_172
migrate_173_to_174 () {
: # Schema stayed the same.
}
TABLES_174=$TABLES_173
migrate_174_to_175 () {
: # Schema stayed the same.
}
TABLES_175=$TABLES_174
migrate_175_to_176 () {
: # Schema stayed the same.
}
TABLES_176=$TABLES_175
migrate_176_to_177 () {
: # Schema stayed the same.
}
TABLES_177=$TABLES_176
migrate_177_to_178 () {
: # Schema stayed the same.
}
TABLES_178=$TABLES_177
migrate_178_to_179 () {
pg "ALTER TABLE reports ADD COLUMN slave_username TEXT;"
pg "ALTER TABLE reports ADD COLUMN slave_password TEXT;"
}
TABLES_179=$TABLES_178
migrate_179_to_180 () {
pg "DROP TABLE slaves;"
pg "DROP TABLE slaves_trash;"
}
TABLES_180=`echo $TABLES_178 | sed -e "s/ slaves_trash//g" | sed -e "s/ slaves//g"`
migrate_180_to_181 () {
pg "ALTER TABLE tasks DROP COLUMN slave;"
pg "ALTER TABLE tasks DROP COLUMN slave_location;"
}
TABLES_181=$TABLES_180
migrate_181_to_182 () {
: # Schema stayed the same.
}
TABLES_182=$TABLES_181
migrate_182_to_183 () {
pg "ALTER TABLE reports DROP COLUMN slave_username;"
pg "ALTER TABLE reports DROP COLUMN slave_password;"
}
TABLES_183=$TABLES_182
migrate_183_to_184 () {
: # Schema stayed the same.
}
TABLES_184=$TABLES_183
## Migrate the schema of the Postgres database from version $1 to $2.
##
migrate () {
if [ $# -ne 2 ]
then
log_notice "Internal Error: migrate must have two args"
exit 1
fi
TO=$2
log_info "Migrating schema from $1 to $TO."
if [ $1 -eq $TO ]
then
return
fi
case $1 in
133)
migrate_133_to_134
if [ $TO -eq 134 ]
then
return
fi
migrate 134 $TO;;
134)
migrate_134_to_135
if [ $TO -eq 135 ]
then
return
fi
migrate 135 $TO;;
135)
migrate_135_to_136
if [ $TO -eq 136 ]
then
return
fi
migrate 136 $TO;;
136)
migrate_136_to_137
if [ $TO -eq 137 ]
then
return
fi
migrate 137 $TO;;
137)
migrate_137_to_138
if [ $TO -eq 138 ]
then
return
fi
migrate 138 $TO;;
138)
migrate_138_to_139
if [ $TO -eq 139 ]
then
return
fi
migrate 140 $TO;;
140)
migrate_140_to_141
if [ $TO -eq 141 ]
then
return
fi
migrate 141 $TO;;
141)
migrate_141_to_142
if [ $TO -eq 142 ]
then
return
fi
migrate 142 $TO;;
142)
migrate_142_to_143
if [ $TO -eq 143 ]
then
return
fi
migrate 143 $TO;;
143)
migrate_143_to_144
if [ $TO -eq 144 ]
then
return
fi
migrate 144 $TO;;
144)
migrate_144_to_145
if [ $TO -eq 145 ]
then
return
fi
migrate 145 $TO;;
145)
migrate_145_to_146
if [ $TO -eq 146 ]
then
return
fi
migrate 146 $TO;;
146)
migrate_146_to_147
if [ $TO -eq 147 ]
then
return
fi
migrate 147 $TO;;
147)
migrate_147_to_148
if [ $TO -eq 148 ]
then
return
fi
migrate 148 $TO;;
148)
migrate_148_to_149
if [ $TO -eq 149 ]
then
return
fi
migrate 149 $TO;;
149)
migrate_149_to_150
if [ $TO -eq 150 ]
then
return
fi
migrate 150 $TO;;
150)
migrate_150_to_151
if [ $TO -eq 151 ]
then
return
fi
migrate 151 $TO;;
151)
migrate_151_to_152
if [ $TO -eq 152 ]
then
return
fi
migrate 152 $TO;;
152)
migrate_152_to_153
if [ $TO -eq 153 ]
then
return
fi
migrate 153 $TO;;
153)
migrate_153_to_154
if [ $TO -eq 154 ]
then
return
fi
migrate 154 $TO;;
154)
migrate_154_to_155
if [ $TO -eq 155 ]
then
return
fi
migrate 155 $TO;;
155)
migrate_155_to_156
if [ $TO -eq 156 ]
then
return
fi
migrate 156 $TO;;
156)
migrate_156_to_157
if [ $TO -eq 157 ]
then
return
fi
migrate 157 $TO;;
157)
migrate_157_to_158
if [ $TO -eq 158 ]
then
return
fi
migrate 158 $TO;;
158)
migrate_158_to_159
if [ $TO -eq 159 ]
then
return
fi
migrate 159 $TO;;
159)
migrate_159_to_160
if [ $TO -eq 160 ]
then
return
fi
migrate 160 $TO;;
160)
migrate_160_to_161
if [ $TO -eq 161 ]
then
return
fi
migrate 161 $TO;;
161)
migrate_161_to_162
if [ $TO -eq 162 ]
then
return
fi
migrate 162 $TO;;
162)
migrate_162_to_163
if [ $TO -eq 163 ]
then
return
fi
migrate 163 $TO;;
163)
migrate_163_to_164
if [ $TO -eq 164 ]
then
return
fi
migrate 164 $TO;;
164)
migrate_164_to_165
if [ $TO -eq 165 ]
then
return
fi
migrate 165 $TO;;
165)
migrate_165_to_166
if [ $TO -eq 166 ]
then
return
fi
migrate 166 $TO;;
166)
migrate_166_to_167
if [ $TO -eq 167 ]
then
return
fi
migrate 167 $TO;;
167)
migrate_167_to_168
if [ $TO -eq 168 ]
then
return
fi
migrate 168 $TO;;
168)
migrate_168_to_169
if [ $TO -eq 169 ]
then
return
fi
migrate 169 $TO;;
169)
migrate_169_to_170
if [ $TO -eq 170 ]
then
return
fi
migrate 170 $TO;;
170)
migrate_170_to_171
if [ $TO -eq 171 ]
then
return
fi
migrate 171 $TO;;
171)
migrate_171_to_172
if [ $TO -eq 172 ]
then
return
fi
migrate 172 $TO;;
172)
migrate_172_to_173
if [ $TO -eq 173 ]
then
return
fi
migrate 173 $TO;;
173)
migrate_173_to_174
if [ $TO -eq 174 ]
then
return
fi
migrate 174 $TO;;
174)
migrate_174_to_175
if [ $TO -eq 175 ]
then
return
fi
migrate 175 $TO;;
175)
migrate_175_to_176
if [ $TO -eq 176 ]
then
return
fi
migrate 176 $TO;;
176)
migrate_176_to_177
if [ $TO -eq 177 ]
then
return
fi
migrate 177 $TO;;
177)
migrate_177_to_178
if [ $TO -eq 178 ]
then
return
fi
migrate 178 $TO;;
178)
migrate_178_to_179
if [ $TO -eq 179 ]
then
return
fi
migrate 179 $TO;;
179)
migrate_179_to_180
if [ $TO -eq 180 ]
then
return
fi
migrate 180 $TO;;
180)
migrate_180_to_181
if [ $TO -eq 181 ]
then
return
fi
migrate 181 $TO;;
181)
migrate_181_to_182
if [ $TO -eq 182 ]
then
return
fi
migrate 182 $TO;;
182)
migrate_182_to_183
if [ $TO -eq 183 ]
then
return
fi
migrate 183 $TO;;
183)
migrate_183_to_184
if [ $TO -eq 184 ]
then
return
fi
migrate 184 $TO;;
*)
log_notice "Version in Postgres database out of sync."
exit 1
esac
}
## Create PG 133, migrate it to version $1, then clean SQLite db.
#
create_migrate_clean () {
log_info "Creating Postgres database (version $1)."
create_tables_133
cleanup_sqlite_db
migrate 133 $1
clean_columns
clean_fkey_columns
}
## Create a Postgres database based on the SQLite one.
#
# Migrate the Postgres schema from the 133 base version; then clean the SQLite
# data; then copy the SQLite data to Postgres.
#
# There's no need to get the Postgres database to the latest version. Postgres
# Manager will do that. Just get from the SQLite database to the same version,
# but under Postgres.
#
create () {
DB_VERSION=`sqlite "select value from meta where name = 'database_version';" 2>/dev/null | tr -d '\n\r' || echo 0`
case "$DB_VERSION" in
133)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_133"
;;
134)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_134"
;;
135)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_135"
;;
136)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_136"
;;
137)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_137"
;;
138)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_138"
;;
139)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_139"
;;
140)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_140"
;;
141)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_141"
;;
142)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_142"
;;
143)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_143"
;;
144)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_144"
;;
145)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_145"
;;
146)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_146"
;;
147)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_147"
;;
148)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_148"
;;
149)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_149"
;;
150)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_150"
;;
151)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_151"
;;
152)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_152"
;;
153)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_153"
;;
154)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_154"
;;
155)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_155"
;;
156)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_156"
;;
157)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_157"
;;
158)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_158"
;;
159)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_159"
;;
160)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_160"
;;
161)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_161"
;;
162)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_162"
;;
163)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_163"
;;
164)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_164"
;;
165)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_165"
;;
166)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_166"
;;
167)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_167"
;;
168)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_168"
;;
169)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_169"
;;
170)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_170"
;;
171)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_171"
;;
172)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_172"
;;
173)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_173"
;;
174)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_174"
;;
175)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_175"
;;
176)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_176"
;;
177)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_177"
;;
178)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_178"
;;
179)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_179"
;;
180)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_180"
;;
181)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_181"
;;
182)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_182"
;;
183)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_183"
;;
184)
create_migrate_clean $DB_VERSION
copy_data "$TABLES_184"
;;
*)
log_notice "Database version not supported."
exit 1
esac
}
do_self_test () {
if [ -z "$SELFTEST_STDERR" ]
then
SELFTEST_STDERR=0
fi
if [ -z "$SQLITE" ]; then
if [ 0 -ne $SELFTEST_STDERR ]
then
echo "sqlite3 not found (required)." 1>&2
fi
log_err "sqlite3 not found (required)."
SELFTEST_FAIL=1
fi
if [ ! -f $SQLITE_DB ]
then
if [ 0 -ne $SELFTEST_STDERR ]
then
echo "SQLITE_DB ($SQLITE_DB) not found." 1>&2
fi
log_err "SQLITE_DB ($SQLITE_DB) not found."
SELFTEST_FAIL=1
fi
}
while test $# -gt 0; do
case "$1" in
--version)
echo $VERSION
exit 0
;;
--help)
do_help
exit 0
;;
--selftest)
SELFTEST_FAIL=0
SELFTEST_STDERR=1
do_self_test
exit $SELFTEST_FAIL
;;
esac
shift
done
SELFTEST_FAIL=0
do_self_test
if [ $SELFTEST_FAIL -ne 0 ]
then
exit 1
fi
is_db_broken
if [ $DB_BROKEN -eq 1 ]
then
log_notice "Corrupt database, skipping migration."
exit 1
else
does_pg_db_exist
if [ $PG_DB_EXISTS -eq 1 ]
then
log_notice "Postgres database exists already, skipping migration."
exit 0
fi
fi
create
log_info "Done."
exit 0
|