qore icon indicating copy to clipboard operation
qore copied to clipboard

sqlutil: oracle: schema alignment results in wrong SQL order when table contains a PK and new schema hash (template) requests its drop

Open pvanek opened this issue 8 years ago • 0 comments

wrong order is:

STD SQL> drop index h3g_it_int_code_comb_pk

ERROR: DBI:ORACLE:OCI-ERROR: h3g_apex@ebssib01: QorePreparedStatement::exec(): ORA-02429: cannot drop index used for enforcement of unique/primary key

STD SQL> alter table h3g_apex.h3g_it_int_code_combinations drop primary key keep index

but it should be (probably)

STD SQL> alter table h3g_apex.h3g_it_int_code_combinations drop primary key

theoretical fix:

diff --git a/qore/share/qore-modules/0.8.12/SqlUtil.qm b/qore/share/qore-modules/0.8.12/SqlUtil.qm
index ad67a40..822cbd4 100644
--- a/qore/share/qore-modules/0.8.12/SqlUtil.qm
+++ b/qore/share/qore-modules/0.8.12/SqlUtil.qm
@@ -13211,6 +13211,14 @@ list l = table.getAlignSql(table2);
                 # check for indexes not in the template and drop them
                 foreach AbstractIndex ix in (indexes.iterator()) {
                     if (!t.indexes.hasKey(ix.name)) {
+
+# PK is in table, but it is not present in schema template hash
+if (ix.unique && !primaryKey.empty() && primaryKey.matchKeys(ix.columns)) {
+    if (primaryKey.setIndexBase(ix.name)) {
+        ix.setSupportingConstraint(primaryKey);
+        continue;
+    }
+}

pvanek avatar Feb 24 '17 14:02 pvanek