Add token version

This commit is contained in:
2026-03-12 17:36:16 -06:00
parent 389b5685e3
commit 52889b90b3
4 changed files with 55 additions and 1 deletions

View File

@@ -37,6 +37,9 @@ public class User {
@Column(nullable = false)
private Boolean active = true;
@Column(nullable = false)
private Integer tokenVersion = 0;
@CreationTimestamp
@Column(name = "created_at", updatable = false)
private LocalDateTime createdAt;
@@ -52,7 +55,7 @@ public class User {
public User() {
}
public User(Long id, String username, String password, String email, String fullName, String avatarUrl, Role role, Boolean active, LocalDateTime createdAt, LocalDateTime updatedAt) {
public User(Long id, String username, String password, String email, String fullName, String avatarUrl, Role role, Boolean active, Integer tokenVersion, LocalDateTime createdAt, LocalDateTime updatedAt) {
this.id = id;
this.username = username;
this.password = password;
@@ -61,6 +64,7 @@ public class User {
this.avatarUrl = avatarUrl;
this.role = role;
this.active = active;
this.tokenVersion = tokenVersion;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
@@ -129,6 +133,14 @@ public class User {
this.active = active;
}
public Integer getTokenVersion() {
return tokenVersion;
}
public void setTokenVersion(Integer tokenVersion) {
this.tokenVersion = tokenVersion;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
@@ -169,6 +181,7 @@ public class User {
", avatarUrl='" + avatarUrl + '\'' +
", role=" + role +
", active=" + active +
", tokenVersion=" + tokenVersion +
", createdAt=" + createdAt +
", updatedAt=" + updatedAt +
'}';

View File

@@ -0,0 +1,2 @@
ALTER TABLE users
ADD COLUMN tokenVersion INT NOT NULL DEFAULT 0 AFTER active;

View File

@@ -77,4 +77,19 @@ class RunConfigValidationTest {
assertTrue(migrationContent.contains("active BOOLEAN") || migrationContent.contains("active boolean"),
"Baseline migration should contain the users.active column definition.");
}
@Test
void userTokenVersionMigrationExists() {
File migrationFile = new File("src/main/resources/db/migration/V5__user_token_version.sql");
assertTrue(migrationFile.exists(), "User tokenVersion migration should exist.");
}
@Test
void userTokenVersionMigrationAddsColumn() throws Exception {
File migrationFile = new File("src/main/resources/db/migration/V5__user_token_version.sql");
String migrationContent = new String(java.nio.file.Files.readAllBytes(migrationFile.toPath()));
assertTrue(migrationContent.contains("ADD COLUMN tokenVersion INT NOT NULL DEFAULT 0"),
"User tokenVersion migration should add the tokenVersion column with default 0.");
}
}

View File

@@ -0,0 +1,24 @@
package com.petshop.backend.entity;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class UserEntityTest {
@Test
void tokenVersionDefaultsToZero() {
User user = new User();
assertEquals(0, user.getTokenVersion());
}
@Test
void tokenVersionCanBeUpdated() {
User user = new User();
user.setTokenVersion(3);
assertEquals(3, user.getTokenVersion());
}
}