Skip to content
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

Introduce remote table entity #4994

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"pg": "^8.11.3",
"pg-boss": "^9.0.3",
"planer": "^1.2.0",
"pluralize": "^8.0.0",
"prettier": "^3.0.3",
"prism-react-renderer": "^2.1.0",
"qs": "^6.11.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class CreateRemoteTable1713270565699 implements MigrationInterface {
name = 'CreateRemoteTable1713270565699';

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "metadata"."remoteTable" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "distantTableName" character varying NOT NULL, "localTableName" character varying NOT NULL, "workspaceId" uuid NOT NULL, "remoteServerId" uuid NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_632b3858de52c8c2eb00c709b52" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "metadata"."remoteTable" ADD CONSTRAINT "FK_3db5ae954f9197def326053f06a" FOREIGN KEY ("remoteServerId") REFERENCES "metadata"."remoteServer"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "metadata"."remoteTable" DROP CONSTRAINT "FK_3db5ae954f9197def326053f06a"`,
);
await queryRunner.query(`DROP TABLE "metadata"."remoteTable"`);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { ObjectType } from '@nestjs/graphql';

import {
Column,
CreateDateColumn,
Entity,
Generated,
OneToMany,
PrimaryGeneratedColumn,
Relation,
UpdateDateColumn,
} from 'typeorm';

import { RemoteTableEntity } from 'src/engine/metadata-modules/remote-server/remote-table/remote-table.entity';

export enum RemoteServerType {
POSTGRES_FDW = 'postgres_fdw',
}
Expand All @@ -30,7 +32,6 @@ export type UserMappingOptions = {
};

@Entity('remoteServer')
@ObjectType('RemoteServer')
export class RemoteServerEntity<T extends RemoteServerType> {
@PrimaryGeneratedColumn('uuid')
id: string;
Expand All @@ -51,6 +52,11 @@ export class RemoteServerEntity<T extends RemoteServerType> {
@Column({ nullable: false, type: 'uuid' })
workspaceId: string;

@OneToMany(() => RemoteTableEntity, (table) => table.server, {
cascade: true,
})
tables: Relation<RemoteTableEntity[]>;

@CreateDateColumn({ type: 'timestamptz' })
createdAt: Date;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,10 @@ export class RemoteServerService<T extends RemoteServerType> {
});

if (!remoteServer) {
throw new NotFoundException('Object does not exist');
throw new NotFoundException('Remote server does not exist');
}

const foreignTablesToRemove =
await this.remoteTableService.fetchForeignTableNamesWithinWorkspace(
workspaceId,
remoteServer.foreignDataWrapperId,
);

if (foreignTablesToRemove.length) {
for (const foreignTableName of foreignTablesToRemove) {
await this.remoteTableService.removeForeignTableAndMetadata(
foreignTableName,
workspaceId,
remoteServer,
);
}
}
await this.remoteTableService.unsyncAll(workspaceId, remoteServer);

return this.metadataDataSource.transaction(
async (entityManager: EntityManager) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ObjectType, Field, registerEnumType } from '@nestjs/graphql';

import { IsEnum } from 'class-validator';
import { PrimaryGeneratedColumn } from 'typeorm';

export enum RemoteTableStatus {
SYNCED = 'SYNCED',
Expand All @@ -14,6 +15,9 @@ registerEnumType(RemoteTableStatus, {

@ObjectType('RemoteTable')
export class RemoteTableDTO {
@PrimaryGeneratedColumn('uuid')
id: string;

@Field(() => String)
name: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
JoinColumn,
ManyToOne,
Relation,
} from 'typeorm';

import {
RemoteServerEntity,
RemoteServerType,
} from 'src/engine/metadata-modules/remote-server/remote-server.entity';

@Entity('remoteTable')
export class RemoteTableEntity {
@PrimaryGeneratedColumn('uuid')
id: string;

@Column({ nullable: false })
distantTableName: string;

@Column({ nullable: false })
localTableName: string;

@Column({ nullable: false, type: 'uuid' })
workspaceId: string;

@Column({ nullable: false, type: 'uuid' })
remoteServerId: string;

@ManyToOne(() => RemoteServerEntity, (server) => server.tables, {
onDelete: 'CASCADE',
})
@JoinColumn({ name: 'remoteServerId' })
server: Relation<RemoteServerEntity<RemoteServerType>>;

@CreateDateColumn({ type: 'timestamptz' })
createdAt: Date;

@UpdateDateColumn({ type: 'timestamptz' })
updatedAt: Date;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { FieldMetadataModule } from 'src/engine/metadata-modules/field-metadata/
import { ObjectMetadataModule } from 'src/engine/metadata-modules/object-metadata/object-metadata.module';
import { RemoteServerEntity } from 'src/engine/metadata-modules/remote-server/remote-server.entity';
import { RemotePostgresTableModule } from 'src/engine/metadata-modules/remote-server/remote-table/remote-postgres-table/remote-postgres-table.module';
import { RemoteTableEntity } from 'src/engine/metadata-modules/remote-server/remote-table/remote-table.entity';
import { RemoteTableResolver } from 'src/engine/metadata-modules/remote-server/remote-table/remote-table.resolver';
import { RemoteTableService } from 'src/engine/metadata-modules/remote-server/remote-table/remote-table.service';
import { WorkspaceCacheVersionModule } from 'src/engine/metadata-modules/workspace-cache-version/workspace-cache-version.module';
Expand All @@ -16,7 +17,10 @@ import { WorkspaceMigrationRunnerModule } from 'src/engine/workspace-manager/wor

@Module({
imports: [
TypeOrmModule.forFeature([RemoteServerEntity], 'metadata'),
TypeOrmModule.forFeature(
[RemoteServerEntity, RemoteTableEntity],
'metadata',
),
TypeOrmModule.forFeature([FeatureFlagEntity], 'core'),
DataSourceModule,
ObjectMetadataModule,
Expand Down
Loading
Loading