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.DBFactoryUtil;
020 import com.liferay.portal.kernel.log.Log;
021 import com.liferay.portal.kernel.log.LogFactoryUtil;
022 import com.liferay.portal.kernel.upgrade.UpgradeException;
023 import com.liferay.portal.kernel.util.FileUtil;
024 import com.liferay.portal.kernel.util.StringUtil;
025 import com.liferay.portal.kernel.util.Validator;
026
027
031 public abstract class BaseUpgradeTableImpl extends Table {
032
033 public BaseUpgradeTableImpl(String tableName) {
034 super(tableName);
035 }
036
037 public BaseUpgradeTableImpl(String tableName, Object[][] columns) {
038 super(tableName, columns);
039 }
040
041 public String[] getIndexesSQL() throws Exception {
042 return _indexesSQL;
043 }
044
045 public boolean isAllowUniqueIndexes() throws Exception {
046 return _allowUniqueIndexes;
047 }
048
049 public void setAllowUniqueIndexes(boolean allowUniqueIndexes)
050 throws Exception {
051
052 _allowUniqueIndexes = allowUniqueIndexes;
053 }
054
055 @Override
056 public void setCreateSQL(String createSQL) throws Exception {
057 if (_calledUpdateTable) {
058 throw new UpgradeException(
059 "setCreateSQL is called after updateTable");
060 }
061
062 super.setCreateSQL(createSQL);
063 }
064
065 public void setIndexesSQL(String[] indexesSQL) throws Exception {
066 _indexesSQL = indexesSQL;
067 }
068
069 public void updateTable() throws Exception {
070 _calledUpdateTable = true;
071
072 String tempFileName = generateTempFile();
073
074 try {
075 DB db = DBFactoryUtil.getDB();
076
077 if (Validator.isNotNull(tempFileName)) {
078 String deleteSQL = getDeleteSQL();
079
080 db.runSQL(deleteSQL);
081 }
082
083 String createSQL = getCreateSQL();
084
085 if (Validator.isNotNull(createSQL)) {
086 db.runSQL("drop table " + getTableName());
087
088 db.runSQL(createSQL);
089 }
090
091 if (Validator.isNotNull(tempFileName)) {
092 populateTable(tempFileName);
093 }
094
095 String[] indexesSQL = getIndexesSQL();
096
097 boolean dropIndexes = false;
098
099 for (String indexSQL : indexesSQL) {
100 if (!isAllowUniqueIndexes()) {
101 if (indexSQL.contains("create unique index")) {
102 indexSQL = StringUtil.replace(
103 indexSQL, "create unique index ", "create index ");
104
105 dropIndexes = true;
106 }
107 }
108
109 try {
110 db.runSQL(indexSQL);
111 }
112 catch (Exception e) {
113 _log.warn(e.getMessage() + ": " + indexSQL);
114 }
115 }
116
117 if (dropIndexes) {
118 StartupHelperUtil.setDropIndexes(true);
119 }
120 }
121 finally {
122 if (Validator.isNotNull(tempFileName)) {
123 FileUtil.delete(tempFileName);
124 }
125 }
126 }
127
128 private static Log _log = LogFactoryUtil.getLog(BaseUpgradeTableImpl.class);
129
130 private boolean _allowUniqueIndexes;
131 private boolean _calledUpdateTable;
132 private String[] _indexesSQL = new String[0];
133
134 }