From bfc74d92ea937c68a2bb10dba06580e67fa886a7 Mon Sep 17 00:00:00 2001 From: mdb Date: Wed, 15 Aug 2007 23:35:20 +0000 Subject: [PATCH] Column migration in Postgres is more complicated. There may be other things than nullability that will fuck us in the future, but this'll do for now. git-svn-id: https://samskivert.googlecode.com/svn/trunk@2190 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- src/java/com/samskivert/jdbc/BaseLiaison.java | 2 +- .../samskivert/jdbc/PostgreSQLLiaison.java | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/java/com/samskivert/jdbc/BaseLiaison.java b/src/java/com/samskivert/jdbc/BaseLiaison.java index 7570bad0..5ceb4b18 100644 --- a/src/java/com/samskivert/jdbc/BaseLiaison.java +++ b/src/java/com/samskivert/jdbc/BaseLiaison.java @@ -164,7 +164,7 @@ public abstract class BaseLiaison implements DatabaseLiaison throws SQLException { executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " CHANGE " + - columnSQL(column) + " " + column + " " + definition); + columnSQL(column) + " " + columnSQL(column) + " " + definition); Log.info("Database column '" + column + "' of table '" + table + "' modified to have " + "definition '" + definition + "'."); return true; diff --git a/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java b/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java index 585ae34b..69fdbab0 100644 --- a/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java +++ b/src/java/com/samskivert/jdbc/PostgreSQLLiaison.java @@ -21,6 +21,10 @@ package com.samskivert.jdbc; import java.sql.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.samskivert.Log; /** * A database liaison for the MySQL database. @@ -81,6 +85,26 @@ public class PostgreSQLLiaison extends BaseLiaison " restart with " + first + " increment " + step); } + @Override // from DatabaseLiaison + public boolean changeColumn (Connection conn, String table, String column, String definition) + throws SQLException + { + // we need to handle nullability separately; TODO: make this less of a hack + boolean notNull = false; + Matcher match = NOT_NULL.matcher(definition); + if (match.find()) { + definition = match.replaceFirst(""); + notNull = true; + } + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ALTER COLUMN " + + columnSQL(column) + " TYPE " + definition); + executeQuery(conn, "ALTER TABLE " + tableSQL(table) + " ALTER COLUMN " + + columnSQL(column) + " " + (notNull ? "SET NOT NULL" : "DROP NOT NULL")); + Log.info("Database column '" + column + "' of table '" + table + "' modified to have " + + "definition '" + definition + "'."); + return true; + } + // from DatabaseLiaison public String columnSQL (String column) { @@ -98,4 +122,7 @@ public class PostgreSQLLiaison extends BaseLiaison { return "\"" + index + "\""; } + + protected static final Pattern NOT_NULL = + Pattern.compile(" NOT NULL", Pattern.CASE_INSENSITIVE); }