Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: SYS.INDEXES does not work properly #2496

Open
1 task done
kewinbrand opened this issue Apr 15, 2024 · 4 comments
Open
1 task done

[Bug]: SYS.INDEXES does not work properly #2496

kewinbrand opened this issue Apr 15, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@kewinbrand
Copy link

What happened?

we use the SYS.INDEXES table during our update process, but it seems that babelfish is not converting the names correctly.
see example

Script

use [master]
drop database if exists index_testes
create database index_testes

use [index_testes]

CREATE TABLE [dbo].[ESTOQUE_EMPRESA](
	[R_E_C_N_O_] [int] IDENTITY(1,1) NOT NULL,
	[CODIGO] [varchar](50) NULL,
	[Id] [uniqueidentifier] NOT NULL,
	CONSTRAINT [PK_ESTOQUE_EMPRESA] PRIMARY KEY CLUSTERED 
	(
		[R_E_C_N_O_] ASC
	),
	UNIQUE NONCLUSTERED 
	(
		[Id] ASC
	)
)
GO

CREATE NONCLUSTERED INDEX [IX_ESTOQUE_EMPRESA_CODIGO] ON ESTOQUE_EMPRESA ([CODIGO])
GO

IF NOT EXISTS (SELECT TOP 1 1 FROM SYS.INDEXES WHERE NAME = 'IX_ESTOQUE_EMPRESA_CODIGO') 
BEGIN
  CREATE NONCLUSTERED INDEX [IX_ESTOQUE_EMPRESA_CODIGO] ON ESTOQUE_EMPRESA ([CODIGO])
END
GO

use [master]
GO

fails with
Msg 2714, Level 16, State 1, Line 27
relation "ix_estoque_empresa_codigoestoquab8ae8734a5c39898c705577384d1f72" already exists

it is also worth to mention that we are using BABEL_4_X_DEV branch

Version

BABEL_3_X_DEV (Default)

Extension

babelfishpg_tsql (Default)

Which flavor of Linux are you using when you see the bug?

Ubuntu (Default)

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct.
@kewinbrand kewinbrand added the bug Something isn't working label Apr 15, 2024
@staticlibs
Copy link
Contributor

Hi, the last command should work in this form (computing actual index name, see details in wiki):

IF NOT EXISTS (SELECT TOP 1 1 FROM SYS.INDEXES WHERE NAME = sys.babelfish_truncate_identifier(lower('IX_ESTOQUE_EMPRESA_CODIGO' + 'ESTOQUE_EMPRESA') + md5(lower('IX_ESTOQUE_EMPRESA_CODIGO')))) 
BEGIN
  CREATE NONCLUSTERED INDEX [IX_ESTOQUE_EMPRESA_CODIGO] ON ESTOQUE_EMPRESA ([CODIGO])
END
GO

@staticlibs
Copy link
Contributor

Minor correction, to get correct mangled index or constraint name it is necessary to apply babelfish_truncate_identifier to index and table names before concatenation:

select sys.babelfish_truncate_identifier(
    sys.babelfish_truncate_identifier(lower('idx_or_constraint_name')) +
    sys.babelfish_truncate_identifier(lower('table_name')) +
    md5(sys.babelfish_truncate_identifier(lower('idx_or_constraint_name'))))

@PauloHMattos
Copy link

This is also affecting my application (we are using BABEL_4_X_DEV).

While the workaround provided by @staticlibs works, it's not ideal for my case, as the migration scripts need to be shared between a Babelfish instance and a SQL Server one.

A fix is planned for this issue or this workaround is the way for now?

@staticlibs
Copy link
Contributor

@PauloHMattos portable DB migration scripts for both Babelfish and MSSQL can work reasonably well with current name mangling, see details in these issues - 1, 2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants