001/*-
002 * #%L
003 * HAPI FHIR Server - SQL Migration
004 * %%
005 * Copyright (C) 2014 - 2025 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 ca.uhn.fhir.jpa.migrate.tasks.api.ISchemaInitializationProvider;
025import ca.uhn.fhir.jpa.migrate.tasks.api.TaskFlagEnum;
026import org.apache.commons.lang3.builder.EqualsBuilder;
027import org.apache.commons.lang3.builder.HashCodeBuilder;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031import java.sql.SQLException;
032import java.util.List;
033import java.util.Set;
034
035public class InitializeSchemaTask extends BaseTask {
036        private static final String DESCRIPTION_PREFIX = "Initialize schema for ";
037        private static final Logger ourLog = LoggerFactory.getLogger(InitializeSchemaTask.class);
038        private final ISchemaInitializationProvider mySchemaInitializationProvider;
039        private boolean myInitializedSchema;
040
041        public InitializeSchemaTask(
042                        String theProductVersion,
043                        String theSchemaVersion,
044                        ISchemaInitializationProvider theSchemaInitializationProvider) {
045                super(theProductVersion, theSchemaVersion);
046                mySchemaInitializationProvider = theSchemaInitializationProvider;
047                setDescription(DESCRIPTION_PREFIX + mySchemaInitializationProvider.getSchemaDescription());
048                addFlag(TaskFlagEnum.RUN_DURING_SCHEMA_INITIALIZATION);
049                // Some schema initialization statements (e.g. system settings) may not support transactions
050                setTransactional(false);
051        }
052
053        @Override
054        public void validate() {
055                // nothing
056        }
057
058        @Override
059        public void doExecute() throws SQLException {
060                DriverTypeEnum driverType = getDriverType();
061
062                Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
063                String schemaExistsIndicatorTable = mySchemaInitializationProvider.getSchemaExistsIndicatorTable();
064                if (tableNames.contains(schemaExistsIndicatorTable)) {
065                        logInfo(
066                                        ourLog,
067                                        "The table {} already exists.  Skipping schema initialization for {}",
068                                        schemaExistsIndicatorTable,
069                                        driverType);
070                        if (mySchemaInitializationProvider.canInitializeSchema()) {
071                                myInitializedSchema = true;
072                        }
073                        return;
074                }
075
076                logInfo(
077                                ourLog,
078                                "Initializing {} schema for {} with dry-run: {}",
079                                driverType,
080                                mySchemaInitializationProvider.getSchemaDescription(),
081                                isDryRun());
082
083                List<String> sqlStatements = mySchemaInitializationProvider.getSqlStatements(driverType);
084
085                for (String nextSql : sqlStatements) {
086                        executeSql(null, nextSql);
087                }
088
089                if (mySchemaInitializationProvider.canInitializeSchema()) {
090                        myInitializedSchema = true;
091                }
092
093                logInfo(
094                                ourLog,
095                                "{} schema for {} initialized successfully",
096                                driverType,
097                                mySchemaInitializationProvider.getSchemaDescription());
098        }
099
100        @Override
101        public boolean initializedSchema() {
102                return myInitializedSchema;
103        }
104
105        @Override
106        protected void generateEquals(EqualsBuilder theBuilder, BaseTask theOtherObject) {
107                InitializeSchemaTask otherObject = (InitializeSchemaTask) theOtherObject;
108                theBuilder.append(getSchemaInitializationProvider(), otherObject.getSchemaInitializationProvider());
109        }
110
111        @Override
112        protected void generateHashCode(HashCodeBuilder theBuilder) {
113                theBuilder.append(getSchemaInitializationProvider());
114        }
115
116        public ISchemaInitializationProvider getSchemaInitializationProvider() {
117                return mySchemaInitializationProvider;
118        }
119}