summaryrefslogtreecommitdiffstats
path: root/src/com/android/launcher3/model/DbDowngradeHelper.java
blob: cd86b728b1042ffbeddb96341dd3f1e469fc9dc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.launcher3.model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;
import android.util.SparseArray;

import com.android.launcher3.provider.LauncherDbUtils.SQLiteTransaction;
import com.android.launcher3.util.IOUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;

/**
 * Utility class to handle DB downgrade
 */
public class DbDowngradeHelper {

    private static final String TAG = "DbDowngradeHelper";

    private static final String KEY_VERSION = "version";
    private static final String KEY_DOWNGRADE_TO = "downgrade_to_";

    private final SparseArray<String[]> mStatements = new SparseArray<>();
    public final int version;

    private DbDowngradeHelper(int version) {
        this.version = version;
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ArrayList<String> allCommands = new ArrayList<>();

        for (int i = oldVersion - 1; i >= newVersion; i--) {
            String[] commands = mStatements.get(i);
            if (commands == null) {
                throw new SQLiteException("Downgrade path not supported to version " + i);
            }
            Collections.addAll(allCommands, commands);
        }

        try (SQLiteTransaction t = new SQLiteTransaction(db)) {
            for (String sql : allCommands) {
                db.execSQL(sql);
            }
            t.commit();
        }
    }

    public static DbDowngradeHelper parse(File file) throws JSONException, IOException {
        JSONObject obj = new JSONObject(new String(IOUtils.toByteArray(file)));
        DbDowngradeHelper helper = new DbDowngradeHelper(obj.getInt(KEY_VERSION));
        for (int version = helper.version - 1; version > 0; version--) {
            if (obj.has(KEY_DOWNGRADE_TO + version)) {
                JSONArray statements = obj.getJSONArray(KEY_DOWNGRADE_TO + version);
                String[] parsed = new String[statements.length()];
                for (int i = 0; i < parsed.length; i++) {
                    parsed[i] = statements.getString(i);
                }
                helper.mStatements.put(version, parsed);
            }
        }
        return helper;
    }

    public static void updateSchemaFile(File schemaFile, int expectedVersion,
            Context context, int schemaResId) {
        try {
            if (DbDowngradeHelper.parse(schemaFile).version >= expectedVersion) {
                return;
            }
        } catch (Exception e) {
            // Schema error
        }

        // Write the updated schema
        try (FileOutputStream fos = new FileOutputStream(schemaFile);
            InputStream in = context.getResources().openRawResource(schemaResId)) {
            IOUtils.copy(in, fos);
        } catch (IOException e) {
            Log.e(TAG, "Error writing schema file", e);
        }
    }
}