• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-engine / geoengine / 5620465129

21 Jul 2023 08:49AM UTC coverage: 89.156% (-0.04%) from 89.193%
5620465129

push

github

web-flow
Merge pull request #830 from geo-engine/chunk-bencher

Chunk bencher

51 of 51 new or added lines in 2 files covered. (100.0%)

102797 of 115300 relevant lines covered (89.16%)

62790.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

76.56
/services/src/pro/layers/postgres_layer_db.rs
1
use crate::api::model::datatypes::{DataProviderId, LayerId};
2

3
use crate::error;
4
use crate::layers::layer::Property;
5
use crate::pro::contexts::PostgresDb;
6
use crate::pro::permissions::{Permission, PermissionDb, RoleId};
7

8
use crate::{
9
    error::Result,
10
    layers::{
11
        external::{DataProvider, DataProviderDefinition},
12
        layer::{
13
            AddLayer, AddLayerCollection, CollectionItem, Layer, LayerCollection,
14
            LayerCollectionListOptions, LayerCollectionListing, LayerListing,
15
            ProviderLayerCollectionId, ProviderLayerId,
16
        },
17
        listing::{LayerCollectionId, LayerCollectionProvider},
18
        storage::{
19
            LayerDb, LayerProviderDb, LayerProviderListing, LayerProviderListingOptions,
20
            INTERNAL_LAYER_DB_ROOT_COLLECTION_ID, INTERNAL_PROVIDER_ID,
21
        },
22
        LayerDbError,
23
    },
24
};
25
use async_trait::async_trait;
26
use bb8_postgres::tokio_postgres::{
27
    tls::{MakeTlsConnect, TlsConnect},
28
    Socket,
29
};
30

31
use snafu::{ensure, ResultExt};
32
use std::str::FromStr;
33
use uuid::Uuid;
34

35
/// delete all collections without parent collection
36
async fn _remove_collections_without_parent_collection(
3✔
37
    transaction: &tokio_postgres::Transaction<'_>,
3✔
38
) -> Result<()> {
3✔
39
    // HINT: a recursive delete statement seems reasonable, but hard to implement in postgres
40
    //       because you have a graph with potential loops
41

42
    let remove_layer_collections_without_parents_stmt = transaction
3✔
43
        .prepare(
3✔
44
            "DELETE FROM layer_collections
3✔
45
                 WHERE  id <> $1 -- do not delete root collection
3✔
46
                 AND    id NOT IN (
3✔
47
                    SELECT child FROM collection_children
3✔
48
                 );",
3✔
49
        )
3✔
50
        .await?;
3✔
51
    while 0 < transaction
5✔
52
        .execute(
5✔
53
            &remove_layer_collections_without_parents_stmt,
5✔
54
            &[&INTERNAL_LAYER_DB_ROOT_COLLECTION_ID],
5✔
55
        )
5✔
56
        .await?
5✔
57
    {
2✔
58
        // whenever one collection is deleted, we have to check again if there are more
2✔
59
        // collections without parents
2✔
60
    }
2✔
61

62
    Ok(())
3✔
63
}
3✔
64

65
/// delete all layers without parent collection
66
async fn _remove_layers_without_parent_collection(
5✔
67
    transaction: &tokio_postgres::Transaction<'_>,
5✔
68
) -> Result<()> {
5✔
69
    let remove_layers_without_parents_stmt = transaction
5✔
70
        .prepare(
5✔
71
            "DELETE FROM layers
5✔
72
                 WHERE id NOT IN (
5✔
73
                    SELECT layer FROM collection_layers
5✔
74
                 );",
5✔
75
        )
5✔
76
        .await?;
4✔
77
    transaction
5✔
78
        .execute(&remove_layers_without_parents_stmt, &[])
5✔
79
        .await?;
4✔
80

81
    Ok(())
5✔
82
}
5✔
83

84
#[async_trait]
85
impl<Tls> LayerDb for PostgresDb<Tls>
86
where
87
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
88
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
89
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
90
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
91
{
92
    async fn add_layer(&self, layer: AddLayer, collection: &LayerCollectionId) -> Result<LayerId> {
6✔
93
        ensure!(
6✔
94
            self.has_permission(collection.clone(), Permission::Owner)
6✔
95
                .await?,
17✔
96
            error::PermissionDenied
×
97
        );
98

99
        let collection_id =
6✔
100
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
6✔
101
                found: collection.0.clone(),
×
102
            })?;
