Database Migration with Spring JPA

Viewed 52

So I've read tons and tons of posts, forums, question and answers about this topic but I am still quite unsure about what migration means.

Let me first introduce you to my problem.

I have a Spring Boot backend working with MySQL Database with the help of Spring JPA Entities.

Now my problem is the following:

I already have a Spring JPA Entity defined as follows:

@Entity
@Getter
@Setter
public class Blog {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    private String content;
    private LocalDateTime createdAt = LocalDateTime.now();
}

After having this entity persisted to the database, I realized that the simple String title wouldn't be enough as it maps to a varchar(255) making my Blog posts couldn't exceed 255 characters.

So I would like to change this in the database without running any custom built SQL scripts to modify the column type in the table.

I have something in mind like I would modify the current JPA Entity as follows:

@Entity
@Getter
@Setter
public class Blog {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    @Lob // <--- Notice the Lob here
    private String content;
    private LocalDateTime createdAt = LocalDateTime.now();
}

If I am right, the Large Object annotation would mean that in the Database the column type becomes longtext allowing a lot more characters than 255 so my blog posts would fit.

So in short this is how I stumbled upon the topic of Database Migration I am just unsure if that is the thing I need in this situation. Can you please confirm if I am right about this, also can you please suggest possible solutions for this particular problem? I know there is a tool called Liquibase used for migrations which is quite popular. Should I go for it, or is there somthing else I would need?

Thanks in advance for the help.

0 Answers
Related