Unverified Commit c64ac69e authored by Zzm0809's avatar Zzm0809 Committed by GitHub

[Fix] Dealing with the issue of Postgre database being unable to execute Flyway (#3506)

parent 017d7634
......@@ -40,7 +40,8 @@ public class TenantContextHolder {
}
public static Object get() {
return TENANT_CONTEXT.get();
return Optional.ofNullable(TENANT_CONTEXT.get())
.orElseThrow(() -> new IllegalStateException("current Tenant context is not set"));
}
public static void clear() {
......
/*
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.flywaydb.core.internal.database.postgresql;
import java.sql.Connection;
import java.sql.SQLException;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.database.base.Database;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.jdbc.JdbcConnectionFactory;
import org.flywaydb.core.internal.jdbc.StatementInterceptor;
import org.flywaydb.core.internal.util.StringUtils;
public class PostgreSQLDatabase extends Database<PostgreSQLConnection> {
public PostgreSQLDatabase(
Configuration configuration,
JdbcConnectionFactory jdbcConnectionFactory,
StatementInterceptor statementInterceptor) {
super(configuration, jdbcConnectionFactory, statementInterceptor);
}
@Override
protected PostgreSQLConnection doGetConnection(Connection connection) {
return new PostgreSQLConnection(this, connection);
}
@Override
public String getInsertStatement(Table table) {
return "INSERT INTO " + table
+ " (" + quote("installed_rank")
+ ", " + quote("version")
+ ", " + quote("description")
+ ", " + quote("type")
+ ", " + quote("script")
+ ", " + quote("checksum")
+ ", " + quote("installed_by")
+ ", " + quote("execution_time")
+ ", " + quote("success")
+ ")"
+ " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
}
@Override
public void ensureSupported() {
ensureDatabaseIsRecentEnough("9.0");
ensureDatabaseNotOlderThanOtherwiseRecommendUpgradeToFlywayEdition(
"10", org.flywaydb.core.internal.license.Edition.ENTERPRISE);
recommendFlywayUpgradeIfNecessaryForMajorVersion("15");
}
@Override
public String getRawCreateScript(Table table, boolean baseline) {
String tablespace =
configuration.getTablespace() == null ? "" : " TABLESPACE \"" + configuration.getTablespace() + "\"";
return "CREATE TABLE " + table + " (\n" + " \"installed_rank\" INT NOT NULL,\n"
+ " \"version\" VARCHAR(50),\n"
+ " \"description\" VARCHAR(200) NOT NULL,\n"
+ " \"type\" VARCHAR(20) NOT NULL,\n"
+ " \"script\" VARCHAR(1000) NOT NULL,\n"
+ " \"checksum\" INTEGER,\n"
+ " \"installed_by\" VARCHAR(100) NOT NULL,\n"
+ " \"installed_on\" TIMESTAMP NOT NULL DEFAULT now(),\n"
+ " \"execution_time\" INTEGER NOT NULL,\n"
+ " \"success\" BOOLEAN NOT NULL DEFAULT FALSE\n"
+ ")"
+ tablespace + ";\n" + "ALTER TABLE "
+ table + " ADD CONSTRAINT \"" + table.getName() + "_pk\" PRIMARY KEY (\"installed_rank\");\n"
+ "CREATE INDEX \""
+ table.getName() + "_s_idx\" ON " + table + " (\"success\");";
}
@Override
protected String doGetCurrentUser() throws SQLException {
return getMainConnection().getJdbcTemplate().queryForString("SELECT current_user");
}
@Override
public boolean supportsDdlTransactions() {
return true;
}
@Override
public boolean supportsChangingCurrentSchema() {
return true;
}
@Override
public String getBooleanTrue() {
return "TRUE";
}
@Override
public String getBooleanFalse() {
return "FALSE";
}
@Override
public String doQuote(String identifier) {
return getOpenQuote()
+ StringUtils.replaceAll(identifier, getCloseQuote(), getEscapedQuote())
+ getCloseQuote();
}
@Override
public String getEscapedQuote() {
return "\"\"";
}
@Override
public boolean catalogIsSchema() {
return false;
}
@Override
public boolean useSingleConnection() {
return true;
}
/**
* This exists to fix this issue: https://github.com/flyway/flyway/issues/2638
* See https://www.pgpool.net/docs/latest/en/html/runtime-config-load-balancing.html
*/
@Override
public String getSelectStatement(Table table) {
return "/*NO LOAD BALANCE*/\n"
+ "SELECT " + quote("installed_rank")
+ "," + quote("version")
+ "," + quote("description")
+ "," + quote("type")
+ "," + quote("script")
+ "," + quote("checksum")
+ "," + quote("installed_on")
+ "," + quote("installed_by")
+ "," + quote("execution_time")
+ "," + quote("success")
+ " FROM " + table
+ " WHERE " + quote("installed_rank") + " > ?"
+ " ORDER BY " + quote("installed_rank");
}
}
......@@ -172,7 +172,7 @@ public class MySQLDatabase extends Database<MySQLConnection> {
+ " `success` BOOL NOT NULL,\n"
+ " CONSTRAINT "
+ getConstraintName(table.getName()) + " PRIMARY KEY (`installed_rank`)\n" + ")"
+ tablespace + " ENGINE=InnoDB" + baselineMarker
+ tablespace + " DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci ENGINE=InnoDB" + baselineMarker
+ ";\n"
+ "CREATE INDEX `"
+ table.getName() + "_s_idx` ON " + table + " (`success`);";
......
......@@ -3300,7 +3300,7 @@ CREATE TABLE IF NOT EXISTS public.dinky_resources
updater INT
);
CREATE UNIQUE INDEX IF NOT EXISTS public.dinky_resources_un ON public.dinky_resources (full_name, type);
CREATE UNIQUE INDEX IF NOT EXISTS dinky_resources_un ON public.dinky_resources (full_name, type);
COMMENT ON TABLE public.dinky_resources IS 'resources';
......@@ -3967,7 +3967,7 @@ CREATE TABLE IF NOT EXISTS public.dinky_alert_rules
creator INT,
updater INT
);
CREATE UNIQUE INDEX IF NOT EXISTS public.dinky_alert_rules_name ON public.dinky_alert_rules (name);
CREATE UNIQUE INDEX IF NOT EXISTS dinky_alert_rules_name ON public.dinky_alert_rules (name);
COMMENT ON COLUMN public.dinky_alert_rules.id IS 'id';
COMMENT ON COLUMN public.dinky_alert_rules.name IS 'rule name';
......
......@@ -21,6 +21,64 @@ END IF;
END;
$$ LANGUAGE plpgsql;
-- 创建函数/操作符用于比较整数和布尔值 用于解决PostgreSQL 查询时报错 `operator does not exist: boolean = integer` 的问题
-- Create a function to compare integers and booleans to solve the problem of `operator does not exist: boolean = integer` when querying in PostgreSQL
CREATE OR REPLACE FUNCTION equal_int_bool(x int, y bool)
RETURNS BOOLEAN AS $$
begin
return x = y::int;
end;
$$ LANGUAGE PLPGSQL IMMUTABLE STRICT;
-- 创建操作符 用于比较整数和布尔值 用于解决PostgreSQL 查询时报错 `operator does not exist: boolean = integer` 的问题
-- Create an operator to compare integers and booleans to solve the problem of `operator does not exist: boolean = integer` when querying in PostgreSQL
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_operator
WHERE oprname = '=' AND
oprleft = 'integer'::regtype AND
oprright = 'boolean'::regtype
) THEN
CREATE OPERATOR = (
leftarg = INTEGER,
rightarg = BOOLEAN,
procedure = equal_int_bool);
END IF;
END;
$$ LANGUAGE plpgsql;
-- 创建函数/操作符用于比较整数和布尔值 用于解决PostgreSQL 查询时报错 `operator does not exist: boolean = integer` 的问题
-- Create a function to compare integers and booleans to solve the problem of `operator does not exist: boolean = integer` when querying in PostgreSQL
CREATE OR REPLACE FUNCTION equal_bool_int(x bool, y int)
RETURNS BOOLEAN AS $$
begin
return x::int = y;
end;
$$ LANGUAGE PLPGSQL IMMUTABLE STRICT;
-- 创建操作符 用于比较整数和布尔值 用于解决PostgreSQL 查询时报错 `operator does not exist: boolean = integer` 的问题
-- Create an operator to compare integers and booleans to solve the problem of `operator does not exist: boolean = integer` when querying in PostgreSQL
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_operator
WHERE oprname = '=' AND
oprleft = 'boolean'::regtype AND
oprright = 'integer'::regtype
) THEN
CREATE OPERATOR = (
leftarg = BOOLEAN,
rightarg = INTEGER,
procedure = equal_bool_int);
END IF;
END;
$$ LANGUAGE plpgsql;
update public.dinky_sys_menu
......@@ -57,3 +115,5 @@ SELECT add_column_if_not_exists('public','dinky_task', 'second_level_owners', 'v
update public.dinky_task set "first_level_owner" = "creator";
UPDATE public.dinky_user SET "password" = 'f4b3a484ee745b98d64cd69c429b2aa2' WHERE "id" =1 and "password"= '21232f297a57a5a743894a0e4a801fc3';
\ No newline at end of file
......@@ -71,6 +71,9 @@
<Logger name="org.springframework.web.socket" level="info" additivity="false">
<AppenderRef ref="RollingFileWs"/>
</Logger>
<Logger name="org.flywaydb" level="debug" >
<AppenderRef ref="Console"/>
</Logger>
<root level="info">
<appender-ref ref="Console"/>
<!--一般不使用这个,只是让你知道有这个输出日志文件的方式而已-->
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment