WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Boot & Hibernate · Expert · question 70 of 100

How can you handle database schema migrations in a Spring Boot application using tools like Flyway or Liquibase?

📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

When building an application, the database schema can change over time, and it’s important to have a reliable way to manage these changes. Spring Boot provides support for database schema migrations through third-party tools like Flyway or Liquibase.

Flyway and Liquibase are both open-source tools that offer similar functionality for database schema management. They allow developers to define database schema changes as scripts that can be version-controlled, applied, and rolled back as needed.

To use Flyway in a Spring Boot application, you need to include the org.flywaydb:flyway-core dependency in your project, and create a directory db/migration under src/main/resources to store your Flyway migration scripts. Each script must have a filename starting with a version number and ending with .sql extension, such as V1__create_user_table.sql. The version number is used to track which scripts have been applied to the database, and should be incremented for each new script.

Here’s an example Flyway migration script for creating a user table:

    CREATE TABLE user (
                    id BIGINT PRIMARY KEY,
                username VARCHAR(255) NOT NULL,
            password VARCHAR(255) NOT NULL,
        email VARCHAR(255) NOT NULL
    );

To configure Flyway in your Spring Boot application, you can add the following properties to your application.properties or application.yml file:

    spring.flyway.url=jdbc:mysql://localhost:3306/mydb
    spring.flyway.user=myuser
    spring.flyway.password=mypassword
    spring.flyway.locations=classpath:db/migration

This configuration tells Flyway to connect to a MySQL database at localhost:3306/mydb with the username myuser and password mypassword, and to look for migration scripts in the classpath:db/migration directory.

Liquibase is another popular tool for database schema management that can be used in a similar way. To use Liquibase in a Spring Boot application, you need to include the org.liquibase:liquibase-core dependency in your project, and create a db/changelog directory under src/main/resources to store your Liquibase changelog files. Each changelog file describes a set of changes to be applied to the database.

Here’s an example Liquibase changelog file for creating a user table:

<changeSet id="1" author="me">
  <createTable tableName="user">
    <column name="id" type="BIGINT" autoIncrement="true" primaryKey="true"/>
    <column name="username" type="VARCHAR(255)"/>
    <column name="password" type="VARCHAR(255)"/>
    <column name="email" type="VARCHAR(255)"/>
  </createTable>
</changeSet>

To configure Liquibase in your Spring Boot application, you can add the following properties to your application.properties or application.yml file:

    spring.liquibase.url=jdbc:mysql://localhost:3306/mydb
    spring.liquibase.user=myuser
    spring.liquibase.password=mypassword
    spring.liquibase.change-log=classpath:db/changelog/db.changelog.xml

This configuration tells Liquibase to connect to a MySQL database at localhost:3306/mydb with the username myuser and password mypassword, and to use the classpath:db/changelog/db.changelog.xml file as the changelog.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Boot & Hibernate interview — then scores it.
📞 Practice Spring Boot & Hibernate — free 15 min
📕 Buy this interview preparation book: 100 Spring Boot & Hibernate questions & answers — PDF + EPUB for $5

All 100 Spring Boot & Hibernate questions · All topics