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