<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.kde.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.6"
          urlbase="https://bugs.kde.org/"
          
          maintainer="sysadmin@kde.org"
>

    <bug>
          <bug_id>354536</bug_id>
          
          <creation_ts>2015-10-29 07:48:52 +0000</creation_ts>
          <short_desc>Akonadi fails to migrate database from version 30 (foreign key constraint fails)</short_desc>
          <delta_ts>2016-03-11 00:13:26 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>Frameworks and Libraries</classification>
          <product>Akonadi</product>
          <component>Migration</component>
          <version>GIT (master)</version>
          <rep_platform>Kubuntu</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>NOR</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>0</everconfirmed>
          <reporter name="Lukáš Karas">lukas.karas</reporter>
          <assigned_to name="kdepim bugs">pim-bugs-null</assigned_to>
          <cc>ab4bd</cc>
    
    <cc>auxsvr</cc>
    
    <cc>dvratil</cc>
    
    <cc>martin</cc>
    
    <cc>tpr</cc>
          
          <cf_commitlink>http://commits.kde.org/akonadi/989f7eba0d1b093e00730a8444a9c088a3805ffd</cf_commitlink>
          <cf_versionfixedin>15.12.3</cf_versionfixedin>
          <cf_sentryurl></cf_sentryurl>
          <votes>1</votes>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1553314</commentid>
    <comment_count>0</comment_count>
    <who name="Lukáš Karas">lukas.karas</who>
    <bug_when>2015-10-29 07:48:52 +0000</bug_when>
    <thetext>Hi. I had akonadi installed from Kubuntu repository from package version 15.08.2-0ubuntu1, then I tried to compile and install from git sources (current HEAD is 1d84aca004ae08bf954bd7b9f1e20efe26c910b7 ). 

When I start new version of Akonadi, it exited in a moment with message:

Adding new foreign key constraints
&quot;ALTER TABLE PimItemFlagRelation ADD FOREIGN KEY (PimItem_id) REFERENCES PimItemTable(id) ON UPDATE CASCADE ON DELETE CASCADE&quot;
Updating index failed:  
Sql error: Cannot add or update a child row: a foreign key constraint fails (`akonadi`.`#sql-4c21_3`, CONSTRAINT `#sql-4c21_3_ibfk_1` FOREIGN KEY (`PimItem_id`) REFERENCES `pimitemtable` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) QMYSQL: Unable to execute query
Query: ALTER TABLE PimItemFlagRelation ADD FOREIGN KEY (PimItem_id) REFERENCES PimItemTable(id) ON UPDATE CASCADE ON DELETE CASCADE
&quot;&quot;
Unable to initialize database.
terminating service threads
terminating connection threads
stopping db process
Application &apos;akonadiserver&apos; exited normally...


Full log: http://pastebin.com/DH1SntcY

It seems that previous version of Akonadi keeps some rows in PimItemFlagRelation table that don&apos;t match any row in PimItemTable...

mysql&gt; select * from schemaversiontable ; 
+---------+
| version |
+---------+
|      30 |
+---------+
1 row in set (0,00 sec)

mysql&gt; select count(*) from PimItemFlagRelation ; 
+----------+
| count(*) |
+----------+
|   723468 |
+----------+
1 row in set (0,18 sec)

mysql&gt; select count(*) from PimItemTable ;
+----------+
| count(*) |
+----------+
|   230366 |
+----------+
1 row in set (0,09 sec)


mysql&gt; select count(*) from PimItemFlagRelation as r left join PimItemTable as p on r.PimItem_id = p.id where p.id is null ;
+----------+
| count(*) |
+----------+
|   485150 |
+----------+
1 row in set (0,91 sec)




Reproducible: Always

Steps to Reproduce:
akonadictl start


Actual Results:  
database schema upgrade fails

Expected Results:  
akonadi is running

