Skip to content

Commit 52134dc

Browse files
author
Robin Komiwes
committed
nearly close to an alpha release! still have to find out a smart way to store and execute pending SQL
1 parent 621c0c0 commit 52134dc

File tree

20 files changed

+599
-198
lines changed

20 files changed

+599
-198
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>com.spreadthesource</groupId> <artifactId>tapestry5-db-migration</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Spread The Source - Tapestry 5 Database Migration</name> <parent> <groupId>com.spreadthesource</groupId> <artifactId>parent-pom</artifactId> <version>1.2</version> </parent> <dependencies> <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-ioc</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-hibernate-core</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.11</version> <classifier>jdk15</classifier> <scope>test</scope> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.7</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.2.133</version> </dependency> </dependencies> <build> <finalName>tapestry5-db-migration</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <Tapestry-Module-Classes>com.spreadthesource.tapestry.dbmigrations.services.RomeModule </Tapestry-Module-Classes> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.3</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/conf/testng.xml</suiteXmlFile> </suiteXmlFiles> <argLine>-Xmx500m</argLine> <redirectTestOutputToFile>false</redirectTestOutputToFile> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> <optimize>true</optimize> </configuration> </plugin> <!-- Run the application using "mvn jetty:run" --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.9</version> <configuration> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <append>true</append> </requestLog> <useTestClasspath>true</useTestClasspath> <webAppSourceDirectory>src/test/webapp</webAppSourceDirectory> </configuration> </plugin> <plugin> <groupId>com.mycila.maven-license-plugin</groupId> <artifactId>maven-license-plugin</artifactId> <version>1.5.1</version> <configuration> <header>LICENSE.txt</header> <excludes> <exclude>**/*.sql</exclude> <exclude>**/*.txt</exclude> <exclude>**/PLACEHOLDER</exclude> <exclude>**/*.properties</exclude> <exclude>**/web.xml</exclude> <exclude>**/*.apt</exclude> <exclude>**/*.tml</exclude> <exclude>**/.*</exclude> <exclude>**/*.ent</exclude> <exclude>**/*.xsl</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.html</exclude> <exclude>**/*.dtd</exclude> <exclude>src/test/conf/**</exclude> </excludes> <useDefaultExcludes>true</useDefaultExcludes> <mapping> <java>DOUBLESLASH_STYLE</java> </mapping> <useDefaultMapping>true</useDefaultMapping> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>apache</id> <url>https://repository.apache.org/content/repositories/releases</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>apache-snapshots</id> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>devlab722-repo</id> <url>http://nexus.devlab722.net/nexus/content/repositories/releases </url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>devlab722-snapshot-repo</id> <url>http://nexus.devlab722.net/nexus/content/repositories/snapshots </url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>codehaus.snapshots</id> <url>http://snapshots.repository.codehaus.org</url> </repository> <repository> <id>java.net</id> <url>http://download.java.net/maven/2/</url> </repository> <repository> <id>OpenQA_Release</id> <name>OpenQA Release Repository</name> <url>http://archiva.openqa.org/repository/releases/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>mc-release</id> <name>Local Maven repository of releases</name> <url>http://mc-repo.googlecode.com/svn/maven2/releases</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> <properties> <tapestry-release-version>5.2.0-SNAPSHOT</tapestry-release-version> </properties></project>
1+
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"> <modelVersion>4.0.0</modelVersion> <groupId>com.spreadthesource</groupId> <artifactId>tapestry5-db-migration</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>Spread The Source - Tapestry 5 Database Migration</name> <parent> <groupId>com.spreadthesource</groupId> <artifactId>parent-pom</artifactId> <version>1.2</version> </parent> <dependencies> <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-ioc</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>org.apache.tapestry</groupId> <artifactId>tapestry-hibernate-core</artifactId> <version>${tapestry-release-version}</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>5.11</version> <classifier>jdk15</classifier> <scope>test</scope> </dependency> <dependency> <groupId>hsqldb</groupId> <artifactId>hsqldb</artifactId> <version>1.8.0.7</version> <scope>test</scope> </dependency> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.2.133</version> </dependency> </dependencies> <build> <finalName>tapestry5-db-migration</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <configuration> <archive> <manifestEntries> <Tapestry-Module-Classes>com.spreadthesource.tapestry.dbmigrations.services.RomeModule </Tapestry-Module-Classes> </manifestEntries> </archive> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.4.3</version> <configuration> <suiteXmlFiles> <suiteXmlFile>src/test/conf/testng.xml</suiteXmlFile> </suiteXmlFiles> <argLine>-Xmx500m</argLine> <redirectTestOutputToFile>false</redirectTestOutputToFile> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.5</source> <target>1.5</target> <optimize>true</optimize> </configuration> </plugin> <!-- Run the application using "mvn jetty:run" --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>6.1.9</version> <configuration> <requestLog implementation="org.mortbay.jetty.NCSARequestLog"> <append>true</append> </requestLog> <useTestClasspath>true</useTestClasspath> <webAppSourceDirectory>src/test/webapp</webAppSourceDirectory> </configuration> </plugin> <plugin> <groupId>com.mycila.maven-license-plugin</groupId> <artifactId>maven-license-plugin</artifactId> <version>1.5.1</version> <configuration> <header>LICENSE.txt</header> <excludes> <exclude>**/*.sql</exclude> <exclude>**/*.txt</exclude> <exclude>**/PLACEHOLDER</exclude> <exclude>**/*.properties</exclude> <exclude>**/web.xml</exclude> <exclude>**/*.apt</exclude> <exclude>**/*.tml</exclude> <exclude>**/.*</exclude> <exclude>**/*.ent</exclude> <exclude>**/*.xsl</exclude> <exclude>**/*.xml</exclude> <exclude>**/*.html</exclude> <exclude>**/*.dtd</exclude> <exclude>src/test/conf/**</exclude> </excludes> <useDefaultExcludes>true</useDefaultExcludes> <mapping> <java>DOUBLESLASH_STYLE</java> </mapping> <useDefaultMapping>true</useDefaultMapping> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> <repositories> <repository> <id>apache</id> <url>https://repository.apache.org/content/repositories/releases</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>apache-snapshots</id> <url>https://repository.apache.org/content/repositories/snapshots</url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>devlab722-repo</id> <url>http://nexus.devlab722.net/nexus/content/repositories/releases </url> <snapshots> <enabled>false</enabled> </snapshots> </repository> <repository> <id>devlab722-snapshot-repo</id> <url>http://nexus.devlab722.net/nexus/content/repositories/snapshots </url> <releases> <enabled>false</enabled> </releases> </repository> <repository> <id>codehaus.snapshots</id> <url>http://snapshots.repository.codehaus.org</url> </repository> <repository> <id>java.net</id> <url>http://download.java.net/maven/2/</url> </repository> <repository> <id>OpenQA_Release</id> <name>OpenQA Release Repository</name> <url>http://archiva.openqa.org/repository/releases/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>mc-release</id> <name>Local Maven repository of releases</name> <url>http://mc-repo.googlecode.com/svn/maven2/releases</url> <snapshots> <enabled>false</enabled> </snapshots> <releases> <enabled>true</enabled> </releases> </pluginRepository> </pluginRepositories> <properties> <tapestry-release-version>5.2.0-SNAPSHOT</tapestry-release-version> </properties></project>

src/main/java/com/spreadthesource/tapestry/dbmigration/data/Column.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,26 @@
22

33
import java.util.Map;
44

5+
import org.hibernate.cfg.Ejb3Column;
6+
import org.hibernate.type.NullableType;
7+
58
public class Column
69
{
710
private String name;
811

912
private boolean unique;
1013

14+
private int length;
15+
1116
private Map<String, String> meta;
1217

13-
private int type;
18+
private NullableType type;
1419

15-
public Column(String name, int type) {
20+
public Column(String name, NullableType type) {
1621
this.name = name;
1722
this.type = type;
23+
24+
this.length = Ejb3Column.DEFAULT_COLUMN_LENGTH;
1825
}
1926

2027
public void setMeta(Map<String, String> meta)
@@ -27,10 +34,14 @@ public Map<String, String> getMeta()
2734
return meta;
2835
}
2936

30-
public int getType() {
37+
public NullableType getType() {
3138
return type;
3239
}
3340

41+
public int getSQLType() {
42+
return type.sqlType();
43+
}
44+
3445
public String getName() {
3546
return name;
3647
}
@@ -44,4 +55,14 @@ public boolean isUnique()
4455
{
4556
return unique;
4657
}
58+
59+
public void setLength(int length)
60+
{
61+
this.length = length;
62+
}
63+
64+
public int getLength()
65+
{
66+
return length;
67+
}
4768
}

src/main/java/com/spreadthesource/tapestry/dbmigration/data/Table.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.ArrayList;
55
import java.util.List;
66

7+
import org.hibernate.type.NullableType;
8+
79
public class Table
810
{
911
private final String name;
@@ -20,14 +22,22 @@ public Table(String name)
2022
* @param columnName
2123
* name of the column
2224
* @param columnType
23-
* should be one of the referenced {@link Types}
25+
* should be one of the referenced hibernate NullableType
2426
*/
25-
public Column addColumn(String columnName, int columnType)
27+
public Column addColumn(String columnName, NullableType columnType)
2628
{
2729
Column c = new Column(columnName, columnType);
2830
columns.add(c);
2931
return c;
3032
}
33+
34+
public Column addColumn(String columnName, NullableType columnType, int length)
35+
{
36+
Column c = this.addColumn(columnName, columnType);
37+
c.setLength(length);
38+
39+
return c;
40+
}
3141

