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

Sync metadata generate migrations #2864

Merged
merged 11 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
4 changes: 2 additions & 2 deletions server/src/command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import { DatabaseCommandModule } from 'src/database/commands/database-command.mo

import { AppModule } from './app.module';

import { WorkspaceManagerCommandsModule } from './workspace/workspace-manager/commands/workspace-manager-commands.module';
import { WorkspaceSyncMetadataCommandsModule } from './workspace/workspace-sync-metadata/commands/workspace-sync-metadata-commands.module';
import { WorkspaceMigrationRunnerCommandsModule } from './workspace/workspace-migration-runner/commands/workspace-migration-runner-commands.module';

@Module({
imports: [
AppModule,
WorkspaceMigrationRunnerCommandsModule,
WorkspaceManagerCommandsModule,
WorkspaceSyncMetadataCommandsModule,
DatabaseCommandModule,
],
})
Expand Down
35 changes: 16 additions & 19 deletions server/src/database/commands/data-seed-dev-workspace.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { seedWorkspaceMember } from 'src/database/typeorm-seeds/workspace/worksp
import { seedPeople } from 'src/database/typeorm-seeds/workspace/people';
import { seedCoreSchema } from 'src/database/typeorm-seeds/core';
import { EnvironmentService } from 'src/integrations/environment/environment.service';
import { WorkspaceSyncMetadataService } from 'src/workspace/workspace-sync-metadata/workspace-sync.metadata.service';
import { WorkspaceDataSourceService } from 'src/workspace/workspace-datasource/workspace-datasource.service';

