Skip to content

Commit

Permalink
add ability to link features to (sub)sets of (geodb)features_data rows
Browse files Browse the repository at this point in the history
  • Loading branch information
hotzevzl committed Feb 22, 2024
1 parent c86f9f9 commit 9f774c8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class PrepareToStoreListOfRelevantFeaturesDataAlongsideFeatures1708525796000
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
/**
* Only create the column; this is going to be populated - for any existing
* features - through a separate script to be run outside of the
* NestJS/TypeORM db migration flow, as this operation needs to deal with
* data across apidb and geoprocessingdb.
*/
await queryRunner.query(`
ALTER TABLE features
ADD COLUMN feature_data_stable_ids uuid[];
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE features
DROP COLUMN feature_data__stable_ids;
`);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ export class GeoFeature extends BaseEntity {
@Column('uuid')
intersection?: string[];

/**
* Array of uuids that match all the stable_ids of (geodb)features_data rows
* relevant to the feature.
*/
@ApiPropertyOptional()
@Column('uuid', { array: true, name: 'feature_data_stable_ids' })
featureDataStableIds?: string[];

@Column('varchar', { name: 'creation_status' })
creationStatus?: JobStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { MigrationInterface, QueryRunner } from 'typeorm';

export class AddStableIdsToFeaturesDataRows1708524274000
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
DROP TRIGGER tr_precompute_feature_property_list ON features_data;
CREATE TRIGGER tr_precompute_feature_property_list AFTER INSERT ON features_data
FOR EACH ROW EXECUTE
PROCEDURE precompute_feature_property_list();
`);

await queryRunner.query(`
ALTER TABLE features_data
ADD COLUMN stable_id uuid;
CREATE INDEX features_data_stable_id__idx ON features_data(stable_id);
CREATE UNIQUE INDEX features_data_unique_stable_ids_within_feature__idx ON features_data(feature_id, stable_id);
`);

await queryRunner.query(`
UPDATE features_data
SET stable_id = id;
`);

await queryRunner.query(`
ALTER TABLE features_data
ALTER COLUMN stable_id SET NOT NULL;
ALTER TABLE features_data
ALTER COLUMN stable_id SET DEFAULT gen_random_uuid();
`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
ALTER TABLE features_data
DROP COLUMN stable_id;
`);
}
}
15 changes: 15 additions & 0 deletions api/libs/geofeatures/src/geo-feature.geo.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ export class GeoFeatureGeometry {
@PrimaryColumn()
id!: string;

/**
* A stable id is not guaranteed to be globally unique, but it will be unique
* across each feature.
*
* This stable id is kept as is (hence the name) when feature data is copied
* over to a new project as part of project cloning. This way, the link
* established between features (in apidb) and all the matching
* `features_data` rows, via `GeoFeature.featureDataIds`, is preserved across
* clones (i.e. no need to update `GeoFeature.featureDataIds` throughout the
* import side of a cloning or project upload flow).
*/
@ApiProperty()
@Column('uuid', { name: 'stable_id' })
stableId!: string;

@Column('geometry', { name: 'the_geom' })
theGeom?: Geometry;

Expand Down

0 comments on commit 9f774c8

Please sign in to comment.