Fix tracking of tables created with Run SQL with SQL comments

[DSF-420]: https://hasurahq.atlassian.net/browse/DSF-420?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/9442
GitOrigin-RevId: 94fcb5e8a6073f359ee64cd7cbb0fdaf7218782b
This commit is contained in:
Luca Restagno 2023-06-13 20:50:32 +02:00 committed by hasura-bot
parent d49c476176
commit 23057b6d4a
2 changed files with 49 additions and 1 deletions

View File

@ -39,8 +39,9 @@ const getDefaultSchema = driver => {
*/
export const parseCreateSQL = (sql, driver = currentDriver) => {
const _objects = [];
const sanitizedSql = removeCommentsSQL(sql);
const regExp = services[driver].createSQLRegex;
for (const result of sql.matchAll(regExp)) {
for (const result of sanitizedSql.matchAll(regExp)) {
const { type, schema, name, nameWithSchema, partition } =
result.groups ?? {};
if (!type || !(name || nameWithSchema)) continue;

View File

@ -0,0 +1,47 @@
import { parseCreateSQL, removeCommentsSQL } from './utils';
describe('removeCommentsSQL', () => {
it('removes comments', () => {
const sql = `
-- Create tables in the public schema
CREATE TABLE public.locations (
location_id SERIAL PRIMARY KEY, // comment
name VARCHAR(100) NOT NULL,
address VARCHAR(200) NOT NULL
);
/* another comment */
`;
expect(removeCommentsSQL(sql).trim()).toEqual(
`
CREATE TABLE public.locations (
location_id SERIAL PRIMARY KEY, // comment
name VARCHAR(100) NOT NULL,
address VARCHAR(200) NOT NULL
);
`.trim()
);
});
});
describe('parseCreateSQL', () => {
it('return the correct tables', () => {
const sql = `
-- Create tables in the public schema
CREATE TABLE public.locations (
location_id SERIAL PRIMARY KEY, // comment
name VARCHAR(100) NOT NULL,
address VARCHAR(200) NOT NULL
);
/* another comment */
`;
expect(parseCreateSQL(sql, 'postgres')).toEqual([
{
isPartition: false,
name: 'locations',
schema: 'public',
type: 'table',
},
]);
});
});