// TODO: implement dry-run
@Command({
Expand All @@ -26,28 +28,30 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
workspaceId = '20202020-1c25-4d02-bf25-6aeccf7ea419';

constructor(
private readonly environmentService: EnvironmentService,
private readonly dataSourceService: DataSourceService,
private readonly typeORMService: TypeORMService,
private readonly workspaceMigrationService: WorkspaceMigrationService,
private readonly workspaceMigrationRunnerService: WorkspaceMigrationRunnerService,
private readonly workspaceSyncMetadataService: WorkspaceSyncMetadataService,
private readonly workspaceDataSourceService: WorkspaceDataSourceService,
) {
super();
}

async run(): Promise<void> {
try {
const dataSource = new DataSource({
url: this.environmentService.getPGDatabaseUrl(),
type: 'postgres',
logging: true,
schema: 'public',
});
const schemaName = await this.workspaceDataSourceService.createWorkspaceDBSchema(
this.workspaceId,
);

await dataSource.initialize();
const dataSourceMetadata =
await this.dataSourceService.createDataSourceMetadata(
this.workspaceId,
schemaName,
);

await seedCoreSchema(dataSource, this.workspaceId);
await seedMetadataSchema(dataSource);
await this.workspaceSyncMetadataService.syncStandardObjectsAndFieldsMetadata(
dataSourceMetadata.id,
this.workspaceId,
);
} catch (error) {
console.error(error);

Expand All @@ -68,13 +72,6 @@ export class DataSeedWorkspaceCommand extends CommandRunner {
}

try {
await this.workspaceMigrationService.insertStandardMigrations(
this.workspaceId,
);
await this.workspaceMigrationRunnerService.executeMigrationFromPendingMigrations(
this.workspaceId,
);

await seedCompanies(workspaceDataSource, dataSourceMetadata.schema);
await seedPeople(workspaceDataSource, dataSourceMetadata.schema);
await seedPipelineStep(workspaceDataSource, dataSourceMetadata.schema);
Expand Down
6 changes: 4 additions & 2 deletions server/src/database/commands/database-command.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,17 @@ import { TypeORMModule } from 'src/database/typeorm/typeorm.module';
import { WorkspaceModule } from 'src/core/workspace/workspace.module';
import { DataSeedWorkspaceCommand } from 'src/database/commands/data-seed-dev-workspace.command';
import { DataSeedDemoWorkspaceCommand } from 'src/database/commands/data-seed-demo-workspace.command';
import { WorkspaceDataSourceModule } from 'src/workspace/workspace-datasource/workspace-datasource.module';
import { WorkspaceSyncMetadataModule } from 'src/workspace/workspace-sync-metadata/worksapce-sync-metadata.module';

@Module({
imports: [
WorkspaceManagerModule,
DataSourceModule,
TypeORMModule,
WorkspaceMigrationModule,
WorkspaceMigrationRunnerModule,
WorkspaceModule,
WorkspaceDataSourceModule,
WorkspaceSyncMetadataModule,
],
providers: [
DataSeedWorkspaceCommand,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,19 +124,19 @@ export const seedViewFilterFieldMetadata = async (
isCustom: false,
workspaceId: SeedWorkspaceId,
isActive: true,
type: FieldMetadataType.UUID,
name: 'viewId',
label: 'View Id',
type: FieldMetadataType.RELATION,
name: 'view',
label: 'View',
targetColumnMap: {},
description: 'View Filter related view',
icon: 'IconLayoutCollage',
isNullable: false,
isNullable: true,
isSystem: false,
defaultValue: undefined,
},
{
id: SeedViewFilterFieldMetadataIds.ViewForeignKey,
objectMetadataId: SeedObjectMetadataIds.ViewField,
objectMetadataId: SeedObjectMetadataIds.ViewFilter,
isCustom: false,
workspaceId: SeedWorkspaceId,
isActive: true,
Expand Down
2 changes: 2 additions & 0 deletions server/src/database/typeorm/typeorm.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Module } from '@nestjs/common';
import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm';

import { typeORMCoreModuleOptions } from 'src/database/typeorm/core/core.datasource';
import { EnvironmentModule } from 'src/integrations/environment/environment.module';

import { TypeORMService } from './typeorm.service';

Expand All @@ -27,6 +28,7 @@ const coreTypeORMFactory = async (): Promise<TypeOrmModuleOptions> => ({
useFactory: coreTypeORMFactory,
name: 'core',
}),
EnvironmentModule,
],
providers: [TypeORMService],
exports: [TypeORMService],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/f

import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';

export const currencyObjectDefinition = {
id: FieldMetadataType.CURRENCY.toString(),
nameSingular: 'currency',
namePlural: 'currency',
labelSingular: 'Currency',
labelPlural: 'Currency',
targetTableName: '',
fields: [
export const currencyFields = (
fieldMetadata?: FieldMetadataInterface,
): FieldMetadataInterface[] => {
return [
{
id: 'amountMicros',
type: FieldMetadataType.NUMERIC,
objectMetadataId: FieldMetadataType.CURRENCY.toString(),
name: 'amountMicros',
label: 'AmountMicros',
targetColumnMap: { value: 'amountMicros' },
targetColumnMap: {
value: fieldMetadata
? `${fieldMetadata.name}AmountMicros`
: 'amountMicros',
},
isNullable: true,
} satisfies FieldMetadataInterface,
{
Expand All @@ -26,10 +26,24 @@ export const currencyObjectDefinition = {
objectMetadataId: FieldMetadataType.CURRENCY.toString(),
name: 'currencyCode',
label: 'Currency Code',
targetColumnMap: { value: 'currencyCode' },
targetColumnMap: {
value: fieldMetadata
? `${fieldMetadata.name}CurrencyCode`
: 'currencyCode',
},
isNullable: true,
} satisfies FieldMetadataInterface,
],
];
};

export const currencyObjectDefinition = {
id: FieldMetadataType.CURRENCY.toString(),
nameSingular: 'currency',
namePlural: 'currency',
labelSingular: 'Currency',
labelPlural: 'Currency',
targetTableName: '',
fields: currencyFields(),
fromRelations: [],
toRelations: [],
} satisfies ObjectMetadataInterface;
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/f

import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';

export const fullNameObjectDefinition = {
id: FieldMetadataType.FULL_NAME.toString(),
nameSingular: 'fullName',
namePlural: 'fullName',
labelSingular: 'FullName',
labelPlural: 'FullName',
targetTableName: '',
fields: [
export const fullNameFields = (
fieldMetadata?: FieldMetadataInterface,
): FieldMetadataInterface[] => {
return [
{
id: 'firstName',
type: FieldMetadataType.TEXT,
objectMetadataId: FieldMetadataType.FULL_NAME.toString(),
name: 'firstName',
label: 'First Name',
targetColumnMap: { value: 'firstName' },
targetColumnMap: {
value: fieldMetadata ? `${fieldMetadata.name}FirstName` : 'firstName',
},
isNullable: true,
} satisfies FieldMetadataInterface,
{
Expand All @@ -26,10 +24,22 @@ export const fullNameObjectDefinition = {
objectMetadataId: FieldMetadataType.FULL_NAME.toString(),
name: 'lastName',
label: 'Last Name',
targetColumnMap: { value: 'lastName' },
targetColumnMap: {
value: fieldMetadata ? `${fieldMetadata.name}LastName` : 'lastName',
},
isNullable: true,
} satisfies FieldMetadataInterface,
],
];
};

export const fullNameObjectDefinition = {
id: FieldMetadataType.FULL_NAME.toString(),
nameSingular: 'fullName',
namePlural: 'fullName',
labelSingular: 'FullName',
labelPlural: 'FullName',
targetTableName: '',
fields: fullNameFields(),
fromRelations: [],
toRelations: [],
} satisfies ObjectMetadataInterface;
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@ import { FieldMetadataInterface } from 'src/metadata/field-metadata/interfaces/f

import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';

export const linkObjectDefinition = {
id: FieldMetadataType.LINK.toString(),
nameSingular: 'link',
namePlural: 'link',
labelSingular: 'Link',
labelPlural: 'Link',
targetTableName: '',
fields: [
export const linkFields = (
fieldMetadata?: FieldMetadataInterface,
): FieldMetadataInterface[] => {
return [
{
id: 'label',
type: FieldMetadataType.TEXT,
objectMetadataId: FieldMetadataType.LINK.toString(),
name: 'label',
label: 'Label',
targetColumnMap: { value: 'label' },
targetColumnMap: {
value: fieldMetadata ? `${fieldMetadata.name}Label` : 'label',
},
isNullable: true,
} satisfies FieldMetadataInterface,
{
Expand All @@ -26,10 +24,22 @@ export const linkObjectDefinition = {
objectMetadataId: FieldMetadataType.LINK.toString(),
name: 'url',
label: 'Url',
targetColumnMap: { value: 'url' },
targetColumnMap: {
value: fieldMetadata ? `${fieldMetadata.name}Url` : 'url',
},
isNullable: true,
} satisfies FieldMetadataInterface,
],
];
};

export const linkObjectDefinition = {
id: FieldMetadataType.LINK.toString(),
nameSingular: 'link',
namePlural: 'link',
labelSingular: 'Link',
labelPlural: 'Link',
targetTableName: '',
fields: linkFields(),
fromRelations: [],
toRelations: [],
} satisfies ObjectMetadataInterface;
23 changes: 23 additions & 0 deletions server/src/metadata/field-metadata/utils/generate-default-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { FieldMetadataDefaultValue } from 'src/metadata/field-metadata/interfaces/field-metadata-default-value.interface';

import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';

export function generateDefaultValue(
type: FieldMetadataType,
): FieldMetadataDefaultValue {
switch (type) {
case FieldMetadataType.TEXT:
case FieldMetadataType.PHONE:
case FieldMetadataType.EMAIL:
return {
value: '',
};
case FieldMetadataType.FULL_NAME:
return {
firstName: '',
lastName: '',
};
default:
return null;
}
}
Loading
Loading