-
Notifications
You must be signed in to change notification settings - Fork 166
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature/add postgres schema support #2540
Open
shubham-padia
wants to merge
3
commits into
boxyhq:main
Choose a base branch
from
shubham-padia:feature/add-postgres-schema-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,30 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
const schema = process.env.POSTGRES_SCHEMA || "public"; | ||
const jacksonStoreTableName = `${schema}.jackson_store`; | ||
const jacksonIndexTableName = `${schema}.jackson_index`; | ||
const jacksonTTLTableName = `${schema}.hackson_ttl`; | ||
|
||
export class Initial1640877103193 implements MigrationInterface { | ||
name = 'Initial1640877103193' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`CREATE TABLE "jackson_store" ("key" character varying(1500) NOT NULL, "value" text NOT NULL, "iv" character varying(64), "tag" character varying(64), CONSTRAINT "PK_87b6fc1475fbd1228d2f53c6f4a" PRIMARY KEY ("key"))`); | ||
await queryRunner.query(`CREATE TABLE "jackson_index" ("id" SERIAL NOT NULL, "key" character varying(1500) NOT NULL, "storeKey" character varying(1500) NOT NULL, CONSTRAINT "PK_a95aa83f01e3c73e126856b7820" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_index_key" ON "jackson_index" ("key") `); | ||
await queryRunner.query(`CREATE INDEX "_jackson_index_key_store" ON "jackson_index" ("key", "storeKey") `); | ||
await queryRunner.query(`CREATE TABLE "jackson_ttl" ("key" character varying(1500) NOT NULL, "expiresAt" bigint NOT NULL, CONSTRAINT "PK_7c9bcdfb4d82e873e19935ec806" PRIMARY KEY ("key"))`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_ttl_expires_at" ON "jackson_ttl" ("expiresAt") `); | ||
await queryRunner.query(`ALTER TABLE "jackson_index" ADD CONSTRAINT "FK_937b040fb2592b4671cbde09e83" FOREIGN KEY ("storeKey") REFERENCES "jackson_store"("key") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
await queryRunner.query(`CREATE TABLE ${jacksonStoreTableName} ("key" character varying(1500) NOT NULL, "value" text NOT NULL, "iv" character varying(64), "tag" character varying(64), CONSTRAINT "PK_87b6fc1475fbd1228d2f53c6f4a" PRIMARY KEY ("key"))`); | ||
await queryRunner.query(`CREATE TABLE ${jacksonIndexTableName} ("id" SERIAL NOT NULL, "key" character varying(1500) NOT NULL, "storeKey" character varying(1500) NOT NULL, CONSTRAINT "PK_a95aa83f01e3c73e126856b7820" PRIMARY KEY ("id"))`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_index_key" ON ${jacksonIndexTableName} ("key") `); | ||
await queryRunner.query(`CREATE INDEX "_jackson_index_key_store" ON ${jacksonIndexTableName} ("key", "storeKey") `); | ||
await queryRunner.query(`CREATE TABLE ${jacksonTTLTableName} ("key" character varying(1500) NOT NULL, "expiresAt" bigint NOT NULL, CONSTRAINT "PK_7c9bcdfb4d82e873e19935ec806" PRIMARY KEY ("key"))`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_ttl_expires_at" ON ${jacksonTTLTableName} ("expiresAt") `); | ||
await queryRunner.query(`ALTER TABLE ${jacksonIndexTableName} ADD CONSTRAINT "FK_937b040fb2592b4671cbde09e83" FOREIGN KEY ("storeKey") REFERENCES ${jacksonStoreTableName}("key") ON DELETE CASCADE ON UPDATE NO ACTION`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "jackson_index" DROP CONSTRAINT "FK_937b040fb2592b4671cbde09e83"`); | ||
await queryRunner.query(`DROP INDEX "public"."_jackson_ttl_expires_at"`); | ||
await queryRunner.query(`DROP TABLE "jackson_ttl"`); | ||
await queryRunner.query(`DROP INDEX "public"."_jackson_index_key_store"`); | ||
await queryRunner.query(`DROP INDEX "public"."_jackson_index_key"`); | ||
await queryRunner.query(`DROP TABLE "jackson_index"`); | ||
await queryRunner.query(`DROP TABLE "jackson_store"`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonIndexTableName} DROP CONSTRAINT "FK_937b040fb2592b4671cbde09e83"`); | ||
await queryRunner.query(`DROP INDEX ${schema}."_jackson_ttl_expires_at"`); | ||
await queryRunner.query(`DROP TABLE ${jacksonTTLTableName}`); | ||
await queryRunner.query(`DROP INDEX ${schema}."_jackson_index_key_store"`); | ||
await queryRunner.query(`DROP INDEX ${schema}."_jackson_index_key"`); | ||
await queryRunner.query(`DROP TABLE ${jacksonIndexTableName}`); | ||
await queryRunner.query(`DROP TABLE ${jacksonStoreTableName}`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
|
||
const schema = process.env.POSTGRES_SCHEMA || "public"; | ||
const jacksonStoreTableName = `${schema}.jackson_store`; | ||
|
||
export class createdAt1644332647279 implements MigrationInterface { | ||
name = 'createdAt1644332647279' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "jackson_store" ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`); | ||
await queryRunner.query(`ALTER TABLE "jackson_store" ADD "modifiedAt" TIMESTAMP`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} ADD "createdAt" TIMESTAMP NOT NULL DEFAULT now()`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} ADD "modifiedAt" TIMESTAMP`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "jackson_store" DROP COLUMN "modifiedAt"`); | ||
await queryRunner.query(`ALTER TABLE "jackson_store" DROP COLUMN "createdAt"`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} DROP COLUMN "modifiedAt"`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} DROP COLUMN "createdAt"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
const schema = process.env.POSTGRES_SCHEMA || "public"; | ||
const jacksonStoreTableName = `${schema}.jackson_store`; | ||
|
||
export class PgNamespace1692767993709 implements MigrationInterface { | ||
name = 'PgNamespace1692767993709' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "jackson_store" ADD "namespace" character varying(64)`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_store_namespace" ON "jackson_store" ("namespace") `); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} ADD "namespace" character varying(64)`); | ||
await queryRunner.query(`CREATE INDEX "_jackson_store_namespace" ON ${jacksonStoreTableName} ("namespace") `); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP INDEX "public"."_jackson_store_namespace"`); | ||
await queryRunner.query(`ALTER TABLE "jackson_store" DROP COLUMN "namespace"`); | ||
await queryRunner.query(`DROP INDEX ${schema}."_jackson_store_namespace"`); | ||
await queryRunner.query(`ALTER TABLE ${jacksonStoreTableName} DROP COLUMN "namespace"`); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm" | ||
|
||
// This file is same as npm/migration/sql/1692817789888-namespace.ts, | ||
// but, with the added postgres schema name. | ||
|
||
const schema = process.env.POSTGRES_SCHEMA || "public"; | ||
const jacksonStoreTableName = `${schema}.jackson_store`; | ||
|
||
export class namespace1692817789888 implements MigrationInterface { | ||
name = 'namespace1692817789888' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const response = await queryRunner.query(`select jackson.key from ${jacksonStoreTableName} jackson`) | ||
const searchTerm = ':'; | ||
for (const k in response) { | ||
const key = response[k].key; | ||
const tokens2 = key.split(searchTerm).slice(0, 2); | ||
const value = tokens2.join(searchTerm); | ||
queryRunner.query(`update ${jacksonStoreTableName} set namespace = '${value}' where ${jacksonStoreTableName}.key = '${key}'`) | ||
} | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
const response = await queryRunner.query(`select jackson.key from ${jacksonStoreTableName} jackson`) | ||
for (const k in response) { | ||
const key = response[k].key; | ||
queryRunner.query(`update ${jacksonStoreTableName} set namespace = NULL where ${jacksonStoreTableName}.key = '${key}'`) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const DEFAULT_POSTGRES_SCHEMA = 'public'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ const dbObjs: { [key: string]: DatabaseDriver } = {}; | |
const connectionStores: Storable[] = []; | ||
const ttlStores: Storable[] = []; | ||
const ttl = 2; | ||
const non_default_schema = 'non_default'; | ||
|
||
const record1 = { | ||
id: '1', | ||
|
@@ -130,6 +131,12 @@ const dbs = [ | |
...postgresDbConfig, | ||
encryptionKey, | ||
}, | ||
{ | ||
...postgresDbConfig, | ||
postgres: { | ||
schema: non_default_schema, | ||
}, | ||
}, | ||
{ | ||
...mongoDbConfig, | ||
}, | ||
|
@@ -188,7 +195,11 @@ tap.before(async () => { | |
for (const idx in dbs) { | ||
const opts = dbs[idx]; | ||
const db = await DB.new(opts, true); | ||
dbObjs[opts.engine! + (opts.type ? opts.type : '')] = db; | ||
if (opts.type === 'postgres' && opts['schema'] === non_default_schema) { | ||
dbObjs[opts['schema'] + opts.engine! + (opts.type ? opts.type : '')] = db; | ||
} else { | ||
dbObjs[opts.engine! + (opts.type ? opts.type : '')] = db; | ||
} | ||
|
||
const randomSession = Date.now(); | ||
connectionStores.push(db.store('saml:config:' + randomSession + randomBytes(4).toString('hex'))); | ||
|
@@ -201,15 +212,32 @@ tap.teardown(async () => { | |
}); | ||
|
||
tap.test('dbs', async () => { | ||
// We need this to ensure that the test runs atleast once. | ||
// It is quite easy to skip the test by mistake in the future | ||
// if one of the conditions change and it goes unnoticed. | ||
let has_non_default_postgres_schema_test_ran = false; | ||
for (const idx in connectionStores) { | ||
const connectionStore = connectionStores[idx]; | ||
const ttlStore = ttlStores[idx]; | ||
const dbEngine = dbs[idx].engine!; | ||
let dbType = dbEngine; | ||
let dbType = dbEngine.toString(); | ||
if (dbs[idx].type) { | ||
dbType += ': ' + dbs[idx].type; | ||
} | ||
|
||
tap.test('Test non default postgres schema', (t) => { | ||
if (dbType === 'sql: postgres' && dbs[idx].postgres?.schema === non_default_schema) { | ||
t.same( | ||
connectionStore['db']['db']['dataSource']['createQueryBuilder']()['connection']['options'][ | ||
'schema' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not the cleanest solution, but since we want to expose a limited number of methods in DatabaseDriver, we needed to access private members of the class to test our changes |
||
], | ||
non_default_schema | ||
); | ||
} | ||
has_non_default_postgres_schema_test_ran = true; | ||
t.end(); | ||
}); | ||
|
||
tap.test('put(): ' + dbType, async () => { | ||
await connectionStore.put( | ||
record1.id, | ||
|
@@ -527,4 +555,9 @@ tap.test('dbs', async () => { | |
await value.close(); | ||
} | ||
}); | ||
|
||
tap.test('Ensure that the test for non default postgres schema has ran atleast once', (t) => { | ||
t.same(has_non_default_postgres_schema_test_ran, true); | ||
t.end(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We needed a different DB object for the non default schema, that is why this condition. Previously, the previous db would have been overwritten by the new one.