Grails 3.3.x 似乎不支持 PostgreSQL 的每租户模式

Grails 3.3.x does not seem to support schema-per-tenant with PostgreSQL

有没有人设法将 Grails 3.3.x 用于多租户和 PostgreSQL?问题似乎出在 DefaultSchemaHandler.groovy class 中使用的 SQL 语法。对于 PostgreSQL,正确的语法是:SET SCHEMA 'schemaName' 但 DefaultSchemaHandler 省略了单个勾号:SET SCHEMA schemaName,这会导致启动失败:

Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near "information_schema"

进一步的例外列出了 PG DB 中的其他模式,直到启动完全失败。似乎没有任何方法可以覆盖 DefaultSchemaHandler,其他人也没有解决这个问题,这让我感到惊讶。

我应该补充一点,这在我正忙于移植的 Grails 3.2.11 中一切正常。

我通过如下实现自己的 SchemaHandler 解决了我自己的问题:

@CompileStatic
@Slf4j
class PostgreSQLSchemaHandler extends DefaultSchemaHandler {

    PostgreSQLSchemaHandler() {
        super("SET SCHEMA '%s'", "CREATE SCHEMA '%s'", "public")
    }
}

然后在application.yml中我添加了schemaHandler如下:

dataSource:
    pooled: true
    jmxExport: true
    driverClassName: org.postgresql.Driver
    dialect: org.hibernate.dialect.PostgreSQLDialect
    schemaHandler: com.mypackage.PostgreSQLSchemaHandler