6✔
103

104
        let mut conn = self.conn_pool.get().await?;
6✔
105

106
        let layer = layer;
6✔
107

6✔
108
        let layer_id = Uuid::new_v4();
6✔
109
        let symbology = serde_json::to_value(&layer.symbology).context(crate::error::SerdeJson)?;
6✔
110

111
        let metadata = serde_json::to_value(&layer.metadata).context(crate::error::SerdeJson)?;
6✔
112

113
        let trans = conn.build_transaction().start().await?;
6✔
114

115
        let stmt = trans
6✔
116
            .prepare(
6✔
117
                "
6✔
118
            INSERT INTO layers (id, name, description, workflow, symbology, properties, metadata)
6✔
119
            VALUES ($1, $2, $3, $4, $5, $6, $7);",
6✔
120
            )
6✔
121
            .await?;
8✔
122

123
        trans
124
            .execute(
125
                &stmt,
6✔
126
                &[
6✔
127
                    &layer_id,
6✔
128
                    &layer.name,
6✔
129
                    &layer.description,
6✔
130
                    &serde_json::to_value(&layer.workflow).context(crate::error::SerdeJson)?,
6✔
131
                    &symbology,
6✔
132
                    &layer.properties,
6✔
133
                    &metadata,
6✔
134
                ],
135
            )
136
            .await?;
4✔
137

138
        let stmt = trans
6✔
139
            .prepare(
6✔
140
                "
6✔
141
        INSERT INTO collection_layers (collection, layer)
6✔
142
        VALUES ($1, $2) ON CONFLICT DO NOTHING;",
6✔
143
            )
6✔
144
            .await?;
4✔
145

146
        trans.execute(&stmt, &[&collection_id, &layer_id]).await?;
6✔
147

148
        // TODO: `ON CONFLICT DO NOTHING` means, we do not get an error if the permission already exists.
149
        //       Do we want that, or should we report an error and let the caller decide whether to ignore it?
150
        //       We should decide that and adjust all places where `ON CONFILCT DO NOTHING` is used.
151
        let stmt = trans
6✔
152
            .prepare(
6✔
153
                "
6✔
154
        INSERT INTO permissions (role_id, permission, layer_id)
6✔
155
        VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;",
6✔
156
            )
6✔
157
            .await?;
4✔
158

159
        trans
6✔
160
            .execute(
6✔
161
                &stmt,
6✔
162
                &[
6✔
163
                    &RoleId::from(self.session.user.id),
6✔
164
                    &Permission::Owner,
6✔
165
                    &layer_id,
6✔
166
                ],
6✔
167
            )
6✔
168
            .await?;
4✔
169

170
        trans.commit().await?;
6✔
171

172
        Ok(LayerId(layer_id.to_string()))
6✔
173
    }
12✔
174

175
    async fn add_layer_with_id(
×
176
        &self,
×
177
        id: &LayerId,
×
178
        layer: AddLayer,
×
179
        collection: &LayerCollectionId,
×
180
    ) -> Result<()> {
×
181
        ensure!(
×
182
            self.has_permission(collection.clone(), Permission::Owner)
×
183
                .await?,
×
184
            error::PermissionDenied
×
185
        );
186

187
        let layer_id =
×
188
            Uuid::from_str(&id.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
×
189
                found: collection.0.clone(),
×
190
            })?;
×
191

192
        let collection_id =
×
193
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
×
194
                found: collection.0.clone(),
×
195
            })?;
×
196

197
        let mut conn = self.conn_pool.get().await?;
×
198

199
        let layer = layer;
×
200

201
        let symbology = serde_json::to_value(&layer.symbology).context(crate::error::SerdeJson)?;
×
202

203
        let metadata = serde_json::to_value(&layer.metadata).context(crate::error::SerdeJson)?;
×
204

205
        let trans = conn.build_transaction().start().await?;
×
206

207
        let stmt = trans
