fix migrations

This commit is contained in:
EgorAnti
2026-10-20 14:27:59 +03:00
parent 05fcccb9ed
commit 7565b65997
9 changed files with 43 additions and 9 deletions
+8 -4
View File
@@ -9,7 +9,8 @@ COPY build.gradle settings.gradle ./
COPY src src
RUN chmod +x gradlew && ./gradlew bootJar --no-daemon
RUN --mount=type=cache,target=/root/.gradle \
chmod +x gradlew && ./gradlew bootJar --no-daemon
# Stage 2: Runtime
@@ -19,13 +20,16 @@ WORKDIR /app
COPY --from=build /app/build/libs/*.jar app.jar
RUN useradd --system --create-home messenger
RUN apt-get update \
&& apt-get install --yes --no-install-recommends curl \
&& rm -rf /var/lib/apt/lists/* \
&& useradd --system --create-home messenger
USER messenger
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD wget --spider --quiet http://localhost:8080/health/live || exit 1
CMD curl --fail --silent --show-error http://localhost:8080/health/live || exit 1
ENTRYPOINT ["java", "-jar", "app.jar"]
ENTRYPOINT ["java", "-jar", "app.jar"]
+4
View File
@@ -22,5 +22,9 @@ load:
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
test-persistence:
ifeq ($(OS),Windows_NT)
@if "$(ACTION_SCRIPT)"=="" (echo Set ACTION_SCRIPT to your implemented executable restart hook & exit /b 2)
else
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
endif
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
+1
View File
@@ -23,6 +23,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-webmvc'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
implementation 'org.springframework.boot:spring-boot-flyway'
implementation 'org.flywaydb:flyway-core'
implementation 'org.flywaydb:flyway-database-postgresql'
implementation 'org.springframework.boot:spring-boot-starter-security'
+6
View File
@@ -0,0 +1,6 @@
#!/bin/sh
set -eu
# Restart every stateful component used by the application without deleting the
# named PostgreSQL volume. The persistence test waits for readiness afterwards.
docker compose --env-file .env restart postgres api
@@ -14,6 +14,7 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
import java.util.UUID;
@@ -40,7 +41,7 @@ public class AuthenticationService {
public IssuedSession issueSession(User user) {
String token = newToken();
Instant createdAt = Instant.now();
Instant createdAt = Instant.now().truncatedTo(ChronoUnit.MICROS);
Instant expiresAt = createdAt.plusSeconds(ttlSeconds);
sessions.save(new Session(
UUID.randomUUID(),
@@ -6,6 +6,7 @@ import com.example.mtusi_messenger.exception.ApiExceptions;
import com.example.mtusi_messenger.model.Chat;
import com.example.mtusi_messenger.repository.ChatRepository;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
@Service
@@ -16,7 +17,8 @@ public class ChatService {
Set<UUID> ids = new LinkedHashSet<>(r.member_ids());
if (ids.size() != r.member_ids().size() || !ids.contains(caller)) throw ApiExceptions.invalid("Invalid member_ids");
for (UUID id : ids) if (id == null || !users.exists(id)) throw ApiExceptions.invalid("Invalid member_ids");
return chats.save(new Chat(UUID.randomUUID(), r.title(), List.copyOf(ids), Instant.now()));
return chats.save(new Chat(UUID.randomUUID(), r.title(), List.copyOf(ids),
Instant.now().truncatedTo(ChronoUnit.MICROS)));
}
public Chat getAccessible(UUID chatId, UUID userId) {
users.get(userId);
@@ -6,6 +6,7 @@ import com.example.mtusi_messenger.dto.CreateMessageRequest;
import com.example.mtusi_messenger.model.Message;
import com.example.mtusi_messenger.repository.MessageRepository;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.UUID;
@@ -16,7 +17,8 @@ public class MessageService {
public Message create(UUID chatId, UUID senderId, CreateMessageRequest r) {
chats.getAccessible(chatId, senderId);
String text = r.text();
return messages.save(new Message(UuidCreator.getTimeOrderedEpochPlus1(), chatId, senderId, text, Instant.now()));
return messages.save(new Message(UuidCreator.getTimeOrderedEpochPlus1(), chatId, senderId, text,
Instant.now().truncatedTo(ChronoUnit.MICROS)));
}
public List<Message> page(UUID chatId, UUID userId, int limit, String cursor) {
chats.getAccessible(chatId, userId);
@@ -8,6 +8,7 @@ import com.example.mtusi_messenger.exception.ApiExceptions;
import com.example.mtusi_messenger.model.User;
import com.example.mtusi_messenger.repository.UserRepository;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.UUID;
@Service
@@ -23,7 +24,8 @@ public class UserService {
if (users.findByUsername(username).isPresent()) throw ApiExceptions.conflict("Username is already taken");
String passwordHash = passwordEncoder.encode(r.password());
try {
return users.save(new User(UUID.randomUUID(), username, display, Instant.now()), passwordHash);
return users.save(new User(UUID.randomUUID(), username, display,
Instant.now().truncatedTo(ChronoUnit.MICROS)), passwordHash);
} catch (DuplicateKeyException e) {
throw ApiExceptions.conflict("Username is already taken");
}
+13 -1
View File
@@ -3,6 +3,7 @@
import json
import os
from pathlib import Path
import shutil
import subprocess
import time
import uuid
@@ -55,7 +56,18 @@ def main():
check(len(observed)>=2,'less than two API instances observed before failure; verify LB/session affinity')
print('Executing explicit '+args.mode+' hook; hook output suppressed to avoid leaking secrets.')
# No shell interpolation and no Docker commands in this test.
proc = subprocess.Popen([str(script)],env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
command = [str(script)]
if os.name == 'nt' and script.suffix.lower() == '.sh':
interpreter = shutil.which('sh')
if interpreter is None:
git = shutil.which('git')
if git is not None:
candidate = Path(git).resolve().parents[1] / 'bin' / 'bash.exe'
if candidate.is_file():
interpreter = str(candidate)
check(interpreter is not None,'sh interpreter is required to run the action script on Windows')
command = [interpreter,str(script)]
proc = subprocess.Popen(command,env=env,stdout=subprocess.DEVNULL,stderr=subprocess.DEVNULL)
started = time.monotonic()
successes,failures,after_instances = [],0,[]
try: