Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0c676a65 authored by Stefan Niedermann's avatar Stefan Niedermann Committed by Niedermann IT-Dienstleistungen
Browse files

Add some documentation for database migrations

parent dc83340d
Loading
Loading
Loading
Loading
+16 −20
Original line number Diff line number Diff line
@@ -13,17 +13,16 @@ import it.niedermann.owncloud.notes.persistence.migration.Migration_12_13;
import it.niedermann.owncloud.notes.persistence.migration.Migration_13_14;
import it.niedermann.owncloud.notes.persistence.migration.Migration_14_15;
import it.niedermann.owncloud.notes.persistence.migration.Migration_15_16;
import it.niedermann.owncloud.notes.persistence.migration.Migration_16_17;
import it.niedermann.owncloud.notes.persistence.migration.Migration_4_5;
import it.niedermann.owncloud.notes.persistence.migration.Migration_5_6;
import it.niedermann.owncloud.notes.persistence.migration.Migration_6_7;
import it.niedermann.owncloud.notes.persistence.migration.Migration_7_8;
import it.niedermann.owncloud.notes.persistence.migration.Migration_8_9;
import it.niedermann.owncloud.notes.persistence.migration.Migration_9_10;
import it.niedermann.owncloud.notes.util.DatabaseIndexUtil;

// Protected APIs
@SuppressWarnings("WeakerAccess")
abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
    private static final String TAG = AbstractNotesDatabase.class.getSimpleName();

    private static final int database_version = 17;
    @NonNull
@@ -37,7 +36,6 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
    protected static final String table_widget_note_list = "WIDGET_NOTE_LISTS";

    protected static final String key_id = "ID";

    protected static final String key_url = "URL";
    protected static final String key_account_name = "ACCOUNT_NAME";
    protected static final String key_username = "USERNAME";
@@ -163,32 +161,31 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
                recreateDatabase(db);
                return;
            case 4:
                new Migration_4_5(db, oldVersion);
                new Migration_4_5(db);
            case 5:
                new Migration_5_6(db, oldVersion);
                new Migration_5_6(db);
            case 6:
                new Migration_6_7(db, oldVersion);
                new Migration_6_7(db);
            case 7:
                new Migration_7_8(db, oldVersion);
                new Migration_7_8(db);
            case 8:
                new Migration_8_9(db, oldVersion, context, this::recreateDatabase, this::notifyWidgets);
                new Migration_8_9(db, context, this::recreateDatabase, this::notifyWidgets);
            case 9:
                new Migration_9_10(db, oldVersion);
                new Migration_9_10(db);
            case 10:
                new Migration_10_11(oldVersion, context);
                new Migration_10_11(context);
            case 11:
                new Migration_11_12(db, oldVersion, context);
                new Migration_11_12(db, context);
            case 12:
                new Migration_12_13(db, oldVersion, context);
                new Migration_12_13(db, context);
            case 13:
                new Migration_13_14(db, oldVersion, context, this::notifyWidgets);
                new Migration_13_14(db, context, this::notifyWidgets);
            case 14:
                new Migration_14_15(db, oldVersion);
                new Migration_14_15(db);
            case 15:
                new Migration_15_16(db, oldVersion, context, this::notifyWidgets);
        }
        if(oldVersion < 17) {
            db.execSQL("ALTER TABLE NOTES ADD COLUMN SCROLL_Y INTEGER DEFAULT 0");
                new Migration_15_16(db, context, this::notifyWidgets);
            case 16:
                new Migration_16_17(db);
        }
    }

@@ -205,6 +202,5 @@ abstract class AbstractNotesDatabase extends SQLiteOpenHelper {
        onCreate(db);
    }


    protected abstract void notifyWidgets();
}
+0 −28
Original line number Diff line number Diff line
package it.niedermann.owncloud.notes.persistence;

import android.database.sqlite.SQLiteDatabase;

import it.niedermann.owncloud.notes.util.DatabaseIndexUtil;

