Skip to content

Commit 2552879

Browse files
Hyphen in database name support (SDBM-1013) (#17775)
* Hyphen in database name support * add relnote * linter errors * correct ha
1 parent 260a86d commit 2552879

File tree

16 files changed

+114
-110
lines changed

16 files changed

+114
-110
lines changed

sqlserver/changelog.d/17775.fixed

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hyphen in database name support

sqlserver/datadog_checks/sqlserver/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
]
5757

5858
DATABASE_SERVICE_CHECK_QUERY = """SELECT 1;"""
59-
SWITCH_DB_STATEMENT = """USE {};"""
59+
SWITCH_DB_STATEMENT = """USE [{}];"""
6060

6161
VALID_METRIC_TYPES = ('gauge', 'rate', 'histogram')
6262

sqlserver/tests/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ def assert_metrics(
283283
tags = check_tags + ['database:{}'.format(dbname)]
284284
for mname in DB_PERF_COUNT_METRICS_NAMES_SINGLE:
285285
aggregator.assert_metric(mname, hostname=hostname, tags=tags)
286-
if dbname == 'datadog_test' and is_always_on():
286+
if dbname == 'datadog_test-1' and is_always_on():
287287
for mname in DB_PERF_COUNT_METRICS_NAMES_AO:
288288
aggregator.assert_metric(mname, hostname=hostname, tags=tags)
289289
else:

sqlserver/tests/compose-ha/sql/aoag_primary.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ CREATE USER fred FOR LOGIN fred;
2020
GRANT CONNECT ANY DATABASE to fred;
2121
GO
2222

23-
CREATE DATABASE datadog_test;
23+
CREATE DATABASE [datadog_test-1];
2424
GO
2525

2626
-- create an offline database to have an unavailable database to test with
@@ -38,10 +38,10 @@ GO
3838

3939
-- Create test database for integration tests
4040
-- only bob and fred have read/write access to this database
41-
USE datadog_test;
42-
CREATE TABLE datadog_test.dbo.ϑings (id int, name varchar(255));
43-
INSERT INTO datadog_test.dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
44-
CREATE CLUSTERED INDEX thingsindex ON datadog_test.dbo.ϑings (name);
41+
USE [datadog_test-1];
42+
CREATE TABLE [datadog_test-1].dbo.ϑings (id int, name varchar(255));
43+
INSERT INTO [datadog_test-1].dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
44+
CREATE CLUSTERED INDEX thingsindex ON [datadog_test-1].dbo.ϑings (name);
4545
CREATE USER bob FOR LOGIN bob;
4646
CREATE USER fred FOR LOGIN fred;
4747
GO
@@ -184,10 +184,10 @@ USE [master]
184184
GO
185185

186186
--change recovery model and take full backup for db to meet requirements of AOAG
187-
ALTER DATABASE datadog_test SET RECOVERY FULL ;
187+
ALTER DATABASE [datadog_test-1] SET RECOVERY FULL ;
188188
GO
189189

190-
BACKUP DATABASE datadog_test TO DISK = N'/var/opt/mssql/backup/datadog_test.bak' WITH NOFORMAT, NOINIT, NAME = N'datadog_test-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
190+
BACKUP DATABASE [datadog_test-1] TO DISK = N'/var/opt/mssql/backup/[datadog_test-1].bak' WITH NOFORMAT, NOINIT, NAME = N'[datadog_test-1]-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
191191
GO
192192

193193

@@ -271,5 +271,5 @@ USE [master]
271271
GO
272272

273273
WAITFOR DELAY '00:00:10'
274-
ALTER AVAILABILITY GROUP [AG1] ADD DATABASE [datadog_test]
274+
ALTER AVAILABILITY GROUP [AG1] ADD DATABASE [datadog_test-1]
275275
GO

sqlserver/tests/compose-high-cardinality-windows/setup.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ GO
3333
-- Create test database for integration tests
3434
-- only bob and fred have read/write access to this database
3535
-- the datadog user has only connect access but can't read any objects
36-
CREATE DATABASE datadog_test;
36+
CREATE DATABASE [datadog_test-1];
3737
GO
38-
USE datadog_test;
38+
USE [datadog_test-1];
3939
GO
4040

4141
-- This table is pronounced "things" except we've replaced "th" with the greek lower case "theta" to ensure we
4242
-- correctly support unicode throughout the integration.
43-
CREATE TABLE datadog_test.dbo.ϑings (id int, name varchar(255));
44-
INSERT INTO datadog_test.dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
45-
CREATE CLUSTERED INDEX thingsindex ON datadog_test.dbo.ϑings (name);
43+
CREATE TABLE [datadog_test-1].dbo.ϑings (id int, name varchar(255));
44+
INSERT INTO [datadog_test-1].dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
45+
CREATE CLUSTERED INDEX thingsindex ON [datadog_test-1].dbo.ϑings (name);
4646
CREATE USER bob FOR LOGIN bob;
4747
CREATE USER fred FOR LOGIN fred;
4848
-- we don't need to recreate the datadog user in this new DB because it already exists in the model
@@ -196,7 +196,7 @@ GO
196196
------------------------------ HIGH CARDINALITY ENV SETUP ------------------------------
197197

198198
-- Table variables
199-
DECLARE @table_prefix VARCHAR(100) = 'CREATE TABLE datadog_test.dbo.'
199+
DECLARE @table_prefix VARCHAR(100) = 'CREATE TABLE [datadog_test-1].dbo.'
200200
DECLARE @table_columns VARCHAR(500) = ' (id INT NOT NULL IDENTITY, col1_txt TEXT, col2_txt TEXT, col3_txt TEXT, col4_txt TEXT, col5_txt TEXT, col6_txt TEXT, col7_txt TEXT, col8_txt TEXT, col9_txt TEXT, col10_txt TEXT, col11_float FLOAT, col12_float FLOAT, col13_float FLOAT, col14_int INT, col15_int INT, col16_int INT, col17_date DATE, PRIMARY KEY(id));';
201201

202202
-- Create a main table which contains high cardinality data for testing.
@@ -227,7 +227,7 @@ BEGIN
227227
DECLARE @col16_int INT = FLOOR(RAND() * 2500);
228228
DECLARE @col17_date DATE = CAST(CAST(RAND()*100000 AS INT) AS DATETIME);
229229

230-
INSERT INTO datadog_test.dbo.high_cardinality (col1_txt, col2_txt, col3_txt, col4_txt, col5_txt, col6_txt, col7_txt, col8_txt, col9_txt, col10_txt, col11_float, col12_float, col13_float, col14_int, col15_int, col16_int, col17_date) VALUES (@col1_txt, @col2_txt, @col3_txt, @col4_txt, @col5_txt, @col6_txt, @col7_txt, @col8_txt, @col9_txt, @col10_txt, @col11_float, @col12_float, @col13_float, @col14_int, @col15_int, @col16_int, @col17_date);
230+
INSERT INTO [datadog_test-1].dbo.high_cardinality (col1_txt, col2_txt, col3_txt, col4_txt, col5_txt, col6_txt, col7_txt, col8_txt, col9_txt, col10_txt, col11_float, col12_float, col13_float, col14_int, col15_int, col16_int, col17_date) VALUES (@col1_txt, @col2_txt, @col3_txt, @col4_txt, @col5_txt, @col6_txt, @col7_txt, @col8_txt, @col9_txt, @col10_txt, @col11_float, @col12_float, @col13_float, @col14_int, @col15_int, @col16_int, @col17_date);
231231

232232
SET @row_count = @row_count + 1
233233
END;

sqlserver/tests/compose-high-cardinality/setup.sql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ GO
125125

126126
-- Create test database for integration tests.
127127
-- Only bob and fred have read/write access to this database.
128-
CREATE DATABASE datadog_test;
128+
CREATE DATABASE [datadog_test-1];
129129
GO
130-
USE datadog_test;
130+
USE [datadog_test-1];
131131
GO
132132

133133
CREATE USER bob FOR LOGIN bob;
@@ -174,12 +174,12 @@ GO
174174

175175
-- This table is pronounced "things" except we've replaced "th" with the greek lower case "theta" to ensure we
176176
-- correctly support unicode throughout the integration.
177-
CREATE TABLE datadog_test.dbo.ϑings (id int, name varchar(255));
178-
INSERT INTO datadog_test.dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
179-
CREATE CLUSTERED INDEX thingsindex ON datadog_test.dbo.ϑings (name);
177+
CREATE TABLE [datadog_test-1].dbo.ϑings (id int, name varchar(255));
178+
INSERT INTO [datadog_test-1].dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
179+
CREATE CLUSTERED INDEX thingsindex ON [datadog_test-1].dbo.ϑings (name);
180180

181181
-- Table variables
182-
DECLARE @table_prefix VARCHAR(100) = 'CREATE TABLE datadog_test.dbo.'
182+
DECLARE @table_prefix VARCHAR(100) = 'CREATE TABLE [datadog_test-1].dbo.'
183183
DECLARE @table_columns VARCHAR(500) = ' (id INT NOT NULL IDENTITY, col1_txt TEXT, col2_txt TEXT, col3_txt TEXT, col4_txt TEXT, col5_txt TEXT, col6_txt TEXT, col7_txt TEXT, col8_txt TEXT, col9_txt TEXT, col10_txt TEXT, col11_float FLOAT, col12_float FLOAT, col13_float FLOAT, col14_int INT, col15_int INT, col16_int INT, col17_date DATE, PRIMARY KEY(id));';
184184

185185
-- Create a main table which contains high cardinality data for testing.
@@ -210,7 +210,7 @@ BEGIN
210210
DECLARE @col16_int INT = FLOOR(RAND() * 2500);
211211
DECLARE @col17_date DATE = CAST(CAST(RAND()*100000 AS INT) AS DATETIME);
212212

213-
INSERT INTO datadog_test.dbo.high_cardinality (col1_txt, col2_txt, col3_txt, col4_txt, col5_txt, col6_txt, col7_txt, col8_txt, col9_txt, col10_txt, col11_float, col12_float, col13_float, col14_int, col15_int, col16_int, col17_date) VALUES (@col1_txt, @col2_txt, @col3_txt, @col4_txt, @col5_txt, @col6_txt, @col7_txt, @col8_txt, @col9_txt, @col10_txt, @col11_float, @col12_float, @col13_float, @col14_int, @col15_int, @col16_int, @col17_date);
213+
INSERT INTO [datadog_test-1].dbo.high_cardinality (col1_txt, col2_txt, col3_txt, col4_txt, col5_txt, col6_txt, col7_txt, col8_txt, col9_txt, col10_txt, col11_float, col12_float, col13_float, col14_int, col15_int, col16_int, col17_date) VALUES (@col1_txt, @col2_txt, @col3_txt, @col4_txt, @col5_txt, @col6_txt, @col7_txt, @col8_txt, @col9_txt, @col10_txt, @col11_float, @col12_float, @col13_float, @col14_int, @col15_int, @col16_int, @col17_date);
214214

215215
SET @row_count = @row_count + 1
216216
END;

sqlserver/tests/compose-windows/setup.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,16 @@ GO
3333
-- Create test database for integration tests
3434
-- only bob and fred have read/write access to this database
3535
-- the datadog user has only connect access but can't read any objects
36-
CREATE DATABASE datadog_test;
36+
CREATE DATABASE [datadog_test-1];
3737
GO
38-
USE datadog_test;
38+
USE [datadog_test-1];
3939
GO
4040

4141
-- This table is pronounced "things" except we've replaced "th" with the greek lower case "theta" to ensure we
4242
-- correctly support unicode throughout the integration.
43-
CREATE TABLE datadog_test.dbo.ϑings (id int, name varchar(255));
44-
INSERT INTO datadog_test.dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
45-
CREATE CLUSTERED INDEX thingsindex ON datadog_test.dbo.ϑings (name);
43+
CREATE TABLE [datadog_test-1].dbo.ϑings (id int, name varchar(255));
44+
INSERT INTO [datadog_test-1].dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
45+
CREATE CLUSTERED INDEX thingsindex ON [datadog_test-1].dbo.ϑings (name);
4646
CREATE USER bob FOR LOGIN bob;
4747
CREATE USER fred FOR LOGIN fred;
4848
-- we don't need to recreate the datadog user in this new DB because it already exists in the model

sqlserver/tests/compose/setup.sql

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ GO
1717

1818
-- Create test database for integration tests
1919
-- only bob and fred have read/write access to this database
20-
CREATE DATABASE datadog_test;
20+
CREATE DATABASE [datadog_test-1];
2121
GO
22-
USE datadog_test;
22+
USE [datadog_test-1];
2323
-- This table is pronounced "things" except we've replaced "th" with the greek lower case "theta" to ensure we
2424
-- correctly support unicode throughout the integration.
25-
CREATE TABLE datadog_test.dbo.ϑings (id int, name varchar(255));
26-
INSERT INTO datadog_test.dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
25+
CREATE TABLE [datadog_test-1].dbo.ϑings (id int, name varchar(255));
26+
INSERT INTO [datadog_test-1].dbo.ϑings VALUES (1, 'foo'), (2, 'bar');
2727
CREATE USER bob FOR LOGIN bob;
2828
CREATE USER fred FOR LOGIN fred;
29-
CREATE CLUSTERED INDEX thingsindex ON datadog_test.dbo.ϑings (name);
29+
CREATE CLUSTERED INDEX thingsindex ON [datadog_test-1].dbo.ϑings (name);
3030
GO
3131

3232
EXEC sp_addrolemember 'db_datareader', 'bob'

sqlserver/tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from datadog_checks.dev import WaitFor, docker_run
1414
from datadog_checks.dev.conditions import CheckDockerLogs
1515
from datadog_checks.dev.docker import using_windows_containers
16+
from datadog_checks.sqlserver.const import SWITCH_DB_STATEMENT
1617

1718
from .common import (
1819
DOCKER_SERVER,
@@ -198,7 +199,7 @@ def execute_with_retries(self, query, params=(), database=None, retries=3, sleep
198199
logging.info("executing query with retries. query='%s' params=%s attempt=%s", query, params, attempt)
199200
with self.conn.cursor() as cursor:
200201
if database:
201-
cursor.execute("USE {}".format(database))
202+
cursor.execute(SWITCH_DB_STATEMENT.format(database))
202203
cursor.execute(query, params)
203204
if return_result:
204205
return cursor.fetchall()

sqlserver/tests/test_activity.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,14 @@ def dbm_instance(instance_docker):
6464
"database,query,match_pattern,is_proc,expected_comments",
6565
[
6666
[
67-
"datadog_test",
67+
"datadog_test-1",
6868
"/*test=foo*/ SELECT * FROM ϑings",
6969
r"SELECT \* FROM ϑings",
7070
False,
7171
["/*test=foo*/"],
7272
],
7373
[
74-
"datadog_test",
74+
"datadog_test-1",
7575
"EXEC bobProc",
7676
r"SELECT \* FROM ϑings",
7777
True,
@@ -98,7 +98,7 @@ def test_collect_load_activity(
9898

9999
def run_test_query(c, q):
100100
cur = c.cursor()
101-
cur.execute("USE {}".format(database))
101+
cur.execute("USE [{}]".format(database))
102102
# 0xFF can't be decoded to Unicode, which makes it good test data,
103103
# since Unicode is a default format
104104
cur.execute("SET CONTEXT_INFO 0xff")
@@ -166,7 +166,7 @@ def run_test_query(c, q):
166166
assert blocked_row['procedure_signature'], "missing procedure signature"
167167
assert blocked_row['procedure_name'], "missing procedure name"
168168
assert re.match(match_pattern, blocked_row['text'], re.IGNORECASE), "incorrect blocked query"
169-
assert blocked_row['database_name'] == "datadog_test", "incorrect database_name"
169+
assert blocked_row['database_name'] == "datadog_test-1", "incorrect database_name"
170170
assert blocked_row['context_info'] == "ff", "incorrect context_info"
171171
assert blocked_row['id'], "missing session id"
172172
assert blocked_row['now'], "missing current timestamp"
@@ -255,7 +255,7 @@ def test_activity_nested_blocking_transactions(
255255

256256
def run_queries(conn, queries):
257257
cur = conn.cursor()
258-
cur.execute("USE {}".format("datadog_test"))
258+
cur.execute("USE [{}]".format("datadog_test-1"))
259259
cur.execute("BEGIN TRANSACTION")
260260
for q in queries:
261261
try:
@@ -307,7 +307,7 @@ def run_queries(conn, queries):
307307
# associated sys.dm_exec_requests.
308308
assert root_blocker["user_name"] == "fred"
309309
assert root_blocker["session_status"] == "sleeping"
310-
assert root_blocker["database_name"] == "datadog_test"
310+
assert root_blocker["database_name"] == "datadog_test-1"
311311
assert root_blocker["last_request_start_time"]
312312
assert root_blocker["client_port"]
313313
assert root_blocker["client_address"]
@@ -329,7 +329,7 @@ def run_queries(conn, queries):
329329
assert tx3["session_status"] == "running"
330330
# verify other essential fields are present
331331
assert tx2["user_name"] == "bob"
332-
assert tx2["database_name"] == "datadog_test"
332+
assert tx2["database_name"] == "datadog_test-1"
333333
assert tx2["last_request_start_time"]
334334
assert tx2["client_port"]
335335
assert tx2["client_address"]
@@ -341,7 +341,7 @@ def run_queries(conn, queries):
341341
assert tx2["query_plan_hash"]
342342

343343
assert tx3["user_name"] == "fred"
344-
assert tx3["database_name"] == "datadog_test"
344+
assert tx3["database_name"] == "datadog_test-1"
345345
assert tx3["last_request_start_time"]
346346
assert tx3["client_port"]
347347
assert tx3["client_address"]
@@ -392,7 +392,7 @@ def test_activity_metadata(
392392

393393
def _run_test_query(conn, q):
394394
cur = conn.cursor()
395-
cur.execute("USE {}".format("datadog_test"))
395+
cur.execute("USE [{}]".format("datadog_test-1"))
396396
cur.execute(q)
397397

398398
def _obfuscate_sql(sql_query, options=None):
@@ -647,7 +647,7 @@ def _obfuscate_sql(sql_query, options=None):
647647

648648
def run_test_query(c, q):
649649
cur = c.cursor()
650-
cur.execute("USE datadog_test")
650+
cur.execute("USE [datadog_test-1]")
651651
cur.execute(q)
652652

653653
run_test_query(fred_conn, "EXEC procedureWithLargeCommment")

sqlserver/tests/test_database_metrics.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
SQLSERVER_MAJOR_VERSION,
2525
)
2626

27-
AUTODISCOVERY_DBS = ['master', 'msdb', 'datadog_test']
27+
AUTODISCOVERY_DBS = ['master', 'msdb', 'datadog_test-1']
2828

2929
STATIC_SERVER_INFO = {
3030
STATIC_INFO_MAJOR_VERSION: SQLSERVER_MAJOR_VERSION,
@@ -61,8 +61,8 @@ def test_sqlserver_index_usage_metrics(
6161
('msdb', 'PK__backupse__21F79AAB9439648C', 'backupset', 0, 1, 0, 0),
6262
],
6363
[
64-
('datadog_test', 'idx_something', 'some_table', 10, 60, 12, 18),
65-
('datadog_test', 'idx_something_else', 'some_table', 20, 30, 40, 50),
64+
('datadog_test-1', 'idx_something', 'some_table', 10, 60, 12, 18),
65+
('datadog_test-1', 'idx_something_else', 'some_table', 20, 30, 40, 50),
6666
],
6767
]
6868
mocked_results_tempdb = [
@@ -153,7 +153,7 @@ def test_sqlserver_db_fragmentation_metrics(
153153
('msdb', 'syscachedcredentials', 1, 'PK__syscache__F6D56B562DA81DC6', 0, 0.0, 0, 0.0),
154154
('msdb', 'syscollector_blobs_internal', 1, 'PK_syscollector_blobs_internal_paremeter_name', 0, 0.0, 0, 0.0),
155155
],
156-
[('datadog_test', 'ϑings', 1, 'thingsindex', 1, 1.0, 1, 0.0)],
156+
[('datadog_test-1', 'ϑings', 1, 'thingsindex', 1, 1.0, 1, 0.0)],
157157
]
158158
mocked_results_tempdb = [
159159
[('tempdb', '#TempExample__000000000008', 1, 'PK__#TempExa__3214EC278A26D67E', 1, 1.0, 1, 0.0)],
@@ -250,7 +250,7 @@ def test_sqlserver_database_backup_metrics(
250250
('model', 'model', 2),
251251
('msdb', 'msdb', 0),
252252
('tempdb', 'tempdb', 0),
253-
('datadog_test', 'datadog_test', 10),
253+
('datadog_test-1', 'datadog_test-1', 10),
254254
]
255255

256256
sqlserver_check = SQLServer(CHECK_NAME, init_config, [instance_docker_metrics])

0 commit comments

Comments
 (0)