001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.upgrade;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.LoggingTimer;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.xml.Document;
022    import com.liferay.portal.kernel.xml.Element;
023    import com.liferay.portal.kernel.xml.UnsecureSAXReaderUtil;
024    
025    import java.io.InputStream;
026    
027    import java.sql.DatabaseMetaData;
028    import java.sql.ResultSet;
029    
030    import java.util.List;
031    
032    /**
033     * @author Shuyang Zhou
034     */
035    public class UpgradeMVCCVersion extends UpgradeProcess {
036    
037            public void upgradeMVCCVersion(
038                            DatabaseMetaData databaseMetaData, String tableName)
039                    throws Exception {
040    
041                    for (String excludeTableName : getExcludedTableNames()) {
042                            if (StringUtil.equalsIgnoreCase(excludeTableName, tableName)) {
043                                    return;
044                            }
045                    }
046    
047                    tableName = normalizeName(tableName, databaseMetaData);
048    
049                    try (ResultSet tableResultSet = databaseMetaData.getTables(
050                                    null, null, tableName, null)) {
051    
052                            if (!tableResultSet.next()) {
053                                    _log.error("Table " + tableName + " does not exist");
054    
055                                    return;
056                            }
057    
058                            try (ResultSet columnResultSet = databaseMetaData.getColumns(
059                                            null, null, tableName,
060                                            normalizeName("mvccVersion", databaseMetaData))) {
061    
062                                    if (columnResultSet.next()) {
063                                            return;
064                                    }
065    
066                                    runSQL(
067                                            "alter table " + tableName +
068                                                    " add mvccVersion LONG default 0 not null");
069    
070                                    if (_log.isDebugEnabled()) {
071                                            _log.debug(
072                                                    "Added column mvccVersion to table " + tableName);
073                                    }
074                            }
075                    }
076            }
077    
078            @Override
079            protected void doUpgrade() throws Exception {
080                    upgradeClassElementMVCCVersions();
081                    upgradeModuleTableMVCCVersions();
082            }
083    
084            protected List<Element> getClassElements() throws Exception {
085                    Thread currentThread = Thread.currentThread();
086    
087                    ClassLoader classLoader = currentThread.getContextClassLoader();
088    
089                    InputStream inputStream = classLoader.getResourceAsStream(
090                            "META-INF/portal-hbm.xml");
091    
092                    Document document = UnsecureSAXReaderUtil.read(inputStream);
093    
094                    Element rootElement = document.getRootElement();
095    
096                    return rootElement.elements("class");
097            }
098    
099            protected String[] getExcludedTableNames() {
100                    return new String[0];
101            }
102    
103            protected String[] getModuleTableNames() {
104                    return new String[] {"BackgroundTask", "Lock_"};
105            }
106    
107            protected void upgradeClassElementMVCCVersions() throws Exception {
108                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
109                            DatabaseMetaData databaseMetaData = connection.getMetaData();
110    
111                            List<Element> classElements = getClassElements();
112    
113                            for (Element classElement : classElements) {
114                                    if (classElement.element("version") == null) {
115                                            continue;
116                                    }
117    
118                                    upgradeMVCCVersion(databaseMetaData, classElement);
119                            }
120                    }
121            }
122    
123            protected void upgradeModuleTableMVCCVersions() throws Exception {
124                    try (LoggingTimer loggingTimer = new LoggingTimer()) {
125                            DatabaseMetaData databaseMetaData = connection.getMetaData();
126    
127                            String[] moduleTableNames = getModuleTableNames();
128    
129                            for (String moduleTableName : moduleTableNames) {
130                                    upgradeMVCCVersion(databaseMetaData, moduleTableName);
131                            }
132                    }
133            }
134    
135            protected void upgradeMVCCVersion(
136                            DatabaseMetaData databaseMetaData, Element classElement)
137                    throws Exception {
138    
139                    String tableName = classElement.attributeValue("table");
140    
141                    upgradeMVCCVersion(databaseMetaData, tableName);
142            }
143    
144            private static final Log _log = LogFactoryUtil.getLog(
145                    UpgradeMVCCVersion.class);
146    
147    }