001
014
015 package com.liferay.portal.tools;
016
017 import com.liferay.portal.kernel.dao.db.DB;
018 import com.liferay.portal.kernel.dao.db.DBManagerUtil;
019 import com.liferay.portal.kernel.dao.db.DBType;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.StringPool;
022 import com.liferay.portal.kernel.util.StringUtil;
023
024 import java.io.IOException;
025
026 import java.nio.file.DirectoryStream;
027 import java.nio.file.Files;
028 import java.nio.file.Path;
029 import java.nio.file.Paths;
030
031 import java.util.Map;
032
033
039 public class DBBuilder {
040
041 public static void main(String[] args) throws Exception {
042 ToolDependencies.wireBasic();
043
044 Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
045
046 String databaseName = arguments.get("db.database.name");
047
048 String databaseTypesString = arguments.get("db.database.types");
049
050 DBType[] dbTypes = DBType.values();
051
052 if (databaseTypesString != null) {
053 String[] databaseTypeValues = StringUtil.split(databaseTypesString);
054
055 dbTypes = new DBType[databaseTypeValues.length];
056
057 for (int i = 0; i < dbTypes.length; i++) {
058 dbTypes[i] = DBType.valueOf(
059 StringUtil.toUpperCase(databaseTypeValues[i]));
060 }
061 }
062
063 String sqlDir = arguments.get("db.sql.dir");
064
065 try {
066 new DBBuilder(databaseName, dbTypes, sqlDir);
067 }
068 catch (Exception e) {
069 ArgumentsUtil.processMainException(arguments, e);
070 }
071 }
072
073 public DBBuilder(String databaseName, DBType[] dbTypes, String sqlDir)
074 throws Exception {
075
076 _databaseName = databaseName;
077 _dbTypes = dbTypes;
078
079 if (!sqlDir.endsWith("/META-INF/sql") &&
080 !sqlDir.endsWith("/WEB-INF/sql")) {
081
082 _buildSQLFile(sqlDir, "portal");
083 _buildSQLFile(sqlDir, "portal-tables");
084 }
085 else {
086 _buildSQLFile(sqlDir, "tables");
087 }
088
089 _buildSQLFile(sqlDir, "indexes");
090 _buildSQLFile(sqlDir, "sequences");
091 _buildSQLFile(sqlDir, "update-5.0.1-5.1.0");
092 _buildSQLFile(sqlDir, "update-5.1.1-5.1.2");
093 _buildSQLFile(sqlDir, "update-5.1.2-5.2.0");
094 _buildSQLFile(sqlDir, "update-5.2.0-5.2.1");
095 _buildSQLFile(sqlDir, "update-5.2.2-5.2.3");
096 _buildSQLFile(sqlDir, "update-5.2.3-6.0.0");
097 _buildSQLFile(sqlDir, "update-5.2.5-6.0.0");
098 _buildSQLFile(sqlDir, "update-5.2.7-6.0.0");
099 _buildSQLFile(sqlDir, "update-5.2.8-6.0.5");
100 _buildSQLFile(sqlDir, "update-6.0.0-6.0.1");
101 _buildSQLFile(sqlDir, "update-6.0.1-6.0.2");
102 _buildSQLFile(sqlDir, "update-6.0.2-6.0.3");
103 _buildSQLFile(sqlDir, "update-6.0.4-6.0.5");
104 _buildSQLFile(sqlDir, "update-6.0.5-6.0.6");
105 _buildSQLFile(sqlDir, "update-6.0.6-6.1.0");
106 _buildSQLFile(sqlDir, "update-6.0.12-6.1.0");
107 _buildSQLFile(sqlDir, "update-6.1.0-6.1.1");
108 _buildSQLFiles(sqlDir, "update-6.1.1-6.2.0*");
109 _buildSQLFiles(sqlDir, "update-6.2.0-7.0.0*");
110 _buildSQLFiles(sqlDir, "update-7.0.0-7.0.1*");
111
112 _buildCreateFile(sqlDir);
113 }
114
115 private void _buildCreateFile(String sqlDir) throws IOException {
116 for (DBType dbType : _dbTypes) {
117 if (dbType == DBType.HYPERSONIC) {
118 continue;
119 }
120
121 DB db = DBManagerUtil.getDB(dbType, null);
122
123 if (db != null) {
124 if (!sqlDir.endsWith("/WEB-INF/sql")) {
125 db.buildCreateFile(sqlDir, _databaseName);
126 }
127 else {
128 db.buildCreateFile(sqlDir, _databaseName, DB.DEFAULT);
129 }
130 }
131 }
132 }
133
134 private void _buildSQLFile(String sqlDir, String fileName)
135 throws IOException {
136
137 if (!FileUtil.exists(sqlDir + "/" + fileName + ".sql")) {
138 return;
139 }
140
141 _generateSQLFile(sqlDir, fileName);
142 }
143
144 private void _buildSQLFiles(String sqlDir, String regex)
145 throws IOException {
146
147 try (DirectoryStream<Path> paths = Files.newDirectoryStream(
148 Paths.get(sqlDir), regex)) {
149
150 for (Path path : paths) {
151 Path fileNamePath = path.getFileName();
152
153 String fileName = fileNamePath.toString();
154
155 _generateSQLFile(
156 sqlDir, fileName.replace(".sql", StringPool.BLANK));
157 }
158 }
159 }
160
161 private void _generateSQLFile(String sqlDir, String fileName)
162 throws IOException {
163
164 for (DBType dbType : _dbTypes) {
165 DB db = DBManagerUtil.getDB(dbType, null);
166
167 if (db != null) {
168 db.buildSQLFile(sqlDir, fileName);
169 }
170 }
171 }
172
173 private final String _databaseName;
174 private final DBType[] _dbTypes;
175
176 }