Created
April 13, 2011 20:38
-
-
Save jrdalpra/918354 to your computer and use it in GitHub Desktop.
Esboço de um framework de migração de banco usando 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
// TODO ao gerar uma nova versão, criar um ID único da mesma | |
public interface Migration { | |
void up (Database database) throws MigrationException; | |
void down(Database database) throws MigrationException; | |
String version(); | |
String tag(); | |
} | |
public interface FieldModifier{ | |
} | |
public class Field implements DatabaseObject{ | |
public Field(String name, Set<FieldModifier> modifiers){ | |
// TODO | |
} | |
public Field(String name, FieldModifier... modifiers){ | |
this(name,new HashSet<FieldModifier>(Arrays.asList(modifiers))); | |
} | |
} | |
public class Table implements DatabaseObject{ | |
public Table( String tableName , Set<Field> fields , Set<Index> indexes ) { | |
} | |
public Table( String tableName ){ | |
} | |
public Table with(Field... fields){ | |
setFields(new HashSet<Field>(Arrays.asList(fields))); | |
return this; | |
} | |
} | |
public interface Database{ | |
<T> create(T databaseObject); | |
<T> execute (T databaseObject); | |
void process() throws MigrationException; | |
} | |
public class DefaultDatabase implements Database{ | |
public List<DatabaseObject> todo = new ArrayList<DatabaseObject>(); | |
public <T extends DatabaseObject> create(T databaseObject){ | |
todo.append(databaseObject); | |
return databaseObject; | |
} | |
public <T extends DatabaseOperation> execute(T databaseOperation){ | |
todo.append(databaseOperation); | |
return databaseOperation; | |
} | |
} | |
public final class Definitions { | |
public static final Field field(String name, FieldModifier... modifiers){ | |
return new Field(name,modifiers); | |
} | |
public static final Insert insert(String table){ | |
return new Insert(table); | |
} | |
public static final Table table(String name){ | |
return new Table(name); | |
} | |
} | |
public final class Filter{ | |
public static final Filter eq(String field,Object value){ | |
return new Filter(field,Operator.EQ,value); | |
} | |
public static final Filter not(Filter filter){ | |
return new Filter(null,Operator.NOT,filter); | |
} | |
public static final Filter isNotNull(String field){ | |
return new Filter(field,Operator.NOT_NULL,null); | |
} | |
public static final Filter isNull(String field){ | |
return new Filter(field,Operator.NULL,null); | |
} | |
private Field(String field, Operator operator, Object value){ | |
// TODO | |
} | |
} | |
// imports | |
public class CreateNewUserTableMigration implements Migration { | |
public void up(Database database) throws MigrationException{ | |
database.create (table ("user").fields(field("login" ,Type.STRING,Key.PRIMARY,Key.UNIQUE), | |
field("password",Type.STRING,Check.NOT_NULL ))); | |
database.create(field("login",Type.String).on(table("user"))); | |
database.execute(insert("user").column("login","admin").column("password","admin")); | |
} | |
public void down(Database database) throws MigrationException { | |
database.execute(delete("user").where(eq("login","admin")); | |
database.execute(delete("user").where(eq("login","teste")); | |
database.drop(table("user")); | |
} | |
public String version(){ | |
// TODO fica a critério definir a versão da migração | |
} | |
public String tag(){ | |
// TODO pode criar uma tag que identifique um conjunto de migrations | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment