root

Changeset 634

Show
Ignore:
Timestamp:
07/08/09 20:56:36 (8 months ago)
Author:
wbond
Message:

Fixed ticket #251 - fSchema now properly detects unique constraints on tables that are created as indexes instead of as table constraints

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • fSchema.php

    r610 r634 Hide Line Numbers
    1010 * @link       http://flourishlib.com/fSchema 
    1111 *  
    12  * @version    1.0.0b21 
     12 * @version    1.0.0b22 
     13 * @changes    1.0.0b22  PostgreSQL UNIQUE constraints that are created as indexes and not table constraints are now properly detected [wb, 2009-07-08] 
    1314 * @changes    1.0.0b21  Added support for the UUID data type in PostgreSQL [wb, 2009-06-18] 
    1415 * @changes    1.0.0b20  Add caching of merged info, improved performance of ::getColumnInfo() [wb, 2009-06-15] 
     
    13041305        } 
    13051306         
    1306         $sql  = "SELECT 
     1307        $sql  = "( 
     1308                 SELECT 
    13071309                         t.relname AS table, 
    13081310                         con.conname AS constraint_name, 
     
    13281330                             WHEN 'n' THEN 'set_null' 
    13291331                             WHEN 'd' THEN 'set_default' 
    1330                          END AS on_update 
     1332                         END AS on_update, 
     1333                        CASE WHEN con.conkey IS NOT NULL THEN position('-'||col.attnum||'-' in '-'||array_to_string(con.conkey, '-')||'-') ELSE 0 END AS column_order 
    13311334                     FROM 
    13321335                         pg_attribute AS col INNER JOIN 
     
    13421345                          con.contype = 'f' OR 
    13431346                          con.contype = 'u') 
    1344                      ORDER BY 
    1345                          t.relname, 
    1346                          con.contype, 
    1347                          con.conname, 
    1348                          CASE WHEN con.conkey IS NOT NULL THEN position('-'||col.attnum||'-' in '-'||array_to_string(con.conkey, '-')||'-') ELSE 0 END, 
    1349                          col.attname"; 
     1347                ) UNION ( 
     1348                SELECT 
     1349                        t.relname AS table, 
     1350                        ic.relname AS constraint_name, 
     1351                        'unique' AS type, 
     1352                        col.attname AS column, 
     1353                        NULL AS foreign_table, 
     1354                        NULL AS foreign_column, 
     1355                        NULL AS on_delete, 
     1356                        NULL AS on_update, 
     1357                        CASE WHEN ind.indkey IS NOT NULL THEN position('-'||col.attnum||'-' in '-'||array_to_string(ind.indkey, '-')||'-') ELSE 0 END AS column_order 
     1358                    FROM 
     1359                        pg_class AS t INNER JOIN 
     1360                        pg_index AS ind ON ind.indrelid = t.oid INNER JOIN 
     1361                        pg_namespace AS n ON t.relnamespace = n.oid INNER JOIN 
     1362                        pg_class AS ic ON ind.indexrelid = ic.oid LEFT JOIN 
     1363                        pg_constraint AS con ON con.conrelid = t.oid AND con.contype = 'u' AND con.conname = ic.relname INNER JOIN 
     1364                        pg_attribute AS col ON col.attrelid = t.oid AND col.attnum = ANY (ind.indkey)   
     1365                    WHERE 
     1366                        n.nspname NOT IN ('pg_catalog', 'pg_toast') AND 
     1367                        indisunique = TRUE AND 
     1368                        indisprimary = FALSE AND 
     1369                        con.oid IS NULL 
     1370                ) ORDER BY 1, 3, 2, 9"; 
    13501371         
    13511372        $result = $this->database->query($sql);