mirror of
https://github.com/iSoron/uhabits.git
synced 2025-12-06 09:08:52 -06:00
Move database migrations to uhabits-core
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
41
uhabits-core/src/main/resources/migrations/09.sql
Normal file
41
uhabits-core/src/main/resources/migrations/09.sql
Normal file
@@ -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
|
||||
);
|
||||
@@ -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)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user