×
208
            .prepare(
×
209
                "
×
210
            INSERT INTO layers (id, name, description, workflow, symbology, properties, metadata)
×
211
            VALUES ($1, $2, $3, $4, $5, $6, $7);",
×
212
            )
×
213
            .await?;
×
214

215
        trans
216
            .execute(
217
                &stmt,
×
218
                &[
×
219
                    &layer_id,
×
220
                    &layer.name,
×
221
                    &layer.description,
×
222
                    &serde_json::to_value(&layer.workflow).context(crate::error::SerdeJson)?,
×
223
                    &symbology,
×
224
                    &layer.properties,
×
225
                    &metadata,
×
226
                ],
227
            )
228
            .await?;
×
229

230
        let stmt = trans
×
231
            .prepare(
×
232
                "
×
233
            INSERT INTO collection_layers (collection, layer)
×
234
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
×
235
            )
×
236
            .await?;
×
237

238
        trans.execute(&stmt, &[&collection_id, &layer_id]).await?;
×
239

240
        let stmt = trans
×
241
            .prepare(
×
242
                "
×
243
            INSERT INTO permissions (role_id, permission, layer_id)
×
244
            VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;",
×
245
            )
×
246
            .await?;
×
247

248
        trans
×
249
            .execute(
×
250
                &stmt,
×
251
                &[
×
252
                    &RoleId::from(self.session.user.id),
×
253
                    &Permission::Owner,
×
254
                    &layer_id,
×
255
                ],
×
256
            )
×
257
            .await?;
×
258

259
        trans.commit().await?;
×
260

261
        Ok(())
×
262
    }
×
263

264
    async fn add_layer_to_collection(
2✔
265
        &self,
2✔
266
        layer: &LayerId,
2✔
267
        collection: &LayerCollectionId,
2✔
268
    ) -> Result<()> {
2✔
269
        ensure!(
2✔
270
            self.has_permission(collection.clone(), Permission::Owner)
2✔
271
                .await?,
3✔
272
            error::PermissionDenied
×
273
        );
274

275
        let layer_id =
1✔
276
            Uuid::from_str(&layer.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
277
                found: layer.0.clone(),
1✔
278
            })?;
2✔
279

280
        let collection_id =
1✔
281
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
282
                found: collection.0.clone(),
×
283
            })?;
1✔
284

285
        let conn = self.conn_pool.get().await?;
1✔
286

287
        let stmt = conn
1✔
288
            .prepare(
1✔
289
                "
1✔
290
            INSERT INTO collection_layers (collection, layer)
1✔
291
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
1✔
292
            )
1✔
293
            .await?;
×
294

295
        conn.execute(&stmt, &[&collection_id, &layer_id]).await?;
1✔
296

297
        Ok(())
1✔
298
    }
4✔
299

300
    async fn add_layer_collection(
12✔
301
        &self,
12✔
302
        collection: AddLayerCollection,
12✔
303
        parent: &LayerCollectionId,
12✔
304
    ) -> Result<LayerCollectionId> {
12✔
305
        ensure!(
12✔
306
            self.has_permission(parent.clone(), Permission::Owner)
12✔
307
                .await?,
49✔
308
            error::PermissionDenied
2✔
309
        );
310

311
        let parent =
10✔
312
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
10✔
313
                found: parent.0.clone(),
×
314
            })?;
10✔
315

316
        let mut conn = self.conn_pool.get().await?;
10✔
317

318
        let collection = collection;
10✔
319

10✔
320
        let collection_id = Uuid::new_v4();
10✔
321

322
        let trans = conn.build_transaction().start().await?;
10✔
323

324
        let stmt = trans
10✔
325
            .prepare(
10✔
326
                "
10✔
327
            INSERT INTO layer_collections (id, name, description, properties)
10✔
328
            VALUES ($1, $2, $3, $4);",
10✔
329
            )
10✔
330
            .await?;
23✔
331

332
        trans
10✔
333
            .execute(
10✔
334
                &stmt,
10✔
335
                &[
10✔
336
                    &collection_id,
10✔
337
                    &collection.name,
10✔
338
                    &collection.description,
10✔
339
                    &collection.properties,
10✔
340
                ],
10✔
341
            )
10✔
342
            .await?;
8✔
343

