Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce user migration feature #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,12 @@
<artifactId>hutool-all</artifactId>
<version>5.8.18</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
</dependencies>

<build>
<plugins>
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/dashboardmanager/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.dashboardmanager.repository.UsersRepository;
import com.dashboardmanager.utils.FileUtils;
import com.dashboardmanager.utils.SessionHeader;
import com.mysql.cj.jdbc.ConnectionImpl;
import jakarta.persistence.EntityManager;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.RandomStringUtils;
Expand All @@ -25,6 +27,10 @@

import java.io.*;
import java.security.SecureRandom;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Savepoint;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.commons.codec.binary.Base64.decodeBase64;

Expand All @@ -37,6 +43,9 @@ public class UserController {
@Autowired
private SessionsRepository sessionsRepository;

@Autowired
private EntityManager entityManager;

private LRUFileCache fileCache = new LRUFileCache(100);

@GetMapping("/")
Expand Down Expand Up @@ -103,6 +112,42 @@ public ResponseEntity<Resource> getUserImage(ServletWebRequest request, @Request
}
}

@PostMapping("/user/migrate")
public ResponseEntity<Resource> migrateUser(@RequestParam(value = "username") String username) {
try (var connection = getConnection()) {
doMigrateUser(username, connection);
return ResponseEntity.status(HttpStatus.OK).build();
} catch (SQLException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
}

private void doMigrateUser(String username, ConnectionImpl connection) throws SQLException {
// migration might fail midway, set a savepoint to keep the database consistent
Savepoint savepoint = connection.setSavepoint("before-" + username + "-migration");

User newUser = new User();
newUser.setName("_placeholder_" + username);
newUser.setPassword("_placeholder_");
usersRepository.save(newUser);

User user = usersRepository.findByName(username);
if (user == null) {
connection.rollback(savepoint);
return;
}

newUser.setName(user.getName());
newUser.setPassword("oldpw:" + user.getPassword());
usersRepository.save(newUser);
}

private ConnectionImpl getConnection() {
AtomicReference<ConnectionImpl> impl = new AtomicReference<>();
var session = entityManager.unwrap(org.hibernate.Session.class);
session.doWork(connection -> impl.set((ConnectionImpl) connection.unwrap(Connection.class)));
return impl.get();
}

private String createSessionHeader(Session session) {
SessionHeader sessionHeader = new SessionHeader(session.getUser().getName(), session.getSessionId());
Expand Down
16 changes: 5 additions & 11 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=

#spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
#spring.jpa.defer-datasource-initialization=true

#spring.sql.init.mode=always
spring.datasource.url=jdbc:mysql://localhost:3306/test?allowMultiQueries=true
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root

spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
spring.jpa.hibernate.ddl-auto= update

spring.h2.console.enabled=true