From 18146e239c138788cc899aa8b09cee82219cb727 Mon Sep 17 00:00:00 2001 From: Tigrov Date: Thu, 18 Jan 2024 17:11:21 +0700 Subject: [PATCH] Improve regex, add tests --- src/Schema.php | 4 ++-- tests/SchemaTest.php | 1 + tests/Support/Fixture/sqlite.sql | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Schema.php b/src/Schema.php index 1ddfab06..2532b1b8 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -771,8 +771,8 @@ private function findComments(TableSchemaInterface $tableSchema): void $columnsDefinition = $matches[1]; - $identifierPattern = '(?:([`"])([^`"]+)\1|\[([^\]]+)\]|([A-Za-z_]\w*))'; - $notCommaPattern = '(?:[^,]|\([^()]+\))*?'; + $identifierPattern = '(?:([`"])([^`"]+)\1|\[([^\]]+)\]|([a-zA-Z_]\w*))'; + $notCommaPattern = "(?:[^,]|\([^()]+\)|'[^']+')*?"; $commentPattern = '(?:\s*--[^\n]*|\s*/\*.*?\*/)'; $pattern = "#$identifierPattern\s*$notCommaPattern,?($commentPattern+)#"; diff --git a/tests/SchemaTest.php b/tests/SchemaTest.php index ca2f5484..071c2740 100644 --- a/tests/SchemaTest.php +++ b/tests/SchemaTest.php @@ -376,6 +376,7 @@ public function testColumnComments(): void $table = $schema->getTableSchema('comment'); $this->assertSame('primary key', $table->getColumn('id')->getComment()); + $this->assertSame('USD', $table->getColumn('price')->getComment()); $this->assertSame("Column comment\nsecond line\nthird line", $table->getColumn('name')->getComment()); } } diff --git a/tests/Support/Fixture/sqlite.sql b/tests/Support/Fixture/sqlite.sql index d38ed56e..90879882 100644 --- a/tests/Support/Fixture/sqlite.sql +++ b/tests/Support/Fixture/sqlite.sql @@ -179,7 +179,8 @@ CREATE TABLE `comment` -- Table comment /* third line */ ( `id` integer PRIMARY KEY AUTOINCREMENT NOT NULL, -- primary key - `name` varchar(100) -- Column comment + price decimal(10,2), -- USD + `name` varchar(100) DEFAULT 'Pan, Peter' -- Column comment -- second line /* third line */ );