-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 60f5a85
Showing
24 changed files
with
593 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.DS_Store | ||
.classpath | ||
.project | ||
bin/ | ||
target/ | ||
stub-lib/ |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/com/b50/migrations/generators/DatabaseHelperClassGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
src/com/b50/migrations/generators/MigrationClassGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/com/b50/migrations/generators/MigrationXMLGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |
Oops, something went wrong.