344
        let stmt = trans
10✔
345
            .prepare(
10✔
346
                "
10✔
347
            INSERT INTO collection_children (parent, child)
10✔
348
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
10✔
349
            )
10✔
350
            .await?;
9✔
351

352
        trans.execute(&stmt, &[&parent, &collection_id]).await?;
10✔
353

354
        let stmt = trans
10✔
355
            .prepare(
10✔
356
                "
10✔
357
            INSERT INTO permissions (role_id, permission, layer_collection_id)
10✔
358
            VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;",
10✔
359
            )
10✔
360
            .await?;
9✔
361

362
        trans
10✔
363
            .execute(
10✔
364
                &stmt,
10✔
365
                &[
10✔
366
                    &RoleId::from(self.session.user.id),
10✔
367
                    &Permission::Owner,
10✔
368
                    &collection_id,
10✔
369
                ],
10✔
370
            )
10✔
371
            .await?;
9✔
372

373
        trans.commit().await?;
10✔
374

375
        Ok(LayerCollectionId(collection_id.to_string()))
10✔
376
    }
24✔
377

378
    async fn add_layer_collection_with_id(
×
379
        &self,
×
380
        id: &LayerCollectionId,
×
381
        collection: AddLayerCollection,
×
382
        parent: &LayerCollectionId,
×
383
    ) -> Result<()> {
×
384
        ensure!(
×
385
            self.has_permission(parent.clone(), Permission::Owner)
×
386
                .await?,
×
387
            error::PermissionDenied
×
388
        );
389

390
        let collection_id =
×
391
            Uuid::from_str(&id.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
×
392
                found: id.0.clone(),
×
393
            })?;
×
394

395
        let parent =
×
396
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
×
397
                found: parent.0.clone(),
×
398
            })?;
×
399

400
        let mut conn = self.conn_pool.get().await?;
×
401

402
        let trans = conn.build_transaction().start().await?;
×
403

404
        let stmt = trans
×
405
            .prepare(
×
406
                "
×
407
            INSERT INTO layer_collections (id, name, description, properties)
×
408
            VALUES ($1, $2, $3, $4);",
×
409
            )
×
410
            .await?;
×
411

412
        trans
×
413
            .execute(
×
414
                &stmt,
×
415
                &[
×
416
                    &collection_id,
×
417
                    &collection.name,
×
418
                    &collection.description,
×
419
                    &collection.properties,
×
420
                ],
×
421
            )
×
422
            .await?;
×
423

424
        let stmt = trans
×
425
            .prepare(
×
426
                "
×
427
            INSERT INTO collection_children (parent, child)
×
428
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
×
429
            )
×
430
            .await?;
×
431

432
        trans.execute(&stmt, &[&parent, &collection_id]).await?;
×
433

434
        let stmt = trans
×
435
            .prepare(
×
436
                "
×
437
            INSERT INTO permissions (role_id, permission, layer_collection_id)
×
438
            VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;",
×
439
            )
×
440
            .await?;
×
441

442
        trans
×
443
            .execute(
×
444
                &stmt,
×
445
                &[
×
446
                    &RoleId::from(self.session.user.id),
×
447
                    &Permission::Owner,
×
448
                    &collection_id,
×
449
                ],
×
450
            )
×
451
            .await?;
×
452

453
        trans.commit().await?;
×
454

455
        Ok(())
×
456
    }
×
457

458
    async fn add_collection_to_parent(
1✔
459
        &self,
1✔
460
        collection: &LayerCollectionId,
1✔
461
        parent: &LayerCollectionId,
1✔
462
    ) -> Result<()> {
1✔
463
        ensure!(
1✔
464
            self.has_permission(collection.clone(), Permission::Owner)
1✔
465
                .await?,
3✔
466
            error::PermissionDenied
×
467
        );
468

469
        let collection =
1✔
470
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
471
                found: collection.0.clone(),
×
472
            })?;
1✔
473

474
        let parent =
1✔
475
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
476
                found: parent.0.clone(),
×
477
            })?;
1✔
478

479
        let conn = self.conn_pool.get().await?;
1✔
480

481
        let stmt = conn
