Skip to content

Commit

Permalink
Regex and test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahramadan committed Aug 1, 2024
1 parent b4449b4 commit 7bb9db0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module PG
module Patches
# Module to prepend to PG::Connection for instrumentation
module Connection # rubocop:disable Metrics/ModuleLength
# Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands
TABLE_NAME = /\b(?:FROM|INTO|UPDATE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|DROP\s+TABLE(?:\s+IF\s+EXISTS)?|ALTER\s+TABLE(?:\s+IF\s+EXISTS)?)\s+([\w\.]+)/i

PG::Constants::EXEC_ISH_METHODS.each do |method|
define_method method do |*args, &block|
span_name, attrs = span_attrs(:query, *args)
Expand Down Expand Up @@ -129,10 +132,7 @@ def validated_operation(operation)
end

def collection_name(text)
# Capture the first word (including letters, digits, underscores, & '.', ) that follows common table commands
pattern = /\b(?:FROM|INTO|UPDATE|CREATE\s+TABLE(?:\s+IF\s+NOT\s+EXISTS)?|DROP\s+TABLE\s+IF\s+EXISTS)\s+([\w\.]+)/i

text.scan(pattern).flatten[0]
text.scan(TABLE_NAME).flatten[0]
end

def client_attributes
Expand Down
26 changes: 11 additions & 15 deletions instrumentation/pg/test/fixtures/sql_table_name.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,50 @@
},
{
"name": "select_count_from",
"sql": "SELECT COUNT(*) FROM table_name WHERE condition"
"sql": "SELECT COUNT(*) FROM test_table WHERE condition"
},
{
"name": "from_with_subquery",
"sql": "SELECT * FROM (SELECT * FROM table_name) AS table_alias"
"sql": "SELECT * FROM (SELECT * FROM test_table) AS table_alias"
},
{
"name": "insert_into",
"sql": "INSERT INTO table_name (column1, column2) VALUES (value1, value2)"
},
{
"name": "drop_table",
"sql": "DROP TABLE table_name"
},
{
"name": "update",
"sql": "UPDATE table_name SET column1 = value1 WHERE condition"
"sql": "UPDATE test_table SET column1 = value1 WHERE condition"
},
{
"name": "delete_from",
"sql": "DELETE FROM table_name WHERE condition"
"sql": "DELETE FROM test_table WHERE condition"
},
{
"name": "create_table",
"sql": "CREATE TABLE table_name (column1 datatype, column2 datatype)"
"sql": "CREATE TABLE test_table (column1 datatype, column2 datatype)"
},
{
"name": "create_table_if_not_exists",
"sql": "CREATE TABLE IF NOT EXISTS table_name (column1 datatype, column2 datatype)"
"sql": "CREATE TABLE IF NOT EXISTS test_table (column1 datatype, column2 datatype)"
},
{
"name": "alter_table",
"sql": "ALTER TABLE table_name ADD column_name datatype"
"sql": "ALTER TABLE test_table ADD column_name datatype"
},
{
"name": "drop_table",
"sql": "DROP TABLE table_name"
"sql": "DROP TABLE test_table"
},
{
"name": "drop_table_if_exists",
"sql": "DROP TABLE IF EXISTS table_name"
"sql": "DROP TABLE IF EXISTS test_table"
},
{
"name": "insert_into",
"sql": "INSERT INTO X values('', 'a''b c',0, 1 , 'd''e f''s h')"
"sql": "INSERT INTO test_table values('', 'a''b c',0, 1 , 'd''e f''s h')"
},
{
"name": "from_with_join",
"sql": "SELECT columns FROM table1 JOIN table2 ON table1.column = table2.column"
"sql": "SELECT columns FROM test_table JOIN table2 ON table1.column = table2.column"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def self.load_fixture
define_method(:"test_sql_table_name_#{name}") do
table_name = client.send(:collection_name, query)

assert_equal('table_name', table_name) # TODO: use an expected name from fixtures or update fixtures to always use "table_name"
assert_equal('test_table', table_name) # TODO: use an expected name from fixtures or update fixtures to always use "table_name"
end
end
end
Expand Down

0 comments on commit 7bb9db0

Please sign in to comment.