mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-12-14 08:02:15 +03:00
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:
parent
d49c476176
commit
23057b6d4a
@ -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;
|
||||
|
@ -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',
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user