001/*- 002 * #%L 003 * HAPI FHIR Server - SQL Migration 004 * %% 005 * Copyright (C) 2014 - 2023 Smile CDR, Inc. 006 * %% 007 * Licensed under the Apache License, Version 2.0 (the "License"); 008 * you may not use this file except in compliance with the License. 009 * You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 * #L% 019 */ 020package ca.uhn.fhir.jpa.migrate.taskdef; 021 022import ca.uhn.fhir.jpa.migrate.DriverTypeEnum; 023import ca.uhn.fhir.jpa.migrate.JdbcUtils; 024import org.slf4j.Logger; 025import org.slf4j.LoggerFactory; 026 027import java.sql.SQLException; 028import java.util.Locale; 029 030public class MigratePostgresTextClobToBinaryClobTask extends BaseTableColumnTask { 031 private static final Logger ourLog = LoggerFactory.getLogger(MigratePostgresTextClobToBinaryClobTask.class); 032 033 /** 034 * Constructor 035 */ 036 public MigratePostgresTextClobToBinaryClobTask(String theProductVersion, String theSchemaVersion) { 037 super(theProductVersion, theSchemaVersion); 038 } 039 040 @Override 041 public void validate() { 042 super.validate(); 043 setDescription("Migrate text clob " + getColumnName() + " from table " + getTableName() + " (only affects Postgresql)"); 044 } 045 046 @Override 047 protected void doExecute() throws SQLException { 048 if (getConnectionProperties().getDriverType() != DriverTypeEnum.POSTGRES_9_4) { 049 return; 050 } 051 052 String tableName = getTableName(); 053 String columnName = getColumnName(); 054 JdbcUtils.ColumnType columnType = JdbcUtils.getColumnType(getConnectionProperties(), tableName, columnName); 055 if (columnType.getColumnTypeEnum() == ColumnTypeEnum.LONG) { 056 ourLog.info("Table {} column {} is already of type LONG, no migration needed", tableName, columnName); 057 return; 058 } 059 060 String tempColumnName = columnName + "_m".toLowerCase(); 061 tableName = tableName.toLowerCase(); 062 columnName = columnName.toLowerCase(); 063 064 executeSql(tableName, "alter table " + tableName + " add column " + tempColumnName + " oid"); 065 executeSql(tableName, "update " + tableName + " set " + tempColumnName + " = cast(" + columnName + " as oid) where " + columnName + " is not null"); 066 executeSql(tableName, "alter table " + tableName + " drop column " + columnName); 067 executeSql(tableName, "alter table " + tableName + " rename column " + tempColumnName + " to " + columnName); 068 069 } 070}