001
014
015 package com.liferay.portal.upgrade.v7_0_0;
016
017 import com.liferay.portal.dao.db.PostgreSQLDB;
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.db.DBType;
021 import com.liferay.portal.kernel.upgrade.UpgradeException;
022 import com.liferay.portal.kernel.upgrade.UpgradeProcess;
023 import com.liferay.portal.kernel.util.LoggingTimer;
024 import com.liferay.portal.kernel.util.StringBundler;
025
026 import java.sql.PreparedStatement;
027 import java.sql.ResultSet;
028
029 import java.util.HashMap;
030 import java.util.Map;
031
032
035 public class UpgradePostgreSQL extends UpgradeProcess {
036
037 @Override
038 protected void doUpgrade() throws Exception {
039 DB db = DBManagerUtil.getDB();
040
041 if (db.getDBType() != DBType.POSTGRESQL) {
042 return;
043 }
044
045 Map<String, String> oidColumnNames = getOidColumnNames();
046
047 updatePostgreSQLRules(oidColumnNames);
048
049 updateOrphanedLargeObjects(oidColumnNames);
050 }
051
052 protected String getCurrentSchema() throws Exception {
053 try (PreparedStatement ps = connection.prepareStatement(
054 "select current_schema();");
055 ResultSet rs = ps.executeQuery()) {
056
057 if (rs.next()) {
058 return (String)rs.getObject("current_schema");
059 }
060
061 return null;
062 }
063 }
064
065 protected Map<String, String> getOidColumnNames() throws Exception {
066 try (LoggingTimer loggingTimer = new LoggingTimer()) {
067 Map<String, String> columnsWithOids = new HashMap<>();
068
069 StringBundler sb = new StringBundler(4);
070
071 sb.append("select table_name, column_name from ");
072 sb.append("information_schema.columns where table_schema='");
073
074 String schema = getCurrentSchema();
075
076 if (schema == null) {
077 throw new UpgradeException("Unable to get current schema");
078 }
079
080 sb.append(schema);
081
082 sb.append("' and data_type='oid';");
083
084 try (PreparedStatement ps = connection.prepareStatement(
085 sb.toString());
086 ResultSet rs = ps.executeQuery()) {
087
088 while (rs.next()) {
089 String tableName = (String)rs.getObject("table_name");
090 String columnName = (String)rs.getObject("column_name");
091
092 columnsWithOids.put(tableName, columnName);
093 }
094
095 return columnsWithOids;
096 }
097 }
098 }
099
100 protected void updateOrphanedLargeObjects(
101 Map<String, String> oidColumnNames)
102 throws Exception {
103
104 try (LoggingTimer loggingTimer = new LoggingTimer()) {
105 StringBundler sb = new StringBundler();
106
107 sb.append(
108 "select lo_unlink(l.oid) from pg_largeobject_metadata l ");
109 sb.append("where ");
110
111 int i = 1;
112
113 for (Map.Entry<String, String> column : oidColumnNames.entrySet()) {
114 String tableName = column.getKey();
115 String columnName = column.getValue();
116
117 sb.append("(not exists (select 1 from ");
118 sb.append(tableName);
119 sb.append(" t where t.");
120 sb.append(columnName);
121 sb.append(" = l.oid))");
122
123 if (i < oidColumnNames.size()) {
124 sb.append(" and ");
125 }
126
127 i++;
128 }
129
130 try (PreparedStatement ps = connection.prepareStatement(
131 sb.toString())) {
132
133 ps.execute();
134 }
135 }
136 }
137
138 protected void updatePostgreSQLRules(Map<String, String> oidColumnNames)
139 throws Exception {
140
141 try (LoggingTimer loggingTimer = new LoggingTimer()) {
142 for (Map.Entry<String, String> entry : oidColumnNames.entrySet()) {
143 String tableName = entry.getKey();
144 String columnName = entry.getValue();
145
146 try (PreparedStatement ps = connection.prepareStatement(
147 PostgreSQLDB.getCreateRulesSQL(
148 tableName, columnName))) {
149
150 ps.executeUpdate();
151 }
152 }
153 }
154 }
155
156 }