1✔
482
            .prepare(
1✔
483
                "
1✔
484
            INSERT INTO collection_children (parent, child)
1✔
485
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
1✔
486
            )
1✔
487
            .await?;
1✔
488

489
        conn.execute(&stmt, &[&parent, &collection]).await?;
1✔
490

491
        Ok(())
1✔
492
    }
2✔
493

494
    async fn remove_layer_collection(&self, collection: &LayerCollectionId) -> Result<()> {
3✔
495
        ensure!(
3✔
496
            self.has_permission(collection.clone(), Permission::Owner)
3✔
497
                .await?,
9✔
498
            error::PermissionDenied
×
499
        );
500

501
        let collection =
3✔
502
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
3✔
503
                found: collection.0.clone(),
×
504
            })?;
3✔
505

506
        if collection == INTERNAL_LAYER_DB_ROOT_COLLECTION_ID {
3✔
507
            return Err(LayerDbError::CannotRemoveRootCollection.into());
1✔
508
        }
2✔
509

510
        let mut conn = self.conn_pool.get().await?;
2✔
511
        let transaction = conn.transaction().await?;
2✔
512

513
        // delete the collection!
514
        // on delete cascade removes all entries from `collection_children` and `collection_layers`
515

516
        let remove_layer_collection_stmt = transaction
2✔
517
            .prepare(
2✔
518
                "DELETE FROM layer_collections
2✔
519
                 WHERE id = $1;",
2✔
520
            )
2✔
521
            .await?;
2✔
522
        transaction
2✔
523
            .execute(&remove_layer_collection_stmt, &[&collection])
2✔
524
            .await?;
2✔
525

526
        _remove_collections_without_parent_collection(&transaction).await?;
4✔
527

528
        _remove_layers_without_parent_collection(&transaction).await?;
4✔
529

530
        transaction.commit().await.map_err(Into::into)
2✔
531
    }
6✔
532

533
    async fn remove_layer_from_collection(
2✔
534
        &self,
2✔
535
        layer: &LayerId,
2✔
536
        collection: &LayerCollectionId,
2✔
537
    ) -> Result<()> {
2✔
538
        ensure!(
2✔
539
            self.has_permission(layer.clone(), Permission::Owner)
2✔
540
                .await?,
3✔
541
            error::PermissionDenied
×
542
        );
543

544
        let collection_uuid =
2✔
545
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
546
                found: collection.0.clone(),
×
547
            })?;
2✔
548

549
        let layer_uuid =
2✔
550
            Uuid::from_str(&layer.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
551
                found: layer.0.clone(),
×
552
            })?;
2✔
553

554
        let mut conn = self.conn_pool.get().await?;
2✔
555
        let transaction = conn.transaction().await?;
2✔
556

557
        let remove_layer_collection_stmt = transaction
2✔
558
            .prepare(
2✔
559
                "DELETE FROM collection_layers
2✔
560
                 WHERE collection = $1
2✔
561
                 AND layer = $2;",
2✔
562
            )
2✔
563
            .await?;
1✔
564
        let num_results = transaction
2✔
565
            .execute(
2✔
566
                &remove_layer_collection_stmt,
2✔
567
                &[&collection_uuid, &layer_uuid],
2✔
568
            )
2✔
569
            .await?;
1✔
570

571
        if num_results == 0 {
2✔
572
            return Err(LayerDbError::NoLayerForGivenIdInCollection {
×
573
                layer: layer.clone(),
×
574
                collection: collection.clone(),
×
575
            }
×
576
            .into());
×
577
        }
2✔
578

2✔
579
        _remove_layers_without_parent_collection(&transaction).await?;
2✔
580

581
        transaction.commit().await.map_err(Into::into)
2✔
582
    }
4✔
583

584
    async fn remove_layer_collection_from_parent(
1✔
585
        &self,
1✔
586
        collection: &LayerCollectionId,
1✔
587
        parent: &LayerCollectionId,
1✔
588
    ) -> Result<()> {
1✔
589
        ensure!(
1✔
590
            self.has_permission(collection.clone(), Permission::Owner)
1✔
591
                .await?,
3✔
592
            error::PermissionDenied
×
593
        );
594

595
        let collection_uuid =
1✔
596
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
597
                found: collection.0.clone(),
×
598
            })?;
