From f70cee3c52ee581a6cf090b031f378ff01babb47 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 22 Sep 2021 17:46:54 +0100 Subject: [PATCH] Fixing issue #2673 - discovered issues with some column types not being worked out to be auto-columns in postgres. --- .../service-vehicles/docker-compose.yml | 28 ++++++++++ .../integrations/service-vehicles/init.sql | 52 +++++++++++++++++++ .../integrations/service-vehicles/reset.sh | 3 ++ .../api/controllers/row/ExternalRequest.ts | 6 ++- packages/server/src/integrations/mysql.ts | 15 +----- packages/server/src/integrations/postgres.ts | 25 ++++----- packages/server/src/integrations/utils.ts | 18 +++++++ 7 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 packages/server/scripts/integrations/service-vehicles/docker-compose.yml create mode 100644 packages/server/scripts/integrations/service-vehicles/init.sql create mode 100755 packages/server/scripts/integrations/service-vehicles/reset.sh diff --git a/packages/server/scripts/integrations/service-vehicles/docker-compose.yml b/packages/server/scripts/integrations/service-vehicles/docker-compose.yml new file mode 100644 index 0000000000..7473e540db --- /dev/null +++ b/packages/server/scripts/integrations/service-vehicles/docker-compose.yml @@ -0,0 +1,28 @@ +version: "3.8" +services: + db: + container_name: postgres-vehicle + image: postgres + restart: always + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: main + ports: + - "5432:5432" + volumes: + #- pg_data:/var/lib/postgresql/data/ + - ./init.sql:/docker-entrypoint-initdb.d/init.sql + + pgadmin: + container_name: pgadmin + image: dpage/pgadmin4 + restart: always + environment: + PGADMIN_DEFAULT_EMAIL: root@root.com + PGADMIN_DEFAULT_PASSWORD: root + ports: + - "5050:80" + +#volumes: +# pg_data: diff --git a/packages/server/scripts/integrations/service-vehicles/init.sql b/packages/server/scripts/integrations/service-vehicles/init.sql new file mode 100644 index 0000000000..3e0485313e --- /dev/null +++ b/packages/server/scripts/integrations/service-vehicles/init.sql @@ -0,0 +1,52 @@ +SELECT 'CREATE DATABASE main' +WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec +CREATE TABLE Vehicles ( + id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ), + Registration text COLLATE pg_catalog."default", + Make text COLLATE pg_catalog."default", + Model text COLLATE pg_catalog."default", + Colour text COLLATE pg_catalog."default", + Year smallint, + CONSTRAINT Vehicles_pkey PRIMARY KEY (id) +); + +CREATE TABLE ServiceLog ( + id bigint NOT NULL GENERATED ALWAYS AS IDENTITY ( INCREMENT 1 START 1 MINVALUE 1 MAXVALUE 9223372036854775807 CACHE 1 ), + Description text COLLATE pg_catalog."default", + VehicleId bigint, + ServiceDate timestamp without time zone, + Category text COLLATE pg_catalog."default", + Mileage bigint, + CONSTRAINT ServiceLog_pkey PRIMARY KEY (id), + CONSTRAINT vehicle_foreign_key FOREIGN KEY (VehicleId) + REFERENCES Vehicles (id) MATCH SIMPLE + ON UPDATE NO ACTION + ON DELETE NO ACTION +); + +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('FAZ 9837','Volkswagen','Polo','White',2002); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('JHI 8827','BMW','M3','Black',2013); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('D903PI','Volvo','XC40','Grey',2014); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('YFI002','Volkswagen','Golf','Dark Blue',2018); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('HGT5677','Skoda','Octavia','Graphite',2009); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('PPF9276','Skoda','Octavia','Graphite',2021); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('J893FT','Toyota','Corolla','Red',2015); +INSERT INTO Vehicles (Registration, Make, Model, Colour, Year) +VALUES ('MJK776','Honda','HR-V','Silver',2015); + + +INSERT INTO ServiceLog (Description, VehicleId, ServiceDate, Category, Mileage) +VALUES ('Change front brakes', 1, '2021-05-04', 'Brakes', 20667); +INSERT INTO ServiceLog (Description, VehicleId, ServiceDate, Category, Mileage) +VALUES ('Tyres - full set', 1, '2021-05-04', 'Brakes', 20667); +INSERT INTO ServiceLog (Description, VehicleId, ServiceDate, Category, Mileage) +VALUES ('Engine tune up', 2, '2021-07-14', 'Brakes', 50889); +INSERT INTO ServiceLog (Description, VehicleId, ServiceDate, Category, Mileage) +VALUES ('Replace transmission', 3, '2021-09-26', 'Transmission', 98002); diff --git a/packages/server/scripts/integrations/service-vehicles/reset.sh b/packages/server/scripts/integrations/service-vehicles/reset.sh new file mode 100755 index 0000000000..32778bd11f --- /dev/null +++ b/packages/server/scripts/integrations/service-vehicles/reset.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker-compose down +docker volume prune -f diff --git a/packages/server/src/api/controllers/row/ExternalRequest.ts b/packages/server/src/api/controllers/row/ExternalRequest.ts index eced518604..b809e597e4 100644 --- a/packages/server/src/api/controllers/row/ExternalRequest.ts +++ b/packages/server/src/api/controllers/row/ExternalRequest.ts @@ -437,7 +437,11 @@ module External { for (let [colName, { isMany, rows, tableId }] of Object.entries( related )) { - const table = this.getTable(tableId) + const table: Table = this.getTable(tableId) + // if its not the foreign key skip it, nothing to do + if (table.primary && table.primary.indexOf(colName) !== -1) { + continue + } for (let row of rows) { const filters = buildFilters(generateIdForRow(row, table), {}, table) // safety check, if there are no filters on deletion bad things happen diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 3ce21675d9..c5db35ed2a 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -12,7 +12,7 @@ import { getSqlQuery } from "./utils" module MySQLModule { const mysql = require("mysql") const Sql = require("./base/sql") - const { buildExternalTableId, convertType } = require("./utils") + const { buildExternalTableId, convertType, copyExistingPropsOver } = require("./utils") const { FieldTypes } = require("../constants") interface MySQLConfig { @@ -194,18 +194,7 @@ module MySQLModule { } } - // add the existing relationships from the entities if they exist, to prevent them from being overridden - if (entities && entities[tableName]) { - const existingTableSchema = entities[tableName].schema - for (let key in existingTableSchema) { - if (!existingTableSchema.hasOwnProperty(key)) { - continue - } - if (existingTableSchema[key].type === "link") { - tables[tableName].schema[key] = existingTableSchema[key] - } - } - } + copyExistingPropsOver(tableName, tables, entities) } this.client.end() diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index dd46652871..63719980fb 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -12,7 +12,7 @@ module PostgresModule { const { Pool } = require("pg") const Sql = require("./base/sql") const { FieldTypes } = require("../constants") - const { buildExternalTableId, convertType } = require("./utils") + const { buildExternalTableId, convertType, copyExistingPropsOver } = require("./utils") interface PostgresConfig { host: string @@ -173,31 +173,24 @@ module PostgresModule { name: tableName, schema: {}, } - - // add the existing relationships from the entities if they exist, to prevent them from being overridden - if (entities && entities[tableName]) { - const existingTableSchema = entities[tableName].schema - for (let key in existingTableSchema) { - if (!existingTableSchema.hasOwnProperty(key)) { - continue - } - if (existingTableSchema[key].type === "link") { - tables[tableName].schema[key] = existingTableSchema[key] - } - } - } } const type: string = convertType(column.data_type, TYPE_MAP) - const isAuto: boolean = - typeof column.column_default === "string" && + const identity = !!(column.identity_generation || column.identity_start || column.identity_increment) + const hasDefault = typeof column.column_default === "string" && column.column_default.startsWith("nextval") + const isGenerated = column.is_generated && column.is_generated !== "NEVER" + const isAuto: boolean = hasDefault || identity || isGenerated tables[tableName].schema[columnName] = { autocolumn: isAuto, name: columnName, type, } } + + for (let tableName of Object.keys(tables)) { + copyExistingPropsOver(tableName, tables, entities) + } this.tables = tables } diff --git a/packages/server/src/integrations/utils.ts b/packages/server/src/integrations/utils.ts index 5b247213c0..82c35bc2d9 100644 --- a/packages/server/src/integrations/utils.ts +++ b/packages/server/src/integrations/utils.ts @@ -82,3 +82,21 @@ export function isIsoDateString(str: string) { let d = new Date(str) return d.toISOString() === str } + +// add the existing relationships from the entities if they exist, to prevent them from being overridden +export function copyExistingPropsOver(tableName: string, tables: { [key: string]: any }, entities: { [key: string]: any }) { + if (entities && entities[tableName]) { + if (entities[tableName].primaryDisplay) { + tables[tableName].primaryDisplay = entities[tableName].primaryDisplay + } + const existingTableSchema = entities[tableName].schema + for (let key in existingTableSchema) { + if (!existingTableSchema.hasOwnProperty(key)) { + continue + } + if (existingTableSchema[key].type === "link") { + tables[tableName].schema[key] = existingTableSchema[key] + } + } + } +}