New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database migration issue #30

Closed
nis73 opened this Issue May 1, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@nis73

nis73 commented May 1, 2015

Hi, I'm encountering an issue with auto database migration. When I add new fields to my database objects the auto migration crashes. Please see the following MWE.

This is the simple class I'm starting with:

public class MyObject extends RushObject {

    private String value;

    public MyObject(String value) {
        this.value = value;
    }
}

In my activity I create an instance and write it to the database:

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyObject obj = new MyObject("Test");
        obj.save();
    }
}

Everything works fine until now. But when adding a new member to my object and running the code from the activity above, it crashes.

public class MyObject extends RushObject {
    private String value;
    private String nextField;
    public MyObject(String value) {
        this.value = value;
        this.nextField = value;
    }
}

The exception:

java.lang.RuntimeException: Unable to start activity ComponentInfo{de.test.testrushorm/de.test.testrushorm.MainActivity}: co.uk.rushorm.core.exceptions.RushSqlException: This is most likely caused by a change in data structure or new RushObject.
    This issue should be resolved by a database migration.
    This can be done by updating db version number in the manifest or setting Rush in debug mode.
    To set Rush in debug mode add this to your manifest file - <meta-data android:name="Rush_debug" android:value="true" />
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2103)
            at android.app.ActivityThread.access$600(ActivityThread.java:137)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1211)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4827)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: co.uk.rushorm.core.exceptions.RushSqlException: This is most likely caused by a change in data structure or new RushObject.
    This issue should be resolved by a database migration.
    This can be done by updating db version number in the manifest or setting Rush in debug mode.
    To set Rush in debug mode add this to your manifest file - <meta-data android:name="Rush_debug" android:value="true" />
            at co.uk.rushorm.android.AndroidRushStatementRunner.runRaw(AndroidRushStatementRunner.java:42)
            at co.uk.rushorm.core.RushCore$13.createdOrUpdateStatement(RushCore.java:464)
            at co.uk.rushorm.core.implementation.Insert.SqlBulkInsertGenerator$2.doAction(SqlBulkInsertGenerator.java:116)
            at co.uk.rushorm.core.implementation.ReflectionUtils.doLoop(ReflectionUtils.java:67)
            at co.uk.rushorm.core.implementation.Insert.SqlBulkInsertGenerator.createOrUpdateObjects(SqlBulkInsertGenerator.java:80)
            at co.uk.rushorm.core.implementation.Insert.ReflectionSaveStatementGenerator.createOrUpdateObjects(ReflectionSaveStatementGenerator.java:184)
            at co.uk.rushorm.core.implementation.Insert.ReflectionSaveStatementGenerator.generateSaveOrUpdate(ReflectionSaveStatementGenerator.java:56)
            at co.uk.rushorm.core.RushCore.save(RushCore.java:455)
            at co.uk.rushorm.core.RushCore.save(RushCore.java:146)
            at co.uk.rushorm.core.RushCore.save(RushCore.java:141)
            at co.uk.rushorm.core.RushObject.save(RushObject.java:12)
            at de.test.testrushorm.MainActivity.onCreate(MainActivity.java:19)
            at android.app.Activity.performCreate(Activity.java:5013)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2042)
            ... 11 more

My manifest:

<application
    android:name=".TestApplication"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme">

    <!-- Updating this will cause a database upgrade -->
    <meta-data android:name="Rush_db_version" android:value="4" />
    <!-- Database name -->
    <meta-data android:name="Rush_db_name" android:value="rush.db" />
    <!-- Setting this to true will cause a migration to happen every launch,
    this is very handy during development although could cause data loss -->
    <meta-data android:name="Rush_debug" android:value="false" />
    <!-- Setting this to true mean that tables will only be created of classes that
    extend RushObject and are annotated with @RushTableAnnotation -->
    <meta-data android:name="Rush_requires_table_annotation" android:value="false" />
....

I'm using version 1.1.3 and I updated the database after each change in the data model. I would expect the auto migration to be able to handle a single new field.

Am I wrong or did missed something? Thanks in advance.

@Stuart-campbell

This comment has been minimized.

Show comment
Hide comment
@Stuart-campbell

Stuart-campbell May 1, 2015

Owner

Does MyObject have an empty constructor?

Owner

Stuart-campbell commented May 1, 2015

Does MyObject have an empty constructor?

@nis73

This comment has been minimized.

Show comment
Hide comment
@nis73

nis73 May 1, 2015

Yes, now it does have an empty constructor :) But unfortunatelly, the same exception still occurs.
I know about the debug feature, but I'm on the way to production state, so this is no option for me.
As I've seen just right now, the exception also occurs, if don't change the data model but only the db version in manifest.

nis73 commented May 1, 2015

Yes, now it does have an empty constructor :) But unfortunatelly, the same exception still occurs.
I know about the debug feature, but I'm on the way to production state, so this is no option for me.
As I've seen just right now, the exception also occurs, if don't change the data model but only the db version in manifest.

Stuart-campbell added a commit that referenced this issue May 5, 2015

How to delete/drop/empty whole database #32, Customizable table name #31
, Database migration issue #30, How to clear a table? #27,

 Edit New issue
How to execute SQL? #26

@Stuart-campbell Stuart-campbell added the bug label May 5, 2015

@Stuart-campbell

This comment has been minimized.

Show comment
Hide comment
@Stuart-campbell

Stuart-campbell May 5, 2015

Owner

Sorry a bit slow to get back to you. This was a bug.
I'm hopping it is now resolved in v1.1.4

Let me know if it works.

Thanks

Owner

Stuart-campbell commented May 5, 2015

Sorry a bit slow to get back to you. This was a bug.
I'm hopping it is now resolved in v1.1.4

Let me know if it works.

Thanks

@nis73

This comment has been minimized.

Show comment
Hide comment
@nis73

nis73 May 7, 2015

Thanks, it works now.

nis73 commented May 7, 2015

Thanks, it works now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment