001
014
015 package com.liferay.portal.upgrade.util;
016
017 import com.liferay.portal.events.StartupHelperUtil;
018 import com.liferay.portal.kernel.dao.db.DB;
019 import com.liferay.portal.kernel.dao.db.DBManagerUtil;
020 import com.liferay.portal.kernel.dao.jdbc.DataAccess;
021 import com.liferay.portal.kernel.log.Log;
022 import com.liferay.portal.kernel.log.LogFactoryUtil;
023 import com.liferay.portal.kernel.upgrade.UpgradeException;
024 import com.liferay.portal.kernel.util.FileUtil;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.Validator;
027
028 import java.sql.Connection;
029
030
034 public abstract class BaseUpgradeTableImpl extends Table {
035
036 public BaseUpgradeTableImpl(String tableName) {
037 super(tableName);
038 }
039
040 public String[] getIndexesSQL() throws Exception {
041 return _indexesSQL;
042 }
043
044 public boolean isAllowUniqueIndexes() throws Exception {
045 return _allowUniqueIndexes;
046 }
047
048 public boolean isDeleteTempFile() {
049 return _deleteTempFile;
050 }
051
052 public void setAllowUniqueIndexes(boolean allowUniqueIndexes)
053 throws Exception {
054
055 _allowUniqueIndexes = allowUniqueIndexes;
056 }
057
058 @Override
059 public void setCreateSQL(String createSQL) throws Exception {
060 if (_calledUpdateTable) {
061 throw new UpgradeException(
062 "setCreateSQL is called after updateTable");
063 }
064
065 super.setCreateSQL(createSQL);
066 }
067
068 public void setDeleteTempFile(boolean deleteTempFile) {
069 _deleteTempFile = deleteTempFile;
070 }
071
072 public void setIndexesSQL(String[] indexesSQL) throws Exception {
073 _indexesSQL = indexesSQL;
074 }
075
076 public void updateTable() throws Exception {
077 Connection connection = DataAccess.getUpgradeOptimizedConnection();
078
079 try {
080 updateTable(connection, connection, true);
081 }
082 finally {
083 DataAccess.cleanUp(connection);
084 }
085 }
086
087 protected void updateTable(
088 Connection sourceConnection, Connection targetConnection,
089 boolean deleteSource)
090 throws Exception {
091
092 _calledUpdateTable = true;
093
094 generateTempFile(sourceConnection);
095
096 String tempFileName = getTempFileName();
097
098 try {
099 DB db = DBManagerUtil.getDB();
100
101 if (Validator.isNotNull(tempFileName) && deleteSource) {
102 String deleteSQL = getDeleteSQL();
103
104 db.runSQL(sourceConnection, deleteSQL);
105 }
106
107 String createSQL = getCreateSQL();
108
109 if (Validator.isNotNull(createSQL)) {
110 if (deleteSource) {
111 db.runSQL(sourceConnection, "drop table " + getTableName());
112 }
113
114 db.runSQL(targetConnection, createSQL);
115 }
116
117 populateTable(targetConnection);
118
119 String[] indexesSQL = getIndexesSQL();
120
121 boolean dropIndexes = false;
122
123 for (String indexSQL : indexesSQL) {
124 if (!isAllowUniqueIndexes()) {
125 if (indexSQL.contains("create unique index")) {
126 indexSQL = StringUtil.replace(
127 indexSQL, "create unique index ", "create index ");
128
129 dropIndexes = true;
130 }
131 }
132
133 try {
134 db.runSQLTemplateString(
135 targetConnection, indexSQL, false, false);
136 }
137 catch (Exception e) {
138 if (_log.isWarnEnabled()) {
139 _log.warn(e.getMessage() + ": " + indexSQL);
140 }
141 }
142 }
143
144 if (dropIndexes) {
145 StartupHelperUtil.setDropIndexes(true);
146 }
147 }
148 finally {
149 if (Validator.isNotNull(tempFileName) && _deleteTempFile) {
150 FileUtil.delete(tempFileName);
151 }
152 }
153 }
154
155 private static final Log _log = LogFactoryUtil.getLog(
156 BaseUpgradeTableImpl.class);
157
158 private boolean _allowUniqueIndexes;
159 private boolean _calledUpdateTable;
160 private boolean _deleteTempFile;
161 private String[] _indexesSQL = new String[0];
162
163 }