Commit f9ab031e authored by Liang Ding's avatar Liang Ding

🎨 Fix #12657

parent b247815b
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!-- <!--
Description: Solo POM. Description: Solo POM.
Version: 3.18.3.47, Feb 16, 2019 Version: 3.18.3.48, Feb 22, 2019
Author: <a href="http://88250.b3log.org">Liang Ding</a> Author: <a href="http://88250.b3log.org">Liang Ding</a>
Author: <a href="http://www.annpeter.cn">Ann Peter</a> Author: <a href="http://www.annpeter.cn">Ann Peter</a>
Author: <a href="http://vanessa.b3log.org">Vanessa</a> Author: <a href="http://vanessa.b3log.org">Vanessa</a>
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
</scm> </scm>
<properties> <properties>
<org.b3log.latke.version>2.4.42</org.b3log.latke.version> <org.b3log.latke.version>2.4.43</org.b3log.latke.version>
<slf4j.version>1.7.5</slf4j.version> <slf4j.version>1.7.5</slf4j.version>
<jsoup.version>1.9.1</jsoup.version> <jsoup.version>1.9.1</jsoup.version>
......
...@@ -84,6 +84,8 @@ public final class SoloServletListener extends AbstractServletListener { ...@@ -84,6 +84,8 @@ public final class SoloServletListener extends AbstractServletListener {
validateSkin(); validateSkin();
final InitService initService = beanManager.getReference(InitService.class); final InitService initService = beanManager.getReference(InitService.class);
initService.initTables();
if (initService.isInited()) { if (initService.isInited()) {
// Upgrade check https://github.com/b3log/solo/issues/12040 // Upgrade check https://github.com/b3log/solo/issues/12040
final UpgradeService upgradeService = beanManager.getReference(UpgradeService.class); final UpgradeService upgradeService = beanManager.getReference(UpgradeService.class);
......
...@@ -28,9 +28,9 @@ import org.b3log.latke.logging.Logger; ...@@ -28,9 +28,9 @@ import org.b3log.latke.logging.Logger;
import org.b3log.latke.model.Role; import org.b3log.latke.model.Role;
import org.b3log.latke.model.User; import org.b3log.latke.model.User;
import org.b3log.latke.plugin.PluginManager; import org.b3log.latke.plugin.PluginManager;
import org.b3log.latke.repository.Query;
import org.b3log.latke.repository.RepositoryException; import org.b3log.latke.repository.RepositoryException;
import org.b3log.latke.repository.Transaction; import org.b3log.latke.repository.Transaction;
import org.b3log.latke.repository.jdbc.util.Connections;
import org.b3log.latke.repository.jdbc.util.JdbcRepositories; import org.b3log.latke.repository.jdbc.util.JdbcRepositories;
import org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult; import org.b3log.latke.repository.jdbc.util.JdbcRepositories.CreateTableResult;
import org.b3log.latke.service.LangPropsService; import org.b3log.latke.service.LangPropsService;
...@@ -48,9 +48,6 @@ import org.json.JSONArray; ...@@ -48,9 +48,6 @@ import org.json.JSONArray;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException; import java.text.ParseException;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
...@@ -162,43 +159,40 @@ public class InitService { ...@@ -162,43 +159,40 @@ public class InitService {
return true; return true;
} }
try (final Connection connection = Connections.getConnection()) { try {
final PreparedStatement preparedStatement = connection.prepareStatement("SELECT COUNT(1) AS `c` FROM `" + userRepository.getName() + "`"); inited = !optionRepository.getList(new Query()).isEmpty();
final ResultSet resultSet = preparedStatement.executeQuery(); if (!inited && !printedInitMsg) {
resultSet.next(); LOGGER.log(Level.WARN, "Solo has not been initialized, please open your browser and visit [" + Latkes.getServePath() + "] to init Solo");
final int c = resultSet.getInt("c"); printedInitMsg = true;
inited = 0 < c; }
return inited; return inited;
} catch (final Exception e) { } catch (final Exception e) {
if (!printedInitMsg) { LOGGER.log(Level.ERROR, "Check init failed", e);
LOGGER.log(Level.WARN, "Solo has not been initialized, please open your browser and visit [" + Latkes.getServePath() + "] to init Solo");
}
printedInitMsg = true;
System.exit(-1);
return false; return false;
} }
} }
/** /**
* Initializes Solo. * Initializes database tables.
*
* @param requestJSONObject the specified request json object, for example,
* {
* "userName": "",
* "userEmail": "",
* "userAvatar": "", // optional
* "userB3Key": "", // optional
* "userGitHubId": "" // optional
* }
* @throws ServiceException service exception
*/ */
public void init(final JSONObject requestJSONObject) throws ServiceException { public void initTables() {
if (isInited()) { try {
final String tablePrefix = Latkes.getLocalProperty("jdbc.tablePrefix") + "_";
final boolean userTableExist = JdbcRepositories.existTable(tablePrefix + User.USER);
final boolean optionTableExist = JdbcRepositories.existTable(tablePrefix + Option.OPTION);
if (userTableExist && optionTableExist) {
return; return;
} }
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Check tables failed", e);
LOGGER.log(Level.DEBUG, "Solo is running with database [{0}], creates all tables", Latkes.getRuntimeDatabase()); System.exit(-1);
}
LOGGER.info("It's your first time setup Solo, initialize tables in database [" + Latkes.getRuntimeDatabase() + "]");
if (Latkes.RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) { if (Latkes.RuntimeDatabase.H2 == Latkes.getRuntimeDatabase()) {
String dataDir = Latkes.getLocalProperty("jdbc.URL"); String dataDir = Latkes.getLocalProperty("jdbc.URL");
...@@ -209,9 +203,28 @@ public class InitService { ...@@ -209,9 +203,28 @@ public class InitService {
final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables(); final List<CreateTableResult> createTableResults = JdbcRepositories.initAllTables();
for (final CreateTableResult createTableResult : createTableResults) { for (final CreateTableResult createTableResult : createTableResults) {
LOGGER.log(Level.DEBUG, "Create table result [tableName={0}, isSuccess={1}]", LOGGER.log(Level.DEBUG, "Creates table result [tableName={0}, isSuccess={1}]",
createTableResult.getName(), createTableResult.isSuccess()); createTableResult.getName(), createTableResult.isSuccess());
} }
}
/**
* Initializes Solo.
*
* @param requestJSONObject the specified request json object, for example,
* {
* "userName": "",
* "userEmail": "",
* "userAvatar": "", // optional
* "userB3Key": "", // optional
* "userGitHubId": "" // optional
* }
* @throws ServiceException service exception
*/
public void init(final JSONObject requestJSONObject) throws ServiceException {
if (isInited()) {
return;
}
final Transaction transaction = userRepository.beginTransaction(); final Transaction transaction = userRepository.beginTransaction();
try { try {
...@@ -270,7 +283,7 @@ public class InitService { ...@@ -270,7 +283,7 @@ public class InitService {
final JSONObject comment = new JSONObject(); final JSONObject comment = new JSONObject();
comment.put(Keys.OBJECT_ID, articleId); comment.put(Keys.OBJECT_ID, articleId);
comment.put(Comment.COMMENT_NAME, "Daniel"); comment.put(Comment.COMMENT_NAME, "88250");
comment.put(Comment.COMMENT_EMAIL, "d@b3log.org"); comment.put(Comment.COMMENT_EMAIL, "d@b3log.org");
comment.put(Comment.COMMENT_URL, "https://hacpai.com/member/88250"); comment.put(Comment.COMMENT_URL, "https://hacpai.com/member/88250");
comment.put(Comment.COMMENT_CONTENT, langPropsService.get("helloWorld.comment.content")); comment.put(Comment.COMMENT_CONTENT, langPropsService.get("helloWorld.comment.content"));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment