001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2024 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;
028
029public class MigratePostgresTextClobToBinaryClobTask extends BaseTableColumnTask {
030        private static final Logger ourLog = LoggerFactory.getLogger(MigratePostgresTextClobToBinaryClobTask.class);
031
032        /**
033         * Constructor
034         */
035        public MigratePostgresTextClobToBinaryClobTask(String theProductVersion, String theSchemaVersion) {
036                super(theProductVersion, theSchemaVersion);
037        }
038
039        @Override
040        public void validate() {
041                super.validate();
042                setDescription("Migrate text clob " + getColumnName() + " from table " + getTableName()
043                                + " (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(
066                                tableName,
067                                "update " + tableName + " set " + tempColumnName + " = cast(" + columnName + " as oid) where "
068                                                + columnName + " is not null");
069                executeSql(tableName, "alter table " + tableName + " drop column " + columnName);
070                executeSql(tableName, "alter table " + tableName + " rename column " + tempColumnName + " to " + columnName);
071        }
072}