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 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994
|
import itertools
from operator import itemgetter
import re
import sqlalchemy as sa
from sqlalchemy import BigInteger
from sqlalchemy import Column
from sqlalchemy import exc
from sqlalchemy import ForeignKey
from sqlalchemy import ForeignKeyConstraint
from sqlalchemy import Identity
from sqlalchemy import Index
from sqlalchemy import inspect
from sqlalchemy import Integer
from sqlalchemy import join
from sqlalchemy import MetaData
from sqlalchemy import PrimaryKeyConstraint
from sqlalchemy import Sequence
from sqlalchemy import SmallInteger
from sqlalchemy import String
from sqlalchemy import Table
from sqlalchemy import testing
from sqlalchemy import Text
from sqlalchemy import text
from sqlalchemy import UniqueConstraint
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.dialects.postgresql import base as postgresql
from sqlalchemy.dialects.postgresql import DOMAIN
from sqlalchemy.dialects.postgresql import ExcludeConstraint
from sqlalchemy.dialects.postgresql import INET
from sqlalchemy.dialects.postgresql import INTEGER
from sqlalchemy.dialects.postgresql import INTERVAL
from sqlalchemy.dialects.postgresql import pg_catalog
from sqlalchemy.dialects.postgresql import TSRANGE
from sqlalchemy.engine import ObjectKind
from sqlalchemy.engine import ObjectScope
from sqlalchemy.schema import CreateIndex
from sqlalchemy.sql import ddl as sa_ddl
from sqlalchemy.sql.schema import CheckConstraint
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import config
from sqlalchemy.testing import fixtures
from sqlalchemy.testing import mock
from sqlalchemy.testing.assertions import assert_warns
from sqlalchemy.testing.assertions import AssertsExecutionResults
from sqlalchemy.testing.assertions import ComparesIndexes
from sqlalchemy.testing.assertions import eq_
from sqlalchemy.testing.assertions import expect_raises
from sqlalchemy.testing.assertions import expect_warnings
from sqlalchemy.testing.assertions import is_
from sqlalchemy.testing.assertions import is_false
from sqlalchemy.testing.assertions import is_true
from sqlalchemy.types import NullType
class ReflectionFixtures:
@testing.fixture(
params=[
"engine",
"connection",
]
)
def inspect_fixture(self, request, metadata, testing_engine):
engine = request.param
eng = testing_engine()
conn = eng.connect()
if engine == "connection":
yield inspect(eng), conn
else:
yield inspect(conn), conn
conn.close()
class ForeignTableReflectionTest(
ReflectionFixtures, fixtures.TablesTest, AssertsExecutionResults
):
"""Test reflection on foreign tables"""
__requires__ = ("postgresql_test_dblink",)
__only_on__ = "postgresql >= 9.3"
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
from sqlalchemy.testing import config
dblink = config.file_config.get(
"sqla_testing", "postgres_test_db_link"
)
Table(
"testtable",
metadata,
Column("id", Integer, primary_key=True),
Column("data", String(30)),
)
for ddl in [
"CREATE SERVER test_server FOREIGN DATA WRAPPER postgres_fdw "
"OPTIONS (dbname 'test', host '%s')" % dblink,
"CREATE USER MAPPING FOR public \
SERVER test_server options (user 'scott', password 'tiger')",
"CREATE FOREIGN TABLE test_foreigntable ( "
" id INT, "
" data VARCHAR(30) "
") SERVER test_server OPTIONS (table_name 'testtable')",
]:
sa.event.listen(metadata, "after_create", sa.DDL(ddl))
for ddl in [
"DROP FOREIGN TABLE test_foreigntable",
"DROP USER MAPPING FOR public SERVER test_server",
"DROP SERVER test_server",
]:
sa.event.listen(metadata, "before_drop", sa.DDL(ddl))
def test_foreign_table_is_reflected(self, connection):
metadata = MetaData()
table = Table("test_foreigntable", metadata, autoload_with=connection)
eq_(
set(table.columns.keys()),
{"id", "data"},
"Columns of reflected foreign table didn't equal expected columns",
)
def test_get_foreign_table_names(self, inspect_fixture):
inspector, conn = inspect_fixture
ft_names = inspector.get_foreign_table_names()
eq_(ft_names, ["test_foreigntable"])
def test_get_table_names_no_foreign(self, connection):
inspector = inspect(connection)
names = inspector.get_table_names()
eq_(names, ["testtable"])
class PartitionedReflectionTest(fixtures.TablesTest, AssertsExecutionResults):
# partitioned table reflection, issue #4237
__only_on__ = "postgresql >= 10"
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
# the actual function isn't reflected yet
dv = Table(
"data_values",
metadata,
Column("modulus", Integer, nullable=False),
Column("data", String(30)),
Column("q", Integer),
postgresql_partition_by="range(modulus)",
)
# looks like this is reflected prior to #4237
sa.event.listen(
dv,
"after_create",
sa.DDL(
"CREATE TABLE data_values_4_10 PARTITION OF data_values "
"FOR VALUES FROM (4) TO (10)"
),
)
if testing.against("postgresql >= 11"):
Index("my_index", dv.c.q)
def test_get_tablenames(self, connection):
assert {"data_values", "data_values_4_10"}.issubset(
inspect(connection).get_table_names()
)
def test_reflect_cols(self, connection):
cols = inspect(connection).get_columns("data_values")
eq_([c["name"] for c in cols], ["modulus", "data", "q"])
def test_reflect_cols_from_partition(self, connection):
cols = inspect(connection).get_columns("data_values_4_10")
eq_([c["name"] for c in cols], ["modulus", "data", "q"])
@testing.only_on("postgresql >= 11")
def test_reflect_index(self, connection):
idx = inspect(connection).get_indexes("data_values")
eq_(
idx,
[
{
"name": "my_index",
"unique": False,
"column_names": ["q"],
"include_columns": [],
"dialect_options": {"postgresql_include": []},
}
],
)
@testing.only_on("postgresql >= 11")
def test_reflect_index_from_partition(self, connection):
idx = inspect(connection).get_indexes("data_values_4_10")
# note the name appears to be generated by PG, currently
# 'data_values_4_10_q_idx'
eq_(
idx,
[
{
"column_names": ["q"],
"include_columns": [],
"dialect_options": {"postgresql_include": []},
"name": mock.ANY,
"unique": False,
}
],
)
class MaterializedViewReflectionTest(
ReflectionFixtures, fixtures.TablesTest, AssertsExecutionResults
):
"""Test reflection on materialized views"""
__only_on__ = "postgresql >= 9.3"
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
testtable = Table(
"testtable",
metadata,
Column("id", Integer, primary_key=True),
Column("data", String(30)),
)
# insert data before we create the view
@sa.event.listens_for(testtable, "after_create")
def insert_data(target, connection, **kw):
connection.execute(target.insert(), {"id": 89, "data": "d1"})
materialized_view = sa.DDL(
"CREATE MATERIALIZED VIEW test_mview AS SELECT * FROM testtable"
)
plain_view = sa.DDL(
"CREATE VIEW test_regview AS SELECT data FROM testtable"
)
sa.event.listen(testtable, "after_create", plain_view)
sa.event.listen(testtable, "after_create", materialized_view)
sa.event.listen(
testtable,
"after_create",
sa.DDL("COMMENT ON VIEW test_regview IS 'regular view comment'"),
)
sa.event.listen(
testtable,
"after_create",
sa.DDL(
"COMMENT ON MATERIALIZED VIEW test_mview "
"IS 'materialized view comment'"
),
)
sa.event.listen(
testtable,
"after_create",
sa.DDL("CREATE INDEX mat_index ON test_mview(data DESC)"),
)
sa.event.listen(
testtable,
"before_drop",
sa.DDL("DROP MATERIALIZED VIEW test_mview"),
)
sa.event.listen(
testtable, "before_drop", sa.DDL("DROP VIEW test_regview")
)
def test_has_type(self, connection):
insp = inspect(connection)
is_true(insp.has_type("test_mview"))
is_true(insp.has_type("test_regview"))
is_true(insp.has_type("testtable"))
def test_mview_is_reflected(self, connection):
metadata = MetaData()
table = Table("test_mview", metadata, autoload_with=connection)
eq_(
set(table.columns.keys()),
{"id", "data"},
"Columns of reflected mview didn't equal expected columns",
)
def test_mview_select(self, connection):
metadata = MetaData()
table = Table("test_mview", metadata, autoload_with=connection)
eq_(connection.execute(table.select()).fetchall(), [(89, "d1")])
def test_get_view_names(self, inspect_fixture):
insp, conn = inspect_fixture
eq_(set(insp.get_view_names()), {"test_regview"})
def test_get_materialized_view_names(self, inspect_fixture):
insp, conn = inspect_fixture
eq_(set(insp.get_materialized_view_names()), {"test_mview"})
def test_get_view_names_reflection_cache_ok(self, connection):
insp = inspect(connection)
eq_(set(insp.get_view_names()), {"test_regview"})
eq_(
set(insp.get_materialized_view_names()),
{"test_mview"},
)
eq_(
set(insp.get_view_names()).union(
insp.get_materialized_view_names()
),
{"test_regview", "test_mview"},
)
def test_get_view_definition(self, connection):
insp = inspect(connection)
def normalize(definition):
# pg16 returns "SELECT" without qualifying tablename.
# older pgs include it
definition = re.sub(
r"testtable\.(\w+)", lambda m: m.group(1), definition
)
return re.sub(r"[\n\t ]+", " ", definition.strip())
eq_(
normalize(insp.get_view_definition("test_mview")),
"SELECT id, data FROM testtable;",
)
eq_(
normalize(insp.get_view_definition("test_regview")),
"SELECT data FROM testtable;",
)
def test_get_view_comment(self, connection):
insp = inspect(connection)
eq_(
insp.get_table_comment("test_regview"),
{"text": "regular view comment"},
)
eq_(
insp.get_table_comment("test_mview"),
{"text": "materialized view comment"},
)
def test_get_multi_view_comment(self, connection):
insp = inspect(connection)
eq_(
insp.get_multi_table_comment(),
{(None, "testtable"): {"text": None}},
)
plain = {(None, "test_regview"): {"text": "regular view comment"}}
mat = {(None, "test_mview"): {"text": "materialized view comment"}}
eq_(insp.get_multi_table_comment(kind=ObjectKind.VIEW), plain)
eq_(
insp.get_multi_table_comment(kind=ObjectKind.MATERIALIZED_VIEW),
mat,
)
eq_(
insp.get_multi_table_comment(kind=ObjectKind.ANY_VIEW),
{**plain, **mat},
)
eq_(
insp.get_multi_table_comment(
kind=ObjectKind.ANY_VIEW, scope=ObjectScope.TEMPORARY
),
{},
)
def test_get_multi_view_indexes(self, connection):
insp = inspect(connection)
eq_(insp.get_multi_indexes(), {(None, "testtable"): []})
exp = {
"name": "mat_index",
"unique": False,
"column_names": ["data"],
"column_sorting": {"data": ("desc",)},
}
if connection.dialect.server_version_info >= (11, 0):
exp["include_columns"] = []
exp["dialect_options"] = {"postgresql_include": []}
plain = {(None, "test_regview"): []}
mat = {(None, "test_mview"): [exp]}
eq_(insp.get_multi_indexes(kind=ObjectKind.VIEW), plain)
eq_(insp.get_multi_indexes(kind=ObjectKind.MATERIALIZED_VIEW), mat)
eq_(insp.get_multi_indexes(kind=ObjectKind.ANY_VIEW), {**plain, **mat})
eq_(
insp.get_multi_indexes(
kind=ObjectKind.ANY_VIEW, scope=ObjectScope.TEMPORARY
),
{},
)
class DomainReflectionTest(fixtures.TestBase, AssertsExecutionResults):
"""Test PostgreSQL domains"""
__only_on__ = "postgresql > 8.3"
__sparse_driver_backend__ = True
# these fixtures are all currently using individual test scope,
# on a connection that's in a transaction that's rolled back.
# previously, this test would build up all the domains / tables
# at the class level and commit them. PostgreSQL seems to be extremely
# fast at building up / tearing down domains / schemas etc within an
# uncommitted transaction so it seems OK to keep these at per-test
# scope.
@testing.fixture()
def broken_nullable_domains(self):
if not testing.requires.postgresql_working_nullable_domains.enabled:
config.skip_test(
"reflection of nullable domains broken on PG 17.0-17.2"
)
@testing.fixture()
def testdomain(self, connection, broken_nullable_domains):
connection.exec_driver_sql(
"CREATE DOMAIN testdomain INTEGER NOT NULL DEFAULT 42"
)
yield
connection.exec_driver_sql("DROP DOMAIN testdomain")
@testing.fixture
def testtable(self, connection, testdomain):
connection.exec_driver_sql(
"CREATE TABLE testtable (question integer, answer testdomain)"
)
yield
connection.exec_driver_sql("DROP TABLE testtable")
@testing.fixture
def nullable_domains(self, connection, broken_nullable_domains):
connection.exec_driver_sql(
'CREATE DOMAIN nullable_domain AS TEXT COLLATE "C" CHECK '
"(VALUE IN('FOO', 'BAR'))"
)
connection.exec_driver_sql(
"CREATE DOMAIN not_nullable_domain AS TEXT NOT NULL"
)
yield
connection.exec_driver_sql("DROP DOMAIN nullable_domain")
connection.exec_driver_sql("DROP DOMAIN not_nullable_domain")
@testing.fixture
def nullable_domain_table(self, connection, nullable_domains):
connection.exec_driver_sql(
"CREATE TABLE nullable_domain_test "
"(not_nullable_domain_col nullable_domain not null,"
"nullable_local not_nullable_domain)"
)
yield
connection.exec_driver_sql("DROP TABLE nullable_domain_test")
@testing.fixture
def enum_domain(self, connection):
connection.exec_driver_sql("CREATE TYPE testtype AS ENUM ('test')")
connection.exec_driver_sql("CREATE DOMAIN enumdomain AS testtype")
yield
connection.exec_driver_sql("drop domain enumdomain")
connection.exec_driver_sql("drop type testtype")
@testing.fixture
def enum_table(self, connection, enum_domain):
connection.exec_driver_sql(
"CREATE TABLE enum_test (id integer, data enumdomain)"
)
yield
connection.exec_driver_sql("DROP TABLE enum_test")
@testing.fixture
def array_domains(self, connection):
connection.exec_driver_sql("CREATE DOMAIN arraydomain AS INTEGER[]")
connection.exec_driver_sql(
"CREATE DOMAIN arraydomain_2d AS INTEGER[][]"
)
connection.exec_driver_sql(
"CREATE DOMAIN arraydomain_3d AS INTEGER[][][]"
)
yield
connection.exec_driver_sql("DROP DOMAIN arraydomain")
connection.exec_driver_sql("DROP DOMAIN arraydomain_2d")
connection.exec_driver_sql("DROP DOMAIN arraydomain_3d")
@testing.fixture
def array_table(self, connection, array_domains):
connection.exec_driver_sql(
"CREATE TABLE array_test ("
"id integer, "
"datas arraydomain, "
"datass arraydomain_2d, "
"datasss arraydomain_3d"
")"
)
yield
connection.exec_driver_sql("DROP TABLE array_test")
@testing.fixture
def some_schema(self, connection):
connection.exec_driver_sql('CREATE SCHEMA IF NOT EXISTS "SomeSchema"')
yield
connection.exec_driver_sql('DROP SCHEMA IF EXISTS "SomeSchema"')
@testing.fixture
def quoted_schema_domain(self, connection, some_schema):
connection.exec_driver_sql(
'CREATE DOMAIN "SomeSchema"."Quoted.Domain" INTEGER DEFAULT 0'
)
yield
connection.exec_driver_sql('DROP DOMAIN "SomeSchema"."Quoted.Domain"')
@testing.fixture
def int_domain(self, connection):
connection.exec_driver_sql(
"CREATE DOMAIN my_int AS int CONSTRAINT b_my_int_one CHECK "
"(VALUE > 1) CONSTRAINT a_my_int_two CHECK (VALUE < 42) "
"CHECK(VALUE != 22)"
)
yield
connection.exec_driver_sql("DROP DOMAIN my_int")
@testing.fixture
def quote_table(self, connection, quoted_schema_domain):
connection.exec_driver_sql(
"CREATE TABLE quote_test "
'(id integer, data "SomeSchema"."Quoted.Domain")'
)
yield
connection.exec_driver_sql("drop table quote_test")
@testing.fixture
def testdomain_schema(self, connection):
connection.exec_driver_sql(
"CREATE DOMAIN test_schema.testdomain INTEGER DEFAULT 0"
)
yield
connection.exec_driver_sql("DROP DOMAIN test_schema.testdomain")
@testing.fixture
def testtable_schema(self, connection, testdomain_schema):
connection.exec_driver_sql(
"CREATE TABLE test_schema.testtable(question "
"integer, answer test_schema.testdomain, anything "
"integer)"
)
yield
connection.exec_driver_sql("drop table test_schema.testtable")
@testing.fixture
def crosschema_table(self, connection, testdomain_schema):
connection.exec_driver_sql(
"CREATE TABLE crosschema (question integer, answer "
f"{config.test_schema}.testdomain)"
)
yield
connection.exec_driver_sql("DROP TABLE crosschema")
def test_table_is_reflected(self, connection, testtable):
metadata = MetaData()
table = Table("testtable", metadata, autoload_with=connection)
eq_(
set(table.columns.keys()),
{"question", "answer"},
"Columns of reflected table didn't equal expected columns",
)
assert isinstance(table.c.answer.type, DOMAIN)
assert table.c.answer.type.name, "testdomain"
assert isinstance(table.c.answer.type.data_type, Integer)
def test_nullable_from_domain(self, connection, nullable_domain_table):
metadata = MetaData()
table = Table(
"nullable_domain_test", metadata, autoload_with=connection
)
is_(table.c.not_nullable_domain_col.nullable, False)
is_(table.c.nullable_local.nullable, False)
def test_domain_is_reflected(self, connection, testtable):
metadata = MetaData()
table = Table("testtable", metadata, autoload_with=connection)
eq_(
str(table.columns.answer.server_default.arg),
"42",
"Reflected default value didn't equal expected value",
)
assert (
not table.columns.answer.nullable
), "Expected reflected column to not be nullable."
def test_enum_domain_is_reflected(self, connection, enum_table):
metadata = MetaData()
table = Table("enum_test", metadata, autoload_with=connection)
assert isinstance(table.c.data.type, DOMAIN)
eq_(table.c.data.type.data_type.enums, ["test"])
def test_array_domain_is_reflected(self, connection, array_table):
metadata = MetaData()
table = Table("array_test", metadata, autoload_with=connection)
def assert_is_integer_array_domain(domain, name):
# Postgres does not persist the dimensionality of the array.
# It's always treated as integer[]
assert isinstance(domain, DOMAIN)
assert domain.name == name
assert isinstance(domain.data_type, ARRAY)
assert isinstance(domain.data_type.item_type, INTEGER)
array_domain = table.c.datas.type
assert_is_integer_array_domain(array_domain, "arraydomain")
array_domain_2d = table.c.datass.type
assert_is_integer_array_domain(array_domain_2d, "arraydomain_2d")
array_domain_3d = table.c.datasss.type
assert_is_integer_array_domain(array_domain_3d, "arraydomain_3d")
def test_quoted_remote_schema_domain_is_reflected(
self, connection, quote_table
):
metadata = MetaData()
table = Table("quote_test", metadata, autoload_with=connection)
assert isinstance(table.c.data.type, DOMAIN)
assert table.c.data.type.name, "Quoted.Domain"
assert isinstance(table.c.data.type.data_type, Integer)
def test_table_is_reflected_test_schema(
self, connection, testtable_schema
):
metadata = MetaData()
table = Table(
"testtable",
metadata,
autoload_with=connection,
schema=config.test_schema,
)
eq_(
set(table.columns.keys()),
{"question", "answer", "anything"},
"Columns of reflected table didn't equal expected columns",
)
assert isinstance(table.c.anything.type, Integer)
def test_schema_domain_is_reflected(self, connection, testtable_schema):
metadata = MetaData()
table = Table(
"testtable",
metadata,
autoload_with=connection,
schema=config.test_schema,
)
eq_(
str(table.columns.answer.server_default.arg),
"0",
"Reflected default value didn't equal expected value",
)
assert (
table.columns.answer.nullable
), "Expected reflected column to be nullable."
def test_crosschema_domain_is_reflected(
self, connection, crosschema_table
):
metadata = MetaData()
table = Table("crosschema", metadata, autoload_with=connection)
eq_(
str(table.columns.answer.server_default.arg),
"0",
"Reflected default value didn't equal expected value",
)
assert (
table.columns.answer.nullable
), "Expected reflected column to be nullable."
def test_unknown_types(self, connection, testtable):
from sqlalchemy.dialects.postgresql import base
ischema_names = base.PGDialect.ischema_names
base.PGDialect.ischema_names = {}
try:
m2 = MetaData()
assert_warns(
exc.SAWarning, Table, "testtable", m2, autoload_with=connection
)
@testing.emits_warning("Did not recognize type")
def warns():
m3 = MetaData()
t3 = Table("testtable", m3, autoload_with=connection)
assert t3.c.answer.type.__class__ == sa.types.NullType
finally:
base.PGDialect.ischema_names = ischema_names
@testing.fixture
def all_domains(
self,
quoted_schema_domain,
array_domains,
enum_domain,
nullable_domains,
int_domain,
testdomain,
testdomain_schema,
):
return {
"public": [
{
"visible": True,
"name": "arraydomain",
"schema": "public",
"nullable": True,
"type": "integer[]",
"default": None,
"constraints": [],
"collation": None,
},
{
"visible": True,
"name": "arraydomain_2d",
"schema": "public",
"nullable": True,
"type": "integer[]",
"default": None,
"constraints": [],
"collation": None,
},
{
"visible": True,
"name": "arraydomain_3d",
"schema": "public",
"nullable": True,
"type": "integer[]",
"default": None,
"constraints": [],
"collation": None,
},
{
"visible": True,
"name": "enumdomain",
"schema": "public",
"nullable": True,
"type": "testtype",
"default": None,
"constraints": [],
"collation": None,
},
{
"visible": True,
"name": "my_int",
"schema": "public",
"nullable": True,
"type": "integer",
"default": None,
"constraints": [
{"check": "VALUE < 42", "name": "a_my_int_two"},
{"check": "VALUE > 1", "name": "b_my_int_one"},
# autogenerated name by pg
{"check": "VALUE <> 22", "name": "my_int_check"},
],
"collation": None,
},
{
"visible": True,
"name": "not_nullable_domain",
"schema": "public",
"nullable": False,
"type": "text",
"default": None,
"constraints": [],
"collation": "default",
},
{
"visible": True,
"name": "nullable_domain",
"schema": "public",
"nullable": True,
"type": "text",
"default": None,
"constraints": [
{
"check": "VALUE = ANY (ARRAY['FOO'::text, "
"'BAR'::text])",
# autogenerated name by pg
"name": "nullable_domain_check",
}
],
"collation": "C",
},
{
"visible": True,
"name": "testdomain",
"schema": "public",
"nullable": False,
"type": "integer",
"default": "42",
"constraints": [],
"collation": None,
},
],
"test_schema": [
{
"visible": False,
"name": "testdomain",
"schema": "test_schema",
"nullable": True,
"type": "integer",
"default": "0",
"constraints": [],
"collation": None,
}
],
"SomeSchema": [
{
"visible": False,
"name": "Quoted.Domain",
"schema": "SomeSchema",
"nullable": True,
"type": "integer",
"default": "0",
"constraints": [],
"collation": None,
}
],
}
def test_inspect_domains(self, connection, all_domains):
inspector = inspect(connection)
domains = inspector.get_domains()
domain_names = {d["name"] for d in domains}
expect_domain_names = {d["name"] for d in all_domains["public"]}
eq_(domain_names, expect_domain_names)
eq_(domains, all_domains["public"])
def test_inspect_domains_schema(self, connection, all_domains):
inspector = inspect(connection)
eq_(
inspector.get_domains("test_schema"),
all_domains["test_schema"],
)
eq_(inspector.get_domains("SomeSchema"), all_domains["SomeSchema"])
def test_inspect_domains_star(self, connection, all_domains):
inspector = inspect(connection)
all_ = [d for dl in all_domains.values() for d in dl]
all_ += inspector.get_domains("information_schema")
exp = sorted(all_, key=lambda d: (d["schema"], d["name"]))
domains = inspector.get_domains("*")
eq_(domains, exp)
class ArrayReflectionTest(fixtures.TablesTest):
__only_on__ = "postgresql >= 10"
__sparse_driver_backend__ = True
@classmethod
def define_tables(cls, metadata):
Table(
"array_table",
metadata,
Column("id", INTEGER, primary_key=True),
Column("datas", ARRAY(INTEGER)),
Column("datass", ARRAY(INTEGER, dimensions=2)),
Column("datasss", ARRAY(INTEGER, dimensions=3)),
)
def test_array_table_is_reflected(self, connection):
metadata = MetaData()
table = Table("array_table", metadata, autoload_with=connection)
def assert_is_integer_array(data_type):
assert isinstance(data_type, ARRAY)
# posgres treats all arrays as one-dimensional arrays
assert isinstance(data_type.item_type, INTEGER)
assert_is_integer_array(table.c.datas.type)
assert_is_integer_array(table.c.datass.type)
assert_is_integer_array(table.c.datasss.type)
class ReflectionTest(
ReflectionFixtures, AssertsCompiledSQL, ComparesIndexes, fixtures.TestBase
):
__only_on__ = "postgresql"
__sparse_driver_backend__ = True
def test_reflected_primary_key_order(self, metadata, connection):
meta1 = metadata
subject = Table(
"subject",
meta1,
Column("p1", Integer, primary_key=True),
Column("p2", Integer, primary_key=True),
PrimaryKeyConstraint("p2", "p1"),
)
meta1.create_all(connection)
meta2 = MetaData()
subject = Table("subject", meta2, autoload_with=connection)
eq_(subject.primary_key.columns.keys(), ["p2", "p1"])
@testing.skip_if(
"postgresql < 15.0", "on delete with column list not supported"
)
def test_reflected_foreign_key_ondelete_column_list(
self, metadata, connection
):
meta1 = metadata
pktable = Table(
"pktable",
meta1,
Column("tid", Integer, primary_key=True),
Column("id", Integer, primary_key=True),
)
Table(
"fktable",
meta1,
Column("tid", Integer),
Column("id", Integer),
Column("fk_id_del_set_null", Integer),
Column("fk_id_del_set_default", Integer, server_default=text("0")),
ForeignKeyConstraint(
name="fktable_tid_fk_id_del_set_null_fkey",
columns=["tid", "fk_id_del_set_null"],
refcolumns=[pktable.c.tid, pktable.c.id],
ondelete="SET NULL (fk_id_del_set_null)",
),
ForeignKeyConstraint(
name="fktable_tid_fk_id_del_set_default_fkey",
columns=["tid", "fk_id_del_set_default"],
refcolumns=[pktable.c.tid, pktable.c.id],
ondelete="SET DEFAULT(fk_id_del_set_default)",
),
)
meta1.create_all(connection)
meta2 = MetaData()
fktable = Table("fktable", meta2, autoload_with=connection)
fkey_set_null = next(
c
for c in fktable.foreign_key_constraints
if c.name == "fktable_tid_fk_id_del_set_null_fkey"
)
eq_(fkey_set_null.ondelete, "SET NULL (fk_id_del_set_null)")
fkey_set_default = next(
c
for c in fktable.foreign_key_constraints
if c.name == "fktable_tid_fk_id_del_set_default_fkey"
)
eq_(fkey_set_default.ondelete, "SET DEFAULT (fk_id_del_set_default)")
def test_pg_weirdchar_reflection(self, metadata, connection):
meta1 = metadata
subject = Table(
"subject", meta1, Column("id$", Integer, primary_key=True)
)
referer = Table(
"referer",
meta1,
Column("id", Integer, primary_key=True),
Column("ref", Integer, ForeignKey("subject.id$")),
)
meta1.create_all(connection)
meta2 = MetaData()
subject = Table("subject", meta2, autoload_with=connection)
referer = Table("referer", meta2, autoload_with=connection)
self.assert_(
(subject.c["id$"] == referer.c.ref).compare(
subject.join(referer).onclause
)
)
def test_reflect_default_over_128_chars(self, metadata, connection):
Table(
"t",
metadata,
Column("x", String(200), server_default="abcd" * 40),
).create(connection)
m = MetaData()
t = Table("t", m, autoload_with=connection)
eq_(
t.c.x.server_default.arg.text,
"'%s'::character varying" % ("abcd" * 40),
)
def test_renamed_sequence_reflection(self, metadata, connection):
Table("t", metadata, Column("id", Integer, primary_key=True))
metadata.create_all(connection)
m2 = MetaData()
t2 = Table("t", m2, autoload_with=connection, implicit_returning=False)
eq_(t2.c.id.server_default.arg.text, "nextval('t_id_seq'::regclass)")
r = connection.execute(t2.insert())
eq_(r.inserted_primary_key, (1,))
connection.exec_driver_sql(
"alter table t_id_seq rename to foobar_id_seq"
)
m3 = MetaData()
t3 = Table("t", m3, autoload_with=connection, implicit_returning=False)
eq_(
t3.c.id.server_default.arg.text,
"nextval('foobar_id_seq'::regclass)",
)
r = connection.execute(t3.insert())
eq_(r.inserted_primary_key, (2,))
def test_altered_type_autoincrement_pk_reflection(
self, metadata, connection
):
metadata = metadata
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", Integer),
)
metadata.create_all(connection)
connection.exec_driver_sql(
"alter table t alter column id type varchar(50)"
)
m2 = MetaData()
t2 = Table("t", m2, autoload_with=connection)
eq_(t2.c.id.autoincrement, False)
eq_(t2.c.x.autoincrement, False)
def test_renamed_pk_reflection(self, metadata, connection):
metadata = metadata
Table("t", metadata, Column("id", Integer, primary_key=True))
metadata.create_all(connection)
connection.exec_driver_sql("alter table t rename id to t_id")
m2 = MetaData()
t2 = Table("t", m2, autoload_with=connection)
eq_([c.name for c in t2.primary_key], ["t_id"])
def test_has_temporary_table(self, metadata, connection):
assert not inspect(connection).has_table("some_temp_table")
user_tmp = Table(
"some_temp_table",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(50)),
prefixes=["TEMPORARY"],
)
user_tmp.create(connection)
assert inspect(connection).has_table("some_temp_table")
def test_cross_schema_reflection_one(self, metadata, connection):
meta1 = metadata
users = Table(
"users",
meta1,
Column("user_id", Integer, primary_key=True),
Column("user_name", String(30), nullable=False),
schema="test_schema",
)
addresses = Table(
"email_addresses",
meta1,
Column("address_id", Integer, primary_key=True),
Column("remote_user_id", Integer, ForeignKey(users.c.user_id)),
Column("email_address", String(20)),
schema="test_schema",
)
meta1.create_all(connection)
meta2 = MetaData()
addresses = Table(
"email_addresses",
meta2,
autoload_with=connection,
schema="test_schema",
)
users = Table("users", meta2, must_exist=True, schema="test_schema")
j = join(users, addresses)
self.assert_(
(users.c.user_id == addresses.c.remote_user_id).compare(j.onclause)
)
def test_cross_schema_reflection_two(self, metadata, connection):
meta1 = metadata
subject = Table(
"subject", meta1, Column("id", Integer, primary_key=True)
)
referer = Table(
"referer",
meta1,
Column("id", Integer, primary_key=True),
Column("ref", Integer, ForeignKey("subject.id")),
schema="test_schema",
)
meta1.create_all(connection)
meta2 = MetaData()
subject = Table("subject", meta2, autoload_with=connection)
referer = Table(
"referer", meta2, schema="test_schema", autoload_with=connection
)
self.assert_(
(subject.c.id == referer.c.ref).compare(
subject.join(referer).onclause
)
)
def test_cross_schema_reflection_three(self, metadata, connection):
meta1 = metadata
subject = Table(
"subject",
meta1,
Column("id", Integer, primary_key=True),
schema="test_schema_2",
)
referer = Table(
"referer",
meta1,
Column("id", Integer, primary_key=True),
Column("ref", Integer, ForeignKey("test_schema_2.subject.id")),
schema="test_schema",
)
meta1.create_all(connection)
meta2 = MetaData()
subject = Table(
"subject", meta2, autoload_with=connection, schema="test_schema_2"
)
referer = Table(
"referer", meta2, autoload_with=connection, schema="test_schema"
)
self.assert_(
(subject.c.id == referer.c.ref).compare(
subject.join(referer).onclause
)
)
def test_cross_schema_reflection_four(self, metadata, connection):
meta1 = metadata
subject = Table(
"subject",
meta1,
Column("id", Integer, primary_key=True),
schema="test_schema_2",
)
referer = Table(
"referer",
meta1,
Column("id", Integer, primary_key=True),
Column("ref", Integer, ForeignKey("test_schema_2.subject.id")),
schema="test_schema",
)
meta1.create_all(connection)
connection.detach()
connection.exec_driver_sql(
"SET search_path TO test_schema, test_schema_2"
)
meta2 = MetaData()
subject = Table(
"subject",
meta2,
autoload_with=connection,
schema="test_schema_2",
postgresql_ignore_search_path=True,
)
referer = Table(
"referer",
meta2,
autoload_with=connection,
schema="test_schema",
postgresql_ignore_search_path=True,
)
self.assert_(
(subject.c.id == referer.c.ref).compare(
subject.join(referer).onclause
)
)
def test_cross_schema_reflection_five(self, metadata, connection):
meta1 = metadata
# we assume 'public'
default_schema = connection.dialect.default_schema_name
subject = Table(
"subject", meta1, Column("id", Integer, primary_key=True)
)
referer = Table(
"referer",
meta1,
Column("id", Integer, primary_key=True),
Column("ref", Integer, ForeignKey("subject.id")),
)
meta1.create_all(connection)
meta2 = MetaData()
subject = Table(
"subject",
meta2,
autoload_with=connection,
schema=default_schema,
postgresql_ignore_search_path=True,
)
referer = Table(
"referer",
meta2,
autoload_with=connection,
schema=default_schema,
postgresql_ignore_search_path=True,
)
assert subject.schema == default_schema
self.assert_(
(subject.c.id == referer.c.ref).compare(
subject.join(referer).onclause
)
)
def test_cross_schema_reflection_six(self, metadata, connection):
# test that the search path *is* taken into account
# by default
meta1 = metadata
Table(
"some_table",
meta1,
Column("id", Integer, primary_key=True),
schema="test_schema",
)
Table(
"some_other_table",
meta1,
Column("id", Integer, primary_key=True),
Column("sid", Integer, ForeignKey("test_schema.some_table.id")),
schema="test_schema_2",
)
meta1.create_all(connection)
connection.detach()
connection.exec_driver_sql(
"set search_path to test_schema_2, test_schema, public"
)
m1 = MetaData()
Table("some_table", m1, schema="test_schema", autoload_with=connection)
t2_schema = Table(
"some_other_table",
m1,
schema="test_schema_2",
autoload_with=connection,
)
t2_no_schema = Table("some_other_table", m1, autoload_with=connection)
t1_no_schema = Table("some_table", m1, autoload_with=connection)
m2 = MetaData()
t1_schema_isp = Table(
"some_table",
m2,
schema="test_schema",
autoload_with=connection,
postgresql_ignore_search_path=True,
)
t2_schema_isp = Table(
"some_other_table",
m2,
schema="test_schema_2",
autoload_with=connection,
postgresql_ignore_search_path=True,
)
# t2_schema refers to t1_schema, but since "test_schema"
# is in the search path, we instead link to t2_no_schema
assert t2_schema.c.sid.references(t1_no_schema.c.id)
# the two no_schema tables refer to each other also.
assert t2_no_schema.c.sid.references(t1_no_schema.c.id)
# but if we're ignoring search path, then we maintain
# those explicit schemas vs. what the "default" schema is
assert t2_schema_isp.c.sid.references(t1_schema_isp.c.id)
def test_cross_schema_reflection_seven(self, metadata, connection):
# test that the search path *is* taken into account
# by default
meta1 = metadata
Table(
"some_table",
meta1,
Column("id", Integer, primary_key=True),
schema="test_schema",
)
Table(
"some_other_table",
meta1,
Column("id", Integer, primary_key=True),
Column("sid", Integer, ForeignKey("test_schema.some_table.id")),
schema="test_schema_2",
)
meta1.create_all(connection)
connection.detach()
connection.exec_driver_sql(
"set search_path to test_schema_2, test_schema, public"
)
meta2 = MetaData()
meta2.reflect(connection, schema="test_schema_2")
eq_(
set(meta2.tables),
{"test_schema_2.some_other_table", "some_table"},
)
meta3 = MetaData()
meta3.reflect(
connection,
schema="test_schema_2",
postgresql_ignore_search_path=True,
)
eq_(
set(meta3.tables),
{
"test_schema_2.some_other_table",
"test_schema.some_table",
},
)
def test_cross_schema_reflection_metadata_uses_schema(
self, metadata, connection
):
# test [ticket:3716]
Table(
"some_table",
metadata,
Column("id", Integer, primary_key=True),
Column("sid", Integer, ForeignKey("some_other_table.id")),
schema="test_schema",
)
Table(
"some_other_table",
metadata,
Column("id", Integer, primary_key=True),
schema=None,
)
metadata.create_all(connection)
meta2 = MetaData(schema="test_schema")
meta2.reflect(connection)
eq_(
set(meta2.tables),
{"some_other_table", "test_schema.some_table"},
)
def test_uppercase_lowercase_table(self, metadata, connection):
a_table = Table("a", metadata, Column("x", Integer))
A_table = Table("A", metadata, Column("x", Integer))
a_table.create(connection)
assert inspect(connection).has_table("a")
assert not inspect(connection).has_table("A")
A_table.create(connection, checkfirst=True)
assert inspect(connection).has_table("A")
def test_uppercase_lowercase_sequence(self, connection):
a_seq = Sequence("a")
A_seq = Sequence("A")
a_seq.create(connection)
assert connection.dialect.has_sequence(connection, "a")
assert not connection.dialect.has_sequence(connection, "A")
A_seq.create(connection, checkfirst=True)
assert connection.dialect.has_sequence(connection, "A")
a_seq.drop(connection)
A_seq.drop(connection)
def test_index_reflection(self, metadata, connection):
"""Reflecting expression-based indexes works"""
Table(
"party",
metadata,
Column("id", String(10), nullable=False),
Column("name", String(20), index=True),
Column("aname", String(20)),
Column("other", String(20)),
)
metadata.create_all(connection)
connection.exec_driver_sql(
"""
create index idx3 on party
(lower(name::text), other, lower(aname::text) desc)
"""
)
connection.exec_driver_sql(
"create index idx1 on party ((id || name), (other || id::text))"
)
connection.exec_driver_sql(
"create unique index idx2 on party (id) where name = 'test'"
)
connection.exec_driver_sql(
"""
create index idx4 on party using btree
(name nulls first, lower(other), aname desc)
where name != 'foo'
"""
)
version = connection.dialect.server_version_info
if version >= (15,):
connection.exec_driver_sql(
"""
create unique index zz_idx5 on party
(name desc, upper(other))
nulls not distinct
"""
)
expected = [
{
"name": "idx1",
"column_names": [None, None],
"expressions": [
"(id::text || name::text)",
"(other::text || id::text)",
],
"unique": False,
"include_columns": [],
"dialect_options": {"postgresql_include": []},
},
{
"name": "idx2",
"column_names": ["id"],
"unique": True,
"include_columns": [],
"dialect_options": {
"postgresql_include": [],
"postgresql_where": "((name)::text = 'test'::text)",
},
},
{
"name": "idx3",
"column_names": [None, "other", None],
"expressions": [
"lower(name::text)",
"other",
"lower(aname::text)",
],
"unique": False,
"include_columns": [],
"dialect_options": {"postgresql_include": []},
"column_sorting": {"lower(aname::text)": ("desc",)},
},
{
"name": "idx4",
"column_names": ["name", None, "aname"],
"expressions": ["name", "lower(other::text)", "aname"],
"unique": False,
"include_columns": [],
"dialect_options": {
"postgresql_include": [],
"postgresql_where": "((name)::text <> 'foo'::text)",
},
"column_sorting": {
"aname": ("desc",),
"name": ("nulls_first",),
},
},
{
"name": "ix_party_name",
"column_names": ["name"],
"unique": False,
"include_columns": [],
"dialect_options": {"postgresql_include": []},
},
]
if version > (15,):
expected.append(
{
"name": "zz_idx5",
"column_names": ["name", None],
"expressions": ["name", "upper(other::text)"],
"unique": True,
"include_columns": [],
"dialect_options": {
"postgresql_include": [],
"postgresql_nulls_not_distinct": True,
},
"column_sorting": {"name": ("desc",)},
},
)
if version < (11,):
for index in expected:
index.pop("include_columns")
index["dialect_options"].pop("postgresql_include")
if not index["dialect_options"]:
index.pop("dialect_options")
insp = inspect(connection)
eq_(insp.get_indexes("party"), expected)
m2 = MetaData()
t2 = Table("party", m2, autoload_with=connection)
self.compare_table_index_with_expected(t2, expected, "postgresql")
def test_index_reflection_partial(self, metadata, connection):
"""Reflect the filter definition on partial indexes"""
metadata = metadata
t1 = Table(
"table1",
metadata,
Column("id", Integer, primary_key=True),
Column("name", String(20)),
Column("x", Integer),
)
Index("idx1", t1.c.id, postgresql_where=t1.c.name == "test")
Index("idx2", t1.c.id, postgresql_where=t1.c.x >= 5)
metadata.create_all(connection)
ind = connection.dialect.get_indexes(connection, t1.name, None)
partial_definitions = []
for ix in ind:
if "dialect_options" in ix:
partial_definitions.append(
ix["dialect_options"]["postgresql_where"]
)
eq_(
sorted(partial_definitions),
["((name)::text = 'test'::text)", "(x >= 5)"],
)
t2 = Table("table1", MetaData(), autoload_with=connection)
idx = list(sorted(t2.indexes, key=lambda idx: idx.name))[0]
self.assert_compile(
CreateIndex(idx),
"CREATE INDEX idx1 ON table1 (id) "
"WHERE ((name)::text = 'test'::text)",
)
def test_index_reflection_with_sorting(self, metadata, connection):
"""reflect indexes with sorting options set"""
t1 = Table(
"party",
metadata,
Column("id", String(10), nullable=False),
Column("name", String(20)),
Column("aname", String(20)),
)
t1.create(connection)
# check ASC, DESC options alone
connection.exec_driver_sql(
"""
create index idx1 on party
(id, name ASC, aname DESC)
"""
)
# check DESC w/ NULLS options
connection.exec_driver_sql(
"""
create index idx2 on party
(name DESC NULLS FIRST, aname DESC NULLS LAST)
"""
)
# check ASC w/ NULLS options
connection.exec_driver_sql(
"""
create index idx3 on party
(name ASC NULLS FIRST, aname ASC NULLS LAST)
"""
)
# reflect data
m2 = MetaData()
t2 = Table("party", m2, autoload_with=connection)
eq_(len(t2.indexes), 3)
# Make sure indexes are in the order we expect them in
r1, r2, r3 = sorted(t2.indexes, key=lambda idx: idx.name)
eq_(r1.name, "idx1")
eq_(r2.name, "idx2")
eq_(r3.name, "idx3")
# "ASC NULLS LAST" is implicit default for indexes,
# and "NULLS FIRST" is implicit default for "DESC".
# (https://www.postgresql.org/docs/current/indexes-ordering.html)
def compile_exprs(exprs):
return list(map(str, exprs))
eq_(
compile_exprs([t2.c.id, t2.c.name, t2.c.aname.desc()]),
compile_exprs(r1.expressions),
)
eq_(
compile_exprs([t2.c.name.desc(), t2.c.aname.desc().nulls_last()]),
compile_exprs(r2.expressions),
)
eq_(
compile_exprs([t2.c.name.nulls_first(), t2.c.aname]),
compile_exprs(r3.expressions),
)
def test_index_reflection_modified(self, metadata, connection):
"""reflect indexes when a column name has changed - PG 9
does not update the name of the column in the index def.
[ticket:2141]
"""
metadata = metadata
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", Integer),
)
metadata.create_all(connection)
connection.exec_driver_sql("CREATE INDEX idx1 ON t (x)")
connection.exec_driver_sql("ALTER TABLE t RENAME COLUMN x to y")
ind = connection.dialect.get_indexes(connection, "t", None)
expected = [{"name": "idx1", "unique": False, "column_names": ["y"]}]
if testing.requires.index_reflects_included_columns.enabled:
expected[0]["include_columns"] = []
expected[0]["dialect_options"] = {"postgresql_include": []}
eq_(ind, expected)
def test_index_reflection_with_storage_options(self, metadata, connection):
"""reflect indexes with storage options set"""
metadata = metadata
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", Integer),
)
metadata.create_all(connection)
connection.exec_driver_sql(
"CREATE INDEX idx1 ON t (x) WITH (fillfactor = 50)"
)
ind = testing.db.dialect.get_indexes(connection, "t", None)
expected = [
{
"unique": False,
"column_names": ["x"],
"name": "idx1",
"dialect_options": {"postgresql_with": {"fillfactor": "50"}},
}
]
if testing.requires.index_reflects_included_columns.enabled:
expected[0]["include_columns"] = []
expected[0]["dialect_options"]["postgresql_include"] = []
eq_(ind, expected)
m = MetaData()
t1 = Table("t", m, autoload_with=connection)
eq_(
list(t1.indexes)[0].dialect_options["postgresql"]["with"],
{"fillfactor": "50"},
)
def test_index_reflection_with_access_method(self, metadata, connection):
"""reflect indexes with storage options set"""
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", ARRAY(Integer)),
)
metadata.create_all(connection)
connection.exec_driver_sql("CREATE INDEX idx1 ON t USING gin (x)")
ind = testing.db.dialect.get_indexes(connection, "t", None)
expected = [
{
"unique": False,
"column_names": ["x"],
"name": "idx1",
"dialect_options": {"postgresql_using": "gin"},
}
]
if testing.requires.index_reflects_included_columns.enabled:
expected[0]["include_columns"] = []
expected[0]["dialect_options"]["postgresql_include"] = []
eq_(ind, expected)
m = MetaData()
t1 = Table("t", m, autoload_with=connection)
eq_(
list(t1.indexes)[0].dialect_options["postgresql"]["using"],
"gin",
)
def test_index_reflection_with_operator_class(self, metadata, connection):
"""reflect indexes with operator class on columns"""
Table(
"t",
metadata,
Column("id", Integer, nullable=False),
Column("name", String),
Column("alias", String),
Column("addr1", INET),
Column("addr2", INET),
)
metadata.create_all(connection)
# 'name' and 'addr1' use a non-default operator, 'addr2' uses the
# default one, and 'alias' uses no operator.
connection.exec_driver_sql(
"CREATE INDEX ix_t ON t USING btree"
" (name text_pattern_ops, alias, addr1 cidr_ops, addr2 inet_ops)"
)
ind = inspect(connection).get_indexes("t", None)
expected = [
{
"unique": False,
"column_names": ["name", "alias", "addr1", "addr2"],
"name": "ix_t",
"dialect_options": {
"postgresql_ops": {
"addr1": "cidr_ops",
"name": "text_pattern_ops",
},
},
}
]
if connection.dialect.server_version_info >= (11, 0):
expected[0]["include_columns"] = []
expected[0]["dialect_options"]["postgresql_include"] = []
eq_(ind, expected)
m = MetaData()
t1 = Table("t", m, autoload_with=connection)
r_ind = list(t1.indexes)[0]
eq_(
r_ind.dialect_options["postgresql"]["ops"],
{"name": "text_pattern_ops", "addr1": "cidr_ops"},
)
@testing.skip_if("postgresql < 15.0", "nullsnotdistinct not supported")
def test_nullsnotdistinct(self, metadata, connection):
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", ARRAY(Integer)),
Column("y", ARRAY(Integer)),
Index(
"idx1", "x", unique=True, postgresql_nulls_not_distinct=True
),
UniqueConstraint(
"y", name="unq1", postgresql_nulls_not_distinct=True
),
)
metadata.create_all(connection)
ind = inspect(connection).get_indexes("t", None)
expected_ind = [
{
"unique": True,
"column_names": ["x"],
"name": "idx1",
"dialect_options": {
"postgresql_nulls_not_distinct": True,
"postgresql_include": [],
},
"include_columns": [],
},
{
"unique": True,
"column_names": ["y"],
"name": "unq1",
"dialect_options": {
"postgresql_nulls_not_distinct": True,
"postgresql_include": [],
},
"include_columns": [],
"duplicates_constraint": "unq1",
},
]
eq_(ind, expected_ind)
unq = inspect(connection).get_unique_constraints("t", None)
expected_unq = [
{
"column_names": ["y"],
"name": "unq1",
"dialect_options": {
"postgresql_include": [],
"postgresql_nulls_not_distinct": True,
},
"comment": None,
}
]
eq_(unq, expected_unq)
m = MetaData()
t1 = Table("t", m, autoload_with=connection)
eq_(len(t1.indexes), 1)
idx_options = list(t1.indexes)[0].dialect_options["postgresql"]
eq_(idx_options["nulls_not_distinct"], True)
cst = {c.name: c for c in t1.constraints}
cst_options = cst["unq1"].dialect_options["postgresql"]
eq_(cst_options["nulls_not_distinct"], True)
@testing.skip_if("postgresql < 11.0", "indnkeyatts not supported")
def test_index_reflection_with_include(self, metadata, connection):
"""reflect indexes with include set"""
Table(
"t",
metadata,
Column("id", Integer, primary_key=True),
Column("x", ARRAY(Integer)),
Column("name", String(20)),
Column("aname", String(20)),
Column("other", Text()),
)
metadata.create_all(connection)
connection.exec_driver_sql("CREATE INDEX idx1 ON t (x) INCLUDE (name)")
connection.exec_driver_sql(
"""
create index idx3 on t
(lower(name::text), other desc nulls last, lower(aname::text))
include (id, x)
"""
)
connection.exec_driver_sql(
"""
create unique index idx2 on t using btree
(lower(other), (id * id)) include (id)
"""
)
ind = connection.dialect.get_indexes(connection, "t", None)
eq_(
ind,
[
{
"unique": False,
"column_names": ["x"],
"include_columns": ["name"],
"dialect_options": {"postgresql_include": ["name"]},
"name": "idx1",
},
{
"name": "idx2",
"column_names": [None, None],
"expressions": ["lower(other)", "(id * id)"],
"unique": True,
"include_columns": ["id"],
"dialect_options": {"postgresql_include": ["id"]},
},
{
"name": "idx3",
"column_names": [None, "other", None],
"expressions": [
"lower(name::text)",
"other",
"lower(aname::text)",
],
"unique": False,
"include_columns": ["id", "x"],
"dialect_options": {"postgresql_include": ["id", "x"]},
"column_sorting": {
"other": ("desc", "nulls_last"),
},
},
],
)
def test_foreign_key_option_inspection(self, metadata, connection):
Table(
"person",
metadata,
Column("id", String(length=32), nullable=False, primary_key=True),
Column(
"company_id",
ForeignKey(
"company.id",
name="person_company_id_fkey",
match="FULL",
onupdate="RESTRICT",
ondelete="RESTRICT",
deferrable=True,
initially="DEFERRED",
),
),
)
Table(
"company",
metadata,
Column("id", String(length=32), nullable=False, primary_key=True),
Column("name", String(length=255)),
Column(
"industry_id",
ForeignKey(
"industry.id",
name="company_industry_id_fkey",
onupdate="CASCADE",
ondelete="CASCADE",
deferrable=False, # PG default
# PG default
initially="IMMEDIATE",
),
),
)
Table(
"industry",
metadata,
Column("id", Integer(), nullable=False, primary_key=True),
Column("name", String(length=255)),
)
fk_ref = {
"person_company_id_fkey": {
"name": "person_company_id_fkey",
"constrained_columns": ["company_id"],
"referred_columns": ["id"],
"referred_table": "company",
"referred_schema": None,
"options": {
"onupdate": "RESTRICT",
"deferrable": True,
"ondelete": "RESTRICT",
"initially": "DEFERRED",
"match": "FULL",
},
"comment": None,
},
"company_industry_id_fkey": {
"name": "company_industry_id_fkey",
"constrained_columns": ["industry_id"],
"referred_columns": ["id"],
"referred_table": "industry",
"referred_schema": None,
"options": {"onupdate": "CASCADE", "ondelete": "CASCADE"},
"comment": None,
},
}
metadata.create_all(connection)
inspector = inspect(connection)
fks = inspector.get_foreign_keys(
"person"
) + inspector.get_foreign_keys("company")
for fk in fks:
eq_(fk, fk_ref[fk["name"]])
def test_inspect_enums_schema(self, metadata, connection):
enum_type = postgresql.ENUM(
"sad",
"ok",
"happy",
name="mood",
schema="test_schema",
metadata=metadata,
)
enum_type.create(connection)
inspector = inspect(connection)
eq_(
inspector.get_enums("test_schema"),
[
{
"visible": False,
"name": "mood",
"schema": "test_schema",
"labels": ["sad", "ok", "happy"],
}
],
)
is_true(inspector.has_type("mood", "test_schema"))
is_true(inspector.has_type("mood", "*"))
is_false(inspector.has_type("mood"))
def test_inspect_enums(self, metadata, inspect_fixture):
inspector, conn = inspect_fixture
enum_type = postgresql.ENUM(
"cat", "dog", "rat", name="pet", metadata=metadata
)
enum_type.create(conn)
conn.commit()
res = [
{
"visible": True,
"labels": ["cat", "dog", "rat"],
"name": "pet",
"schema": "public",
}
]
eq_(inspector.get_enums(), res)
is_true(inspector.has_type("pet", "*"))
is_true(inspector.has_type("pet"))
is_false(inspector.has_type("pet", "test_schema"))
enum_type.drop(conn)
conn.commit()
eq_(inspector.get_enums(), res)
is_true(inspector.has_type("pet"))
inspector.clear_cache()
eq_(inspector.get_enums(), [])
is_false(inspector.has_type("pet"))
def test_get_table_oid(self, metadata, connection):
Table("t1", metadata, Column("col", Integer))
Table("t1", metadata, Column("col", Integer), schema="test_schema")
metadata.create_all(connection)
insp = inspect(connection)
oid = insp.get_table_oid("t1")
oid_schema = insp.get_table_oid("t1", schema="test_schema")
is_true(isinstance(oid, int))
is_true(isinstance(oid_schema, int))
is_true(oid != oid_schema)
with expect_raises(exc.NoSuchTableError):
insp.get_table_oid("does_not_exist")
metadata.tables["t1"].drop(connection)
eq_(insp.get_table_oid("t1"), oid)
insp.clear_cache()
with expect_raises(exc.NoSuchTableError):
insp.get_table_oid("t1")
def test_inspect_enums_case_sensitive(self, metadata, connection):
sa.event.listen(
metadata,
"before_create",
sa.DDL('create schema "TestSchema"'),
)
sa.event.listen(
metadata,
"after_drop",
sa.DDL('drop schema if exists "TestSchema" cascade'),
)
for enum in "lower_case", "UpperCase", "Name.With.Dot":
for schema in None, "test_schema", "TestSchema":
postgresql.ENUM(
"CapsOne",
"CapsTwo",
name=enum,
schema=schema,
metadata=metadata,
)
metadata.create_all(connection)
inspector = inspect(connection)
for schema in None, "test_schema", "TestSchema":
eq_(
sorted(
inspector.get_enums(schema=schema), key=itemgetter("name")
),
[
{
"visible": schema is None,
"labels": ["CapsOne", "CapsTwo"],
"name": "Name.With.Dot",
"schema": "public" if schema is None else schema,
},
{
"visible": schema is None,
"labels": ["CapsOne", "CapsTwo"],
"name": "UpperCase",
"schema": "public" if schema is None else schema,
},
{
"visible": schema is None,
"labels": ["CapsOne", "CapsTwo"],
"name": "lower_case",
"schema": "public" if schema is None else schema,
},
],
)
def test_inspect_enums_case_sensitive_from_table(
self, metadata, connection
):
sa.event.listen(
metadata,
"before_create",
sa.DDL('create schema "TestSchema"'),
)
sa.event.listen(
metadata,
"after_drop",
sa.DDL('drop schema if exists "TestSchema" cascade'),
)
counter = itertools.count()
for enum in "lower_case", "UpperCase", "Name.With.Dot":
for schema in None, "test_schema", "TestSchema":
enum_type = postgresql.ENUM(
"CapsOne",
"CapsTwo",
name=enum,
metadata=metadata,
schema=schema,
)
Table(
"t%d" % next(counter),
metadata,
Column("q", enum_type),
)
metadata.create_all(connection)
inspector = inspect(connection)
counter = itertools.count()
for enum in "lower_case", "UpperCase", "Name.With.Dot":
for schema in None, "test_schema", "TestSchema":
cols = inspector.get_columns("t%d" % next(counter))
cols[0]["type"] = (
cols[0]["type"].schema,
cols[0]["type"].name,
cols[0]["type"].enums,
)
eq_(
cols,
[
{
"name": "q",
"type": (schema, enum, ["CapsOne", "CapsTwo"]),
"nullable": True,
"default": None,
"autoincrement": False,
"comment": None,
}
],
)
def test_inspect_enums_star(self, metadata, connection):
enum_type = postgresql.ENUM(
"cat", "dog", "rat", name="pet", metadata=metadata
)
schema_enum_type = postgresql.ENUM(
"sad",
"ok",
"happy",
name="mood",
schema="test_schema",
metadata=metadata,
)
enum_type.create(connection)
schema_enum_type.create(connection)
inspector = inspect(connection)
eq_(
inspector.get_enums(),
[
{
"visible": True,
"labels": ["cat", "dog", "rat"],
"name": "pet",
"schema": "public",
}
],
)
eq_(
inspector.get_enums("*"),
[
{
"visible": True,
"labels": ["cat", "dog", "rat"],
"name": "pet",
"schema": "public",
},
{
"visible": False,
"name": "mood",
"schema": "test_schema",
"labels": ["sad", "ok", "happy"],
},
],
)
def test_inspect_enum_empty(self, metadata, connection):
enum_type = postgresql.ENUM(name="empty", metadata=metadata)
enum_type.create(connection)
inspector = inspect(connection)
eq_(
inspector.get_enums(),
[
{
"visible": True,
"labels": [],
"name": "empty",
"schema": "public",
}
],
)
def test_inspect_enum_empty_from_table(self, metadata, connection):
Table(
"t", metadata, Column("x", postgresql.ENUM(name="empty"))
).create(connection)
t = Table("t", MetaData(), autoload_with=connection)
eq_(t.c.x.type.enums, [])
def test_enum_starts_with_interval(self, metadata, connection):
"""Test for #12744"""
enum_type = postgresql.ENUM("day", "week", name="intervalunit")
t1 = Table("t1", metadata, Column("col", enum_type))
t1.create(connection)
insp = inspect(connection)
cols = insp.get_columns("t1")
is_true(isinstance(cols[0]["type"], postgresql.ENUM))
eq_(cols[0]["type"].enums, ["day", "week"])
def test_reflection_with_unique_constraint(self, metadata, connection):
insp = inspect(connection)
meta = metadata
uc_table = Table(
"pgsql_uc",
meta,
Column("a", String(10)),
UniqueConstraint("a", name="uc_a"),
)
uc_table.create(connection)
# PostgreSQL will create an implicit index for a unique
# constraint. Separately we get both
indexes = {i["name"] for i in insp.get_indexes("pgsql_uc")}
constraints = {
i["name"] for i in insp.get_unique_constraints("pgsql_uc")
}
self.assert_("uc_a" in indexes)
self.assert_("uc_a" in constraints)
# reflection corrects for the dupe
reflected = Table("pgsql_uc", MetaData(), autoload_with=connection)
indexes = {i.name for i in reflected.indexes}
constraints = {uc.name for uc in reflected.constraints}
self.assert_("uc_a" not in indexes)
self.assert_("uc_a" in constraints)
@testing.requires.btree_gist
def test_reflection_with_exclude_constraint(self, metadata, connection):
m = metadata
Table(
"t",
m,
Column("id", Integer, primary_key=True),
Column("period", TSRANGE),
ExcludeConstraint(("period", "&&"), name="quarters_period_excl"),
)
m.create_all(connection)
insp = inspect(connection)
# PostgreSQL will create an implicit index for an exclude constraint.
# we don't reflect the EXCLUDE yet.
expected = [
{
"unique": False,
"name": "quarters_period_excl",
"duplicates_constraint": "quarters_period_excl",
"dialect_options": {"postgresql_using": "gist"},
"column_names": ["period"],
}
]
if testing.requires.index_reflects_included_columns.enabled:
expected[0]["include_columns"] = []
expected[0]["dialect_options"]["postgresql_include"] = []
eq_(insp.get_indexes("t"), expected)
# reflection corrects for the dupe
reflected = Table("t", MetaData(), autoload_with=connection)
eq_(set(reflected.indexes), set())
def test_reflect_unique_index(self, metadata, connection):
insp = inspect(connection)
meta = metadata
# a unique index OTOH we are able to detect is an index
# and not a unique constraint
uc_table = Table(
"pgsql_uc",
meta,
Column("a", String(10)),
Index("ix_a", "a", unique=True),
)
uc_table.create(connection)
indexes = {i["name"]: i for i in insp.get_indexes("pgsql_uc")}
constraints = {
i["name"] for i in insp.get_unique_constraints("pgsql_uc")
}
self.assert_("ix_a" in indexes)
assert indexes["ix_a"]["unique"]
self.assert_("ix_a" not in constraints)
reflected = Table("pgsql_uc", MetaData(), autoload_with=connection)
indexes = {i.name: i for i in reflected.indexes}
constraints = {uc.name for uc in reflected.constraints}
self.assert_("ix_a" in indexes)
assert indexes["ix_a"].unique
self.assert_("ix_a" not in constraints)
def test_reflect_check_constraint(self, metadata, connection):
meta = metadata
udf_create = """\
CREATE OR REPLACE FUNCTION is_positive(
x integer DEFAULT '-1'::integer)
RETURNS boolean
LANGUAGE 'plpgsql'
COST 100
VOLATILE
AS $BODY$BEGIN
RETURN x > 0;
END;$BODY$;
"""
sa.event.listen(meta, "before_create", sa.DDL(udf_create))
sa.event.listen(
meta,
"after_drop",
sa.DDL("DROP FUNCTION IF EXISTS is_positive(integer)"),
)
Table(
"pgsql_cc",
meta,
Column("a", Integer()),
Column("b", String),
CheckConstraint("a > 1 AND a < 5", name="cc1"),
CheckConstraint("a = 1 OR (a > 2 AND a < 5)", name="cc2"),
CheckConstraint("is_positive(a)", name="cc3"),
CheckConstraint("b != 'hi\nim a name \nyup\n'", name="cc4"),
)
meta.create_all(connection)
reflected = Table("pgsql_cc", MetaData(), autoload_with=connection)
check_constraints = {
uc.name: uc.sqltext.text
for uc in reflected.constraints
if isinstance(uc, CheckConstraint)
}
eq_(
check_constraints,
{
"cc1": "a > 1 AND a < 5",
"cc2": "a = 1 OR a > 2 AND a < 5",
"cc3": "is_positive(a)",
"cc4": "b::text <> 'hi\nim a name \nyup\n'::text",
},
)
def test_reflect_check_warning(self):
rows = [("foo", "some name", "NOTCHECK foobar", None)]
conn = mock.Mock(
execute=lambda *arg, **kw: mock.MagicMock(
fetchall=lambda: rows, __iter__=lambda self: iter(rows)
)
)
with testing.expect_warnings(
"Could not parse CHECK constraint text: 'NOTCHECK foobar'"
):
testing.db.dialect.get_check_constraints(conn, "foo")
def test_reflect_extra_newlines(self):
rows = [
(
"foo",
"some name",
"CHECK (\n(a \nIS\n NOT\n\n NULL\n)\n)",
None,
),
("foo", "some other name", "CHECK ((b\nIS\nNOT\nNULL))", None),
(
"foo",
"some CRLF name",
"CHECK ((c\r\n\r\nIS\r\nNOT\r\nNULL))",
None,
),
("foo", "some name", "CHECK (c != 'hi\nim a name\n')", None),
]
conn = mock.Mock(
execute=lambda *arg, **kw: mock.MagicMock(
fetchall=lambda: rows, __iter__=lambda self: iter(rows)
)
)
check_constraints = testing.db.dialect.get_check_constraints(
conn, "foo"
)
eq_(
check_constraints,
[
{
"name": "some name",
"sqltext": "a \nIS\n NOT\n\n NULL\n",
"comment": None,
},
{
"name": "some other name",
"sqltext": "b\nIS\nNOT\nNULL",
"comment": None,
},
{
"name": "some CRLF name",
"sqltext": "c\r\n\r\nIS\r\nNOT\r\nNULL",
"comment": None,
},
{
"name": "some name",
"sqltext": "c != 'hi\nim a name\n'",
"comment": None,
},
],
)
def test_reflect_with_not_valid_check_constraint(self):
rows = [
("foo", "some name", "CHECK ((a IS NOT NULL)) NOT VALID", None)
]
conn = mock.Mock(
execute=lambda *arg, **kw: mock.MagicMock(
fetchall=lambda: rows, __iter__=lambda self: iter(rows)
)
)
check_constraints = testing.db.dialect.get_check_constraints(
conn, "foo"
)
eq_(
check_constraints,
[
{
"name": "some name",
"sqltext": "a IS NOT NULL",
"dialect_options": {"not_valid": True},
"comment": None,
}
],
)
def test_reflect_with_no_inherit_check_constraint(self):
rows = [
("foo", "some name", "CHECK ((a IS NOT NULL)) NO INHERIT", None),
(
"foo",
"some name",
"CHECK ((a IS NOT NULL)) NO INHERIT NOT VALID",
None,
),
]
conn = mock.Mock(
execute=lambda *arg, **kw: mock.MagicMock(
fetchall=lambda: rows, __iter__=lambda self: iter(rows)
)
)
check_constraints = testing.db.dialect.get_check_constraints(
conn, "foo"
)
eq_(
check_constraints,
[
{
"name": "some name",
"sqltext": "a IS NOT NULL",
"dialect_options": {"no_inherit": True},
"comment": None,
},
{
"name": "some name",
"sqltext": "a IS NOT NULL",
"dialect_options": {"not_valid": True, "no_inherit": True},
"comment": None,
},
],
)
def _apply_stm(self, connection, use_map):
if use_map:
return connection.execution_options(
schema_translate_map={
None: "foo",
testing.config.test_schema: "bar",
}
)
else:
return connection
@testing.combinations(True, False, argnames="use_map")
@testing.combinations(True, False, argnames="schema")
def test_schema_translate_map(self, metadata, connection, use_map, schema):
schema = testing.config.test_schema if schema else None
Table(
"foo",
metadata,
Column("id", Integer, primary_key=True),
Column("a", Integer, index=True),
Column(
"b",
ForeignKey(f"{schema}.foo.id" if schema else "foo.id"),
unique=True,
),
CheckConstraint("a>10", name="foo_check"),
comment="comm",
schema=schema,
)
metadata.create_all(connection)
if use_map:
connection = connection.execution_options(
schema_translate_map={
None: "foo",
testing.config.test_schema: "bar",
}
)
insp = inspect(connection)
eq_(
[c["name"] for c in insp.get_columns("foo", schema=schema)],
["id", "a", "b"],
)
eq_(
[
i["column_names"]
for i in insp.get_indexes("foo", schema=schema)
],
[["b"], ["a"]],
)
eq_(
insp.get_pk_constraint("foo", schema=schema)[
"constrained_columns"
],
["id"],
)
eq_(insp.get_table_comment("foo", schema=schema), {"text": "comm"})
eq_(
[
f["constrained_columns"]
for f in insp.get_foreign_keys("foo", schema=schema)
],
[["b"]],
)
eq_(
[
c["name"]
for c in insp.get_check_constraints("foo", schema=schema)
],
["foo_check"],
)
eq_(
[
u["column_names"]
for u in insp.get_unique_constraints("foo", schema=schema)
],
[["b"]],
)
def test_reflection_constraint_comments(self, connection, metadata):
t = Table(
"foo",
metadata,
Column("id", Integer),
Column("foo_id", ForeignKey("foo.id", name="fk_1")),
Column("foo_other_id", ForeignKey("foo.id", name="fk_2")),
CheckConstraint("id>0", name="ch_1"),
CheckConstraint("id<1000", name="ch_2"),
PrimaryKeyConstraint("id", name="foo_pk"),
UniqueConstraint("id", "foo_id", name="un_1"),
UniqueConstraint("id", "foo_other_id", name="un_2"),
)
metadata.create_all(connection)
def check(elements, exp):
elements = {c["name"]: c["comment"] for c in elements}
eq_(elements, exp)
def all_none():
insp = inspect(connection)
is_(insp.get_pk_constraint("foo")["comment"], None)
check(
insp.get_check_constraints("foo"), {"ch_1": None, "ch_2": None}
)
check(
insp.get_unique_constraints("foo"),
{"un_1": None, "un_2": None},
)
check(insp.get_foreign_keys("foo"), {"fk_1": None, "fk_2": None})
all_none()
c = next(c for c in t.constraints if c.name == "ch_1")
u = next(c for c in t.constraints if c.name == "un_1")
f = next(c for c in t.foreign_key_constraints if c.name == "fk_1")
p = t.primary_key
c.comment = "cc comment"
u.comment = "uc comment"
f.comment = "fc comment"
p.comment = "pk comment"
for cst in [c, u, f, p]:
connection.execute(sa_ddl.SetConstraintComment(cst))
insp = inspect(connection)
eq_(insp.get_pk_constraint("foo")["comment"], "pk comment")
check(
insp.get_check_constraints("foo"),
{"ch_1": "cc comment", "ch_2": None},
)
check(
insp.get_unique_constraints("foo"),
{"un_1": "uc comment", "un_2": None},
)
check(
insp.get_foreign_keys("foo"), {"fk_1": "fc comment", "fk_2": None}
)
for cst in [c, u, f, p]:
connection.execute(sa_ddl.DropConstraintComment(cst))
all_none()
@testing.skip_if("postgresql < 11.0", "not supported")
def test_reflection_constraints_with_include(self, connection, metadata):
Table(
"foo",
metadata,
Column("id", Integer, nullable=False),
Column("value", Integer, nullable=False),
Column("foo", String),
Column("arr", ARRAY(Integer)),
Column("bar", SmallInteger),
)
metadata.create_all(connection)
connection.exec_driver_sql(
"ALTER TABLE foo ADD UNIQUE (id) INCLUDE (value)"
)
connection.exec_driver_sql(
"ALTER TABLE foo "
"ADD PRIMARY KEY (id) INCLUDE (arr, foo, bar, value)"
)
unq = inspect(connection).get_unique_constraints("foo")
expected_unq = [
{
"column_names": ["id"],
"name": "foo_id_value_key",
"dialect_options": {
"postgresql_nulls_not_distinct": False,
"postgresql_include": ["value"],
},
"comment": None,
}
]
eq_(unq, expected_unq)
pk = inspect(connection).get_pk_constraint("foo")
expected_pk = {
"comment": None,
"constrained_columns": ["id"],
"dialect_options": {
"postgresql_include": ["arr", "foo", "bar", "value"]
},
"name": "foo_pkey",
}
eq_(pk, expected_pk)
class CustomTypeReflectionTest(fixtures.TestBase):
class CustomType:
def __init__(self, arg1=None, arg2=None, collation=None):
self.arg1 = arg1
self.arg2 = arg2
self.collation = collation
ischema_names = None
def setup_test(self):
ischema_names = postgresql.PGDialect.ischema_names
postgresql.PGDialect.ischema_names = ischema_names.copy()
self.ischema_names = ischema_names
def teardown_test(self):
postgresql.PGDialect.ischema_names = self.ischema_names
self.ischema_names = None
def _assert_reflected(self, dialect):
for sch, args in [
("my_custom_type", (None, None)),
("my_custom_type()", (None, None)),
("my_custom_type(ARG1)", ("ARG1", None)),
("my_custom_type(ARG1, ARG2)", ("ARG1", "ARG2")),
]:
row_dict = {
"name": "colname",
"table_name": "tblname",
"format_type": sch,
"default": None,
"not_null": False,
"collation": "cc" if sch == "my_custom_type()" else None,
"comment": None,
"generated": "",
"identity_options": None,
}
column_info = dialect._get_columns_info(
[row_dict], {}, {}, "public"
)
assert ("public", "tblname") in column_info
column_info = column_info[("public", "tblname")]
assert len(column_info) == 1
column_info = column_info[0]
assert isinstance(column_info["type"], self.CustomType)
eq_(column_info["type"].arg1, args[0])
eq_(column_info["type"].arg2, args[1])
if sch == "my_custom_type()":
eq_(column_info["type"].collation, "cc")
else:
eq_(column_info["type"].collation, None)
def test_clslevel(self):
postgresql.PGDialect.ischema_names["my_custom_type"] = self.CustomType
dialect = postgresql.PGDialect()
self._assert_reflected(dialect)
def test_instancelevel(self):
dialect = postgresql.PGDialect()
dialect.ischema_names = dialect.ischema_names.copy()
dialect.ischema_names["my_custom_type"] = self.CustomType
self._assert_reflected(dialect)
def test_no_format_type(self):
"""test #8748"""
dialect = postgresql.PGDialect()
dialect.ischema_names = dialect.ischema_names.copy()
dialect.ischema_names["my_custom_type"] = self.CustomType
with expect_warnings(
r"PostgreSQL format_type\(\) returned NULL for column 'colname'"
):
row_dict = {
"name": "colname",
"table_name": "tblname",
"format_type": None,
"default": None,
"not_null": False,
"collation": None,
"comment": None,
"generated": "",
"identity_options": None,
}
column_info = dialect._get_columns_info(
[row_dict], {}, {}, "public"
)
assert ("public", "tblname") in column_info
column_info = column_info[("public", "tblname")]
assert len(column_info) == 1
column_info = column_info[0]
assert isinstance(column_info["type"], NullType)
class IntervalReflectionTest(fixtures.TestBase):
__only_on__ = "postgresql"
__sparse_driver_backend__ = True
@testing.combinations(
("YEAR",),
("MONTH",),
("DAY",),
("HOUR",),
("MINUTE",),
("SECOND",),
("YEAR TO MONTH",),
("DAY TO HOUR",),
("DAY TO MINUTE",),
("DAY TO SECOND",),
("HOUR TO MINUTE",),
("HOUR TO SECOND",),
("MINUTE TO SECOND",),
argnames="sym",
)
def test_interval_types(self, sym, metadata, connection):
t = Table(
"i_test",
metadata,
Column("id", Integer, primary_key=True),
Column("data1", INTERVAL(fields=sym)),
)
t.create(connection)
columns = {
rec["name"]: rec
for rec in inspect(connection).get_columns("i_test")
}
assert isinstance(columns["data1"]["type"], INTERVAL)
eq_(columns["data1"]["type"].fields, sym.lower())
eq_(columns["data1"]["type"].precision, None)
def test_interval_precision(self, metadata, connection):
t = Table(
"i_test",
metadata,
Column("id", Integer, primary_key=True),
Column("data1", INTERVAL(precision=6)),
)
t.create(connection)
columns = {
rec["name"]: rec
for rec in inspect(connection).get_columns("i_test")
}
assert isinstance(columns["data1"]["type"], INTERVAL)
eq_(columns["data1"]["type"].fields, None)
eq_(columns["data1"]["type"].precision, 6)
class IdentityReflectionTest(fixtures.TablesTest):
__only_on__ = "postgresql"
__sparse_driver_backend__ = True
__requires__ = ("identity_columns",)
_names = ("t1", "T2", "MiXeDCaSe!")
@classmethod
def define_tables(cls, metadata):
for name in cls._names:
Table(
name,
metadata,
Column(
"id1",
Integer,
Identity(
always=True,
start=2,
increment=3,
minvalue=-2,
maxvalue=42,
cycle=True,
cache=4,
),
),
Column("id2", Integer, Identity()),
Column("id3", BigInteger, Identity()),
Column("id4", SmallInteger, Identity()),
)
@testing.combinations(*_names, argnames="name")
def test_reflect_identity(self, connection, name):
insp = inspect(connection)
default = dict(
always=False,
start=1,
increment=1,
minvalue=1,
cycle=False,
cache=1,
)
cols = insp.get_columns(name)
for col in cols:
if col["name"] == "id1":
is_true("identity" in col)
eq_(
col["identity"],
dict(
always=True,
start=2,
increment=3,
minvalue=-2,
maxvalue=42,
cycle=True,
cache=4,
),
)
elif col["name"] == "id2":
is_true("identity" in col)
exp = default.copy()
exp.update(maxvalue=2**31 - 1)
eq_(col["identity"], exp)
elif col["name"] == "id3":
is_true("identity" in col)
exp = default.copy()
exp.update(maxvalue=2**63 - 1)
eq_(col["identity"], exp)
elif col["name"] == "id4":
is_true("identity" in col)
exp = default.copy()
exp.update(maxvalue=2**15 - 1)
eq_(col["identity"], exp)
class TestReflectDifficultColTypes(fixtures.TablesTest):
__only_on__ = "postgresql"
__sparse_driver_backend__ = True
def define_tables(metadata):
Table(
"sample_table",
metadata,
Column("c1", Integer, primary_key=True),
Column("c2", Integer, unique=True),
Column("c3", Integer),
Index("sample_table_index", "c2", "c3"),
)
def check_int_list(self, row, key):
value = row[key]
is_true(isinstance(value, list))
is_true(len(value) > 0)
is_true(all(isinstance(v, int) for v in value))
def test_pg_index(self, connection):
insp = inspect(connection)
pgc_oid = insp.get_table_oid("sample_table")
cols = [
col
for col in pg_catalog.pg_index.c
if testing.db.dialect.server_version_info
>= col.info.get("server_version", (0,))
]
stmt = sa.select(*cols).filter_by(indrelid=pgc_oid)
rows = connection.execute(stmt).mappings().all()
is_true(len(rows) > 0)
cols = [
col
for col in ["indkey", "indoption", "indclass", "indcollation"]
if testing.db.dialect.server_version_info
>= pg_catalog.pg_index.c[col].info.get("server_version", (0,))
]
for row in rows:
for col in cols:
self.check_int_list(row, col)
def test_pg_constraint(self, connection):
insp = inspect(connection)
pgc_oid = insp.get_table_oid("sample_table")
cols = [
col
for col in pg_catalog.pg_constraint.c
if testing.db.dialect.server_version_info
>= col.info.get("server_version", (0,))
]
stmt = sa.select(*cols).filter_by(conrelid=pgc_oid)
rows = connection.execute(stmt).mappings().all()
is_true(len(rows) > 0)
for row in rows:
self.check_int_list(row, "conkey")
|