Migration tool should delete useless rows from PimItemFlagRelation before it adds new constrains.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1553365</commentid>
    <comment_count>1</comment_count>
    <who name="Lukáš Karas">lukas.karas</who>
    <bug_when>2015-10-29 10:37:29 +0000</bug_when>
    <thetext>This bug was probably solved for PostgreSQL backend by commit 54e1f0979a894d289110a1a71618ce7be54fe595 ( https://quickgit.kde.org/?p=akonadi.git&amp;a=commit&amp;h=54e1f0979a894d289110a1a71618ce7be54fe595 ). In commit message Dan mentions that it should not happen with mysql backend. 

This problem did not affect MySQL which creates the FKs as part of CREATE TABLE
queries, or SQLite because we don&apos;t support FKs there (yet). 

I don&apos;t know why, but I don&apos;t have these FK in my database... What changes that FK check works now? Should we add similar DELETE commands to dbupdate.xml for mysql backend? For which db version? 31?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1553624</commentid>
    <comment_count>2</comment_count>
      <attachid>95224</attachid>
    <who name="Lukáš Karas">lukas.karas</who>
    <bug_when>2015-10-30 12:42:24 +0000</bug_when>
    <thetext>Created attachment 95224
dbupdate.patch

This patch with extra update queries fixes migration for me.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1563197</commentid>
    <comment_count>3</comment_count>
    <who name="Martin Bednar">martin</who>
    <bug_when>2015-12-13 20:37:51 +0000</bug_when>
    <thetext>I think I just got hit by this.
Log here : https://paste.kde.org/paxixlmnn

Using a system-wide mysql server. (also used for web development)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1565047</commentid>
    <comment_count>4</comment_count>
    <who name="">auxsvr</who>
    <bug_when>2015-12-21 11:08:31 +0000</bug_when>
    <thetext>I have the same problem now that I upgraded akonadi to 15.12 on openSUSE Leap 42.1. I fixed it by deleting the non-matching rows with the patch in comment #2 (by the way, the second delete misses &quot;AS id&quot; after flag_id).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1566638</commentid>
    <comment_count>5</comment_count>
    <who name="Teemu Rytilahti">tpr</who>
    <bug_when>2015-12-30 15:20:30 +0000</bug_when>
    <thetext>I can confirm this on Arch with Mariadb, with Akonadi 15.12.0-1 and Mariadb 10.1.10-1.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1567188</commentid>
    <comment_count>6</comment_count>
    <who name="Daniel Vrátil">dvratil</who>
    <bug_when>2016-01-03 17:23:00 +0000</bug_when>
    <thetext>Running &quot;akonadictl fsck&quot; will make sure that are no duplicate or invalid entriers. After that the update from revision 30 should be OK.

Note that although &quot;akonadictl fsck&quot; returns immediatelly, the actual task, which is running on the Server may take couple minutes to complete (this was fixed in 15.12)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1567273</commentid>
    <comment_count>7</comment_count>
    <who name="Lukáš Karas">lukas.karas</who>
    <bug_when>2016-01-04 08:46:27 +0000</bug_when>
    <thetext>Good to know that. What about automatic fsck operation before every schema upgrade? We can avoid similar situations in the future. But we should trust fsck code and keep it updated then...</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1581740</commentid>
    <comment_count>8</comment_count>
    <who name="Daniel Vrátil">dvratil</who>
    <bug_when>2016-03-11 00:10:45 +0000</bug_when>
    <thetext>Git commit 989f7eba0d1b093e00730a8444a9c088a3805ffd by Daniel Vrátil, on behalf of Lukáš Karas.
Committed on 11/03/2016 at 00:10.
Pushed by dvratil into branch &apos;Applications/15.12&apos;.

Clean orphaned relations when upgrading DB schema to r31

Commit 54e1f097 introduced automatic recovery of missing foreign keys during
startup (this has been accompanied by DB schema upgrade to revision 31). If
they FKs were previously missing MySQL will fail to create the keys and Akonadi
startup will fail.

To make sure there are no inconsistencies in DB we manually delete all orphaned
relations before proceeding with the DB check.
FIXED-IN: 15.12.3

M  +60   -0    src/server/storage/dbupdate.xml

http://commits.kde.org/akonadi/989f7eba0d1b093e00730a8444a9c088a3805ffd</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1581741</commentid>
    <comment_count>9</comment_count>
    <who name="Daniel Vrátil">dvratil</who>
    <bug_when>2016-03-11 00:13:26 +0000</bug_when>
    <thetext>There seem to be more and more people affected by this as distros slowly adopt KF5-based KDE PIM and I now realized that you can&apos;t possibly run akonadictl fsck when Akonadi is not running because your DB is inconsistent :) so I merged the patch from Lukas.

