Skip to content
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

Add table fail on onUpgrade #10

Closed
JulienDev opened this issue Mar 31, 2013 · 8 comments
Closed

Add table fail on onUpgrade #10

JulienDev opened this issue Mar 31, 2013 · 8 comments

Comments

@JulienDev
Copy link
Contributor

Imagine we have a database with current database version 4.
Let's add a table called "Tabs" on database version 5.

{
            "table_name":"Tabs",
            "version":5,
            "fields":[
                {
                    "name":"id",
                    "type":"long",
                    "is_primary_key":true,
                    "is_id":true
                        },
                {
                    "name":"name",
                    "type":"text"
                }
            ]
        }

The code generated is the following :

// Version 6 : Creation of the table
        public static void upgradeTable(SQLiteDatabase db, int oldVersion, int newVersion) {

            if (oldVersion < 1) {
                Log.i(LOG_TAG, "Upgrading from version " + oldVersion + " to " + newVersion 
                        + ", data will be lost!");

                db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
                createTable(db);
                return;
            }


            if (oldVersion != newVersion) {
                throw new IllegalStateException("Error upgrading the database to version " 
                        + newVersion);
            }
        }

The SQLiteOpenHelper with its method getDatabaseLocked detects a new version and then launches "onUpgrade(db, version, mNewVersion);.

Tabs.upgradeTable(db, oldVersion, newVersion); is called, oldVersion=4 and newVersion=5 so the first condition is false and the second is true and we throw an exception. Shouldn't we add a mechanism to force the table creation?

Julien

@frapontillo
Copy link
Contributor

This is an issue I have as well.
I haven't quite understood the upgrade mechanism, why is there the if (oldVersion < 1) check? Shouldn't it simply check if the new table version is higher than the older one, and if so create the table?

@frapontillo
Copy link
Contributor

Is there any update on this? The generator basically has no way of adding new tables in newer versions.

@frapontillo
Copy link
Contributor

I may have found a fix: frapontillo@7b49c01

The comparison between the oldVersion has to be done against the new table version, not a fixed number. I have tested it in a project of mine and it seems to be working. Will submit a PR.

@JulienDev
Copy link
Contributor Author

Something like that? #3

@frapontillo
Copy link
Contributor

Yeah, but the comparison shouldn't be done against newVersion (which is the database version) but the new version of the table (that, in some cases, may equal the db version).

@foxykeep
Copy link
Owner

foxykeep commented Apr 7, 2013

Working on it. (sorry work is crazy currently so I don't have a lot of time to work on my open source projects ...)

Trying to have a fix by tonight

@JulienDev
Copy link
Contributor Author

Thank you Nicolas ;)

@frapontillo
Copy link
Contributor

Great! :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants