From 71fe6137be0b5104013e55eac77a2d86e14f5318 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Mon, 19 Jun 2017 12:08:06 -0400 Subject: [PATCH] Remove old database migrations; add missing ones --- .../storage/BaseSQLiteOpenHelper.java | 6 +++ .../UnsupportedDatabaseVersionException.java | 26 +++++++++ .../src/main/assets/migrations/10.sql | 2 +- .../src/main/assets/migrations/11.sql | 2 +- .../src/main/assets/migrations/13.sql | 8 +-- .../src/main/assets/migrations/17.sql | 16 ++++-- .../src/main/assets/migrations/19.sql | 6 +++ .../src/main/assets/migrations/5.sql | 2 - .../src/main/assets/migrations/6.sql | 1 - .../src/main/assets/migrations/7.sql | 1 - .../src/main/assets/migrations/9.sql | 41 ++++++++++++++ .../isoron/uhabits/HabitsDatabaseOpener.java | 54 +++++++++++++++++++ .../isoron/uhabits/utils/DatabaseUtils.java | 11 ++-- 13 files changed, 155 insertions(+), 21 deletions(-) create mode 100644 android-base/src/main/java/org/isoron/androidbase/storage/UnsupportedDatabaseVersionException.java create mode 100644 uhabits-android/src/main/assets/migrations/19.sql delete mode 100644 uhabits-android/src/main/assets/migrations/5.sql delete mode 100644 uhabits-android/src/main/assets/migrations/6.sql delete mode 100644 uhabits-android/src/main/assets/migrations/7.sql create mode 100644 uhabits-android/src/main/assets/migrations/9.sql create mode 100644 uhabits-android/src/main/java/org/isoron/uhabits/HabitsDatabaseOpener.java diff --git a/android-base/src/main/java/org/isoron/androidbase/storage/BaseSQLiteOpenHelper.java b/android-base/src/main/java/org/isoron/androidbase/storage/BaseSQLiteOpenHelper.java index 78dd2965b..64d99b170 100644 --- a/android-base/src/main/java/org/isoron/androidbase/storage/BaseSQLiteOpenHelper.java +++ b/android-base/src/main/java/org/isoron/androidbase/storage/BaseSQLiteOpenHelper.java @@ -76,4 +76,10 @@ public class BaseSQLiteOpenHelper extends SQLiteOpenHelper throw new RuntimeException(e); } } + + @Override + public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) + { + throw new UnsupportedDatabaseVersionException(); + } } diff --git a/android-base/src/main/java/org/isoron/androidbase/storage/UnsupportedDatabaseVersionException.java b/android-base/src/main/java/org/isoron/androidbase/storage/UnsupportedDatabaseVersionException.java new file mode 100644 index 000000000..7e00f7d55 --- /dev/null +++ b/android-base/src/main/java/org/isoron/androidbase/storage/UnsupportedDatabaseVersionException.java @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2017 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * + */ + +package org.isoron.androidbase.storage; + +public class UnsupportedDatabaseVersionException extends RuntimeException +{ +} diff --git a/uhabits-android/src/main/assets/migrations/10.sql b/uhabits-android/src/main/assets/migrations/10.sql index 70c3de873..e13df67f7 100644 --- a/uhabits-android/src/main/assets/migrations/10.sql +++ b/uhabits-android/src/main/assets/migrations/10.sql @@ -1,3 +1,3 @@ delete from Score; delete from Streak; -delete from Checkmarks; +delete from Checkmarks; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/11.sql b/uhabits-android/src/main/assets/migrations/11.sql index 25b30ef51..4c9493cc1 100644 --- a/uhabits-android/src/main/assets/migrations/11.sql +++ b/uhabits-android/src/main/assets/migrations/11.sql @@ -1 +1 @@ -alter table habits add column reminder_days integer not null default 127; \ No newline at end of file +alter table Habits add column reminder_days integer not null default 127; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/13.sql b/uhabits-android/src/main/assets/migrations/13.sql index 1d7eeafcf..3df9bc5de 100644 --- a/uhabits-android/src/main/assets/migrations/13.sql +++ b/uhabits-android/src/main/assets/migrations/13.sql @@ -1,4 +1,4 @@ -create index idx_score_habit_timestamp on score(habit, timestamp); -create index idx_checkmark_habit_timestamp on checkmarks(habit, timestamp); -create index idx_repetitions_habit_timestamp on repetitions(habit, timestamp); -create index idx_streak_habit_end on streak(habit, end); \ No newline at end of file +create index idx_score_habit_timestamp on Score(habit, timestamp); +create index idx_checkmark_habit_timestamp on Checkmarks(habit, timestamp); +create index idx_repetitions_habit_timestamp on Repetitions(habit, timestamp); +create index idx_streak_habit_end on Streak(habit, end); \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/17.sql b/uhabits-android/src/main/assets/migrations/17.sql index 9c9a9b409..fa8f50719 100644 --- a/uhabits-android/src/main/assets/migrations/17.sql +++ b/uhabits-android/src/main/assets/migrations/17.sql @@ -1,5 +1,11 @@ -DROP TABLE Score; -CREATE TABLE Score (Id INTEGER PRIMARY KEY AUTOINCREMENT, habit INTEGER REFERENCES Habits(Id), score REAL, timestamp INTEGER); -CREATE INDEX idx_score_habit_timestamp on score(habit, timestamp); -delete from Streak; -delete from Checkmarks; \ No newline at end of file +drop table Score; +create table Score ( + id integer primary key autoincrement, + habit integer references habits(id), + score real, + timestamp integer); + +create index idx_score_habit_timestamp on Score(habit, timestamp); + +delete from streak; +delete from checkmarks; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/19.sql b/uhabits-android/src/main/assets/migrations/19.sql new file mode 100644 index 000000000..f7e9ec24b --- /dev/null +++ b/uhabits-android/src/main/assets/migrations/19.sql @@ -0,0 +1,6 @@ +create table Events ( + id integer primary key autoincrement, + timestamp integer, + message text, + server_id integer +); \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/5.sql b/uhabits-android/src/main/assets/migrations/5.sql deleted file mode 100644 index bde8462b1..000000000 --- a/uhabits-android/src/main/assets/migrations/5.sql +++ /dev/null @@ -1,2 +0,0 @@ -alter table habits add column reminder_hour integer; -alter table habits add column reminder_min integer; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/6.sql b/uhabits-android/src/main/assets/migrations/6.sql deleted file mode 100644 index 7f1e33c94..000000000 --- a/uhabits-android/src/main/assets/migrations/6.sql +++ /dev/null @@ -1 +0,0 @@ -alter table habits add column highlight integer not null default 0; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/7.sql b/uhabits-android/src/main/assets/migrations/7.sql deleted file mode 100644 index b666e9bf6..000000000 --- a/uhabits-android/src/main/assets/migrations/7.sql +++ /dev/null @@ -1 +0,0 @@ -alter table habits add column archived integer not null default 0; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/9.sql b/uhabits-android/src/main/assets/migrations/9.sql new file mode 100644 index 000000000..5fb4502b4 --- /dev/null +++ b/uhabits-android/src/main/assets/migrations/9.sql @@ -0,0 +1,41 @@ +create table Habits ( + id integer primary key autoincrement, + archived integer, + color integer, + description text, + freq_den integer, + freq_num integer, + highlight integer, + name text, + position integer, + reminder_hour integer, + reminder_min integer +); + +create table Checkmarks ( + id integer primary key autoincrement, + habit integer references habits(id), + timestamp integer, + value integer +); + +create table Repetitions ( + id integer primary key autoincrement, + habit integer references habits(id), + timestamp integer +); + +create table Streak ( + id integer primary key autoincrement, + end integer, + habit integer references habits(id), + length integer, + start integer +); + +create table Score ( + id integer primary key autoincrement, + habit integer references habits(id), + score integer, + timestamp integer +); diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/HabitsDatabaseOpener.java b/uhabits-android/src/main/java/org/isoron/uhabits/HabitsDatabaseOpener.java new file mode 100644 index 000000000..e75368bf8 --- /dev/null +++ b/uhabits-android/src/main/java/org/isoron/uhabits/HabitsDatabaseOpener.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2017 Álinson Santos Xavier + * + * This file is part of Loop Habit Tracker. + * + * Loop Habit Tracker is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * Loop Habit Tracker is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see . + * + * + */ + +package org.isoron.uhabits; + +import android.content.*; +import android.database.sqlite.*; + +import org.isoron.androidbase.storage.*; + + +public class HabitsDatabaseOpener extends BaseSQLiteOpenHelper +{ + private final int version; + + public HabitsDatabaseOpener(Context context, + String databaseFilename, + int version) + { + super(context, databaseFilename, version); + this.version = version; + } + + @Override + public void onCreate(SQLiteDatabase db) + { + onUpgrade(db, 8, version); + } + + @Override + public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) + { + if(oldVersion < 8) throw new UnsupportedDatabaseVersionException(); + super.onUpgrade(db, oldVersion, newVersion); + } +} diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java index 9a2103c83..fd3f399c1 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/utils/DatabaseUtils.java @@ -23,7 +23,6 @@ import android.content.*; import android.database.sqlite.*; import android.support.annotation.*; -import org.isoron.androidbase.storage.*; import org.isoron.androidbase.utils.*; import org.isoron.uhabits.*; import org.isoron.uhabits.core.utils.*; @@ -35,7 +34,7 @@ import java.text.*; public abstract class DatabaseUtils { @Nullable - private static BaseSQLiteOpenHelper helper = null; + private static HabitsDatabaseOpener opener = null; public static void executeAsTransaction(Callback callback) { @@ -83,7 +82,7 @@ public abstract class DatabaseUtils { try { - helper = new BaseSQLiteOpenHelper(context, getDatabaseFilename(), + opener = new HabitsDatabaseOpener(context, getDatabaseFilename(), BuildConfig.databaseVersion); } catch (RuntimeException e) @@ -113,13 +112,13 @@ public abstract class DatabaseUtils @NonNull public static SQLiteDatabase openDatabase() { - if(helper == null) throw new IllegalStateException(); - return helper.getWritableDatabase(); + if(opener == null) throw new IllegalStateException(); + return opener.getWritableDatabase(); } public static void dispose() { - helper = null; + opener = null; } public interface Callback