1✔
599

600
        let parent_collection_uuid =
1✔
601
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
602
                found: parent.0.clone(),
×
603
            })?;
1✔
604

605
        let mut conn = self.conn_pool.get().await?;
1✔
606
        let transaction = conn.transaction().await?;
1✔
607

608
        let remove_layer_collection_stmt = transaction
1✔
609
            .prepare(
1✔
610
                "DELETE FROM collection_children
1✔
611
                 WHERE child = $1
1✔
612
                 AND parent = $2;",
1✔
613
            )
1✔
614
            .await?;
1✔
615
        let num_results = transaction
1✔
616
            .execute(
1✔
617
                &remove_layer_collection_stmt,
1✔
618
                &[&collection_uuid, &parent_collection_uuid],
1✔
619
            )
1✔
620
            .await?;
1✔
621

622
        if num_results == 0 {
1✔
623
            return Err(LayerDbError::NoCollectionForGivenIdInCollection {
×
624
                collection: collection.clone(),
×
625
                parent: parent.clone(),
×
626
            }
×
627
            .into());
×
628
        }
1✔
629

1✔
630
        _remove_collections_without_parent_collection(&transaction).await?;
4✔
631

632
        _remove_layers_without_parent_collection(&transaction).await?;
2✔
633

634
        transaction.commit().await.map_err(Into::into)
1✔
635
    }
2✔
636
}
637

