Skip to content

Commit ac84ee8

Browse files
committed
Fix column type resolution and allow method chaining on Column
1 parent 33fd1f7 commit ac84ee8

File tree

4 files changed

+34
-28
lines changed

4 files changed

+34
-28
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,39 +51,43 @@ public String getName() {
5151
return name;
5252
}
5353

54-
public void setUnique(boolean unique)
54+
public Column setUnique(boolean unique)
5555
{
5656
this.unique = unique;
57+
return this;
5758
}
5859

5960
public boolean isUnique()
6061
{
6162
return unique || primary;
6263
}
6364

64-
public void setLength(int length)
65+
public Column setLength(int length)
6566
{
6667
this.length = length;
68+
return this;
6769
}
6870

6971
public Integer getLength()
7072
{
7173
return length;
7274
}
7375

74-
public void setPrimary(boolean primary)
76+
public Column setPrimary(boolean primary)
7577
{
7678
this.primary = primary;
79+
return this;
7780
}
7881

7982
public boolean isPrimary()
8083
{
8184
return primary;
8285
}
8386

84-
public void setNotNull(boolean notNull)
87+
public Column setNotNull(boolean notNull)
8588
{
8689
this.notNull = notNull;
90+
return this;
8791
}
8892

8993
public boolean isNotNull()
@@ -92,10 +96,10 @@ public boolean isNotNull()
9296
}
9397

9498

95-
96-
public void setIdentityGenerator(String identityGenerator)
99+
public Column setIdentityGenerator(String identityGenerator)
97100
{
98101
this.identityGenerator = identityGenerator;
102+
return this;
99103
}
100104

101105
public String getIdentityGenerator()

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public List<String> createTable(Table table)
180180
SimpleValue idValue = new SimpleValue(hTable);
181181
idValue.setIdentifierGeneratorStrategy("identity");
182182

183-
idValue.setTypeName(dialect.getTypeName(column.getType()));
183+
idValue.setTypeName(dialect.getHibernateTypeName(column.getType()));
184184

185185
hColumn.setValue(idValue);
186186

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import org.apache.tapestry5.ioc.annotations.Symbol;
1414
import org.apache.tapestry5.ioc.services.ClassNameLocator;
1515
import org.hibernate.Hibernate;
16-
import org.hibernate.Session;
1716
import org.hibernate.sql.Insert;
1817
import org.hibernate.sql.JoinFragment;
1918
import org.hibernate.sql.QuerySelect;
@@ -43,7 +42,6 @@ public class MigrationManagerImpl implements MigrationManager
4342
public MigrationManagerImpl(
4443
Collection<String> packages,
4544
Logger logger,
46-
Session session,
4745
ClassNameLocator classNameLocator,
4846
ObjectLocator objectLocator,
4947
MigrationRunner runner,
@@ -93,12 +91,13 @@ public Integer current()
9391

9492
try
9593
{
96-
if (r.next()) {
94+
if (r.next())
95+
{
9796
Integer version = r.getInt("version");
9897
log.debug("Current version is : " + version);
9998
return version;
10099
}
101-
100+
102101
}
103102
catch (SQLException e)
104103
{
@@ -111,9 +110,8 @@ public Integer current()
111110
public Integer down()
112111
{
113112
Integer current = current();
114-
if (current.equals(0))
115-
return current;
116-
113+
if (current.equals(0)) return current;
114+
117115
String className = classes.get(current);
118116

119117
if (className == null)
@@ -122,12 +120,14 @@ public Integer down()
122120
Migration migration = getMigration(className);
123121

124122
migration.down();
125-
123+
126124
runner.update(migration.getPendingSQL());
127125

128-
SortedMap<Integer, String> previousMigrations = (new TreeMap<Integer, String>(classes)).headMap(current);
126+
SortedMap<Integer, String> previousMigrations = (new TreeMap<Integer, String>(classes))
127+
.headMap(current);
129128

130-
if (previousMigrations.size() < 1) {
129+
if (previousMigrations.size() < 1)
130+
{
131131
recordVersion(0);
132132
return 0;
133133
}
@@ -142,8 +142,9 @@ public Integer down()
142142
public Integer up()
143143
{
144144
Integer current = current();
145-
146-
SortedMap<Integer, String> pendingMigrations = (new TreeMap<Integer, String>(classes)).tailMap(current);
145+
146+
SortedMap<Integer, String> pendingMigrations = (new TreeMap<Integer, String>(classes))
147+
.tailMap(current);
147148

148149
pendingMigrations.remove(current);
149150
Iterator<Integer> iterator = pendingMigrations.keySet().iterator();
@@ -187,16 +188,15 @@ public void initialize()
187188
public Integer migrate()
188189
{
189190
Integer current = current();
190-
191-
if (current == null)
192-
initialize();
193-
191+
192+
if (current == null) initialize();
193+
194194
Integer next = up();
195195

196196
while (!current.equals(next))
197197
{
198198
current = next;
199-
199+
200200
next = up();
201201
}
202202

@@ -224,7 +224,7 @@ private void recordVersion(Integer version)
224224
Insert insert = new Insert(helper.getDialect());
225225
insert.setTableName(versioningTableName);
226226
insert.addColumn("version", version.toString());
227-
insert.addColumn("datetime","'" + Hibernate.TIMESTAMP.toString(now) + "'");
227+
insert.addColumn("datetime", "'" + Hibernate.TIMESTAMP.toString(now) + "'");
228228

229229
runner.update(insert.toStatementString());
230230
}
@@ -233,7 +233,8 @@ private Integer getMigrationVersion(String className)
233233
{
234234
try
235235
{
236-
if (MigrationUtils.checkIfImplements(Class.forName(className), MigrationBase.class)) return null;
236+
if (MigrationUtils.checkIfImplements(Class.forName(className), MigrationBase.class))
237+
return null;
237238

238239
Version version = Class.forName(className).getAnnotation(Version.class);
239240

@@ -251,7 +252,8 @@ private Migration getMigration(String className)
251252
{
252253
try
253254
{
254-
if (MigrationUtils.checkIfImplements(Class.forName(className), MigrationBase.class)) return null;
255+
if (MigrationUtils.checkIfImplements(Class.forName(className), MigrationBase.class))
256+
return null;
255257

256258
Version version = Class.forName(className).getAnnotation(Version.class);
257259

src/test/resources/hibernate.h2.cfg.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<property name="hibernate.dialect">org.hibernate.dialect.H2Dialect</property>
1111
<property name="hibernate.connection.url">jdbc:h2:/tmp/dbmigrations</property>
1212
<!-- property name="hibernate.hbm2ddl.auto">update</property-->
13-
<property name="hibernate.show_sql">false</property>
13+
<property name="hibernate.show_sql">true</property>
1414

1515
</session-factory>
1616
</hibernate-configuration>

0 commit comments

Comments
 (0)