Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stefan-niedermann/nextcloud-notes.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Niedermann <info@niedermann.it>2021-05-11 22:15:56 +0300
committerNiedermann IT-Dienstleistungen <stefan-niedermann@users.noreply.github.com>2021-05-12 11:58:43 +0300
commit3ddfde9a5418ba23aef752162c73d423a5e207a3 (patch)
tree0eae9f14d7d8aaa6f487c9b6d7c226094dc6d82c /app/src/main/java/it
parent59c99f7de45e0ed35761f675852c60b5cb8a9714 (diff)
Sanitize API versions while migrating the database
Diffstat (limited to 'app/src/main/java/it')
-rw-r--r--app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_14_15.java20
-rw-r--r--app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_22_23.java75
-rw-r--r--app/src/main/java/it/niedermann/owncloud/notes/shared/util/DatabaseIndexUtil.java41
3 files changed, 91 insertions, 45 deletions
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_14_15.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_14_15.java
index 6938e41c..a66fc0e9 100644
--- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_14_15.java
+++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_14_15.java
@@ -12,10 +12,10 @@ import androidx.sqlite.db.SupportSQLiteDatabase;
import java.util.Hashtable;
-import it.niedermann.owncloud.notes.shared.util.DatabaseIndexUtil;
-
public class Migration_14_15 extends Migration {
+ private static final String TAG = Migration_14_15.class.getSimpleName();
+
public Migration_14_15() {
super(14, 15);
}
@@ -43,14 +43,14 @@ public class Migration_14_15 extends Migration {
"EXCERPT TEXT NOT NULL DEFAULT '', " +
"FOREIGN KEY(CATEGORY) REFERENCES CATEGORIES(CATEGORY_ID), " +
"FOREIGN KEY(ACCOUNT_ID) REFERENCES ACCOUNTS(ID))");
- DatabaseIndexUtil.createIndex(db, "NOTES", "REMOTEID", "ACCOUNT_ID", "STATUS", "FAVORITE", "CATEGORY", "MODIFIED");
+ createIndex(db, "NOTES", "REMOTEID", "ACCOUNT_ID", "STATUS", "FAVORITE", "CATEGORY", "MODIFIED");
db.execSQL("CREATE TABLE CATEGORIES(" +
"CATEGORY_ID INTEGER PRIMARY KEY AUTOINCREMENT, " +
"CATEGORY_ACCOUNT_ID INTEGER NOT NULL, " +
"CATEGORY_TITLE TEXT NOT NULL, " +
"UNIQUE( CATEGORY_ACCOUNT_ID , CATEGORY_TITLE), " +
"FOREIGN KEY(CATEGORY_ACCOUNT_ID) REFERENCES ACCOUNTS(ID))");
- DatabaseIndexUtil.createIndex(db, "CATEGORIES", "CATEGORY_ID", "CATEGORY_ACCOUNT_ID", "CATEGORY_TITLE");
+ createIndex(db, "CATEGORIES", "CATEGORY_ID", "CATEGORY_ACCOUNT_ID", "CATEGORY_TITLE");
// A hashtable storing categoryTitle - categoryId Mapping
// This is used to prevent too many searches in database
Hashtable<String, Integer> categoryTitleIdMap = new Hashtable<>();
@@ -91,4 +91,16 @@ public class Migration_14_15 extends Migration {
tmpNotesCursor.close();
db.execSQL("DROP TABLE IF EXISTS " + tmpTableNotes);
}
+
+ private static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String... columns) {
+ for (String column : columns) {
+ createIndex(db, table, column);
+ }
+ }
+
+ private static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String column) {
+ String indexName = table + "_" + column + "_idx";
+ Log.v(TAG, "Creating database index: CREATE INDEX IF NOT EXISTS " + indexName + " ON " + table + "(" + column + ")");
+ db.execSQL("CREATE INDEX " + indexName + " ON " + table + "(" + column + ")");
+ }
}
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_22_23.java b/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_22_23.java
index 7a92c9d5..db4005ef 100644
--- a/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_22_23.java
+++ b/app/src/main/java/it/niedermann/owncloud/notes/persistence/migration/Migration_22_23.java
@@ -1,15 +1,32 @@
package it.niedermann.owncloud.notes.persistence.migration;
+import android.content.ContentValues;
+import android.database.Cursor;
+import android.text.TextUtils;
+
import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.room.OnConflictStrategy;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;
+import org.json.JSONArray;
+import org.json.JSONException;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Objects;
+import java.util.stream.Collectors;
+
import it.niedermann.owncloud.notes.persistence.entity.Account;
+import it.niedermann.owncloud.notes.shared.model.ApiVersion;
/**
* Add <code>displayName</code> property to {@link Account}.
* <p>
* See: <a href="https://github.com/stefan-niedermann/nextcloud-notes/issues/1079">#1079 Show DisplayName instead of uid attribute for LDAP users</a>
+ * <p>
+ * Sanitizes the stored API versions in the database.
*/
public class Migration_22_23 extends Migration {
@@ -19,6 +36,64 @@ public class Migration_22_23 extends Migration {
@Override
public void migrate(@NonNull SupportSQLiteDatabase db) {
+ addDisplayNameToAccounts(db);
+ sanitizeAccounts(db);
+ }
+
+ private static void addDisplayNameToAccounts(@NonNull SupportSQLiteDatabase db) {
db.execSQL("ALTER TABLE Account ADD COLUMN displayName TEXT");
}
+
+ private static void sanitizeAccounts(@NonNull SupportSQLiteDatabase db) {
+ final Cursor cursor = db.query("SELECT ID, APIVERSION FROM ACCOUNTS", null);
+ final ContentValues values = new ContentValues(1);
+
+ final int COLUMN_POSITION_ID = cursor.getColumnIndex("ID");
+ final int COLUMN_POSITION_APIVERSION = cursor.getColumnIndex("APIVERSION");
+
+ while (cursor.moveToNext()) {
+ values.put("APIVERSION", sanitizeApiVersion(cursor.getString(COLUMN_POSITION_APIVERSION)));
+ db.update("ACCOUNT", OnConflictStrategy.REPLACE, values, "ID = ?", new String[]{String.valueOf(cursor.getLong(COLUMN_POSITION_ID))});
+ }
+ cursor.close();
+ }
+
+ @Nullable
+ public static String sanitizeApiVersion(@Nullable String raw) {
+ if (TextUtils.isEmpty(raw)) {
+ return null;
+ }
+
+ JSONArray a;
+ try {
+ a = new JSONArray(raw);
+ } catch (JSONException e) {
+ try {
+ a = new JSONArray("[" + raw + "]");
+ } catch (JSONException e1) {
+ return null;
+ }
+ }
+
+ final Collection<ApiVersion> result = new ArrayList<>();
+ for (int i = 0; i < a.length(); i++) {
+ try {
+ final ApiVersion version = ApiVersion.of(a.getString(i));
+ if (version.getMajor() != 0 || version.getMinor() != 0) {
+ result.add(version);
+ }
+ } catch (Exception ignored) {
+ }
+ }
+ if (result.isEmpty()) {
+ return null;
+ }
+ return "[" +
+ result
+ .stream()
+ .filter(Objects::nonNull)
+ .map(v -> v.getMajor() + "." + v.getMinor())
+ .collect(Collectors.joining(","))
+ + "]";
+ }
}
diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DatabaseIndexUtil.java b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DatabaseIndexUtil.java
deleted file mode 100644
index 8506c713..00000000
--- a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/DatabaseIndexUtil.java
+++ /dev/null
@@ -1,41 +0,0 @@
-package it.niedermann.owncloud.notes.shared.util;
-
-import android.database.Cursor;
-import android.database.sqlite.SQLiteDatabase;
-import android.util.Log;
-
-import androidx.annotation.NonNull;
-import androidx.sqlite.db.SupportSQLiteDatabase;
-
-public class DatabaseIndexUtil {
-
- private static final String TAG = DatabaseIndexUtil.class.getSimpleName();
-
- private DatabaseIndexUtil() {
-
- }
-
- public static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String... columns) {
- for (String column : columns) {
- createIndex(db, table, column);
- }
- }
-
- public static void createIndex(@NonNull SupportSQLiteDatabase db, @NonNull String table, @NonNull String column) {
- String indexName = table + "_" + column + "_idx";
- Log.v(TAG, "Creating database index: CREATE INDEX IF NOT EXISTS " + indexName + " ON " + table + "(" + column + ")");
- db.execSQL("CREATE INDEX " + indexName + " ON " + table + "(" + column + ")");
- }
-
- public static void dropIndexes(@NonNull SupportSQLiteDatabase db) {
- try (Cursor c = db.query("SELECT name, sql FROM sqlite_master WHERE type = 'index'")) {
- while (c.moveToNext()) {
- // Skip automatic indexes which we can't drop manually
- if (c.getString(1) != null) {
- Log.v(TAG, "Deleting database index: DROP INDEX " + c.getString(0));
- db.execSQL("DROP INDEX " + c.getString(0));
- }
- }
- }
- }
-}