Skip to content

Commit

Permalink
NEOS-969: filter out defaults (nucleuscloud#1843)
Browse files Browse the repository at this point in the history
  • Loading branch information
evisdrenova authored Apr 29, 2024
1 parent 26ec35c commit 3b9a86f
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 289 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[typescriptreact]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
Expand Down
12 changes: 6 additions & 6 deletions backend/gen/go/db/dbschemas/postgresql/system.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

444 changes: 229 additions & 215 deletions backend/gen/go/protos/mgmt/v1alpha1/connection_data.pb.go

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 7 additions & 15 deletions backend/pkg/dbschemas/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,15 @@ func generateCreateTableStatement(

func buildTableCol(record *pg_queries.GetDatabaseTableSchemaRow) string {
pieces := []string{escapeColumnName(record.ColumnName), buildDataType(record), buildNullableText(record)}
colDefault, ok := record.ColumnDefault.(string)
if ok && colDefault != "" {
if strings.HasPrefix(colDefault, "nextval") && record.DataType == "integer" {
if record.ColumnDefault != "" {
if strings.HasPrefix(record.ColumnDefault, "nextval") && record.DataType == "integer" {
pieces[1] = "SERIAL"
} else if strings.HasPrefix(colDefault, "nextval") && record.DataType == "bigint" {
} else if strings.HasPrefix(record.ColumnDefault, "nextval") && record.DataType == "bigint" {
pieces[1] = "BIGSERIAL"
} else if strings.HasPrefix(colDefault, "nextval") && record.DataType == "smallint" {
} else if strings.HasPrefix(record.ColumnDefault, "nextval") && record.DataType == "smallint" {
pieces[1] = "SMALLSERIAL"
} else if colDefault != "NULL" {
pieces = append(pieces, "DEFAULT", colDefault)
} else if record.ColumnDefault != "NULL" {
pieces = append(pieces, "DEFAULT", record.ColumnDefault)
}
}
return strings.Join(pieces, " ")
Expand Down Expand Up @@ -168,16 +167,9 @@ func GetUniqueSchemaColMappings(
}

func toColumnInfo(row *pg_queries.GetDatabaseSchemaRow) *dbschemas.ColumnInfo {
var colDefault string
if row.ColumnDefault != nil {
val, ok := row.ColumnDefault.(string)
if ok {
colDefault = val
}
}
return &dbschemas.ColumnInfo{
OrdinalPosition: int32(row.OrdinalPosition),
ColumnDefault: colDefault,
ColumnDefault: row.ColumnDefault,
IsNullable: row.IsNullable,
DataType: row.DataType,
CharacterMaximumLength: ptr(row.CharacterMaximumLength),
Expand Down
8 changes: 4 additions & 4 deletions backend/pkg/dbschemas/sql/postgresql/queries/system.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ SELECT
c.relname AS table_name,
a.attname AS column_name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type, -- This formats the type into something that should always be a valid postgres type. It also includes constraints if there are any
COALESCE(
COALESCE(
pg_catalog.pg_get_expr(d.adbin, d.adrelid),
''
) AS column_default,
)::text AS column_default,
CASE
WHEN a.attnotnull THEN 'NO'
ELSE 'YES'
Expand Down Expand Up @@ -67,10 +67,10 @@ SELECT
c.relname AS table_name,
a.attname AS column_name,
pg_catalog.format_type(a.atttypid, a.atttypmod) AS data_type, -- This formats the type into something that should always be a valid postgres type. It also includes constraints if there are any
COALESCE(
COALESCE(
pg_catalog.pg_get_expr(d.adbin, d.adrelid),
''
) AS column_default,
)::text AS column_default,
CASE
WHEN a.attnotnull THEN 'NO'
ELSE 'YES'
Expand Down
9 changes: 1 addition & 8 deletions backend/pkg/sqlmanager/postgres-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,12 @@ func (p *PostgresManager) GetDatabaseSchema(ctx context.Context) ([]*DatabaseSch
}
result := []*DatabaseSchemaRow{}
for _, row := range dbschemas {
var colDefault string
if row.ColumnDefault != nil {
val, ok := row.ColumnDefault.(string)
if ok {
colDefault = val
}
}
result = append(result, &DatabaseSchemaRow{
TableSchema: row.TableSchema,
TableName: row.TableName,
ColumnName: row.ColumnName,
DataType: row.DataType,
ColumnDefault: colDefault,
ColumnDefault: row.ColumnDefault,
IsNullable: row.IsNullable,
CharacterMaximumLength: row.CharacterMaximumLength,
NumericPrecision: row.NumericPrecision,
Expand Down
2 changes: 2 additions & 0 deletions backend/protos/mgmt/v1alpha1/connection_data.proto
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ message DatabaseColumn {
string data_type = 4;
// The isNullable Flag of the column
string is_nullable = 5;
// The default value of the column if available
optional string column_default = 6;
}

message GetConnectionSchemaRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,19 @@ func (s *Service) GetConnectionSchema(

schemas := []*mgmtv1alpha1.DatabaseColumn{}
for _, col := range dbschema {
var defaultColumn *string
if col.ColumnDefault != "" {
defaultVal := col.ColumnDefault
defaultColumn = &defaultVal
}

schemas = append(schemas, &mgmtv1alpha1.DatabaseColumn{
Schema: col.TableSchema,
Table: col.TableName,
Column: col.ColumnName,
DataType: col.DataType,
IsNullable: col.IsNullable,
Schema: col.TableSchema,
Table: col.TableName,
Column: col.ColumnName,
DataType: col.DataType,
IsNullable: col.IsNullable,
ColumnDefault: defaultColumn,
})
}

Expand Down Expand Up @@ -404,12 +411,19 @@ func (s *Service) GetConnectionSchema(

schemas := []*mgmtv1alpha1.DatabaseColumn{}
for _, col := range dbschema {
var defaultColumn *string
if col.ColumnDefault != "" {
defaultVal := col.ColumnDefault
defaultColumn = &defaultVal
}

schemas = append(schemas, &mgmtv1alpha1.DatabaseColumn{
Schema: col.TableSchema,
Table: col.TableName,
Column: col.ColumnName,
DataType: col.DataType,
IsNullable: col.IsNullable,
Schema: col.TableSchema,
Table: col.TableName,
Column: col.ColumnName,
DataType: col.DataType,
IsNullable: col.IsNullable,
ColumnDefault: defaultColumn,
})
}

Expand Down
14 changes: 13 additions & 1 deletion docs/protos/data/proto_docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2640,7 +2640,7 @@
"description": "",
"hasExtensions": false,
"hasFields": true,
"hasOneofs": false,
"hasOneofs": true,
"extensions": [],
"fields": [
{
Expand Down Expand Up @@ -2702,6 +2702,18 @@
"isoneof": false,
"oneofdecl": "",
"defaultValue": ""
},
{
"name": "column_default",
"description": "The default value of the column if available",
"label": "optional",
"type": "string",
"longType": "string",
"fullType": "string",
"ismap": false,
"isoneof": true,
"oneofdecl": "_column_default",
"defaultValue": ""
}
]
},
Expand Down
9 changes: 1 addition & 8 deletions frontend/apps/web/app/(mgmt)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
PostHogPageview,
} from '@/components/providers/posthog-provider';
import { ThemeProvider } from '@/components/providers/theme-provider';
import { fontSans } from '@/libs/fonts';
import { cn } from '@/libs/utils';
import { Metadata } from 'next';
import { ReactElement, Suspense } from 'react';
import BaseLayout from '../BaseLayout';
Expand All @@ -24,12 +22,7 @@ export default async function RootLayout({
return (
<html lang="en" suppressHydrationWarning>
<head />
<body
className={cn(
'min-h-screen bg-background font-sans antialiased overflow-scroll',
fontSans.variable
)}
>
<body className="min-h-screen bg-background font-sans antialiased overflow-scroll">
<ThemeProvider
attribute="class"
defaultTheme="system"
Expand Down
9 changes: 1 addition & 8 deletions frontend/apps/web/app/invite/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
PostHogPageview,
} from '@/components/providers/posthog-provider';
import { ThemeProvider } from '@/components/providers/theme-provider';
import { fontSans } from '@/libs/fonts';
import { cn } from '@/libs/utils';
import { Metadata } from 'next';
import { ReactElement, ReactNode, Suspense } from 'react';
import BaseLayout from '../BaseLayout';
Expand All @@ -24,12 +22,7 @@ export default async function InviteLayout({
return (
<html lang="en" suppressHydrationWarning>
<head />
<body
className={cn(
'min-h-screen bg-background font-sans antialiased overflow-scroll',
fontSans.variable
)}
>
<body className="min-h-screen bg-background font-sans antialiased overflow-scroll">
<ThemeProvider
attribute="class"
defaultTheme="system"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface ColDetails {
isNullable: boolean;
dataType: string;
isUniqueConstraint: boolean;
columnDefault: string;
}

export function getSchemaConstraintHandler(
Expand All @@ -48,6 +49,7 @@ export function getSchemaConstraintHandler(
foreignConstraints,
uniqueConstraints
);

return {
getDataType(key) {
return colmap[fromColKey(key)]?.dataType ?? '';
Expand All @@ -74,8 +76,8 @@ export function getSchemaConstraintHandler(
getIsInSchema(key) {
return !!colmap[fromColKey(key)];
},
getHasDefault(_key) {
return true; // todo - NEOS-969
getHasDefault(key) {
return !!colmap[fromColKey(key)]?.columnDefault;
},
};
}
Expand Down Expand Up @@ -213,6 +215,7 @@ function buildColDetailsMap(
],
isPrimaryKey: primaryCols.has(dbcol.column),
isUniqueConstraint: uniqueConstraintCols.has(dbcol.column),
columnDefault: dbcol.columnDefault ?? '',
};
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ function shouldIncludeSystem(
transformer: SystemTransformer,
filters: TransformerFilters
): boolean {
if (
filters.hasDefault &&
transformer.source === TransformerSource.GENERATE_DEFAULT
) {
return true;
if (transformer.source === TransformerSource.GENERATE_DEFAULT) {
return filters.hasDefault;
}
if (filters.isForeignKey) {
if (filters.isNullable) {
Expand Down
6 changes: 0 additions & 6 deletions frontend/apps/web/libs/fonts.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ export class DatabaseColumn extends Message<DatabaseColumn> {
*/
isNullable = "";

/**
* The default value of the column if available
*
* @generated from field: optional string column_default = 6;
*/
columnDefault?: string;

constructor(data?: PartialMessage<DatabaseColumn>) {
super();
proto3.util.initPartial(data, this);
Expand All @@ -492,6 +499,7 @@ export class DatabaseColumn extends Message<DatabaseColumn> {
{ no: 3, name: "column", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 4, name: "data_type", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 5, name: "is_nullable", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 6, name: "column_default", kind: "scalar", T: 9 /* ScalarType.STRING */, opt: true },
]);

static fromBinary(bytes: Uint8Array, options?: Partial<BinaryReadOptions>): DatabaseColumn {
Expand Down

0 comments on commit 3b9a86f

Please sign in to comment.