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.upgrade.v7_0_0;
016    
017    import com.liferay.portal.kernel.upgrade.UpgradeProcess;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.LoggingTimer;
020    
021    import java.sql.PreparedStatement;
022    import java.sql.ResultSet;
023    
024    /**
025     * @author Miguel Pastor
026     */
027    public class UpgradeRelease extends UpgradeProcess {
028    
029            @Override
030            protected void doUpgrade() throws Exception {
031                    upgradeSchemaVersion();
032            }
033    
034            protected String toSchemaVersion(String buildNumber) {
035                    StringBuilder sb = new StringBuilder(2 * buildNumber.length());
036    
037                    for (int i = 0; i < buildNumber.length(); i++) {
038                            sb.append(buildNumber.charAt(i));
039                            sb.append(CharPool.PERIOD);
040                    }
041    
042                    if (buildNumber.length() > 0) {
043                            sb.setLength(sb.length() - 1);
044                    }
045    
046                    return sb.toString();
047            }
048    
049            protected void upgradeSchemaVersion() throws Exception {
050                    try (LoggingTimer loggingTimer = new LoggingTimer();
051                            PreparedStatement ps = connection.prepareStatement(
052                                    "select distinct buildNumber from Release_ " +
053                                            "where schemaVersion is null");
054                            ResultSet rs = ps.executeQuery()) {
055    
056                            while (rs.next()) {
057                                    String buildNumber = rs.getString("buildNumber");
058    
059                                    String schemaVersion = toSchemaVersion(buildNumber);
060    
061                                    runSQL(
062                                            "update Release_ set schemaVersion = '" + schemaVersion +
063                                                    "' where buildNumber = " + buildNumber +
064                                                            " and schemaVersion is null");
065                            }
066                    }
067            }
068    
069    }