Move database migrations to uhabits-core

pull/312/head
Alinson S. Xavier 8 years ago
parent ecb5352134
commit 6dd7e49112

@ -49,6 +49,10 @@ android {
}
}
}
sourceSets {
main.assets.srcDirs += '../uhabits-core/src/main/resources/'
}
}
dependencies {

@ -1,3 +0,0 @@
delete from Score;
delete from Streak;
delete from Checkmarks;

@ -1 +0,0 @@
alter table Habits add column reminder_days integer not null default 127;

@ -1,3 +0,0 @@
delete from Score;
delete from Streak;
delete from Checkmarks;

@ -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);

@ -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;

@ -1,3 +0,0 @@
delete from Score;
delete from Streak;
delete from Checkmarks;

@ -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;

@ -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;

@ -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 "";

@ -1,6 +0,0 @@
create table Events (
id integer primary key autoincrement,
timestamp integer,
message text,
server_id integer
);

@ -1,3 +0,0 @@
drop table checkmarks;
drop table streak;
drop table score;

@ -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;

@ -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);
}
}
}

@ -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)
{

@ -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();

@ -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);
}
}

@ -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)
{

Loading…
Cancel
Save