From 79024ea85a5b991560fe0df75a67ff1418bddf28 Mon Sep 17 00:00:00 2001 From: Par Winzell Date: Tue, 14 Aug 2007 17:00:26 +0000 Subject: [PATCH] Add dialect-agnostic auto-migration of full text indexes. --- .../jdbc/depot/DepotMarshaller.java | 23 ++++++++++++++++++- .../samskivert/jdbc/depot/MySQLBuilder.java | 14 +++++++++++ .../jdbc/depot/PostgreSQLBuilder.java | 14 +++++++++++ .../com/samskivert/jdbc/depot/SQLBuilder.java | 9 +++++++- 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java index 28b8857..7fd9ef3 100644 --- a/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java +++ b/src/java/com/samskivert/jdbc/depot/DepotMarshaller.java @@ -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 }); } + // 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 tableFts = new HashSet(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 diff --git a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java index 08c4dc9..1425b0d 100644 --- a/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/MySQLBuilder.java @@ -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 columns, Iterable indexes) + { + List result = new ArrayList(); + for (String index : indexes) { + if (index.startsWith("ftsIx_")) { + result.add(index.substring("ftsIx_".length())); + } + } + return result.toArray(new String[0]); + } + @Override public boolean addFullTextSearch ( Connection conn, DepotMarshaller marshaller, FullTextIndex fts) diff --git a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java index 92b557b..c2a6533 100644 --- a/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/PostgreSQLBuilder.java @@ -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 columns, Iterable indexes) + { + List result = new ArrayList(); + for (String column : columns) { + if (column.startsWith("ftsCol_")) { + result.add(column.substring("ftsCol_".length())); + } + } + return result.toArray(new String[0]); + } + @Override public boolean addFullTextSearch ( Connection conn, DepotMarshaller marshaller, FullTextIndex fts) diff --git a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java index 37d192c..a725bc0 100644 --- a/src/java/com/samskivert/jdbc/depot/SQLBuilder.java +++ b/src/java/com/samskivert/jdbc/depot/SQLBuilder.java @@ -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 columns, Iterable indexes); + /** * Overridden by subclasses to create a dialect-specific {@link BuildVisitor}. */