Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
aglover committed Jul 12, 2013
0 parents commit 60f5a85
Show file tree
Hide file tree
Showing 24 changed files with 593 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.DS_Store
.classpath
.project
bin/
target/
stub-lib/
Empty file added README.md
Empty file.
79 changes: 79 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<project name="migrations" basedir="." default="jar">

<property name="version" value="0.2"/>
<property name="lib.dir" value="lib"/>
<property name="build.dir" value="target"/>
<property name="build.test.dir" value="${build.dir}/test"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="stubs.dir" value="${build.dir}/ext-classes"/>


<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile.stubs">
<mkdir dir="${stubs.dir}"/>
<javac includeantruntime="false" srcdir="stubs" destdir="${stubs.dir}" classpathref="classpath"/>
</target>

<target name="compile" depends="compile.stubs"
description="compiles source code and puts classes in target/ directory">
<mkdir dir="${classes.dir}"/>
<javac includeantruntime="false" srcdir="src" destdir="${classes.dir}" classpathref="classpath">
<classpath>
<path refid="classpath"/>
<pathelement path="${stubs.dir}"/>
</classpath>
</javac>
</target>

<target name="jar" depends="test" description="creates jar file for distribution">
<jar destfile="${build.dir}/${ant.project.name}-${version}.jar" basedir="${classes.dir}">
</jar>
<delete dir="${classes.dir}"/>
</target>

<target name="compile-tests" depends="compile">
<mkdir dir="${build.dir}/test-classes"/>
<javac srcdir="test/" destdir="${build.dir}/test-classes" includeAntRuntime="false" source="1.5" debug="true">
<classpath>
<path refid="classpath"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${stubs.dir}"/>
</classpath>
</javac>
</target>

<target name="test" depends="compile-tests">
<junit fork="true" forkmode="once" haltonfailure="false" haltonerror="false" failureproperty="tests.failures"
errorproperty="tests.errors" includeantruntime="true" showoutput="true" printsummary="true">
<classpath>
<path refid="classpath"/>
<pathelement path="${classes.dir}"/>
<pathelement path="${build.dir}/test-classes"/>
<pathelement path="${stubs.dir}"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="yes"
todir="./${build.dir}/">
<fileset dir="test/">
<include name="**/*Test.java"/>
</fileset>
</batchtest>
</junit>
<mkdir dir="./${build.dir}/reports"/>
<junitreport todir="./${build.dir}/reports">
<fileset dir="./${build.dir}">
<include name="TEST-*.xml"/>
</fileset>
<report format="frames" todir="./${build.dir}/reports/html"/>
</junitreport>
<fail if="tests.failures" message="There were JUnit failures -- see the reports in ./${build.dir}/reports"/>
</target>

</project>
Binary file added lib/freemarker-2.3.20.jar
Binary file not shown.
Binary file added lib/jopt-simple-4.5.jar
Binary file not shown.
Binary file added lib/junit-4.11.jar
Binary file not shown.
Binary file added lib/mockito-all-1.9.5.jar
Binary file not shown.
23 changes: 23 additions & 0 deletions src/com/b50/migrations/AbstractMigration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.b50.migrations;

import android.database.sqlite.SQLiteDatabase;

public abstract class AbstractMigration {

protected SQLiteDatabase database;

public AbstractMigration(){}

public void setDatabase(SQLiteDatabase database){
this.database = database;
}

protected void execSQL(String sql){
this.database.execSQL(sql);
}

public abstract void up();

public abstract void down();

}
25 changes: 25 additions & 0 deletions src/com/b50/migrations/MigrationException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.b50.migrations;

public class MigrationException extends Exception {

/**
*
*/
private static final long serialVersionUID = 123400001L;

public MigrationException() {
}

public MigrationException(String arg0) {
super(arg0);
}

public MigrationException(Throwable arg0) {
super(arg0);
}

public MigrationException(String arg0, Throwable arg1) {
super(arg0, arg1);
}

}
70 changes: 70 additions & 0 deletions src/com/b50/migrations/Migrator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.b50.migrations;

import android.database.sqlite.SQLiteDatabase;

