You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成Liquibase后仍执行data.sql报错问题咨询

Can Liquibase achieve "run initial data once, skip on subsequent starts" in Spring Boot?

Hey, great question—Liquibase absolutely solves this problem, and it's exactly what it's designed for. Let's walk through how to fix your current issue and get the behavior you want (run initial data once, skip on subsequent starts):

First, fix the root cause: Disable Spring Boot's automatic data.sql execution

The reason your data.sql is still running every time is that Spring Boot's default behavior is to execute data.sql on every startup, regardless of Liquibase being present. You need to turn this off:

  • For Spring Boot 2.5+, add this to your application.properties or application.yaml:
    spring.sql.init.mode=never
    
  • For older Spring Boot versions, use:
    spring.datasource.initialization-mode=never
    

This stops Spring from running your data.sql automatically, leaving Liquibase in full control of database initialization.

Migrate your data.sql logic to a Liquibase ChangeSet

Liquibase tracks every database change in a databasechangelog table (it creates this table automatically on first run). Each change is wrapped in a ChangeSet—a uniquely identified block of logic that only runs once. Here's how to reuse your existing data.sql:

  1. Create a Liquibase master changelog file (default path is src/main/resources/db/changelog/db.changelog-master.yaml—you can use XML, SQL, or JSON too, but YAML is clean for this use case).
  2. Add a ChangeSet that references your data.sql file. Example content:
    databaseChangeLog:
      - changeSet:
          id: "initial-data-setup-1.0"
          author: "your-name" # Replace with your name/team
          changes:
            - sqlFile:
                path: classpath:/data.sql
                relativeToChangelogFile: false
                encoding: UTF-8
    
    • The id and author combination must be unique—this is how Liquibase tracks which ChangeSets have run.
    • path points to your existing data.sql in the classpath.

Key Liquibase configurations to verify

  • Dependency: Make sure you have the Spring Boot Liquibase starter in your build file:
    • For Maven (pom.xml):
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-liquibase</artifactId>
      </dependency>
      
    • For Gradle (build.gradle):
      implementation 'org.springframework.boot:spring-boot-starter-liquibase'
      
  • Changelog path: If you placed your master changelog in a non-default location, configure it:
    spring.liquibase.change-log=classpath:/custom/path/db.changelog-master.yaml
    
  • Preconditions (optional but safe): If you want extra safeguards (e.g., only run the SQL if a table exists, or if data isn't already present), add preconditions to your ChangeSet:
    databaseChangeLog:
      - changeSet:
          id: "initial-data-setup-1.0"
          author: "your-name"
          preConditions:
            - not:
                - tableExists:
                    tableName: your_table_name
          changes:
            - sqlFile:
                path: classpath:/data.sql
                relativeToChangelogFile: false
    
    This ensures the ChangeSet only runs if the target table doesn't exist (adjust based on your needs).

Handling existing data (if you already ran data.sql manually)

If you previously ran data.sql via Spring Boot, Liquibase will try to run it again on first startup, causing duplicate data errors. To fix this:

  • Either manually insert a record into the databasechangelog table for your ChangeSet (matching the id and author you set), or
  • Use the Liquibase CLI command liquibase markNextChangeSetRan to mark the ChangeSet as executed.

Final Result

Once set up, here's what happens:

  1. On first startup: Liquibase creates its tracking tables, runs your data.sql via the ChangeSet, and logs the execution in databasechangelog.
  2. On subsequent startups: Liquibase checks databasechangelog, sees the ChangeSet already ran, and skips it entirely—no more duplicate data errors!

内容的提问来源于stack exchange,提问作者Imran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:33:22