From 7f9334ae5ec192bbf865c1b492fcf2cf8de8cea0 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 4 Mar 2022 15:50:19 +0000 Subject: [PATCH] Making sure that tables are created within the correct schema for MS-SQL. --- .../scripts/integrations/postgres/init.sql | 6 ++++++ .../server/src/integrations/base/sqlTable.ts | 18 +++++++++++------- .../src/integrations/microsoftSqlServer.ts | 1 - 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/packages/server/scripts/integrations/postgres/init.sql b/packages/server/scripts/integrations/postgres/init.sql index c7dd5acebc..78af4c20b9 100644 --- a/packages/server/scripts/integrations/postgres/init.sql +++ b/packages/server/scripts/integrations/postgres/init.sql @@ -1,5 +1,6 @@ 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, @@ -37,6 +38,10 @@ CREATE TABLE Products_Tasks ( 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); @@ -48,3 +53,4 @@ 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'); diff --git a/packages/server/src/integrations/base/sqlTable.ts b/packages/server/src/integrations/base/sqlTable.ts index 47566bdcd5..b9ea7107fc 100644 --- a/packages/server/src/integrations/base/sqlTable.ts +++ b/packages/server/src/integrations/base/sqlTable.ts @@ -101,28 +101,28 @@ function generateSchema( } function buildCreateTable( - knex: Knex, + knex: SchemaBuilder, table: Table, tables: Record ): SchemaBuilder { - return knex.schema.createTable(table.name, schema => { + return knex.createTable(table.name, schema => { generateSchema(schema, table, tables) }) } function buildUpdateTable( - knex: Knex, + knex: SchemaBuilder, table: Table, tables: Record, oldTable: Table ): SchemaBuilder { - return knex.schema.alterTable(table.name, schema => { + return knex.alterTable(table.name, schema => { generateSchema(schema, table, tables, oldTable) }) } -function buildDeleteTable(knex: Knex, table: Table): SchemaBuilder { - return knex.schema.dropTable(table.name) +function buildDeleteTable(knex: SchemaBuilder, table: Table): SchemaBuilder { + return knex.dropTable(table.name) } class SqlTableQueryBuilder { @@ -146,7 +146,11 @@ class SqlTableQueryBuilder { } _tableQuery(json: QueryJson): any { - const client = knex({ client: this.sqlClient }) + let client = knex({ client: this.sqlClient }).schema + if (json?.endpoint?.schema) { + client = client.withSchema(json.endpoint.schema) + } + let query if (!json.table || !json.meta || !json.meta.tables) { throw "Cannot execute without table being specified" diff --git a/packages/server/src/integrations/microsoftSqlServer.ts b/packages/server/src/integrations/microsoftSqlServer.ts index c557b0e796..aa06c47083 100644 --- a/packages/server/src/integrations/microsoftSqlServer.ts +++ b/packages/server/src/integrations/microsoftSqlServer.ts @@ -143,7 +143,6 @@ module MSSQLModule { operation: string | undefined = undefined ) { const client = this.client - const schema = this.config.schema const request = client.request() this.index = 0 try {