638
#[async_trait]
639
impl<Tls> LayerCollectionProvider for PostgresDb<Tls>
640
where
641
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
642
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
643
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
644
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
645
{
646
    #[allow(clippy::too_many_lines)]
647
    async fn load_layer_collection(
15✔
648
        &self,
15✔
649
        collection_id: &LayerCollectionId,
15✔
650
        options: LayerCollectionListOptions,
15✔
651
    ) -> Result<LayerCollection> {
15✔
652
        ensure!(
15✔
653
            self.has_permission(collection_id.clone(), Permission::Read)
15✔
654
                .await?,
45✔
655
            error::PermissionDenied
3✔
656
        );
657
        let collection = Uuid::from_str(&collection_id.0).map_err(|_| {
12✔
658
            crate::error::Error::IdStringMustBeUuid {
×
659
                found: collection_id.0.clone(),
×
660
            }
×
661
        })?;
12✔
662

663
        let conn = self.conn_pool.get().await?;
12✔
664

665
        let stmt = conn
12✔
666
            .prepare(
12✔
667
                "
12✔
668
        SELECT DISTINCT name, description, properties
12✔
669
        FROM user_permitted_layer_collections p 
12✔
670
            JOIN layer_collections c ON (p.layer_collection_id = c.id) 
12✔
671
        WHERE p.user_id = $1 AND layer_collection_id = $2;",
12✔
672
            )
12✔
673
            .await?;
12✔
674

675
        let row = conn
12✔
676
            .query_one(&stmt, &[&self.session.user.id, &collection])
12✔
677
            .await?;
12✔
678

679
        let name: String = row.get(0);
12✔
680
        let description: String = row.get(1);
12✔
681
        let properties: Vec<Property> = row.get(2);
12✔
682

683
        let stmt = conn
12✔
684
            .prepare(
12✔
685
                "
12✔
686
        SELECT DISTINCT id, name, description, properties, is_layer
12✔
687
        FROM (
12✔
688
            SELECT 
12✔
689
                concat(id, '') AS id, 
12✔
690
                name, 
12✔
691
                description, 
12✔
692
                properties, 
12✔
693
                FALSE AS is_layer
12✔
694
            FROM user_permitted_layer_collections u 
12✔
695
                JOIN layer_collections lc ON (u.layer_collection_id = lc.id)
12✔
696
                JOIN collection_children cc ON (layer_collection_id = cc.child)
12✔
697
            WHERE u.user_id = $4 AND cc.parent = $1
12✔
698
        ) u UNION (
12✔
699
            SELECT 
12✔
700
                concat(id, '') AS id, 
12✔
701
                name, 
12✔
702
                description, 
12✔
703
                properties, 
12✔
704
                TRUE AS is_layer
12✔
705
            FROM user_permitted_layers ul
12✔
706
                JOIN layers uc ON (ul.layer_id = uc.id) 
12✔
707
                JOIN collection_layers cl ON (layer_id = cl.layer)
12✔
708
            WHERE ul.user_id = $4 AND cl.collection = $1
12✔
709
        )
12✔
710
        ORDER BY is_layer ASC, name ASC
12✔
711
        LIMIT $2 
12✔
712
        OFFSET $3;            
12✔
713
        ",
12✔
714
            )
12✔
715
            .await?;
12✔
716

717
        let rows = conn
12✔
718
            .query(
12✔
719
                &stmt,
12✔
720
                &[
12✔
721
                    &collection,
12✔
722
                    &i64::from(options.limit),
12✔
723
                    &i64::from(options.offset),
12✔
724
                    &self.session.user.id,
12✔
725
                ],
12✔
726
            )
12✔
727
            .await?;
12✔
728

729
        let items = rows
12✔
730
            .into_iter()
12✔
731
            .map(|row| {
15✔
732
                let is_layer: bool = row.get(4);
15✔
733

15✔
734
                if is_layer {
15✔
735
                    Ok(CollectionItem::Layer(LayerListing {
5✔
736
                        id: ProviderLayerId {
5✔
737
                            provider_id: INTERNAL_PROVIDER_ID,
5✔
738
                            layer_id: LayerId(row.get(0)),
5✔
739
                        },
5✔
740
                        name: row.get(1),
5✔
741
                        description: row.get(2),
5✔
742
                        properties: row.get(3),
5✔
743
                    }))
5✔
744
                } else {
745
                    Ok(CollectionItem::Collection(LayerCollectionListing {
10✔
746
                        id: ProviderLayerCollectionId {
10✔
747
                            provider_id: INTERNAL_PROVIDER_ID,
10✔
748
                            collection_id: LayerCollectionId(row.get(0)),
10✔
749
                        },
10✔
750
                        name: row.get(1),
10✔
751
                        description: row.get(2),
10✔
752
                        properties: row.get(3),
10✔
753
                    }))
10✔
754
                }
755
            })
15✔
756
            .collect::<Result<Vec<CollectionItem>>>()?;
12✔
757

758
        Ok(LayerCollection {
12✔
759
            id: ProviderLayerCollectionId {
12✔
760
                provider_id: INTERNAL_PROVIDER_ID,
12✔
761
                collection_id: collection_id.clone(),
12✔
762
            },
12✔
763
            name,
12✔
764
            description,
12✔
765
            items,
12✔
766
            entry_label: None,
12✔
767
            properties,
12✔
768
        })
12✔
769
    }
30✔
770

771
    async fn get_root_layer_collection_id(&self) -> Result<LayerCollectionId> {
6✔
772
        Ok(LayerCollectionId(
6✔
773
            INTERNAL_LAYER_DB_ROOT_COLLECTION_ID.to_string(),
6✔
774
        ))
6✔
775
    }
6✔
776

777
    async fn load_layer(&self, id: &LayerId) -> Result<Layer> {
5✔
778
        ensure!(
5✔
779
            self.has_permission(id.clone(), Permission::Read).await?,
15✔
780
            error::PermissionDenied
3✔
781
        );
782

783
        let layer_id =
2✔
784
            Uuid::from_str(&id.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
785
                found: id.0.clone(),
×
786
            })?;
2✔
787

788
        let conn = self.conn_pool.get().await?;
2✔
789

790
        let stmt = conn
2✔
791
            .prepare(
2✔
792
                "
2✔
793
            SELECT 
2✔
794
                name,
2✔
795
                description,
2✔
796
                workflow,
2✔
797
                symbology,
2✔
798
                properties,
2✔
799
                metadata
2✔
800
            FROM layers l
2✔
801
            WHERE l.id = $1;",
2✔
802
            )
2✔
803
            .await?;
2✔
804

805
        let row = conn
2✔
806
            .query_one(&stmt, &[&layer_id])
2✔
807
            .await
2✔
808
            .map_err(|_error| LayerDbError::NoLayerForGivenId { id: id.clone() })?;
2✔
809

810
        Ok(Layer {
811
            id: ProviderLayerId {
2✔
812
                provider_id: INTERNAL_PROVIDER_ID,
2✔
813
                layer_id: id.clone(),
2✔
814
            },
2✔
815
            name: row.get(0),
2✔
816
            description: row.get(1),
2✔
817
            workflow: serde_json::from_value(row.get(2)).context(crate::error::SerdeJson)?,
2✔
818
            symbology: serde_json::from_value(row.get(3)).context(crate::error::SerdeJson)?,
2✔
819
            properties: row.get(4),
2✔
820
            metadata: serde_json::from_value(row.get(5)).context(crate::error::SerdeJson)?,
2✔
821
        })
822
    }
