OpenJPA
  1. OpenJPA
  2. OPENJPA-1760

SchemaAction='refresh,deleteTableContents' incorrectly removes data from ALL DB tables.

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.0.0, 2.1.0, 2.2.0
    • Fix Version/s: 2.4.0
    • Component/s: tooling
    • Labels:
      None
    • Environment:
      Win XP, Postgres 8.4, OpenJPA 2.0.0

      Description

      At my usecase not all tables are managed by openjpa so not all of them are listed with the class-tag inside persistence.xml. I set up openjpa to delete the table contents for developping with the following property:
      <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(SchemaAction='add,deleteTableContents')"/>

      But now all contents are deleted even the one of tables which aren't listed inside persistence.xml.

      Shouldn't openjpa only delete contents from tables which are configured plus the openjpa_sequence_table rather than deleting all contents from the whole database? Is this a bug or is there a property which I have to set to get the described behaviour?

      Greets Oli

        Activity

        Hide
        Rick Curtis added a comment -

        It sounds like this may be another bug? From the user manual [1] : deleteTableContents: Execute SQL to delete all rows from all tables that OpenJPA knows about.

        Could you write a simple unit test that recreates this problem?

        Thanks,
        Rick

        [1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_schema_schematool

        Show
        Rick Curtis added a comment - It sounds like this may be another bug? From the user manual [1] : deleteTableContents: Execute SQL to delete all rows from all tables that OpenJPA knows about. Could you write a simple unit test that recreates this problem? Thanks, Rick [1] http://openjpa.apache.org/builds/latest/docs/manual/manual.html#ref_guide_schema_schematool
        Hide
        Oliver Bayer added a comment -

        Hi Rick,

        first of all, sorry for the late reply.

        In the last couple of days I tried to get into writing junit tests but atm I don't have the time to get used to it and write an useful testcase for you to reproduce the problem. So I've coded some console application where openjpa behaves the way it should.

        But in my jsf application running in tomcat it always delets all table contents. Therefor I started some debugging session today.

        SchemaTool.deleteTableContents()
        1) console app: Schema[] schemas (line 421) contains only one scheme named "null".
        2) tomcat app: Schema[] schemas (line 421) contains two schemes named "null" and "public".

        Null seems to be the correct scheme whereas public always contains all available tables.
        Do you know if this occurs because of a misconfiguration or an another error I made?

        Regards Oli

        Show
        Oliver Bayer added a comment - Hi Rick, first of all, sorry for the late reply. In the last couple of days I tried to get into writing junit tests but atm I don't have the time to get used to it and write an useful testcase for you to reproduce the problem. So I've coded some console application where openjpa behaves the way it should. But in my jsf application running in tomcat it always delets all table contents. Therefor I started some debugging session today. SchemaTool.deleteTableContents() 1) console app: Schema[] schemas (line 421) contains only one scheme named "null". 2) tomcat app: Schema[] schemas (line 421) contains two schemes named "null" and "public". Null seems to be the correct scheme whereas public always contains all available tables. Do you know if this occurs because of a misconfiguration or an another error I made? Regards Oli
        Hide
        Rick Curtis added a comment -

        Oli -

        Could you try swapping out Postgres for MySql or Derby? This might help us narrow down where the problem is. I'll point out that as far as I can tell 'deleteTableContents' doesn't clear out all of my tables when running on MySQL.

        Hth,
        Rick

        Show
        Rick Curtis added a comment - Oli - Could you try swapping out Postgres for MySql or Derby? This might help us narrow down where the problem is. I'll point out that as far as I can tell 'deleteTableContents' doesn't clear out all of my tables when running on MySQL. Hth, Rick
        Hide
        Oliver Bayer added a comment -

        Hi Rick,

        once again sorry for not replying for a while.

        I've tried to write a test webapp so you can reproduce the error but I couldn't reproduce it on my own . Today I found the error. First I used SchemaAction='add,delete....' but for a while I changed to SchemaAction='refresh,delete....'.

        So I made a stupid error while creating this ticket in naming the wrong config parameter. Sorry for all the confusion.

        You're right: If I use SchemaAction='add,delete....' all is working correctly.
        BUT if I use SchemaAction='refresh,delete...' openjpa is deleting all contents of ALL tables in the database.

        After comparing both functions (add, refresh) in source code I maybe found the bug.

        SchemaTool - void refresh():
        Line 378:
        SchemaGroup db = getDBSchemaGroup(true);
        should be:
        SchemaGroup db = getDBSchemaGroup(FALSE);

        The add function uses FALSE as parameter too. If I set a breakpoint and change it manually to false openjpa is only deleting content of tables which are defined in persistence.xml so it should work.

        I hope you're now able to reproduce the bug and use my suggested bugfix if it doesn't have any sideeffects which I can't overlook. Can you please check it?

        Greets Oli

        Show
        Oliver Bayer added a comment - Hi Rick, once again sorry for not replying for a while. I've tried to write a test webapp so you can reproduce the error but I couldn't reproduce it on my own . Today I found the error. First I used SchemaAction='add,delete....' but for a while I changed to SchemaAction='refresh,delete....'. So I made a stupid error while creating this ticket in naming the wrong config parameter. Sorry for all the confusion. You're right: If I use SchemaAction='add,delete....' all is working correctly. BUT if I use SchemaAction='refresh,delete...' openjpa is deleting all contents of ALL tables in the database. After comparing both functions (add, refresh) in source code I maybe found the bug. SchemaTool - void refresh(): Line 378: SchemaGroup db = getDBSchemaGroup(true); should be: SchemaGroup db = getDBSchemaGroup(FALSE); The add function uses FALSE as parameter too. If I set a breakpoint and change it manually to false openjpa is only deleting content of tables which are defined in persistence.xml so it should work. I hope you're now able to reproduce the bug and use my suggested bugfix if it doesn't have any sideeffects which I can't overlook. Can you please check it? Greets Oli
        Hide
        Rick Curtis added a comment -

        Oli -

        I took ownership of this JIRA, but just to give you a heads up I probably won't find time to look at it for a couple weeks.

        Thanks,
        Rick

        Show
        Rick Curtis added a comment - Oli - I took ownership of this JIRA, but just to give you a heads up I probably won't find time to look at it for a couple weeks. Thanks, Rick
        Hide
        Oliver Bayer added a comment -

        Hi,

        how do you make progress? Did you find some time for testing the suggested bugfix?
        Thanks in advance for the info.

        Oli

        Show
        Oliver Bayer added a comment - Hi, how do you make progress? Did you find some time for testing the suggested bugfix? Thanks in advance for the info. Oli
        Hide
        Rick Curtis added a comment -

        Oli -

        I haven't found any time yet, but hope to shortly here.

        Thanks,
        Rick

        Show
        Rick Curtis added a comment - Oli - I haven't found any time yet, but hope to shortly here. Thanks, Rick
        Hide
        Oliver Bayer added a comment -

        Hi Rick,

        any updates on your progress?

        Greets Oli

        Show
        Oliver Bayer added a comment - Hi Rick, any updates on your progress? Greets Oli
        Hide
        Rick Curtis added a comment -

        Attaching a testcase and code change suggested by Oliver Bayer.

        I was able to recreate the issue and the suggested code change fixed the problem.

        Show
        Rick Curtis added a comment - Attaching a testcase and code change suggested by Oliver Bayer. I was able to recreate the issue and the suggested code change fixed the problem.
        Hide
        Rick Curtis added a comment -

        Removed earlier posted patch as it wasn't the proper change.

        Re-posting a patch with the testcase.

        Show
        Rick Curtis added a comment - Removed earlier posted patch as it wasn't the proper change. Re-posting a patch with the testcase.
        Hide
        Rick Curtis added a comment -

        Oli -

        I finally got around to reviewing the suggested code change and I don't think this is the correct direction to go. Yes the change does make the reported problem go away, but there is another big problem that wasn't mentioned.

        Per the docs for SchemaTool docs:

        • refresh: Equivalent to retain, then add.
        • retain: Keep all schema components in the given XML definition, but drop the rest from the database. This action never adds any schema components.
        • add: This is the default action if you do not specify one. It brings the schema up-to-date with the given XML document by adding tables, columns, indexes, etc. This action never drops any schema components.

        My interpretation of the docs says that retain should drop ALL tables that aren't known to OpenJPA. That isn't happening.

        The second problem, and in line with the one you reported is that the 'deleteTableContents' action is defined to only run against OpenJPA known tables. This is a problem with the SchemaTool.getDBSchemaGroup(boolean) method and it caches the value returned even though it is dependent on the value of the provided boolean. This appears to be a large problem that isn't as simple as flipping a single bit. This same problem exists if you were to execute 'add,deleteTableContents', 'createDB,deleteTableContents', etc.... Unfortunately I don't see this being a high enough priority issue to spend any more cycles on. Thanks for the patch and feel free to continue to dig into this issue if you want.

        Thanks,
        Rick

        Show
        Rick Curtis added a comment - Oli - I finally got around to reviewing the suggested code change and I don't think this is the correct direction to go. Yes the change does make the reported problem go away, but there is another big problem that wasn't mentioned. Per the docs for SchemaTool docs: refresh: Equivalent to retain, then add. retain: Keep all schema components in the given XML definition, but drop the rest from the database. This action never adds any schema components. add: This is the default action if you do not specify one. It brings the schema up-to-date with the given XML document by adding tables, columns, indexes, etc. This action never drops any schema components. My interpretation of the docs says that retain should drop ALL tables that aren't known to OpenJPA. That isn't happening. The second problem, and in line with the one you reported is that the 'deleteTableContents' action is defined to only run against OpenJPA known tables. This is a problem with the SchemaTool.getDBSchemaGroup(boolean) method and it caches the value returned even though it is dependent on the value of the provided boolean. This appears to be a large problem that isn't as simple as flipping a single bit. This same problem exists if you were to execute 'add,deleteTableContents', 'createDB,deleteTableContents', etc.... Unfortunately I don't see this being a high enough priority issue to spend any more cycles on. Thanks for the patch and feel free to continue to dig into this issue if you want. Thanks, Rick
        Hide
        Oliver Bayer added a comment -

        Hi Rick,

        thanks for your help and the effort you put into this issue.

        Maybe I have to read the spec once again to solve the misunderstanding, sorry for the unusable patch.
        I don't know if I get you right but I understand your answer as follows:

        • retain: should delete all unknown tables which isn't happening in my testcase
        • deleteTableContents: should delete content only from known tables but deletes ALL data

        So there are now two bugs than only the one I assumed?

        I'm sorry to say but I can't agree with the priority of this issue. I don't want to say anything against your work (don't get me wrong) but if a PERSISTENCE framework accidently remove ALL data it's if you would ask me nearly a show-stopper.

        I would like to help but if you can't fix it quickly on the fly it would take nearly months to get a valid bugfix .

        Nevertheless thanks a lot!
        Oli

        Show
        Oliver Bayer added a comment - Hi Rick, thanks for your help and the effort you put into this issue. Maybe I have to read the spec once again to solve the misunderstanding, sorry for the unusable patch. I don't know if I get you right but I understand your answer as follows: retain: should delete all unknown tables which isn't happening in my testcase deleteTableContents: should delete content only from known tables but deletes ALL data So there are now two bugs than only the one I assumed? I'm sorry to say but I can't agree with the priority of this issue. I don't want to say anything against your work (don't get me wrong) but if a PERSISTENCE framework accidently remove ALL data it's if you would ask me nearly a show-stopper. I would like to help but if you can't fix it quickly on the fly it would take nearly months to get a valid bugfix . Nevertheless thanks a lot! Oli
        Hide
        Albert Lee added a comment -

        Move fix version to 2.3.0 in preparation for 2.2.0 release.

        Show
        Albert Lee added a comment - Move fix version to 2.3.0 in preparation for 2.2.0 release.

          People

          • Assignee:
            Unassigned
            Reporter:
            Oliver Bayer
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:

              Development