Spring Boot应用程序:如何在启动时使用Flyway创建数据库模式?

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

Spring Boot Application: how to create shema using flyway on startup?

问题

有一个 Spring Boot 应用程序。
以下是配置:

spring
  flyway:
    locations: classpath:db/migration
    baseline-on-migrate: true
    schemas: ${app.db.schema}
    placeholders:
      schema: ${app.db.schema}
    init-sqls: CREATE SCHEMA IF NOT EXISTS ${app.db.schema}

但是它无法正常工作。

在 Flyway 执行迁移之前,我需要创建数据库模式。

英文:

There is spring boot application.
Here is configuration:

spring
  flyway:
    locations: classpath:db/migration
    baseline-on-migrate: true
    schemas: ${app.db.schema}
    placeholders:
      schema: ${app.db.schema}
    init-sqls: CREATE SCHEMA IF NOT EXISTS ${app.db.schema}

And it doesn't work.

I need to create db schema before flyway will run migrations.

答案1

得分: 1

Flyway默认尝试从classpath:db/migration文件夹中读取数据库迁移脚本。

所有迁移脚本必须遵循特定的命名约定 - V<VERSION_NUMBER>__<NAME>.sql

src/main/resources/db/migration目录中创建一个名为V1__Create_Tables.sql的新文件,并添加SQL脚本,例如:

-- ----------------------------
-- helloservice的模式
-- ----------------------------
CREATE SCHEMA IF NOT EXISTS helloworld;
-- ----------------------------
-- 用户的表结构
-- ----------------------------
CREATE TABLE helloworld.users (
  id                  BIGSERIAL        PRIMARY KEY NOT NULL UNIQUE,
  username            VARCHAR(255)     UNIQUE NOT NULL,
  password            VARCHAR(255)     NOT NULL,
  first_name          VARCHAR(255),
  middle_name         VARCHAR(255),
  last_name           VARCHAR(255),
  email               VARCHAR(255),
  enabled             bool             NOT NULL DEFAULT true,
  account_locked      bool             NOT NULL,
  account_expired     bool             NOT NULL,
  credential_expired  bool             NOT NULL,
  created_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE helloworld.users IS 'User表';

当你运行应用程序时,Flyway会自动检查当前数据库版本并应用任何待处理的迁移。默认情况下,不需要额外的属性。您还可以在此脚本中创建模式。或者,如果指定了一个不存在的模式,Flyway会为您创建它。

如果您正在使用Hibernate,请检查此属性:

spring.jpa.hibernate.ddl-auto=validate

有关更多信息,请参阅说明文档

英文:

Flyway tries to read database migration scripts from classpath:db/migration folder by default.

All the migration scripts must follow a particular naming convention - V<VERSION_NUMBER>__<NAME>.sql.

Create a new file named V1__Create_Tables.sql inside src/main/resources/db/migration directory and add the sql script, for example:

-- ----------------------------
-- Schema for helloservice
-- ----------------------------
CREATE SCHEMA IF NOT EXISTS helloworld;
-- ----------------------------
-- Table structure for user
-- ----------------------------
CREATE TABLE helloworld.users (
  id                  BIGSERIAL        PRIMARY KEY NOT NULL UNIQUE,
  username            VARCHAR(255)     UNIQUE NOT NULL,
  password            VARCHAR(255)     NOT NULL,
  first_name          VARCHAR(255),
  middle_name         VARCHAR(255),
  last_name           VARCHAR(255),
  email               VARCHAR(255),
  enabled             bool             NOT NULL DEFAULT true,
  account_locked      bool             NOT NULL,
  account_expired     bool             NOT NULL,
  credential_expired  bool             NOT NULL,
  created_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_on timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
COMMENT ON TABLE helloworld.users IS 'User table';

When you run the application, flyway will automatically check the current database version and apply any pending migrations. By default, no additional properties are required. You can also create a schema in this script. Or flyway will do it for you if you specify a non-existent scheme.

If you are using hibernate, check this property:

spring.jpa.hibernate.ddl-auto=validate

For more information, see the instructions.

答案2

得分: 0

这里有文档链接: https://flywaydb.org/documentation/commandline/migrate#schemas

实际上,Flyway负责在不存在的情况下创建schemas。

下面是在changelog-history表中的一个示例:

Spring Boot应用程序:如何在启动时使用Flyway创建数据库模式?

英文:

There is documentation link: https://flywaydb.org/documentation/commandline/migrate#schemas

Actually FlyWay is responsible for creating schemas if they don't exist.

Here is an example in changelog-history table:

Spring Boot应用程序:如何在启动时使用Flyway创建数据库模式?

huangapple
  • 本文由 发表于 2020年4月10日 20:50:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/61140620.html
匿名

发表评论

匿名网友

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

确定