From 6dd7e4911216112c2d85e75224b6996901f38495 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Tue, 20 Jun 2017 21:43:47 -0400 Subject: [PATCH] Move database migrations to uhabits-core --- uhabits-android/build.gradle | 4 +++ .../src/main/assets/migrations/10.sql | 3 --- .../src/main/assets/migrations/11.sql | 1 - .../src/main/assets/migrations/12.sql | 3 --- .../src/main/assets/migrations/13.sql | 4 --- .../src/main/assets/migrations/14.sql | 14 ---------- .../src/main/assets/migrations/15.sql | 3 --- .../src/main/assets/migrations/16.sql | 2 -- .../src/main/assets/migrations/17.sql | 11 -------- .../src/main/assets/migrations/18.sql | 3 --- .../src/main/assets/migrations/19.sql | 6 ----- .../src/main/assets/migrations/20.sql | 3 --- .../src/main/assets/migrations/21.sql | 12 --------- .../database/BaseSQLiteOpenHelper.java | 20 ++------------ .../isoron/uhabits/core/db/JdbcCursor.java | 8 +++--- .../isoron/uhabits/core/db/JdbcDatabase.java | 1 - .../uhabits/core/db/MigrationHelper.java | 26 +++++++++++++------ .../src/main/resources/migrations/09.sql | 0 .../java/org/isoron/uhabits/BaseUnitTest.java | 12 ++++++--- 19 files changed, 36 insertions(+), 100 deletions(-) delete mode 100644 uhabits-android/src/main/assets/migrations/10.sql delete mode 100644 uhabits-android/src/main/assets/migrations/11.sql delete mode 100644 uhabits-android/src/main/assets/migrations/12.sql delete mode 100644 uhabits-android/src/main/assets/migrations/13.sql delete mode 100644 uhabits-android/src/main/assets/migrations/14.sql delete mode 100644 uhabits-android/src/main/assets/migrations/15.sql delete mode 100644 uhabits-android/src/main/assets/migrations/16.sql delete mode 100644 uhabits-android/src/main/assets/migrations/17.sql delete mode 100644 uhabits-android/src/main/assets/migrations/18.sql delete mode 100644 uhabits-android/src/main/assets/migrations/19.sql delete mode 100644 uhabits-android/src/main/assets/migrations/20.sql delete mode 100644 uhabits-android/src/main/assets/migrations/21.sql rename uhabits-android/src/main/assets/migrations/9.sql => uhabits-core/src/main/resources/migrations/09.sql (100%) diff --git a/uhabits-android/build.gradle b/uhabits-android/build.gradle index 3f90d2c1b..340938675 100644 --- a/uhabits-android/build.gradle +++ b/uhabits-android/build.gradle @@ -49,6 +49,10 @@ android { } } } + + sourceSets { + main.assets.srcDirs += '../uhabits-core/src/main/resources/' + } } dependencies { diff --git a/uhabits-android/src/main/assets/migrations/10.sql b/uhabits-android/src/main/assets/migrations/10.sql deleted file mode 100644 index e13df67f7..000000000 --- a/uhabits-android/src/main/assets/migrations/10.sql +++ /dev/null @@ -1,3 +0,0 @@ -delete from Score; -delete from Streak; -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 deleted file mode 100644 index 4c9493cc1..000000000 --- a/uhabits-android/src/main/assets/migrations/11.sql +++ /dev/null @@ -1 +0,0 @@ -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/12.sql b/uhabits-android/src/main/assets/migrations/12.sql deleted file mode 100644 index e13df67f7..000000000 --- a/uhabits-android/src/main/assets/migrations/12.sql +++ /dev/null @@ -1,3 +0,0 @@ -delete from Score; -delete from Streak; -delete from Checkmarks; \ 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 deleted file mode 100644 index 3df9bc5de..000000000 --- a/uhabits-android/src/main/assets/migrations/13.sql +++ /dev/null @@ -1,4 +0,0 @@ -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/14.sql b/uhabits-android/src/main/assets/migrations/14.sql deleted file mode 100644 index e267afc2c..000000000 --- a/uhabits-android/src/main/assets/migrations/14.sql +++ /dev/null @@ -1,14 +0,0 @@ -update habits set color=0 where color=-2937041; -update habits set color=1 where color=-1684967; -update habits set color=2 where color=-415707; -update habits set color=3 where color=-5262293; -update habits set color=4 where color=-13070788; -update habits set color=5 where color=-16742021; -update habits set color=6 where color=-16732991; -update habits set color=7 where color=-16540699; -update habits set color=8 where color=-10603087; -update habits set color=9 where color=-7461718; -update habits set color=10 where color=-2614432; -update habits set color=11 where color=-13619152; -update habits set color=12 where color=-5592406; -update habits set color=0 where color<0 or color>12; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/15.sql b/uhabits-android/src/main/assets/migrations/15.sql deleted file mode 100644 index e13df67f7..000000000 --- a/uhabits-android/src/main/assets/migrations/15.sql +++ /dev/null @@ -1,3 +0,0 @@ -delete from Score; -delete from Streak; -delete from Checkmarks; \ No newline at end of file diff --git a/uhabits-android/src/main/assets/migrations/16.sql b/uhabits-android/src/main/assets/migrations/16.sql deleted file mode 100644 index 7a7746f63..000000000 --- a/uhabits-android/src/main/assets/migrations/16.sql +++ /dev/null @@ -1,2 +0,0 @@ -alter table Habits add column type integer not null default 0; -alter table Repetitions add column value integer not null default 2; \ 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 deleted file mode 100644 index fa8f50719..000000000 --- a/uhabits-android/src/main/assets/migrations/17.sql +++ /dev/null @@ -1,11 +0,0 @@ -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/18.sql b/uhabits-android/src/main/assets/migrations/18.sql deleted file mode 100644 index fa6318bcc..000000000 --- a/uhabits-android/src/main/assets/migrations/18.sql +++ /dev/null @@ -1,3 +0,0 @@ -alter table Habits add column target_type integer not null default 0; -alter table Habits add column target_value real not null default 0; -alter table Habits add column unit text not null default ""; \ 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 deleted file mode 100644 index f7e9ec24b..000000000 --- a/uhabits-android/src/main/assets/migrations/19.sql +++ /dev/null @@ -1,6 +0,0 @@ -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/20.sql b/uhabits-android/src/main/assets/migrations/20.sql deleted file mode 100644 index 7a8a44177..000000000 --- a/uhabits-android/src/main/assets/migrations/20.sql +++ /dev/null @@ -1,3 +0,0 @@ -drop table checkmarks; -drop table streak; -drop table score; diff --git a/uhabits-android/src/main/assets/migrations/21.sql b/uhabits-android/src/main/assets/migrations/21.sql deleted file mode 100644 index 20cd9fe89..000000000 --- a/uhabits-android/src/main/assets/migrations/21.sql +++ /dev/null @@ -1,12 +0,0 @@ -update habits set color=19 where color=12; -update habits set color=17 where color=11; -update habits set color=15 where color=10; -update habits set color=14 where color=9; -update habits set color=13 where color=8; -update habits set color=10 where color=7; -update habits set color=9 where color=6; -update habits set color=8 where color=5; -update habits set color=7 where color=4; -update habits set color=5 where color=3; -update habits set color=4 where color=2; -update habits set color=0 where color<0 or color>19; \ No newline at end of file diff --git a/uhabits-android/src/main/java/org/isoron/uhabits/database/BaseSQLiteOpenHelper.java b/uhabits-android/src/main/java/org/isoron/uhabits/database/BaseSQLiteOpenHelper.java index 4d52ad426..6789670ec 100644 --- a/uhabits-android/src/main/java/org/isoron/uhabits/database/BaseSQLiteOpenHelper.java +++ b/uhabits-android/src/main/java/org/isoron/uhabits/database/BaseSQLiteOpenHelper.java @@ -27,11 +27,8 @@ import android.database.sqlite.*; import org.isoron.androidbase.*; import org.isoron.uhabits.core.db.*; -import java.io.*; - public class BaseSQLiteOpenHelper extends SQLiteOpenHelper - implements MigrationHelper.FileOpener { private final Context context; @@ -50,7 +47,7 @@ public class BaseSQLiteOpenHelper extends SQLiteOpenHelper public void onCreate(SQLiteDatabase db) { MigrationHelper helper = - new MigrationHelper(this, new AndroidSQLiteDatabase(db)); + new MigrationHelper(new AndroidSQLiteDatabase(db)); helper.executeMigrations(-1, version); } @@ -58,7 +55,7 @@ public class BaseSQLiteOpenHelper extends SQLiteOpenHelper public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { MigrationHelper helper = - new MigrationHelper(this, new AndroidSQLiteDatabase(db)); + new MigrationHelper(new AndroidSQLiteDatabase(db)); helper.executeMigrations(oldVersion, newVersion); } @@ -67,17 +64,4 @@ public class BaseSQLiteOpenHelper extends SQLiteOpenHelper { throw new UnsupportedDatabaseVersionException(); } - - @Override - public InputStream open(String filename) - { - try - { - return context.getAssets().open(filename); - } - catch (IOException e) - { - throw new RuntimeException(e); - } - } } diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcCursor.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcCursor.java index 27d3cb216..ee5fdd808 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcCursor.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcCursor.java @@ -63,7 +63,7 @@ public class JdbcCursor implements Cursor { try { - return resultSet.getInt(index); + return resultSet.getInt(index + 1); } catch (SQLException e) { @@ -76,7 +76,7 @@ public class JdbcCursor implements Cursor { try { - return resultSet.getLong(index); + return resultSet.getLong(index + 1); } catch (SQLException e) { @@ -89,7 +89,7 @@ public class JdbcCursor implements Cursor { try { - return resultSet.getDouble(index); + return resultSet.getDouble(index + 1); } catch (SQLException e) { @@ -102,7 +102,7 @@ public class JdbcCursor implements Cursor { try { - return resultSet.getString(index); + return resultSet.getString(index + 1); } catch (SQLException e) { diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcDatabase.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcDatabase.java index f571d61bc..a741b459e 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcDatabase.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/JdbcDatabase.java @@ -70,7 +70,6 @@ public class JdbcDatabase implements Database String query = String.format("update %s set %s where %s", tableName, StringUtils.join(fields, ", "), where); - System.out.println(query); PreparedStatement st = buildStatement(query, values.toArray()); return st.executeUpdate(); diff --git a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/MigrationHelper.java b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/MigrationHelper.java index 8569b0db0..973d8c022 100644 --- a/uhabits-core/src/main/java/org/isoron/uhabits/core/db/MigrationHelper.java +++ b/uhabits-core/src/main/java/org/isoron/uhabits/core/db/MigrationHelper.java @@ -23,16 +23,17 @@ import android.support.annotation.*; import java.io.*; import java.util.*; +import java.util.logging.*; public class MigrationHelper { - private final FileOpener opener; + private static final Logger LOGGER = + Logger.getLogger(MigrationHelper.class.getName()); private final Database db; - public MigrationHelper(@NonNull FileOpener opener, @NonNull Database db) + public MigrationHelper(@NonNull Database db) { - this.opener = opener; this.db = db; } @@ -42,9 +43,8 @@ public class MigrationHelper { for (int v = oldVersion + 1; v <= newVersion; v++) { - String fname = String.format(Locale.US, "migrations/%d.sql", v); - InputStream stream = opener.open(fname); - for (String command : SQLParser.parse(stream)) + String fname = String.format(Locale.US, "/migrations/%02d.sql", v); + for (String command : SQLParser.parse(open(fname))) db.execute(command); } } @@ -54,8 +54,18 @@ public class MigrationHelper } } - public interface FileOpener + @NonNull + private InputStream open(String fname) throws IOException { - InputStream open(String filename); + InputStream resource = getClass().getResourceAsStream(fname); + if(resource != null) return resource; + + // Workaround for bug in Android Studio / IntelliJ. Removing this + // causes unit tests to fail when run from within the IDE, although + // everything works fine from the command line. + File file = new File("uhabits-core/src/main/resources/" + fname); + if(file.exists()) return new FileInputStream(file); + + throw new RuntimeException("resource not found: " + fname); } } diff --git a/uhabits-android/src/main/assets/migrations/9.sql b/uhabits-core/src/main/resources/migrations/09.sql similarity index 100% rename from uhabits-android/src/main/assets/migrations/9.sql rename to uhabits-core/src/main/resources/migrations/09.sql diff --git a/uhabits-core/src/test/java/org/isoron/uhabits/BaseUnitTest.java b/uhabits-core/src/test/java/org/isoron/uhabits/BaseUnitTest.java index 2b65756fa..4ab4b0afd 100644 --- a/uhabits-core/src/test/java/org/isoron/uhabits/BaseUnitTest.java +++ b/uhabits-core/src/test/java/org/isoron/uhabits/BaseUnitTest.java @@ -40,6 +40,9 @@ import static org.mockito.Mockito.validateMockitoUsage; public class BaseUnitTest { + // 8:00am, January 25th, 2015 (UTC) + protected static final long FIXED_LOCAL_TIME = 1422172800000L; + protected HabitList habitList; protected HabitFixtures fixtures; @@ -50,9 +53,6 @@ public class BaseUnitTest protected CommandRunner commandRunner; - // 8:00am, January 25th, 2015 (UTC) - protected static final long FIXED_LOCAL_TIME = 1422172800000L; - @Before public void setUp() throws Exception { @@ -89,7 +89,11 @@ public class BaseUnitTest { try { - return new JdbcDatabase(DriverManager.getConnection("jdbc:sqlite::memory:")); + Database db = new JdbcDatabase( + DriverManager.getConnection("jdbc:sqlite::memory:")); + MigrationHelper helper = new MigrationHelper(db); + helper.executeMigrations(8, 21); + return db; } catch (SQLException e) {