From 4e4b8f244ed98964c9e93bc70b2d1a754ffc34d5 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Mon, 5 Jul 2021 18:11:23 +0100 Subject: [PATCH] Giving the system the ability to make auto increment columns auto-columns in postgres or mysql. --- .../backend/DataTable/DataTable.svelte | 4 +--- .../scripts/integrations/mysql/init.sql | 17 ++++++++++++++-- .../scripts/integrations/mysql/reset.sh | 3 +++ .../scripts/integrations/postgres/init.sql | 20 +++++++++---------- packages/server/src/definitions/common.ts | 1 + packages/server/src/integrations/mysql.ts | 2 ++ packages/server/src/integrations/postgres.ts | 2 ++ 7 files changed, 34 insertions(+), 15 deletions(-) create mode 100755 packages/server/scripts/integrations/mysql/reset.sh diff --git a/packages/builder/src/components/backend/DataTable/DataTable.svelte b/packages/builder/src/components/backend/DataTable/DataTable.svelte index e87ad35184..c7bcd52e5d 100644 --- a/packages/builder/src/components/backend/DataTable/DataTable.svelte +++ b/packages/builder/src/components/backend/DataTable/DataTable.svelte @@ -72,9 +72,7 @@ {#if isUsersTable} {/if} - {#if isInternal} - - {/if} + {/if} diff --git a/packages/server/scripts/integrations/mysql/init.sql b/packages/server/scripts/integrations/mysql/init.sql index 3ffe84cd7d..8cd120a8a3 100644 --- a/packages/server/scripts/integrations/mysql/init.sql +++ b/packages/server/scripts/integrations/mysql/init.sql @@ -1,9 +1,22 @@ CREATE DATABASE IF NOT EXISTS main; USE main; CREATE TABLE Persons ( - PersonID int NOT NULL PRIMARY KEY, + PersonID int NOT NULL AUTO_INCREMENT, LastName varchar(255), FirstName varchar(255), Address varchar(255), - City varchar(255) + City varchar(255), + PRIMARY KEY (PersonID) ); +CREATE TABLE Tasks ( + TaskID int NOT NULL AUTO_INCREMENT, + PersonID INT, + TaskName varchar(255), + PRIMARY KEY (TaskID), + CONSTRAINT fkPersons + FOREIGN KEY(PersonID) + REFERENCES Persons(PersonID) +); +INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast'); +INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling'); +INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing'); diff --git a/packages/server/scripts/integrations/mysql/reset.sh b/packages/server/scripts/integrations/mysql/reset.sh new file mode 100755 index 0000000000..32778bd11f --- /dev/null +++ b/packages/server/scripts/integrations/mysql/reset.sh @@ -0,0 +1,3 @@ +#!/bin/bash +docker-compose down +docker volume prune -f diff --git a/packages/server/scripts/integrations/postgres/init.sql b/packages/server/scripts/integrations/postgres/init.sql index 37835af4a7..5a99520c1e 100644 --- a/packages/server/scripts/integrations/postgres/init.sql +++ b/packages/server/scripts/integrations/postgres/init.sql @@ -1,14 +1,14 @@ SELECT 'CREATE DATABASE main' WHERE NOT EXISTS (SELECT FROM pg_database WHERE datname = 'main')\gexec CREATE TABLE Persons ( - PersonID INT NOT NULL PRIMARY KEY, + PersonID SERIAL PRIMARY KEY, LastName varchar(255), FirstName varchar(255), Address varchar(255), - City varchar(255) + City varchar(255) DEFAULT 'Belfast' ); CREATE TABLE Tasks ( - TaskID INT NOT NULL PRIMARY KEY, + TaskID SERIAL PRIMARY KEY, PersonID INT, TaskName varchar(255), CONSTRAINT fkPersons @@ -16,7 +16,7 @@ CREATE TABLE Tasks ( REFERENCES Persons(PersonID) ); CREATE TABLE Products ( - ProductID INT NOT NULL PRIMARY KEY, + ProductID SERIAL PRIMARY KEY, ProductName varchar(255) ); CREATE TABLE Products_Tasks ( @@ -30,12 +30,12 @@ CREATE TABLE Products_Tasks ( REFERENCES Tasks(TaskID), PRIMARY KEY (ProductID, TaskID) ); -INSERT INTO Persons (PersonID, FirstName, LastName, Address, City) VALUES (1, 'Mike', 'Hughes', '123 Fake Street', 'Belfast'); -INSERT INTO Tasks (TaskID, PersonID, TaskName) VALUES (1, 1, 'assembling'); -INSERT INTO Tasks (TaskID, PersonID, TaskName) VALUES (2, 1, 'processing'); -INSERT INTO Products (ProductID, ProductName) VALUES (1, 'Computers'); -INSERT INTO Products (ProductID, ProductName) VALUES (2, 'Laptops'); -INSERT INTO Products (ProductID, ProductName) VALUES (3, 'Chairs'); +INSERT INTO Persons (FirstName, LastName, Address, City) VALUES ('Mike', 'Hughes', '123 Fake Street', 'Belfast'); +INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'assembling'); +INSERT INTO Tasks (PersonID, TaskName) VALUES (1, 'processing'); +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); diff --git a/packages/server/src/definitions/common.ts b/packages/server/src/definitions/common.ts index 497f8f68f2..ec837a8101 100644 --- a/packages/server/src/definitions/common.ts +++ b/packages/server/src/definitions/common.ts @@ -14,6 +14,7 @@ export interface FieldSchema { relationshipType?: string through?: string foreignKey?: string + autocolumn?: boolean constraints?: { type?: string email?: boolean diff --git a/packages/server/src/integrations/mysql.ts b/packages/server/src/integrations/mysql.ts index 1cfe96986f..6b6cbd1601 100644 --- a/packages/server/src/integrations/mysql.ts +++ b/packages/server/src/integrations/mysql.ts @@ -169,8 +169,10 @@ module MySQLModule { const constraints = { presence: column.Null !== "YES", } + const isAuto: boolean = typeof column.Extra === "string" && (column.Extra === "auto_increment" || column.Extra.toLowerCase().includes("generated")) schema[columnName] = { name: columnName, + autocolumn: isAuto, type: convertType(column.Type, TYPE_MAP), constraints, } diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 935bfbeeea..11628574d1 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -184,7 +184,9 @@ module PostgresModule { } const type: string = convertType(column.data_type, TYPE_MAP) + const isAuto: boolean = typeof column.column_default === "string" && column.column_default.startsWith("nextval") tables[tableName].schema[columnName] = { + autocolumn: isAuto, name: columnName, type, }