如何在运行中的Spring Boot应用程序中重新加载初始数据?

huangapple go评论72阅读模式
英文:

How to reload Initial Data in running Spring Boot applicaiton?

问题

我在我的Web应用程序中使用了spring-boot-starter-data-jdbc依赖项。Spring Boot可以在应用程序启动时自动创建模式并从根类路径位置schema.sqldata.sql初始化它,但我很好奇是否有办法在应用程序已经启动后使用相同的SQL脚本重新初始化数据库?

我需要这个功能来进行演示模式,这样用户就可以在与表格玩耍足够长时间后将数据库重置为初始状态。这就是我希望我的重置控制器看起来像的方式:

@Controller
@RequestMapping("/reset")
@Profile("demo")
public class ResetController {

    @GetMapping
    public String resetTables(HttpSession session) {
        // 一些代码重新初始化数据库
        // 来自schema.sql和data.sql的表单在这里
        session.invalidate();
        return "redirect:/home";
    }
}

我知道,我总是可以使用JdbcTemplate及其方法手动删除、创建和重新填充每个表,按照schema.sqldata.sql中定义的SQL语句的逻辑,但这可能有点繁琐。也许Spring有一些针对数据库执行这些脚本的开箱即用方法,可以帮助重新加载具有初始演示数据的表格?

更新:

这是基于接受答案中的建议,基于Flyway迁移的可能解决方案之一:

  1. flyway-core依赖项添加到项目中。
  2. schema.sqldata.sql重命名为V1__schema.sqlV2__data.sql,遵循Flyway的命名要求,并将它们放在/resources/db/migration目录下。
  3. spring.flyway.baseline-on-migrate属性设置为true
  4. 然后,可以重写上述所述的重置控制器,如下所示:
@Controller
@RequestMapping("/reset")
@Profile("demo")
public class ResetController {

    private final Flyway flyway;

    public ResetController(Flyway flyway) {
        this.flyway = flyway;
    }

    @GetMapping
    public String resetTables(HttpSession session) {
        flyway.clean();
        flyway.migrate();
        session.invalidate();
        return "redirect:/home";
    }
}

此外,对于不同的迁移场景,可以设置spring.flyway.locations属性引用不同的SQL文件 - 分别为每个配置文件,甚至通过将spring.flyway.enabled属性设置为false来禁用某些配置文件的Flyway迁移。

英文:

I'm using spring-boot-starter-data-jdbc dependency in my web-application. Spring Boot can automatically create the schema and initialize it from the root classpath locations schema.sql and data.sql on application startup, but I'm curious is there any way to re-initialize the database using very same sql-scripts after the application has already started?

I need this feature for demo mode, so that users can reset the database to initial state after they played around enough with the tables. This is how I would like my reset-controller to look like:

@Controller
@RequestMapping("/reset")
@Profile("demo")
public class ResetController {

    @GetMapping
    public String resetTables(HttpSession session) {
        // some code re-initializing the database 
		// form schema.sql and data.sql goes here
		session.invalidate();
        return "redirect:/home";
    }
}

I know, I can always use JdbcTemplate and its methods to drop, create and re-populate each table manually, following the logic of sql statements defined in schema.sql and data.sql, but that would be a little tedious. Maybe, Spring has some out-of-the-box method for executing those scripts against the database which would help to reload the tables with initial demonstration data?

Updated:

This is one of the possible solutions based on the Flyway migration, as suggested by Charles B in the accepted answer:

  1. Add flyway-core dependency to the project.
  2. Rename schema.sql and data.sql into V1__schema.sql and V2__data.sql following Flyway naming requirements and put them under /resources/db/migration directory.
  3. Set spring.flyway.baseline-on-migrate property to true.
  4. Then, the reset-controller described above could be rewritten, as simple as this:
@Controller
@RequestMapping("/reset")
@Profile("demo")
public class ResetController {

    private final Flyway flyway;

    public ResetController(Flyway flyway) {
        this.flyway = flyway;
    }

    @GetMapping
    public String resetTables(HttpSession session) {
        flyway.clean();
        flyway.migrate();
        session.invalidate();
        return "redirect:/home";
    }
	
}

Additionally, for different migration scenarios, it's possible to set spring.flyway.locations property referencing different SQL files - separately for each profile, or even disable Flyway migration for certain profiles by setting spring.flyway.enabled property to false.

答案1

得分: 2

Spring Boot使数据库变更变得非常容易以一种简单的方式。
我们可以在Spring中使用data.sql和schema.sql文件。

如果我们运行我们的应用程序,Spring Boot将为我们创建一个空表,但不会填充任何内容。

更多详情请查看这里-> https://www.baeldung.com/spring-boot-data-sql-and-schema-sql

英文:

Spring Boot makes it really easy to manage our database changes in an easy way.
We can use the data.sql and schema.sql files in Spring.

If we run our application, Spring Boot will create an empty table for us, but won't populate it with anything.

More details here -> https://www.baeldung.com/spring-boot-data-sql-and-schema-sql

答案2

得分: 1

If you migrate to something like Flyway, you could build a controller that calls flyway's clean method like referenced here. Would be easy to migrate and maintain long term.

英文:

If you migrate to something like Flyway, you could build a controller that calls flyway's clean method like referenced here. Would be easy to migrate and maintain long term.

答案3

得分: 0

你可以尝试使用Spring Boot Actuator端点来重新启动整个应用程序。这应该会重新初始化你的ORM并重置数据库的初始状态。

依赖项:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

配置:

management:
  endpoints:
    web:
      exposure:
        include: restart
  endpoint:
    restart:
      enabled: true

然后你应该能够访问/actuator/restart端点。

http://localhost:8080/actuator/restart
英文:

You can try using Spring Boot Actuator endpoints to restart your entire application. That should reinitialize your ORM and reset the DB initial state.

Dependency:

&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
&lt;/dependency&gt;

Configuration:

management:
  endpoints:
    web:
      exposure:
        include: restart
  endpoint:
    restart:
      enabled: true

And you should be able to hit the /actuator/restart endpoint.

http://localhost:8080/actuator/restart

huangapple
  • 本文由 发表于 2020年8月14日 02:02:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63400825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定