Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

fix: Ignore other types of statements when parsing the SQL query (#19343)

Ivan Vasilovf1021685884c
diff --git a/apps/database-new/lib/utils.ts b/apps/database-new/lib/utils.ts
index 91acb6cc139..b494603ae97 100644
--- a/apps/database-new/lib/utils.ts
+++ b/apps/database-new/lib/utils.ts
@@ -2,8 +2,8 @@ import { parseQuery } from '@gregnr/libpg-query'
import { compact } from 'lodash'
import { z } from 'zod'
-import { PostgresColumn, PostgresTable } from './types'
import dayjs from 'dayjs'
+import { PostgresColumn, PostgresTable } from './types'
const constraintDefinitionSchema = z.object({
Constraint: z.discriminatedUnion('contype', [
@@ -75,12 +75,16 @@ const columnDefinitionSchema = z.object({
})
const tableDefinitionSchema = z.object({
- CreateStmt: z.object({
- relation: z.object({
- relname: z.string(),
- }),
- tableElts: z.array(z.union([columnDefinitionSchema, constraintDefinitionSchema])),
- }),
+ CreateStmt: z
+ .object({
+ relation: z.object({
+ relname: z.string(),
+ }),
+ tableElts: z.array(z.union([columnDefinitionSchema, constraintDefinitionSchema])),
+ })
+ // the optional() allows other types of statements, but only CreateStmt will be type-safe, we
+ // don't care for the other ones.
+ .optional(),
})
const parseQueryResultSchema = z.object({
@@ -109,9 +113,12 @@ export async function parseTables(sql: string) {
}
// This code generates all columns with their constraints
- const pgTables: PostgresTable[] = parsedSql.data.stmts
- .filter(({ stmt }) => 'CreateStmt' in stmt)
- .map(({ stmt }) => {
+ const pgTables: PostgresTable[] = compact(
+ parsedSql.data.stmts.map(({ stmt }) => {
+ if (!stmt.CreateStmt) {
+ return
+ }
+
const statement = stmt.CreateStmt
const columns = compact(
@@ -222,6 +229,7 @@ export async function parseTables(sql: string) {
return table
})
+ )
return pgTables
}