Skip to content

Commit

Permalink
fix: 修复授权时数据库出现特殊字符失败 TencentBlueKing#7200
Browse files Browse the repository at this point in the history
  • Loading branch information
yksitu authored and iSecloud committed Sep 30, 2024
1 parent fce8454 commit 72f4a38
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func GenerateSqlserverSQL(account TbAccounts, rules []TbAccountRules, address st
}
// 拼接db级别的授权语句
for _, dbName := range realDBS {
sqls = append(sqls, fmt.Sprintf(`USE %s
sqls = append(sqls, fmt.Sprintf(`USE [%s]
ALTER AUTHORIZATION ON SCHEMA::db_owner TO dbo;
IF EXISTS (SELECT 1 FROM [%s].sys.database_principals WHERE name = N'%s')
BEGIN
Expand All @@ -148,7 +148,7 @@ func GenerateSqlserverSQL(account TbAccounts, rules []TbAccountRules, address st
`,
dbName, dbName, account.User, dbName, account.User, account.User, account.User,
))
sqls = append(sqls, fmt.Sprintf("USE %s", dbName))
sqls = append(sqls, fmt.Sprintf("USE [%s]", dbName))
for _, p := range strings.Split(rule.Priv, ",") {
sqls = append(sqls, fmt.Sprintf("EXEC SP_ADDROLEMEMBER N'%s', N'%s';", p, account.User))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (r *RenameDBSComp) DoRenameDBWithMirroring() error {
if cnt != 0 {
execDBSQLs = append(execDBSQLs, fmt.Sprintf("ALTER DATABASE [%s] SET PARTNER OFF;", i.DBName))
}
execDBSQLs = append(execDBSQLs, fmt.Sprintf("ALTER DATABASE [%s] MODIFY NAME = %s", i.DBName, i.TargetDBName))
execDBSQLs = append(execDBSQLs, fmt.Sprintf("ALTER DATABASE [%s] MODIFY NAME = [%s]", i.DBName, i.TargetDBName))
// 执行rename 命令
if _, err := r.DB.ExecMore(execDBSQLs); err != nil {
logger.Error(
Expand Down Expand Up @@ -209,7 +209,7 @@ func (r *RenameDBSComp) DoRenameDBWithAlwayson() error {
}
execDBSQLs = append(
execDBSQLs,
fmt.Sprintf("ALTER DATABASE [%s] MODIFY NAME = %s;", i.DBName, i.TargetDBName),
fmt.Sprintf("ALTER DATABASE [%s] MODIFY NAME = [%s];", i.DBName, i.TargetDBName),
)
// 执行rename 命令
if _, err := r.DB.ExecMore(execDBSQLs); err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -960,14 +960,14 @@ BEGIN
BEGIN
IF EXISTS (SELECT name FROM sys.databases WHERE name = @dr)
BEGIN
SET @cmd='DROP DATABASE '+@dr
SET @cmd='DROP DATABASE ['+@dr+']'
EXEC (@cmd)

SET @cmd2='EXEC MASTER.DBO.xp_cmdshell ''del /q d:\gamedb\'+@dr+'.'+@port+'.*.spst'';'
EXEC (@cmd2)
END

select @filename=null,@cmd='',@fileid=0,@cmd='CREATE DATABASE '+@dr+' ON '
select @filename=null,@cmd='',@fileid=0,@cmd='CREATE DATABASE ['+@dr+'] ON '
declare cur1 cursor for select name from sys.master_files where database_id=@dbid and type_desc='ROWS' and state_desc='ONLINE'
open cur1
while 1=1
Expand All @@ -981,7 +981,7 @@ BEGIN
close cur1
deallocate cur1

set @cmd=left(@cmd,len(@cmd)-1)+' AS SNAPSHOT OF '+@db
set @cmd=left(@cmd,len(@cmd)-1)+' AS SNAPSHOT OF ['+@db+']'

BEGIN TRY
EXEC (@cmd)
Expand Down Expand Up @@ -2728,7 +2728,7 @@ BEGIN TRY
WHILE @@FETCH_STATUS = 0
BEGIN
--强制故障切换
set @strsql='Use master alter database '+@dbname+' set partner FORCE_SERVICE_ALLOW_DATA_LOSS;'
set @strsql='Use master alter database ['+@dbname+'] set partner FORCE_SERVICE_ALLOW_DATA_LOSS;'
exec(@strsql)
if @@error>0
begin
Expand Down Expand Up @@ -2936,7 +2936,7 @@ BEGIN TRY
WHILE @@FETCH_STATUS = 0
BEGIN
--强制故障切换
set @strsql='Use master alter database '+@dbname+' set partner FORCE_SERVICE_ALLOW_DATA_LOSS;'
set @strsql='Use master alter database ['+@dbname+'] set partner FORCE_SERVICE_ALLOW_DATA_LOSS;'
exec(@strsql)
if @@error>0
begin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def create(
m.db_module_id = db_module_id
# 保存最新的time_zone
ins.time_zone = get_instance_time_zone(ins)
ins.save(update_fields=["db_module_id"])
m.save(update_fields=["db_module_id"])
ins.save()
m.save()

return cluster
4 changes: 2 additions & 2 deletions dbm-ui/backend/db_meta/api/cluster/sqlserverha/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ def switch_slave(
)[0]
new_slave_obj.db_module_id = cluster.db_module_id
new_slave_obj.machine.db_module_id = cluster.db_module_id
new_slave_obj.save(update_fields=["db_module_id"])
new_slave_obj.machine.save(update_fields=["db_module_id"])
new_slave_obj.save()
new_slave_obj.machine.save()
cluster.storageinstance_set.add(new_slave_obj)

# 切换slave域名信息
Expand Down

0 comments on commit 72f4a38

Please sign in to comment.