public class Migration_7_8 {
    public Migration_7_8(SQLiteDatabase db, int oldVersion) {
        if (oldVersion < 8) {
            final String table_temp = "NOTES_TEMP";
            db.execSQL("CREATE TABLE " + table_temp + " ( " +
                    "ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "REMOTEID INTEGER, " +
                    "STATUS VARCHAR(50), " +
                    "TITLE TEXT, " +
                    "MODIFIED INTEGER DEFAULT 0, " +
                    "CONTENT TEXT, " +
                    "FAVORITE INTEGER DEFAULT 0, " +
                    "CATEGORY TEXT NOT NULL DEFAULT '', " +
                    "ETAG TEXT)");
            DatabaseIndexUtil.createIndex(db, table_temp, "REMOTEID", "STATUS", "FAVORITE", "CATEGORY", "MODIFIED");
            db.execSQL(String.format("INSERT INTO %s(%s,%s,%s,%s,%s,%s,%s,%s,%s) ", table_temp, "ID", "REMOTEID", "STATUS", "TITLE", "MODIFIED", "CONTENT", "FAVORITE", "CATEGORY", "ETAG")
                    + String.format("SELECT %s,%s,%s,%s,strftime('%%s',%s),%s,%s,%s,%s FROM %s", "ID", "REMOTEID", "STATUS", "TITLE", "MODIFIED", "CONTENT", "FAVORITE", "CATEGORY", "ETAG", "NOTES"));
            db.execSQL("DROP TABLE NOTES");
            db.execSQL(String.format("ALTER TABLE %s RENAME TO %s", table_temp, "NOTES"));
        }
    }
}
+14 −13
Original line number Diff line number Diff line
@@ -11,8 +11,10 @@ import java.util.Map;
import it.niedermann.owncloud.notes.android.DarkModeSetting;

public class Migration_10_11 {
    public Migration_10_11(int oldVersion, @NonNull Context context) {
        if (oldVersion < 11) {
    /**
     * Changes the boolean for light / dark mode to {@link DarkModeSetting} to also be able to represent system default value
     */
    public Migration_10_11(@NonNull Context context) {
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        Map<String, ?> prefs = sharedPreferences.getAll();
@@ -27,4 +29,3 @@ public class Migration_10_11 {
        editor.apply();
    }
}
}
+9 −7
Original line number Diff line number Diff line
@@ -5,15 +5,17 @@ import android.database.sqlite.SQLiteDatabase;

import androidx.annotation.NonNull;

import it.niedermann.owncloud.notes.model.ApiVersion;
import it.niedermann.owncloud.notes.persistence.CapabilitiesWorker;

public class Migration_11_12 {
    public Migration_11_12(SQLiteDatabase db, int oldVersion, @NonNull Context context) {
        if (oldVersion < 12) {
    /**
     * Adds columns to store the {@link ApiVersion} and the theme colors
     */
    public Migration_11_12(@NonNull SQLiteDatabase db, @NonNull Context context) {
        db.execSQL("ALTER TABLE ACCOUNTS ADD COLUMN API_VERSION TEXT");
        db.execSQL("ALTER TABLE ACCOUNTS ADD COLUMN COLOR VARCHAR(6) NOT NULL DEFAULT '000000'");
        db.execSQL("ALTER TABLE ACCOUNTS ADD COLUMN TEXT_COLOR VARCHAR(6) NOT NULL DEFAULT '0082C9'");
        CapabilitiesWorker.update(context);
    }
}
}
+9 −6
Original line number Diff line number Diff line
@@ -6,12 +6,15 @@ import android.database.sqlite.SQLiteDatabase;
import androidx.annotation.NonNull;
import androidx.work.WorkManager;

import it.niedermann.owncloud.notes.model.Capabilities;

public class Migration_12_13 {
    public Migration_12_13(SQLiteDatabase db, int oldVersion, @NonNull Context context) {
        if (oldVersion < 13) {
    /**
     * Adds a column to store the ETag of the server {@link Capabilities}
     */
    public Migration_12_13(@NonNull SQLiteDatabase db, @NonNull Context context) {
        db.execSQL("ALTER TABLE ACCOUNTS ADD COLUMN CAPABILITIES_ETAG TEXT");
        WorkManager.getInstance(context.getApplicationContext()).cancelUniqueWork("it.niedermann.owncloud.notes.persistence.SyncWorker");
        WorkManager.getInstance(context.getApplicationContext()).cancelUniqueWork("SyncWorker");
    }
}
}
Loading