diff --git a/src/main/java/com/petshop/backend/entity/User.java b/src/main/java/com/petshop/backend/entity/User.java index 7a2cf43a..54bd202e 100644 --- a/src/main/java/com/petshop/backend/entity/User.java +++ b/src/main/java/com/petshop/backend/entity/User.java @@ -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 + '}'; diff --git a/src/main/resources/db/migration/V5__user_token_version.sql b/src/main/resources/db/migration/V5__user_token_version.sql new file mode 100644 index 00000000..455c4bc8 --- /dev/null +++ b/src/main/resources/db/migration/V5__user_token_version.sql @@ -0,0 +1,2 @@ +ALTER TABLE users + ADD COLUMN tokenVersion INT NOT NULL DEFAULT 0 AFTER active; diff --git a/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java b/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java index 6c1c4a85..58af8c4a 100644 --- a/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java +++ b/src/test/java/com/petshop/backend/config/RunConfigValidationTest.java @@ -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."); + } } diff --git a/src/test/java/com/petshop/backend/entity/UserEntityTest.java b/src/test/java/com/petshop/backend/entity/UserEntityTest.java new file mode 100644 index 00000000..57330bc8 --- /dev/null +++ b/src/test/java/com/petshop/backend/entity/UserEntityTest.java @@ -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()); + } +}