From 2d9dd1a66766073a91c816267425cd7c09c5528a Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 2 Dec 2022 16:17:56 +0000 Subject: [PATCH] Adding some null controls. --- .../scripts/integrations/mssql/data/setup.sql | 6 +-- .../integrations/postgres/emp-skill.sql | 51 ------------------- .../src/integrations/microsoftSqlServer.ts | 44 ++++++++++++++-- packages/server/src/integrations/oracle.ts | 1 + packages/server/src/integrations/postgres.ts | 4 ++ 5 files changed, 48 insertions(+), 58 deletions(-) diff --git a/packages/server/scripts/integrations/mssql/data/setup.sql b/packages/server/scripts/integrations/mssql/data/setup.sql index 5d2eeb8bb9..91c53cced7 100644 --- a/packages/server/scripts/integrations/mssql/data/setup.sql +++ b/packages/server/scripts/integrations/mssql/data/setup.sql @@ -11,7 +11,7 @@ GO CREATE TABLE products ( id int IDENTITY(1,1), - name varchar (20), + name varchar (20) NOT NULL, description varchar(30), CONSTRAINT pk_products PRIMARY KEY NONCLUSTERED (id) ); @@ -22,7 +22,7 @@ GO CREATE TABLE tasks ( taskid int IDENTITY(1,1), - taskname varchar (20), + taskname varchar (20) NOT NULL, productid int, CONSTRAINT pk_tasks PRIMARY KEY NONCLUSTERED (taskid), CONSTRAINT fk_products FOREIGN KEY (productid) REFERENCES products (id), @@ -33,7 +33,7 @@ IF OBJECT_ID ('dbo.people', 'U') IS NOT NULL GO CREATE TABLE people ( - name varchar(30), + name varchar(30) NOT NULL, age varchar(20), CONSTRAINT pk_people PRIMARY KEY NONCLUSTERED (name, age) ); diff --git a/packages/server/scripts/integrations/postgres/emp-skill.sql b/packages/server/scripts/integrations/postgres/emp-skill.sql index 5060b58a28..41c0427a05 100644 --- a/packages/server/scripts/integrations/postgres/emp-skill.sql +++ b/packages/server/scripts/integrations/postgres/emp-skill.sql @@ -1,14 +1,5 @@ SELECT 'CREATE DATABASE main' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec -CREATE SCHEMA test; -CREATE TYPE person_job AS ENUM ('qa', 'programmer', 'designer'); -CREATE TABLE Persons ( - PersonID SERIAL PRIMARY KEY, - LastName varchar(255), - FirstName varchar(255), - Address varchar(255), - City varchar(255) DEFAULT 'Belfast', - Type person_job CREATE TABLE IF NOT EXISTS public."Employee" ( @@ -19,18 +10,6 @@ CREATE TABLE IF NOT EXISTS public."Employee" WITH ( OIDS = FALSE ); -CREATE TABLE Tasks ( - TaskID SERIAL PRIMARY KEY, - ExecutorID INT, - QaID INT, - Completed BOOLEAN, - TaskName varchar(255), - CONSTRAINT fkexecutor - FOREIGN KEY(ExecutorID) - REFERENCES Persons(PersonID), - CONSTRAINT fkqa - FOREIGN KEY(QaID) - REFERENCES Persons(PersonID) INSERT INTO public."Employee" ("id", "name") VALUES (1, 'Alice'); INSERT INTO public."Employee" ("id", "name") VALUES (2, 'Bob'); @@ -43,9 +22,6 @@ CREATE TABLE IF NOT EXISTS public."Skills" WITH ( OIDS = FALSE ); -CREATE TABLE Products ( - ProductID SERIAL PRIMARY KEY, - ProductName varchar(255) INSERT INTO public."Skills" ("id", "name") VALUES (1, 'Docker'); INSERT INTO public."Skills" ("id", "name") VALUES (2, 'Microservices'); @@ -61,33 +37,6 @@ CREATE TABLE IF NOT EXISTS public."jt_employee_skills_Skills_employee" WITH ( OIDS = FALSE ); -CREATE TABLE Products_Tasks ( - ProductID INT NOT NULL, - TaskID INT NOT NULL, - CONSTRAINT fkProducts - FOREIGN KEY(ProductID) - REFERENCES Products(ProductID), - CONSTRAINT fkTasks - FOREIGN KEY(TaskID) - REFERENCES Tasks(TaskID), - PRIMARY KEY (ProductID, TaskID) -); -CREATE TABLE test.table1 ( - id SERIAL PRIMARY KEY, - Name varchar(255) -); -INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast', 'qa'); -INSERT INTO Persons (FirstName, LastName, Address, City, Type) VALUES ('John', 'Smith', '64 Updown Road', 'Dublin', 'programmer'); -INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (1, 2, 'assembling', TRUE); -INSERT INTO Tasks (ExecutorID, QaID, TaskName, Completed) VALUES (2, 1, 'processing', FALSE); -INSERT INTO Products (ProductName) VALUES ('Computers'); -INSERT INTO Products (ProductName) VALUES ('Laptops'); -INSERT INTO Products (ProductName) VALUES ('Chairs'); -INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 1); -INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (2, 1); -INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (3, 1); -INSERT INTO Products_Tasks (ProductID, TaskID) VALUES (1, 2); -INSERT INTO test.table1 (Name) VALUES ('Test'); insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (1, 1, 1); insert into public."jt_employee_skills_Skills_employee" ("id", "employee_id", "skills_id") VALUES (2, 1, 2); diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index 0746d0e3bc..8c136dcc0f 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -38,6 +38,34 @@ interface TablesResponse { TABLE_TYPE: string } +type MSSQLColumn = { + IS_COMPUTED: number + IS_IDENTITY: number + TABLE_CATALOG: string + TABLE_SCHEMA: string + TABLE_NAME: string + COLUMN_NAME: string + ORDINAL_POSITION: number + COLUMN_DEFAULT: null | any + IS_NULLABLE: "NO" | "YES" + DATA_TYPE: string + CHARACTER_MAXIMUM_LENGTH: null | number + CHARACTER_OCTET_LENGTH: null | number + NUMERIC_PRECISION: null | string + NUMERIC_PRECISION_RADIX: null | string + NUMERIC_SCALE: null | string + DATETIME_PRECISION: null | string + CHARACTER_SET_CATALOG: null | string + CHARACTER_SET_SCHEMA: null | string + CHARACTER_SET_NAME: null | string + COLLATION_CATALOG: null | string + COLLATION_SCHEMA: null | string + COLLATION_NAME: null | string + DOMAIN_CATALOG: null | string + DOMAIN_SCHEMA: null | string + DOMAIN_NAME: null | string +} + const SCHEMA: Integration = { docs: "https://github.com/tediousjs/node-mssql", plus: true, @@ -228,15 +256,20 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { // find primary key constraints const constraints = await this.runSQL(this.getConstraintsSQL(tableName)) // find the computed and identity columns (auto columns) - const columns = await this.runSQL(this.getAutoColumnsSQL(tableName)) + const columns: MSSQLColumn[] = await this.runSQL( + this.getAutoColumnsSQL(tableName) + ) const primaryKeys = constraints .filter( (constraint: any) => constraint.CONSTRAINT_TYPE === "PRIMARY KEY" ) .map((constraint: any) => constraint.COLUMN_NAME) const autoColumns = columns - .filter((col: any) => col.IS_COMPUTED || col.IS_IDENTITY) - .map((col: any) => col.COLUMN_NAME) + .filter(col => col.IS_COMPUTED || col.IS_IDENTITY) + .map(col => col.COLUMN_NAME) + const requiredColumns = columns + .filter(col => col.IS_NULLABLE === "NO") + .map(col => col.COLUMN_NAME) let schema: TableSchema = {} for (let def of definition) { @@ -245,8 +278,11 @@ class SqlServerIntegration extends Sql implements DatasourcePlus { continue } schema[name] = { - autocolumn: !!autoColumns.find((col: string) => col === name), + autocolumn: !!autoColumns.find(col => col === name), name: name, + constraints: { + presence: requiredColumns.find(col => col === name), + }, ...convertSqlType(def.DATA_TYPE), externalType: def.DATA_TYPE, } diff --git a/packages/server/src/integrations/oracle.ts b/packages/server/src/integrations/oracle.ts index 101d459ec0..13c2d11b8b 100644 --- a/packages/server/src/integrations/oracle.ts +++ b/packages/server/src/integrations/oracle.ts @@ -334,6 +334,7 @@ class OracleIntegration extends Sql implements DatasourcePlus { fieldSchema = { autocolumn: OracleIntegration.isAutoColumn(oracleColumn), name: columnName, + // TODO: add required constraint ...this.internalConvertType(oracleColumn), } table.schema[columnName] = fieldSchema diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index da1124f421..c0bda099eb 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -260,6 +260,9 @@ class PostgresIntegration extends Sql implements DatasourcePlus { column.identity_start || column.identity_increment ) + const constraints = { + presence: column.is_nullable === "NO", + } const hasDefault = typeof column.column_default === "string" && column.column_default.startsWith("nextval") @@ -269,6 +272,7 @@ class PostgresIntegration extends Sql implements DatasourcePlus { tables[tableName].schema[columnName] = { autocolumn: isAuto, name: columnName, + constraints, ...convertSqlType(column.data_type), externalType: column.data_type, }