Add dialect-agnostic auto-migration of full text indexes.

This commit is contained in:
Par Winzell
2007-08-14 17:00:26 +00:00
parent f80ce4d862
commit 79024ea85a
4 changed files with 58 additions and 2 deletions
@@ -31,6 +31,7 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@@ -51,7 +52,6 @@ import com.samskivert.jdbc.depot.annotation.UniqueConstraint;
import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.util.ArrayUtil;
import com.samskivert.util.StringUtil;
import static com.samskivert.jdbc.depot.Log.log;
@@ -746,6 +746,27 @@ public class DepotMarshaller<T extends PersistentRecord>
});
}
// next we create any full text search indexes that exist on the record but not in the
// table, first step being to do a dialect-sensitive enumeration of existing indexes
Set<String> tableFts = new HashSet<String>(Arrays.asList(
builder.getFtsIndexes(metaData.tableColumns, metaData.indexColumns.keySet())));
// then iterate over what should be there
for (final FullTextIndex recordFts : _fullTextIndexes.values()) {
if (tableFts.contains(recordFts.name())) {
// the table already contains this one
continue;
}
// but not this one, so let's create it
ctx.invoke(new Modifier() {
public int invoke (Connection conn, DatabaseLiaison liaison) throws SQLException {
builder.addFullTextSearch(conn, DepotMarshaller.this, recordFts);
return 0;
}
});
}
// we do not auto-remove columns but rather require that EntityMigration.Drop records be
// registered by hand to avoid accidentally causing the loss of data
@@ -29,6 +29,8 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.samskivert.Log;
import com.samskivert.jdbc.JDBCUtil;
@@ -134,6 +136,18 @@ public class MySQLBuilder
super(types);
}
@Override
public String[] getFtsIndexes (Iterable<String> columns, Iterable<String> indexes)
{
List<String> result = new ArrayList<String>();
for (String index : indexes) {
if (index.startsWith("ftsIx_")) {
result.add(index.substring("ftsIx_".length()));
}
}
return result.toArray(new String[0]);
}
@Override
public <T extends PersistentRecord> boolean addFullTextSearch (
Connection conn, DepotMarshaller<T> marshaller, FullTextIndex fts)
@@ -29,6 +29,8 @@ import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.samskivert.jdbc.DatabaseLiaison;
import com.samskivert.jdbc.JDBCUtil;
@@ -108,6 +110,18 @@ public class PostgreSQLBuilder
super(types);
}
@Override
public String[] getFtsIndexes (Iterable<String> columns, Iterable<String> indexes)
{
List<String> result = new ArrayList<String>();
for (String column : columns) {
if (column.startsWith("ftsCol_")) {
result.add(column.substring("ftsCol_".length()));
}
}
return result.toArray(new String[0]);
}
@Override
public <T extends PersistentRecord> boolean addFullTextSearch (
Connection conn, DepotMarshaller<T> marshaller, FullTextIndex fts)
@@ -25,11 +25,11 @@ import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.samskivert.jdbc.depot.DepotMarshaller.TableMetaData;
import com.samskivert.jdbc.depot.annotation.Column;
import com.samskivert.jdbc.depot.annotation.FullTextIndex;
import com.samskivert.jdbc.depot.annotation.GeneratedValue;
import com.samskivert.jdbc.depot.clause.QueryClause;
import com.samskivert.jdbc.depot.operator.Conditionals;
/**
* At the heart of Depot's SQL generation, this object constructs two {@link ExpressionVisitor}
@@ -186,6 +186,13 @@ public abstract class SQLBuilder
*/
public abstract boolean isPrivateColumn (String column);
/**
* Figure out what full text search indexes already exist on this table and return the names
* of those indexes as an array.
*/
public abstract String[] getFtsIndexes (
Iterable<String> columns, Iterable<String> indexes);
/**
* Overridden by subclasses to create a dialect-specific {@link BuildVisitor}.
*/