如果两个模式相同,可以使用 jooq 进行比较吗?
Can jooq be used to compare if two schemas are identical?
如果两个模式相同,可以使用 jooq 进行比较吗?不是数据而是表、约束、权限等是否相同?
您可以比较 jOOQ API 中表示的所有内容,包括:
- 目录(SQL 仅限服务器)
- 架构
- 表格
- 列及其类型
- 用户定义类型
- 序列
- 身份
- 约束(包括主键、唯一键、外键和检查约束)
不支持的内容(目前在 jOOQ 3.9 中)
- 索引(在路线图上:https://github.com/jOOQ/jOOQ/issues/5638)
- 授予/权限(不在路线图上)
- 程序(尚无可比性)
示例算法
...比较 H2 PUBLIC
模式的两个版本:
assertEquals(v1.Public.PUBLIC, v2.Public.PUBLIC);
List<Table<?>> tables1 = v1.Public.PUBLIC.getTables();
List<Table<?>> tables2 = v2.Public.PUBLIC.getTables();
assertEquals(tables1, tables2);
for (int i = 0; i < tables1.size(); i++) {
Table<?> table1 = tables1.get(i);
Table<?> table2 = tables2.get(i);
assertArrayEquals(format("Fields differ for %s and %s", table1, table2),
table1.fields(), table2.fields());
assertEquals(format("Primary keys differ for %s and %s", table1, table2),
table1.getPrimaryKey(), table2.getPrimaryKey());
assertEquals(format("Schemas differ for %s and %s", table1, table2),
table1.getSchema(), table2.getSchema());
assertEquals(format("Identities differ for %s and %s", table1, table2),
table1.getIdentity(), table2.getIdentity());
assertEquals(format("Keys differ for %s and %s", table1, table2),
table1.getKeys(), table2.getKeys());
assertEquals(format("References differ for %s and %s", table1, table2),
table1.getReferences(), table2.getReferences());
}
如果您想比较同一内容的两个不同架构
它会变得有点棘手,因为 jOOQ QueryPart.equals()
实现通常基于:
toString()
比较
- 合格的名称比较
由于两个不同的模式会有不同的限定名称,您必须调整上述算法并比较每个对象的 getName()
结果,而不是整个对象。
比较XML内容
也许这对您来说更容易...jOOQ 3.9 引入了 DSLContext.informationSchema()
methods with #5460。这些方法 return JAXB 注释 类,可以编组为 XML 字符串:
StringWriter writer = new StringWriter();
JAXB.marshal(ctx.informationSchema(mySchema), writer);
System.out.println(writer.toString());
您可以使用任何 XML 工具比较这两个版本,例如使用 XSLT
如果你只是想手动比较东西
如果您有两个版本的生成代码,您可以简单地在两个目录之间创建一个差异,或者使用您的 VCS 向您显示差异;-)也许这就足够了。
如果两个模式相同,可以使用 jooq 进行比较吗?不是数据而是表、约束、权限等是否相同?
您可以比较 jOOQ API 中表示的所有内容,包括:
- 目录(SQL 仅限服务器)
- 架构
- 表格
- 列及其类型
- 用户定义类型
- 序列
- 身份
- 约束(包括主键、唯一键、外键和检查约束)
不支持的内容(目前在 jOOQ 3.9 中)
- 索引(在路线图上:https://github.com/jOOQ/jOOQ/issues/5638)
- 授予/权限(不在路线图上)
- 程序(尚无可比性)
示例算法
...比较 H2 PUBLIC
模式的两个版本:
assertEquals(v1.Public.PUBLIC, v2.Public.PUBLIC);
List<Table<?>> tables1 = v1.Public.PUBLIC.getTables();
List<Table<?>> tables2 = v2.Public.PUBLIC.getTables();
assertEquals(tables1, tables2);
for (int i = 0; i < tables1.size(); i++) {
Table<?> table1 = tables1.get(i);
Table<?> table2 = tables2.get(i);
assertArrayEquals(format("Fields differ for %s and %s", table1, table2),
table1.fields(), table2.fields());
assertEquals(format("Primary keys differ for %s and %s", table1, table2),
table1.getPrimaryKey(), table2.getPrimaryKey());
assertEquals(format("Schemas differ for %s and %s", table1, table2),
table1.getSchema(), table2.getSchema());
assertEquals(format("Identities differ for %s and %s", table1, table2),
table1.getIdentity(), table2.getIdentity());
assertEquals(format("Keys differ for %s and %s", table1, table2),
table1.getKeys(), table2.getKeys());
assertEquals(format("References differ for %s and %s", table1, table2),
table1.getReferences(), table2.getReferences());
}
如果您想比较同一内容的两个不同架构
它会变得有点棘手,因为 jOOQ QueryPart.equals()
实现通常基于:
toString()
比较- 合格的名称比较
由于两个不同的模式会有不同的限定名称,您必须调整上述算法并比较每个对象的 getName()
结果,而不是整个对象。
比较XML内容
也许这对您来说更容易...jOOQ 3.9 引入了 DSLContext.informationSchema()
methods with #5460。这些方法 return JAXB 注释 类,可以编组为 XML 字符串:
StringWriter writer = new StringWriter();
JAXB.marshal(ctx.informationSchema(mySchema), writer);
System.out.println(writer.toString());
您可以使用任何 XML 工具比较这两个版本,例如使用 XSLT
如果你只是想手动比较东西
如果您有两个版本的生成代码,您可以简单地在两个目录之间创建一个差异,或者使用您的 VCS 向您显示差异;-)也许这就足够了。