001    /**
002     * Copyright (c) 2000-2013 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.verify;
016    
017    import com.liferay.portal.kernel.dao.db.DB;
018    import com.liferay.portal.kernel.dao.db.DBFactoryUtil;
019    import com.liferay.portal.kernel.dao.jdbc.DataAccess;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.util.PropsValues;
024    
025    import java.sql.Connection;
026    import java.sql.PreparedStatement;
027    import java.sql.ResultSet;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class VerifyMySQL extends VerifyProcess {
033    
034            protected void alterTableEngine(String tableName) throws Exception {
035                    if (_log.isInfoEnabled()) {
036                            _log.info(
037                                    "Updating table " + tableName + " to use engine " +
038                                            PropsValues.DATABASE_MYSQL_ENGINE);
039                    }
040    
041                    Connection con = null;
042                    PreparedStatement ps = null;
043    
044                    try {
045                            con = DataAccess.getUpgradeOptimizedConnection();
046    
047                            ps = con.prepareStatement(
048                                    "alter table " + tableName + " engine " +
049                                            PropsValues.DATABASE_MYSQL_ENGINE);
050    
051                            ps.executeUpdate();
052                    }
053                    finally {
054                            DataAccess.cleanUp(con, ps);
055                    }
056            }
057    
058            @Override
059            protected void doVerify() throws Exception {
060                    DB db = DBFactoryUtil.getDB();
061    
062                    String dbType = db.getType();
063    
064                    if (!dbType.equals(DB.TYPE_MYSQL)) {
065                            return;
066                    }
067    
068                    Connection con = null;
069                    PreparedStatement ps = null;
070                    ResultSet rs = null;
071    
072                    try {
073                            con = DataAccess.getUpgradeOptimizedConnection();
074    
075                            ps = con.prepareStatement("show table status");
076    
077                            rs = ps.executeQuery();
078    
079                            while (rs.next()) {
080                                    String tableName = rs.getString("Name");
081    
082                                    if (!isPortalTableName(tableName)) {
083                                            continue;
084                                    }
085    
086                                    String engine = GetterUtil.getString(rs.getString("Engine"));
087                                    String comment = GetterUtil.getString(rs.getString("Comment"));
088    
089                                    if (comment.equalsIgnoreCase("VIEW")) {
090                                            continue;
091                                    }
092    
093                                    if (engine.equalsIgnoreCase(
094                                                    PropsValues.DATABASE_MYSQL_ENGINE)) {
095    
096                                            continue;
097                                    }
098    
099                                    alterTableEngine(tableName);
100                            }
101                    }
102                    finally {
103                            DataAccess.cleanUp(con, ps, rs);
104                    }
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(VerifyMySQL.class);
108    
109    }