public class Migrator {
private String packageName;

public Migrator(String packageName) {
this.packageName = packageName;
}

public void initialMigration(SQLiteDatabase db) throws MigrationException {
try {
handleUp(db, packageName, "DBVersion1");
} catch (InstantiationException e) {
throw new MigrationException(e);
} catch (IllegalAccessException e) {
throw new MigrationException(e);
} catch (ClassNotFoundException e) {
throw new MigrationException(e);
}
}

public void upgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws MigrationException {
for (int x = (oldVersion + 1); x <= newVersion; x++) {
try {
handleUp(db, packageName, "DBVersion" + x);
} catch (InstantiationException e) {
throw new MigrationException(e);
} catch (IllegalAccessException e) {
throw new MigrationException(e);
} catch (ClassNotFoundException e) {
throw new MigrationException(e);
}
}
}

public void downgrade(SQLiteDatabase db, int oldVersion, int newVersion) throws MigrationException {
for (int x = oldVersion; x > newVersion; x--) {
try {
handleDown(db, packageName, "DBVersion" + x);
} catch (InstantiationException e) {
throw new MigrationException(e);
} catch (IllegalAccessException e) {
throw new MigrationException(e);
} catch (ClassNotFoundException e) {
throw new MigrationException(e);
}
}
}

private AbstractMigration getMigrationForPackageAndName(String pckName, String clzzName)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return (AbstractMigration) Class.forName(pckName + "." + clzzName).newInstance();
}

private void handleUp(SQLiteDatabase db, String pckName, String clzzName) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
AbstractMigration migration = getMigrationForPackageAndName(packageName, clzzName);
migration.setDatabase(db);
migration.up();
}

private void handleDown(SQLiteDatabase db, String pckName, String clzzName) throws InstantiationException,
IllegalAccessException, ClassNotFoundException {
AbstractMigration migration = getMigrationForPackageAndName(packageName, clzzName);
migration.setDatabase(db);
migration.down();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.b50.migrations.generators;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class DatabaseHelperClassGenerator {

private String templateLocation;
private String templateName;

public DatabaseHelperClassGenerator(String templateLocation, String templateName) {
this.templateLocation = templateLocation;
this.templateName = templateName;
}

public String generate(String packageName) throws IOException {
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(this.templateLocation));
Template template = cfg.getTemplate(this.templateName);
Map<String, String> input = new HashMap<String, String>();
input.put("package", packageName);
StringWriter out = new StringWriter();
try {
template.process(input, out);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
return out.toString();
}

}
40 changes: 40 additions & 0 deletions src/com/b50/migrations/generators/MigrationClassGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.b50.migrations.generators;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class MigrationClassGenerator {

private String templateLocation;
private String templateName;

public MigrationClassGenerator(String templateLocation, String templateName) {
this.templateLocation = templateLocation;
this.templateName = templateName;
}

public String generate(String packageName, int sequence) throws IOException{
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(this.templateLocation));
Template template = cfg.getTemplate(this.templateName);
Map<String, String> input = new HashMap<String, String>();
input.put("package", packageName);
input.put("sequence_number", ""+sequence);
StringWriter out = new StringWriter();
try {
template.process(input, out);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
return out.toString();
}
}
41 changes: 41 additions & 0 deletions src/com/b50/migrations/generators/MigrationXMLGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.b50.migrations.generators;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class MigrationXMLGenerator {

private String templateLocation;
private String templateName;

public MigrationXMLGenerator(String templateLocation, String templateName) {
this.templateLocation = templateLocation;
this.templateName = templateName;
}

public String generate(String databaseName, String packageName, int sequence) throws IOException{
Configuration cfg = new Configuration();
cfg.setDirectoryForTemplateLoading(new File(templateLocation));
Template template = cfg.getTemplate(templateName);
Map<String, String> input = new HashMap<String, String>();
input.put("package", packageName);
input.put("sequence_number", ""+sequence);
input.put("database_name", databaseName);
StringWriter out = new StringWriter();
try {
template.process(input, out);
} catch (TemplateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
out.flush();
return out.toString();
}
}
12 changes: 12 additions & 0 deletions stubs/android/database/sqlite/SQLiteDatabase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package android.database.sqlite;

public class SQLiteDatabase {

public SQLiteDatabase() {
// TODO Auto-generated constructor stub
}

public void execSQL(String sql) {

}
}
Loading

0 comments on commit 60f5a85

Please sign in to comment.