Giving the system the ability to make auto increment columns auto-columns in postgres or mysql.

This commit is contained in:
mike12345567 2021-07-05 18:11:23 +01:00
parent eaf808b190
commit 8550d00751
7 changed files with 34 additions and 15 deletions

View File

@ -72,9 +72,7 @@
{#if isUsersTable}
<EditRolesButton />
{/if}
{#if isInternal}
<HideAutocolumnButton bind:hideAutocolumns />
{/if}
<HideAutocolumnButton bind:hideAutocolumns />
<!-- always have the export last -->
<ExportButton view={$tables.selected?._id} />
{/if}

View File

@ -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');

View File

@ -0,0 +1,3 @@
#!/bin/bash
docker-compose down
docker volume prune -f

View File

@ -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);

View File

@ -14,6 +14,7 @@ export interface FieldSchema {
relationshipType?: string
through?: string
foreignKey?: string
autocolumn?: boolean
constraints?: {
type?: string
email?: boolean

View File

@ -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,
}

View File

@ -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,
}