I like the idea about automatically running fsck before schema upgrade, I&apos;ll see if we can implement it for 16.04.

Thanks for the patch Lukas!</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>95224</attachid>
            <date>2015-10-30 12:42:24 +0000</date>
            <delta_ts>2015-10-30 12:42:24 +0000</delta_ts>
            <desc>dbupdate.patch</desc>
            <filename>akonadi.patch</filename>
            <type>text/plain</type>
            <size>4107</size>
            <attacher name="Lukáš Karas">lukas.karas</attacher>
            
              <data encoding="base64">ZGlmZiAtLWdpdCBhL3NyYy9zZXJ2ZXIvc3RvcmFnZS9kYnVwZGF0ZS54bWwgYi9zcmMvc2VydmVy
L3N0b3JhZ2UvZGJ1cGRhdGUueG1sCmluZGV4IGVkNDhiM2UuLjRlMGI4NTQgMTAwNjQ0Ci0tLSBh
L3NyYy9zZXJ2ZXIvc3RvcmFnZS9kYnVwZGF0ZS54bWwKKysrIGIvc3JjL3NlcnZlci9zdG9yYWdl
L2RidXBkYXRlLnhtbApAQCAtMjU2LDQgKzI1Niw2NCBAQAogICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICBXSEVSRSBQaW1JdGVtVGFibGUuaWQgSVMgTlVMTCkKICAgICA8L3Jhdy1zcWw+
CiAgIDwvdXBkYXRlPgorIDwhLS0KKyAgIFVwZGF0ZSAzMSBmaXhlcyBtaXNzaW5nIGNvbnN0cmFp
bnRzIG9uIHJlbGF0aW9uIHRhYmxlcyBpbiBNeVNRTC4KKyAgIFdlIGNhbm5vdCBqdXN0IGxldCBE
YkluaXRpYWxpemVyIGNyZWF0ZSB0aGUgZm9yZWlnbiBrZXlzIHRoZXJlLCBiZWNhdXNlIGRhdGEK
KyAgIGluIHRoZSByZWxhdGlvbiB0YWJsZXMgbWlnaHQgcmVmZXJlbmNlIHJvd3MgZnJvbSBvdGhl
ciB0YWJsZXMgdGhhdCBubyBsb25nZXIKKyAgIGV4aXN0LgorICAtLT4KKyAgPHVwZGF0ZSB2ZXJz
aW9uP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VMTCkgeCkKKyAgICA8L3Jhdy1z
cWw+CisgICAgPCEtLSBQaW1JdGVtVGFnUmVsYXRpb24gLS0+CisgICAgPHJhdy1zcWwgYmFja2Vu
ZHM9Im15c3FsIj5ERUxFVEUgRlJPTSBQaW1JdGVtVGFnUmVsYXRpb24gV0hFUkUgcGltSXRlbV9p
ZCBJTiAoCisgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFNFTEVDVCBpZCBGUk9NICgK
KyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgU0VMRUNUIHBpbUl0ZW1faWQgQVMgaWQg
RlJPTSBQaW1JdGVtVGFnUmVsYXRpb24KKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
TEVGVCBKT0lOIFBpbUl0ZW1UYWJsZSBPTiBQaW1JdGVtVGFnUmVsYXRpb24ucGltSXRlbV9pZCA9
IFBpbUl0ZW1UYWJsZS5pZAorICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBXSEVSRSBQ
aW1JdGVtVGFibGUuaWQgSVMgTlVMTCkgeCkKKyAgICA8L3Jhdy1zcWw+CisgICAgPHJhdy1zcWwg
YmFja2VuZHM9Im15c3FsIj5ERUxFVEUgRlJPTSBQaW1JdGVtVGFnUmVsYXRpb24gV0hFUkUgdGFn
X2lkIElOICgKKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgU0VMRUNUIGlkIEZST00g
KAorICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBTRUxFQ1QgdGFnX2lkIEFTIGlkIEZS
T00gUGltSXRlbVRhZ1JlbGF0aW9uCisgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIExF
RlQgSk9JTiBUYWdUYWJsZSBPTiBQaW1JdGVtVGFnUmVsYXRpb24udGFnX2lkID0gVGFnVGFibGUu
aWQKKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgV0hFUkUgVGFnVGFibGUuaWQgSVMg
TlVMTCkgeCkKKyAgICA8L3Jhdy1z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1lVHlw
ZVRhYmxlLmlkCisgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIFdIRVJFIE1pbWVUeXBl
VGFibGUuaWQgSVMgTlVMTCkgeCkKKyAgICA8L3Jhdy1zcWw+CisgICAgPCEtLSBDb2xsZWN0aW9u
UGltSXRlbVJlbGF0aW9uIC0tPgorICAgIDxyYXctc3FsIGJhY2tlbmRzPSJteXNxbCI+REVMRVRF
IEZST00gQ29sbGVjdGlvblBpbUl0ZW1SZWxhdGlvbiBXSEVSRSBjb2xsZWN0aW9uX2lkIElOICgK
KyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgU0VMRUNUIGlkIEZST00gKAorICAgICAg
ICAgICAgICAgICAgICAgICAgICAgICAgICBTRUxFQ1QgY29sbGVjdGlvbl9pZCBBUyBpZCBGUk9N
IENvbGxlY3Rpb25QaW1JdGVtUmVsYXRpb24KKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgTEVGVCBKT0lOIENvbGxlY3Rpb25UYWJsZSBPTiBDb2xsZWN0aW9uUGltSXRlbVJlbGF0aW9u
LmNvbGxlY3Rpb25faWQgPSBDb2xsZWN0aW9uVGFibGUuaWQKKyAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICAgV0hFUkUgQ29sbGVjdGlvblRhYmxlLmlkIElTIE5VTEwpIHgpCisgICAgPC9y
YXctc3FsPgorICAgIDxyYXctc3FsIGJhY2tlbmRzPSJteXNxbCI+REVMRVRFIEZST00gQ29sbGVj
dGlvblBpbUl0ZW1SZWxhdGlvbiBXSEVSRSBwaW1JdGVtX2lkIElOICgKKyAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgU0VMRUNUIGlkIEZST00gKAorICAgICAgICAgICAgICAgICAgICAg
ICAgICAgICAgICBTRUxFQ1QgcGltSXRlbV9pZCBBUyBpZCBGUk9NIENvbGxlY3Rpb25QaW1JdGVt
UmVsYXRpb24KKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgTEVGVCBKT0lOIFBpbUl0
ZW1UYWJsZSBPTiBDb2xsZWN0aW9uUGltSXRlbVJlbGF0aW9uLnBpbUl0ZW1faWQgPSBQaW1JdGVt
VGFibGUuaWQKKyAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgV0hFUkUgUGltSXRlbVRh
YmxlLmlkIElTIE5VTEwpIHgpCisgICAgPC9yYXctc3FsPgorICA8L3VwZGF0ZT4KIDwvdXBkYXRl
cz4K
</data>

          </attachment>
      

    </bug>

</bugzilla>