10✔
823
}
824

825
#[async_trait]
826
impl<Tls> LayerProviderDb for PostgresDb<Tls>
827
where
828
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
829
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
830
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
831
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
832
{
833
    async fn add_layer_provider(
1✔
834
        &self,
1✔
835
        provider: Box<dyn DataProviderDefinition>,
1✔
836
    ) -> Result<DataProviderId> {
1✔
837
        ensure!(self.session.is_admin(), error::PermissionDenied);
1✔
838

839
        let conn = self.conn_pool.get().await?;
1✔
840

841
        let stmt = conn
1✔
842
            .prepare(
1✔
843
                "
1✔
844
              INSERT INTO layer_providers (
1✔
845
                  id, 
1✔
846
                  type_name, 
1✔
847
                  name,
1✔
848
                  definition
1✔
849
              )
1✔
850
              VALUES ($1, $2, $3, $4)",
1✔
851
            )
1✔
852
            .await?;
1✔
853

854
        let id = provider.id();
1✔
855
        conn.execute(
1✔
856
            &stmt,
1✔
857
            &[
1✔
858
                &id,
1✔
859
                &provider.type_name(),
1✔
860
                &provider.name(),
1✔
861
                &serde_json::to_value(provider)?,
1✔
862
            ],
863
        )
864
        .await?;
1✔
865
        Ok(id)
1✔
866
    }
2✔
867

868
    async fn list_layer_providers(
1✔
869
        &self,
1✔
870
        options: LayerProviderListingOptions,
1✔
871
    ) -> Result<Vec<LayerProviderListing>> {
1✔
872
        // TODO: permission
873
        let conn = self.conn_pool.get().await?;
1✔
874

875
        let stmt = conn
1✔
876
            .prepare(
1✔
877
                "
1✔
878
            SELECT 
1✔
879
                id, 
1✔
880
                name,
1✔
881
                type_name
1✔
882
            FROM 
1✔
883
                layer_providers
1✔
884
            ORDER BY name ASC
1✔
885
            LIMIT $1 
1✔
886
            OFFSET $2;",
1✔
887
            )
1✔
888
            .await?;
1✔
889

890
        let rows = conn
1✔
891
            .query(
1✔
892
                &stmt,
1✔
893
                &[&i64::from(options.limit), &i64::from(options.offset)],
1✔
894
            )
1✔
895
            .await?;
1✔
896

897
        Ok(rows
1✔
898
            .iter()
1✔
899
            .map(|row| LayerProviderListing {
1✔
900
                id: row.get(0),
1✔
901
                name: row.get(1),
1✔
902
                description: row.get(2),
1✔
903
            })
1✔
904
            .collect())
1✔
905
    }
2✔
906

907
    async fn load_layer_provider(&self, id: DataProviderId) -> Result<Box<dyn DataProvider>> {
1✔
908
        // TODO: permissions
909
        let conn = self.conn_pool.get().await?;
1✔
910

911
        let stmt = conn
1✔
912
            .prepare(
1✔
913
                "
1✔
914
               SELECT 
1✔
915
                   definition
1✔
916
               FROM 
1✔
917
                   layer_providers
1✔
918
               WHERE
1✔
919
                   id = $1",
1✔
920
            )
1✔
921
            .await?;
1✔
922

923
        let row = conn.query_one(&stmt, &[&id]).await?;
1✔
924

925
        let definition = serde_json::from_value::<Box<dyn DataProviderDefinition>>(row.get(0))?;
1✔
926

927
        definition.initialize().await
1✔
928
    }
2✔
929
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc