From 474c59aed593167f65a4fe964d59199a1179eabb Mon Sep 17 00:00:00 2001 From: Nikitin Aleksandr Date: Mon, 20 Jan 2025 15:28:58 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20ReplaceS?= =?UTF-8?q?chemaName()?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- postgres_gorm/postgres_gorm.go | 9 +++++++++ postgres_gorm/postgres_gorm_test.go | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/postgres_gorm/postgres_gorm.go b/postgres_gorm/postgres_gorm.go index fb7f60c6..47546683 100644 --- a/postgres_gorm/postgres_gorm.go +++ b/postgres_gorm/postgres_gorm.go @@ -562,6 +562,15 @@ func ReplaceSchema(TextSQL string) string { return Otvet } +// ReplaceSchemaName - заменяет имя схемы в тексте SQL +func ReplaceSchemaName(TextSQL, SchemaNameFrom string) string { + Otvet := TextSQL + + Otvet = strings.ReplaceAll(Otvet, SchemaNameFrom+".", Settings.DB_SCHEMA+".") + + return Otvet +} + // ReplaceTemporaryTableNamesToUnique - заменяет "public.TableName" на "public.TableName_UUID" func ReplaceTemporaryTableNamesToUnique(TextSQL string) string { Otvet := TextSQL diff --git a/postgres_gorm/postgres_gorm_test.go b/postgres_gorm/postgres_gorm_test.go index 7e060e4d..2e9fa964 100644 --- a/postgres_gorm/postgres_gorm_test.go +++ b/postgres_gorm/postgres_gorm_test.go @@ -336,3 +336,13 @@ func TestSetSingularTableNames(t *testing.T) { t.Errorf("Expected SingularTable to be %v, but got %v", IsSingular, NamingStrategy.SingularTable) } } + +func TestReplaceSchemaName(t *testing.T) { + TextSQL := "SELECT * FROM public.users" + Settings.DB_SCHEMA = "myschema" + ExpectedSQL := "SELECT * FROM myschema.users" + ActualSQL := ReplaceSchemaName(TextSQL, "public") + if ActualSQL != ExpectedSQL { + t.Errorf("Expected %v, but got %v", ExpectedSQL, ActualSQL) + } +}