1/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.messaging.datamodel;
17
18import android.content.Context;
19import android.database.sqlite.SQLiteDatabase;
20
21import com.android.messaging.Factory;
22import com.android.messaging.util.Assert;
23import com.android.messaging.util.LogUtil;
24
25public class DatabaseUpgradeHelper {
26    private static final String TAG = LogUtil.BUGLE_DATABASE_TAG;
27
28    public void doOnUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
29        Assert.isTrue(newVersion >= oldVersion);
30        if (oldVersion == newVersion) {
31            return;
32        }
33
34        LogUtil.i(TAG, "Database upgrade started from version " + oldVersion + " to " + newVersion);
35        try {
36            doUpgradeWithExceptions(db, oldVersion, newVersion);
37            LogUtil.i(TAG, "Finished database upgrade");
38        } catch (final Exception ex) {
39            LogUtil.e(TAG, "Failed to perform db upgrade from version " +
40                    oldVersion + " to version " + newVersion, ex);
41            DatabaseHelper.rebuildTables(db);
42        }
43    }
44
45    public void doUpgradeWithExceptions(final SQLiteDatabase db, final int oldVersion,
46            final int newVersion) throws Exception {
47        int currentVersion = oldVersion;
48        if (currentVersion < 2) {
49            currentVersion = upgradeToVersion2(db);
50        }
51        // Rebuild all the views
52        final Context context = Factory.get().getApplicationContext();
53        DatabaseHelper.dropAllViews(db);
54        DatabaseHelper.rebuildAllViews(new DatabaseWrapper(context, db));
55        // Finally, check if we have arrived at the final version.
56        checkAndUpdateVersionAtReleaseEnd(currentVersion, Integer.MAX_VALUE, newVersion);
57    }
58
59    private int upgradeToVersion2(final SQLiteDatabase db) {
60        db.execSQL("ALTER TABLE " + DatabaseHelper.CONVERSATIONS_TABLE + " ADD COLUMN " +
61                DatabaseHelper.ConversationColumns.IS_ENTERPRISE + " INT DEFAULT(0)");
62        LogUtil.i(TAG, "Ugraded database to version 2");
63        return 2;
64    }
65
66    /**
67     * Checks db version correctness at the end of each milestone release. If target database
68     * version lies beyond the version range that the current release may handle, we snap the
69     * current version to the end of the release, so that we may go on to the next release' upgrade
70     * path. Otherwise, if target version is within reach of the current release, but we are not
71     * at the target version, then throw an exception to force a table rebuild.
72     */
73    private int checkAndUpdateVersionAtReleaseEnd(final int currentVersion,
74            final int maxVersionForRelease, final int targetVersion) throws Exception {
75        if (maxVersionForRelease < targetVersion) {
76            // Target version is beyond the current release. Snap to max version for the
77            // current release so we can go on to the upgrade path for the next release.
78            return maxVersionForRelease;
79        }
80
81        // If we are here, this means the current release' upgrade handler should upgrade to
82        // target version...
83        if (currentVersion != targetVersion) {
84            // No more upgrade handlers. So we can't possibly upgrade to the final version.
85            throw new Exception("Missing upgrade handler from version " +
86                    currentVersion + " to version " + targetVersion);
87        }
88        // Upgrade succeeded.
89        return targetVersion;
90    }
91
92    public void onDowngrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
93        DatabaseHelper.rebuildTables(db);
94        LogUtil.e(TAG, "Database downgrade requested for version " +
95                oldVersion + " version " + newVersion + ", forcing db rebuild!");
96    }
97}
98