3242
public void dropTable()
3343
{
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.spreadthesource.tapestry.dbmigration.init;
2+
3+
import org.apache.tapestry5.ioc.annotations.Inject;
4+
import org.apache.tapestry5.ioc.annotations.Symbol;
5+
import org.hibernate.Hibernate;
6+
7+
import com.spreadthesource.tapestry.dbmigration.MigrationSymbolConstants;
8+
import com.spreadthesource.tapestry.dbmigration.annotations.Version;
9+
import com.spreadthesource.tapestry.dbmigration.data.Table;
10+
import com.spreadthesource.tapestry.dbmigration.migrations.MigrationBase;
11+
import com.spreadthesource.tapestry.dbmigration.services.MigrationHelper;
12+
13+
@Version(0)
14+
public class SchemaInitialization extends MigrationBase
15+
{
16+
private String versioningTableName;
17+
18+
public SchemaInitialization(
19+
MigrationHelper helper,
20+
@Inject @Symbol(MigrationSymbolConstants.VERSIONING_TABLE_NAME) String versioningTableName)
21+
{
22+
super(helper);
23+
24+
this.versioningTableName = versioningTableName;
25+
}
26+
27+
public void up()
28+
{
29+
Table versions = new Table(versioningTableName);
30+
versions.addColumn("version", Hibernate.INTEGER);
31+
versions.addColumn("datetime", Hibernate.TIMESTAMP);
32+
33+
createTable(versions);
34+
}
35+
36+
public void down()
37+
{
38+
// nothing to do
39+
}
40+
41+
}
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package com.spreadthesource.tapestry.dbmigration.migrations;
22

3+
import com.spreadthesource.tapestry.dbmigration.data.Table;
34
import com.spreadthesource.tapestry.dbmigration.services.MigrationHelper;
45

5-
66
public abstract class MigrationBase implements Migration
77
{
8-
protected MigrationHelper helper;
9-
10-
8+
private MigrationHelper helper;
9+
10+
public MigrationBase(MigrationHelper helper)
11+
{
12+
this.helper = helper;
13+
}
14+
15+
public void createTable(Table table)
16+
{
17+
helper.createTable(table);
18+
}
19+
20+
public void dropTable(String tableName)
21+
{
22+
helper.dropTable(tableName);
23+
}
1124
}

src/main/java/com/spreadthesource/tapestry/dbmigration/services/MigrationHelper.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,36 @@
99
public interface MigrationHelper
1010
{
1111
public Dialect getDialect();
12-
13-
public void createTable(String tableName);
1412

15-
public <T> void addColumn(String tableName, String columnName, T columnType);
16-
17-
public void dropTable(String tableName);
18-
1913
public String getDefaultSchema();
20-
14+
2115
public String getDefaultCatalog();
22-
16+
2317
public ConnectionHelper getConnectionHelper();
24-
18+
2519
public Formatter getFormatter();
26-
27-
public boolean checkIfTableExists(String tableName);
28-
20+
21+
/**
22+
* Create a Table , add the Table creation SQL to pending SQL and also return SQL
23+
*
24+
* @param table
25+
* @return creation SQL
26+
*/
2927
public String createTable(Table table);
3028

29+
/**
30+
* Drop a Table by adding SQL drop code to pending SQL and also return it
31+
*
32+
* @param tableName
33+
* @return drop table SQL
34+
*/
35+
public String dropTable(String tableName);
36+
37+
public boolean checkIfTableExists(String tableName);
38+
39+
/**
40+
* Return pending SQL commands and clear the stack
41+
*/
42+
public String[] getPendingSQL();
43+
3144
}

0 commit comments

Comments
 (0)