Fix merge issue flexible backend (#1685)

* Fix merge issue flexible backend

* Fix tests

* Try fix tests

* Try fix tests
This commit is contained in:
Charles Bochet 2023-09-20 19:11:21 -07:00 committed by GitHub
parent 19365f6639
commit 2d758c990b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 98 additions and 46 deletions

View File

@ -34,8 +34,6 @@ jobs:
- name: Server / Run linter
run: cd server && yarn lint
- name: Server / Run jest tests
run: |
cd server && yarn test
run: cd server && yarn test
- name: Server / Run e2e tests
run: |
cd server && yarn test:e2e
run: cd server && yarn test:e2e

View File

@ -85,6 +85,17 @@ Create a `twenty` user with password `twenty`:
```sql
CREATE USER twenty PASSWORD 'twenty';
ALTER USER twenty CREATEDB;
```
Create `metadata` schema:
```sql
CREATE SCHEMA IF NOT EXISTS "metadata";
GRANT ALL ON SCHEMA metadata TO twenty;
```
Activate `uuid-ossp` extension:
```sql
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
```
</TabItem>
</Tabs>

View File

@ -13,3 +13,13 @@ ALTER USER twenty CREATEDB;
-- Create the metadata schema if it doesn't exist
CREATE SCHEMA IF NOT EXISTS "metadata";
GRANT ALL ON SCHEMA metadata TO twenty;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Connect to the "default" database
\c "test";
-- Create the metadata schema if it doesn't exist
CREATE SCHEMA IF NOT EXISTS "metadata";
GRANT ALL ON SCHEMA metadata TO twenty;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";

View File

@ -9,7 +9,9 @@ EXIT_CODE=$?
if [ $EXIT_CODE -ne 0 ]; then
echo '🟡 - Database is not initialized. Running migrations...'
npx ts-node ./test/utils/setup-db.ts
npx prisma migrate reset --force && yarn prisma:generate
yarn typeorm:migrate
else
echo "🟢 - Database is already initialized."
fi

View File

@ -28,7 +28,7 @@ export class DataSourceMetadataService {
});
}
getDataSourcesMedataFromWorkspaceId(workspaceId: string) {
getDataSourcesMetadataFromWorkspaceId(workspaceId: string) {
return this.dataSourceMetadataRepository.find({
where: { workspaceId },
order: { createdAt: 'DESC' },

View File

@ -106,7 +106,7 @@ export class DataSourceService implements OnModuleInit, OnModuleDestroy {
}
const dataSourcesMetadata =
await this.dataSourceMetadataService.getDataSourcesMedataFromWorkspaceId(
await this.dataSourceMetadataService.getDataSourcesMetadataFromWorkspaceId(
workspaceId,
);

View File

@ -10,7 +10,6 @@ import { DataSourceMetadataService } from './data-source-metadata/data-source-me
import { EntitySchemaGeneratorService } from './entity-schema-generator/entity-schema-generator.service';
import { DataSourceService } from './data-source/data-source.service';
import { MigrationGeneratorService } from './migration-generator/migration-generator.service';
import { uuidToBase36 } from './data-source/data-source.util';
@UseGuards(JwtAuthGuard)
@Controller('metadata')
@ -25,7 +24,7 @@ export class MetadataController {
@Get()
async getMetadata(@AuthWorkspace() workspace: Workspace) {
const dataSourcesMetadata =
await this.dataSourceMetadataService.getDataSourcesMedataFromWorkspaceId(
await this.dataSourceMetadataService.getDataSourcesMetadataFromWorkspaceId(
workspace.id,
);
@ -42,14 +41,10 @@ export class MetadataController {
entities.push(...dataSourceEntities);
}
return await this.migrationGenerator.executeMigrationFromPendingMigrations(
await this.migrationGenerator.executeMigrationFromPendingMigrations(
workspace.id,
);
this.dataSourceService.createWorkspaceSchema(workspace.id);
console.log('entities', uuidToBase36(workspace.id), workspace.id);
this.dataSourceService.connectToWorkspaceDataSource(workspace.id);
return entities;

View File

@ -9,7 +9,7 @@ config();
const configService = new ConfigService();
export const typeORMMetadataModuleOptions: TypeOrmModuleOptions = {
url: configService.get<string>('PG_DATABASE_URL')!,
url: configService.get<string>('PG_DATABASE_URL'),
type: 'postgres',
logging: false,
schema: 'metadata',

View File

@ -14,6 +14,7 @@ import { MigrationGeneratorModule } from './migration-generator/migration-genera
const typeORMFactory = async (): Promise<TypeOrmModuleOptions> => ({
...typeORMMetadataModuleOptions,
name: 'metadata',
});
@Module({

View File

@ -1,29 +0,0 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class Migrations1695198840363 implements MigrationInterface {
name = 'Migrations1695198840363';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE "metadata"."data_source_metadata" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "url" character varying, "schema" character varying, "type" "metadata"."data_source_metadata_type_enum" NOT NULL DEFAULT 'postgres', "name" character varying, "is_remote" boolean NOT NULL DEFAULT false, "workspace_id" character varying NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_923752b7e62a300a4969bd0e038" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "metadata"."field_metadata" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "object_id" uuid NOT NULL, "type" character varying NOT NULL, "name" character varying NOT NULL, "is_custom" boolean NOT NULL DEFAULT false, "workspace_id" character varying NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_c75db587904cad6af109b5c65f1" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`CREATE TABLE "metadata"."object_metadata" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "data_source_id" character varying NOT NULL, "name" character varying NOT NULL, "is_custom" boolean NOT NULL DEFAULT false, "workspace_id" character varying NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_c8c5f885767b356949c18c201c1" PRIMARY KEY ("id"))`,
);
await queryRunner.query(
`ALTER TABLE "metadata"."field_metadata" ADD CONSTRAINT "FK_38179b299795e48887fc99f937a" FOREIGN KEY ("object_id") REFERENCES "metadata"."object_metadata"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "metadata"."field_metadata" DROP CONSTRAINT "FK_38179b299795e48887fc99f937a"`,
);
await queryRunner.query(`DROP TABLE "metadata"."object_metadata"`);
await queryRunner.query(`DROP TABLE "metadata"."field_metadata"`);
await queryRunner.query(`DROP TABLE "metadata"."data_source_metadata"`);
}
}

View File

@ -4,6 +4,9 @@ export class InitMetadataTables1695214465080 implements MigrationInterface {
name = 'InitMetadataTables1695214465080';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TYPE "metadata"."data_source_metadata_type_enum" AS ENUM ('postgres', 'mysql');`,
);
await queryRunner.query(
`CREATE TABLE "metadata"."data_source_metadata" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "url" character varying, "schema" character varying, "type" "metadata"."data_source_metadata_type_enum" NOT NULL DEFAULT 'postgres', "display_name" character varying, "is_remote" boolean NOT NULL DEFAULT false, "workspace_id" character varying NOT NULL, "created_at" TIMESTAMP NOT NULL DEFAULT now(), "updated_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_923752b7e62a300a4969bd0e038" PRIMARY KEY ("id"))`,
);

View File

@ -1,5 +1,7 @@
import { Test, TestingModule } from '@nestjs/testing';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { UniversalResolver } from './universal.resolver';
import { UniversalService } from './universal.service';
@ -14,6 +16,10 @@ describe('UniversalResolver', () => {
provide: UniversalService,
useValue: {},
},
{
provide: EnvironmentService,
useValue: {},
},
],
}).compile();

View File

@ -12,7 +12,6 @@ import { UniversalService } from './universal.service';
import { FindManyUniversalArgs } from './args/find-many-universal.args';
import { FindUniqueUniversalArgs } from './args/find-unique-universal.args';
import { UpdateOneCustomArgs } from './args/update-one-custom.args';
@UseGuards(JwtAuthGuard)
@Resolver(() => UniversalEntity)
@ -47,7 +46,7 @@ export class UniversalResolver {
}
@Query(() => UniversalEntity)
updateOneCustom(@Args() args: UpdateOneCustomArgs): UniversalEntity {
updateOneCustom(): UniversalEntity {
if (!this.environmentService.isFlexibleBackendEnabled()) {
throw new ForbiddenException();
}
@ -61,7 +60,7 @@ export class UniversalResolver {
}
@Query(() => UniversalEntity)
deleteOneCustom(@Args() args: UpdateOneCustomArgs): UniversalEntity {
deleteOneCustom(): UniversalEntity {
if (!this.environmentService.isFlexibleBackendEnabled()) {
throw new ForbiddenException();
}

View File

@ -0,0 +1,56 @@
// check-db.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const createMetadataSchema = async () => {
try {
await prisma.$queryRawUnsafe<[any]>(
`CREATE SCHEMA IF NOT EXISTS "metadata";`,
);
await prisma.$queryRawUnsafe<[any]>(
`GRANT ALL ON SCHEMA metadata TO twenty;`,
);
return true;
} catch {
return false;
}
};
const activateUUIDExtension = async () => {
try {
const result = await prisma.$queryRawUnsafe<[any]>(
`CREATE EXTENSION IF NOT EXISTS "uuid-ossp";`,
);
return result;
} catch {
return false;
}
};
const main = async () => {
const metadataSchemaCreationSuccess = await createMetadataSchema();
const uuidExtensionActivationSuccess = await activateUUIDExtension();
if (!metadataSchemaCreationSuccess) {
throw new Error(`Failed to create metadata schema`);
}
if (!uuidExtensionActivationSuccess) {
throw new Error(`Failed to activate uuid extension`);
}
};
main()
.then(() => {
process.exit(0);
})
.catch(() => {
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});