Skip to content

Commit 1c638d8

Browse files
authored
[mysql] collect foreign key delete and update rule (#19797)
* collect foreign key delete and update rule * add changelog * fix test * fix tests fail for mariadb * update changelog
1 parent 04a1b0a commit 1c638d8

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

mysql/changelog.d/19797.added

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Collect MySQL foreign key delete_rule and update_rule from INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS.
2+
Note: On MariaDB 10.5+, REFERENCES privilege is required to access this information.

mysql/datadog_checks/mysql/databases_data.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ def _collect_databases_data(self, tags):
225225
- referenced_table_schema (str): The schema of the referenced table.
226226
- referenced_table_name (str): The name of the referenced table.
227227
- referenced_column_names (str): The column names in the referenced table.
228+
- update_action (str): The update rule for the foreign key.
229+
- delete_action (str): The delete rule for the foreign key.
228230
- partitions (list): A list of partition dictionaries.
229231
- partition (dict): A dictionary representing a partition.
230232
- name (str): The name of the partition.

mysql/datadog_checks/mysql/queries.py

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,19 +161,32 @@
161161

162162
SQL_FOREIGN_KEYS = """\
163163
SELECT
164-
constraint_schema as `constraint_schema`,
165-
constraint_name as `name`,
166-
table_name as `table_name`,
167-
group_concat(column_name order by ordinal_position asc) as column_names,
168-
referenced_table_schema as `referenced_table_schema`,
169-
referenced_table_name as `referenced_table_name`,
170-
group_concat(referenced_column_name) as referenced_column_names
164+
kcu.constraint_schema as constraint_schema,
165+
kcu.constraint_name as name,
166+
kcu.table_name as table_name,
167+
group_concat(kcu.column_name order by kcu.ordinal_position asc) as column_names,
168+
kcu.referenced_table_schema as referenced_table_schema,
169+
kcu.referenced_table_name as referenced_table_name,
170+
group_concat(kcu.referenced_column_name) as referenced_column_names,
171+
rc.update_rule as update_action,
172+
rc.delete_rule as delete_action
171173
FROM
172-
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
174+
INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
175+
LEFT JOIN
176+
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
177+
ON kcu.CONSTRAINT_SCHEMA = rc.CONSTRAINT_SCHEMA
178+
AND kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
173179
WHERE
174-
table_schema = %s AND table_name in ({})
175-
AND referenced_table_name is not null
176-
GROUP BY constraint_schema, constraint_name, table_name, referenced_table_schema, referenced_table_name;
180+
kcu.table_schema = %s AND kcu.table_name in ({})
181+
AND kcu.referenced_table_name is not null
182+
GROUP BY
183+
kcu.constraint_schema,
184+
kcu.constraint_name,
185+
kcu.table_name,
186+
kcu.referenced_table_schema,
187+
kcu.referenced_table_name,
188+
rc.update_rule,
189+
rc.delete_rule
177190
"""
178191

179192
SQL_PARTITION = """\

mysql/tests/conftest.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,8 @@ def add_schema_test_databases(cursor):
473473
cursor.execute("CREATE DATABASE datadog_test_schemas;")
474474
cursor.execute("USE datadog_test_schemas;")
475475
cursor.execute("GRANT SELECT ON datadog_test_schemas.* TO 'dog'@'%';")
476+
# needed to query INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS in mariadb 10.5 and above
477+
cursor.execute("GRANT REFERENCES ON datadog_test_schemas.* TO 'dog'@'%';")
476478
cursor.execute(
477479
"""CREATE TABLE cities (
478480
id INT NOT NULL DEFAULT 0,
@@ -508,7 +510,8 @@ def add_schema_test_databases(cursor):
508510
"""CREATE TABLE landmarks (
509511
name VARCHAR(255),
510512
city_id INT DEFAULT 0,
511-
CONSTRAINT FK_CityId FOREIGN KEY (city_id) REFERENCES cities(id));
513+
CONSTRAINT FK_CityId FOREIGN KEY (city_id)
514+
REFERENCES cities(id) ON DELETE SET NULL ON UPDATE RESTRICT);
512515
"""
513516
)
514517

@@ -527,7 +530,7 @@ def add_schema_test_databases(cursor):
527530
District VARCHAR(255),
528531
Review TEXT,
529532
CONSTRAINT FK_RestaurantNameDistrict FOREIGN KEY (RestaurantName, District)
530-
REFERENCES Restaurants(RestaurantName, District));
533+
REFERENCES Restaurants(RestaurantName, District) ON DELETE CASCADE ON UPDATE NO ACTION);
531534
"""
532535
)
533536
# Second DB

mysql/tests/test_metadata.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def test_collect_schemas(aggregator, dd_run_check, dbm_instance):
156156
"referenced_table_schema": "datadog_test_schemas",
157157
"referenced_table_name": "Restaurants",
158158
"referenced_column_names": "District,RestaurantName",
159+
"update_action": "NO ACTION",
160+
"delete_action": "CASCADE",
159161
}
160162
],
161163
"indexes": [
@@ -458,6 +460,8 @@ def test_collect_schemas(aggregator, dd_run_check, dbm_instance):
458460
"referenced_table_schema": "datadog_test_schemas",
459461
"referenced_table_name": "cities",
460462
"referenced_column_names": "id",
463+
"update_action": "RESTRICT",
464+
"delete_action": "SET NULL",
461465
}
462466
],
463467
"indexes": [

0 commit comments

Comments
 (0)