【发布时间】:2021-06-26 21:14:35
【问题描述】:
我正在尝试使用 jOOQ 作为模式生成器来生成 DDL 语句,然后生成 SQL 插入语句。
底层数据来自静态 CSV 文件,我基本上想编写一个静态 SQL 脚本,其中包含创建模式和插入数据的语句;只是一个转储文件。
例如,我有一个包含表定义的 Groovy 类:
class ContinentTable extends CustomTable<Record> {
static ContinentTable CONTINENT = new ContinentTable()
static UniqueKey<Record> CONTINENT_PK = createUniqueKey(CONTINENT, name("continent_pk"), [CONTINENT.ID] as TableField[], true)
TableField<Record, String> ID = createField(name("id"), VARCHAR(255).nullable(false))
TableField<Record, String> CODE = createField(name("code"), VARCHAR(2).nullable(false))
TableField<Record, String> NAME = createField(name("name"), VARCHAR(255).nullable(false))
TableField<Record, String> DEMONYM = createField(name("demonym"), VARCHAR(255).nullable(false))
private ContinentTable() {
super(name("continent"))
}
@Override
Class<? extends Record> getRecordType() {
return Record
}
@Override
UniqueKey<Record> getPrimaryKey() {
return CONTINENT_PK
}
}
它包含字段和PK,但是当我使用jOOQ创建SQL语句时:
dsl.createTable(ContinentTable.CONTINENT)
它只生成:
create table "continent";
没有列。 当然,我可以这样做:
dsl.createTable(ContinentTable.CONTINENT).columns(ContinentTable.CONTINENT.fields())
不过好像有点没必要,因为我已经通过表格了??
如果我想添加主键和可能的外键、索引等,它会变得更加复杂。
调用dsl.createTable(ContinentTable.CONTINENT)时这些都没有添加是预期的行为吗?
注意:我正在手动创建 ContinentTable,因为我没有使用 codegen,因为我没有源数据库。
【问题讨论】:
-
我个人不喜欢这种方法。为什么不使用 Flyway 并从 DDL 语句生成 jOOQ 模型?
-
问题是我没有实际的数据库。只想生成一个带有创建表语句和插入的dump.sql。
-
但确实从 DDL 生成 Jooq 类也是我用例的一个选项:jooq.org/doc/latest/manual/code-generation/codegen-ddl
-
这是我在新项目中的默认用例。我总是使用 Flyway(你也可以使用 Liquibase)进行数据库迁移。
-
@SimonMartinelli:即使您使用的是 Flyway,您也可以通过 jOOQ API 的这一部分来使用它的程序化迁移。这些想法是互补的,而不是相互竞争的。