From c26ee4f4005e5ecd1cbfd91c02d89264b2c324d6 Mon Sep 17 00:00:00 2001 From: almazlar Date: Sun, 22 Feb 2026 15:03:11 +0300 Subject: [PATCH] feat: add createdAt field to Todo entity and update database migration script --- backend/pom.xml | 36 ++++++++++++------- .../java/com/todo/backend/model/Todo.java | 10 ++++++ .../src/main/resources/application.properties | 10 ++++-- .../main/resources/db/migration/V1__init.sql | 7 ++++ 4 files changed, 48 insertions(+), 15 deletions(-) create mode 100644 backend/src/main/resources/db/migration/V1__init.sql diff --git a/backend/pom.xml b/backend/pom.xml index c074245..c557325 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -1,30 +1,31 @@ - 4.0.0 org.springframework.boot spring-boot-starter-parent 4.0.3 - + com.example backend 0.0.1-SNAPSHOT backend Demo project for Spring Boot - + - + - + - - - - + + + + 25 @@ -68,9 +69,20 @@ org.springframework.boot spring-boot-starter-actuator - + + + org.springframework.boot + spring-boot-starter-flyway + + + org.flywaydb + flyway-core + + + org.flywaydb + flyway-database-postgresql + - @@ -80,4 +92,4 @@ - + \ No newline at end of file diff --git a/backend/src/main/java/com/todo/backend/model/Todo.java b/backend/src/main/java/com/todo/backend/model/Todo.java index 3f02421..fad56ee 100644 --- a/backend/src/main/java/com/todo/backend/model/Todo.java +++ b/backend/src/main/java/com/todo/backend/model/Todo.java @@ -1,5 +1,7 @@ package com.todo.backend.model; +import java.time.LocalDateTime; + import jakarta.persistence.*; @Entity @@ -19,6 +21,9 @@ public class Todo { @Column(nullable = false) private boolean completed = false; + @Column(nullable = false, updatable = false) + private LocalDateTime createdAt = LocalDateTime.now(); + // Default constructor is required by JPA public Todo() { } @@ -62,4 +67,9 @@ public class Todo { public void setCompleted(boolean completed) { this.completed = completed; } + + public LocalDateTime getCreatedAt() { + return createdAt; + } + } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 3ccaa72..75a05bd 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1,10 +1,14 @@ -# backend/src/main/resources/application.properties - server.port=8082 server.servlet.context-path=/api spring.application.name=backend -spring.jpa.hibernate.ddl-auto=update +# Flyway +spring.flyway.enabled=true +spring.flyway.locations=classpath:db/migration +spring.flyway.baseline-on-migrate=true + +# Database +spring.jpa.hibernate.ddl-auto=none spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect management.endpoints.web.exposure.include=health,info,metrics diff --git a/backend/src/main/resources/db/migration/V1__init.sql b/backend/src/main/resources/db/migration/V1__init.sql new file mode 100644 index 0000000..aa54bc4 --- /dev/null +++ b/backend/src/main/resources/db/migration/V1__init.sql @@ -0,0 +1,7 @@ +CREATE TABLE todos ( + id BIGSERIAL PRIMARY KEY, + title VARCHAR(255) NOT NULL, + description TEXT, + completed BOOLEAN NOT NULL DEFAULT FALSE, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW() +); \ No newline at end of file