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

geo-engine / geoengine / 4546518522

pending completion
4546518522

push

github

GitHub
Merge #765

628 of 628 new or added lines in 49 files covered. (100.0%)

96123 of 107536 relevant lines covered (89.39%)

73410.79 hits per line

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

77.59
/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?;
5✔
77
    transaction
5✔
78
        .execute(&remove_layers_without_parents_stmt, &[])
5✔
79
        .await?;
5✔
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?,
19✔
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?;
5✔
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?;
5✔
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?;
6✔
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?;
5✔
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?;
5✔
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 trans = conn.build_transaction().start().await?;
×
204

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

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

226
        let stmt = trans
×
227
            .prepare(
×
228
                "
×
229
            INSERT INTO collection_layers (collection, layer)
×
230
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
×
231
            )
×
232
            .await?;
×
233

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

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

244
        trans
×
245
            .execute(
×
246
                &stmt,
×
247
                &[
×
248
                    &RoleId::from(self.session.user.id),
×
249
                    &Permission::Owner,
×
250
                    &layer_id,
×
251
                ],
×
252
            )
×
253
            .await?;
×
254

255
        trans.commit().await?;
×
256

257
        Ok(())
×
258
    }
×
259

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

271
        let layer_id =
1✔
272
            Uuid::from_str(&layer.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
273
                found: layer.0.clone(),
1✔
274
            })?;
2✔
275

276
        let collection_id =
1✔
277
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
278
                found: collection.0.clone(),
×
279
            })?;
1✔
280

281
        let conn = self.conn_pool.get().await?;
1✔
282

283
        let stmt = conn
1✔
284
            .prepare(
1✔
285
                "
1✔
286
            INSERT INTO collection_layers (collection, layer)
1✔
287
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
1✔
288
            )
1✔
289
            .await?;
1✔
290

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

293
        Ok(())
1✔
294
    }
4✔
295

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

307
        let parent =
10✔
308
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
10✔
309
                found: parent.0.clone(),
×
310
            })?;
10✔
311

312
        let mut conn = self.conn_pool.get().await?;
10✔
313

314
        let collection = collection;
10✔
315

10✔
316
        let collection_id = Uuid::new_v4();
10✔
317

318
        let trans = conn.build_transaction().start().await?;
10✔
319

320
        let stmt = trans
10✔
321
            .prepare(
10✔
322
                "
10✔
323
            INSERT INTO layer_collections (id, name, description, properties)
10✔
324
            VALUES ($1, $2, $3, $4);",
10✔
325
            )
10✔
326
            .await?;
25✔
327

328
        trans
10✔
329
            .execute(
10✔
330
                &stmt,
10✔
331
                &[
10✔
332
                    &collection_id,
10✔
333
                    &collection.name,
10✔
334
                    &collection.description,
10✔
335
                    &collection.properties,
10✔
336
                ],
10✔
337
            )
10✔
338
            .await?;
8✔
339

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

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

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

358
        trans
10✔
359
            .execute(
10✔
360
                &stmt,
10✔
361
                &[
10✔
362
                    &RoleId::from(self.session.user.id),
10✔
363
                    &Permission::Owner,
10✔
364
                    &collection_id,
10✔
365
                ],
10✔
366
            )
10✔
367
            .await?;
8✔
368

369
        trans.commit().await?;
10✔
370

371
        Ok(LayerCollectionId(collection_id.to_string()))
10✔
372
    }
24✔
373

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

386
        let collection_id =
×
387
            Uuid::from_str(&id.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
×
388
                found: id.0.clone(),
×
389
            })?;
×
390

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

396
        let mut conn = self.conn_pool.get().await?;
×
397

398
        let trans = conn.build_transaction().start().await?;
×
399

400
        let stmt = trans
×
401
            .prepare(
×
402
                "
×
403
            INSERT INTO layer_collections (id, name, description)
×
404
            VALUES ($1, $2, $3);",
×
405
            )
×
406
            .await?;
×
407

408
        trans
×
409
            .execute(
×
410
                &stmt,
×
411
                &[&collection_id, &collection.name, &collection.description],
×
412
            )
×
413
            .await?;
×
414

415
        let stmt = trans
×
416
            .prepare(
×
417
                "
×
418
            INSERT INTO collection_children (parent, child)
×
419
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
×
420
            )
×
421
            .await?;
×
422

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

425
        let stmt = trans
×
426
            .prepare(
×
427
                "
×
428
            INSERT INTO permissions (role_id, permission, layer_collection_id)
×
429
            VALUES ($1, $2, $3) ON CONFLICT DO NOTHING;",
×
430
            )
×
431
            .await?;
×
432

433
        trans
×
434
            .execute(
×
435
                &stmt,
×
436
                &[
×
437
                    &RoleId::from(self.session.user.id),
×
438
                    &Permission::Owner,
×
439
                    &collection_id,
×
440
                ],
×
441
            )
×
442
            .await?;
×
443

444
        trans.commit().await?;
×
445

446
        Ok(())
×
447
    }
×
448

449
    async fn add_collection_to_parent(
1✔
450
        &self,
1✔
451
        collection: &LayerCollectionId,
1✔
452
        parent: &LayerCollectionId,
1✔
453
    ) -> Result<()> {
1✔
454
        ensure!(
1✔
455
            self.has_permission(collection.clone(), Permission::Owner)
1✔
456
                .await?,
3✔
457
            error::PermissionDenied
×
458
        );
459

460
        let collection =
1✔
461
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
462
                found: collection.0.clone(),
×
463
            })?;
1✔
464

465
        let parent =
1✔
466
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
467
                found: parent.0.clone(),
×
468
            })?;
1✔
469

470
        let conn = self.conn_pool.get().await?;
1✔
471

472
        let stmt = conn
1✔
473
            .prepare(
1✔
474
                "
1✔
475
            INSERT INTO collection_children (parent, child)
1✔
476
            VALUES ($1, $2) ON CONFLICT DO NOTHING;",
1✔
477
            )
1✔
478
            .await?;
1✔
479

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

482
        Ok(())
1✔
483
    }
2✔
484

485
    async fn remove_layer_collection(&self, collection: &LayerCollectionId) -> Result<()> {
3✔
486
        ensure!(
3✔
487
            self.has_permission(collection.clone(), Permission::Owner)
3✔
488
                .await?,
9✔
489
            error::PermissionDenied
×
490
        );
491

492
        let collection =
3✔
493
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
3✔
494
                found: collection.0.clone(),
×
495
            })?;
3✔
496

497
        if collection == INTERNAL_LAYER_DB_ROOT_COLLECTION_ID {
3✔
498
            return Err(LayerDbError::CannotRemoveRootCollection.into());
1✔
499
        }
2✔
500

501
        let mut conn = self.conn_pool.get().await?;
2✔
502
        let transaction = conn.transaction().await?;
2✔
503

504
        // delete the collection!
505
        // on delete cascade removes all entries from `collection_children` and `collection_layers`
506

507
        let remove_layer_collection_stmt = transaction
2✔
508
            .prepare(
2✔
509
                "DELETE FROM layer_collections
2✔
510
                 WHERE id = $1;",
2✔
511
            )
2✔
512
            .await?;
2✔
513
        transaction
2✔
514
            .execute(&remove_layer_collection_stmt, &[&collection])
2✔
515
            .await?;
2✔
516

517
        _remove_collections_without_parent_collection(&transaction).await?;
4✔
518

519
        _remove_layers_without_parent_collection(&transaction).await?;
4✔
520

521
        transaction.commit().await.map_err(Into::into)
2✔
522
    }
6✔
523

524
    async fn remove_layer_from_collection(
2✔
525
        &self,
2✔
526
        layer: &LayerId,
2✔
527
        collection: &LayerCollectionId,
2✔
528
    ) -> Result<()> {
2✔
529
        ensure!(
2✔
530
            self.has_permission(layer.clone(), Permission::Owner)
2✔
531
                .await?,
6✔
532
            error::PermissionDenied
×
533
        );
534

535
        let collection_uuid =
2✔
536
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
537
                found: collection.0.clone(),
×
538
            })?;
2✔
539

540
        let layer_uuid =
2✔
541
            Uuid::from_str(&layer.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
542
                found: layer.0.clone(),
×
543
            })?;
2✔
544

545
        let mut conn = self.conn_pool.get().await?;
2✔
546
        let transaction = conn.transaction().await?;
2✔
547

548
        let remove_layer_collection_stmt = transaction
2✔
549
            .prepare(
2✔
550
                "DELETE FROM collection_layers
2✔
551
                 WHERE collection = $1
2✔
552
                 AND layer = $2;",
2✔
553
            )
2✔
554
            .await?;
2✔
555
        let num_results = transaction
2✔
556
            .execute(
2✔
557
                &remove_layer_collection_stmt,
2✔
558
                &[&collection_uuid, &layer_uuid],
2✔
559
            )
2✔
560
            .await?;
2✔
561

562
        if num_results == 0 {
2✔
563
            return Err(LayerDbError::NoLayerForGivenIdInCollection {
×
564
                layer: layer.clone(),
×
565
                collection: collection.clone(),
×
566
            }
×
567
            .into());
×
568
        }
2✔
569

2✔
570
        _remove_layers_without_parent_collection(&transaction).await?;
4✔
571

572
        transaction.commit().await.map_err(Into::into)
2✔
573
    }
4✔
574

575
    async fn remove_layer_collection_from_parent(
1✔
576
        &self,
1✔
577
        collection: &LayerCollectionId,
1✔
578
        parent: &LayerCollectionId,
1✔
579
    ) -> Result<()> {
1✔
580
        ensure!(
1✔
581
            self.has_permission(collection.clone(), Permission::Owner)
1✔
582
                .await?,
3✔
583
            error::PermissionDenied
×
584
        );
585

586
        let collection_uuid =
1✔
587
            Uuid::from_str(&collection.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
588
                found: collection.0.clone(),
×
589
            })?;
1✔
590

591
        let parent_collection_uuid =
1✔
592
            Uuid::from_str(&parent.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
1✔
593
                found: parent.0.clone(),
×
594
            })?;
1✔
595

596
        let mut conn = self.conn_pool.get().await?;
1✔
597
        let transaction = conn.transaction().await?;
1✔
598

599
        let remove_layer_collection_stmt = transaction
1✔
600
            .prepare(
1✔
601
                "DELETE FROM collection_children
1✔
602
                 WHERE child = $1
1✔
603
                 AND parent = $2;",
1✔
604
            )
1✔
605
            .await?;
1✔
606
        let num_results = transaction
1✔
607
            .execute(
1✔
608
                &remove_layer_collection_stmt,
1✔
609
                &[&collection_uuid, &parent_collection_uuid],
1✔
610
            )
1✔
611
            .await?;
1✔
612

613
        if num_results == 0 {
1✔
614
            return Err(LayerDbError::NoCollectionForGivenIdInCollection {
×
615
                collection: collection.clone(),
×
616
                parent: parent.clone(),
×
617
            }
×
618
            .into());
×
619
        }
1✔
620

1✔
621
        _remove_collections_without_parent_collection(&transaction).await?;
4✔
622

623
        _remove_layers_without_parent_collection(&transaction).await?;
2✔
624

625
        transaction.commit().await.map_err(Into::into)
1✔
626
    }
2✔
627
}
628

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

654
        let conn = self.conn_pool.get().await?;
12✔
655

656
        let stmt = conn
12✔
657
            .prepare(
12✔
658
                "
12✔
659
        SELECT DISTINCT name, description, properties
12✔
660
        FROM user_permitted_layer_collections p 
12✔
661
            JOIN layer_collections c ON (p.layer_collection_id = c.id) 
12✔
662
        WHERE p.user_id = $1 AND layer_collection_id = $2;",
12✔
663
            )
12✔
664
            .await?;
11✔
665

666
        let row = conn
12✔
667
            .query_one(&stmt, &[&self.session.user.id, &collection])
12✔
668
            .await?;
11✔
669

670
        let name: String = row.get(0);
12✔
671
        let description: String = row.get(1);
12✔
672
        let properties: Vec<Property> = row.get(2);
12✔
673

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

708
        let rows = conn
12✔
709
            .query(
12✔
710
                &stmt,
12✔
711
                &[
12✔
712
                    &collection,
12✔
713
                    &i64::from(options.limit),
12✔
714
                    &i64::from(options.offset),
12✔
715
                    &self.session.user.id,
12✔
716
                ],
12✔
717
            )
12✔
718
            .await?;
11✔
719

720
        let items = rows
12✔
721
            .into_iter()
12✔
722
            .map(|row| {
15✔
723
                let is_layer: bool = row.get(4);
15✔
724

15✔
725
                if is_layer {
15✔
726
                    Ok(CollectionItem::Layer(LayerListing {
5✔
727
                        id: ProviderLayerId {
5✔
728
                            provider_id: INTERNAL_PROVIDER_ID,
5✔
729
                            layer_id: LayerId(row.get(0)),
5✔
730
                        },
5✔
731
                        name: row.get(1),
5✔
732
                        description: row.get(2),
5✔
733
                        properties: row.get(3),
5✔
734
                    }))
5✔
735
                } else {
736
                    Ok(CollectionItem::Collection(LayerCollectionListing {
10✔
737
                        id: ProviderLayerCollectionId {
10✔
738
                            provider_id: INTERNAL_PROVIDER_ID,
10✔
739
                            collection_id: LayerCollectionId(row.get(0)),
10✔
740
                        },
10✔
741
                        name: row.get(1),
10✔
742
                        description: row.get(2),
10✔
743
                        properties: row.get(3),
10✔
744
                    }))
10✔
745
                }
746
            })
15✔
747
            .collect::<Result<Vec<CollectionItem>>>()?;
12✔
748

749
        Ok(LayerCollection {
12✔
750
            id: ProviderLayerCollectionId {
12✔
751
                provider_id: INTERNAL_PROVIDER_ID,
12✔
752
                collection_id: collection_id.clone(),
12✔
753
            },
12✔
754
            name,
12✔
755
            description,
12✔
756
            items,
12✔
757
            entry_label: None,
12✔
758
            properties,
12✔
759
        })
12✔
760
    }
30✔
761

762
    async fn get_root_layer_collection_id(&self) -> Result<LayerCollectionId> {
6✔
763
        Ok(LayerCollectionId(
6✔
764
            INTERNAL_LAYER_DB_ROOT_COLLECTION_ID.to_string(),
6✔
765
        ))
6✔
766
    }
6✔
767

768
    async fn load_layer(&self, id: &LayerId) -> Result<Layer> {
5✔
769
        ensure!(
5✔
770
            self.has_permission(id.clone(), Permission::Read).await?,
14✔
771
            error::PermissionDenied
3✔
772
        );
773

774
        let layer_id =
2✔
775
            Uuid::from_str(&id.0).map_err(|_| crate::error::Error::IdStringMustBeUuid {
2✔
776
                found: id.0.clone(),
×
777
            })?;
2✔
778

779
        let conn = self.conn_pool.get().await?;
2✔
780

781
        let stmt = conn
2✔
782
            .prepare(
2✔
783
                "
2✔
784
            SELECT 
2✔
785
                name,
2✔
786
                description,
2✔
787
                workflow,
2✔
788
                symbology,
2✔
789
                properties,
2✔
790
                metadata
2✔
791
            FROM layers l
2✔
792
            WHERE l.id = $1;",
2✔
793
            )
2✔
794
            .await?;
2✔
795

796
        let row = conn
2✔
797
            .query_one(&stmt, &[&layer_id])
2✔
798
            .await
1✔
799
            .map_err(|_error| LayerDbError::NoLayerForGivenId { id: id.clone() })?;
2✔
800

801
        Ok(Layer {
802
            id: ProviderLayerId {
2✔
803
                provider_id: INTERNAL_PROVIDER_ID,
2✔
804
                layer_id: id.clone(),
2✔
805
            },
2✔
806
            name: row.get(0),
2✔
807
            description: row.get(1),
2✔
808
            workflow: serde_json::from_value(row.get(2)).context(crate::error::SerdeJson)?,
2✔
809
            symbology: serde_json::from_value(row.get(3)).context(crate::error::SerdeJson)?,
2✔
810
            properties: row.get(4),
2✔
811
            metadata: serde_json::from_value(row.get(5)).context(crate::error::SerdeJson)?,
2✔
812
        })
813
    }
10✔
814
}
815

816
#[async_trait]
817
impl<Tls> LayerProviderDb for PostgresDb<Tls>
818
where
819
    Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
820
    <Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
821
    <Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
822
    <<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
823
{
824
    async fn add_layer_provider(
1✔
825
        &self,
1✔
826
        provider: Box<dyn DataProviderDefinition>,
1✔
827
    ) -> Result<DataProviderId> {
1✔
828
        ensure!(self.session.is_admin(), error::PermissionDenied);
1✔
829

830
        let conn = self.conn_pool.get().await?;
1✔
831

832
        let stmt = conn
1✔
833
            .prepare(
1✔
834
                "
1✔
835
              INSERT INTO layer_providers (
1✔
836
                  id, 
1✔
837
                  type_name, 
1✔
838
                  name,
1✔
839
                  definition
1✔
840
              )
1✔
841
              VALUES ($1, $2, $3, $4)",
1✔
842
            )
1✔
843
            .await?;
1✔
844

845
        let id = provider.id();
1✔
846
        conn.execute(
1✔
847
            &stmt,
1✔
848
            &[
1✔
849
                &id,
1✔
850
                &provider.type_name(),
1✔
851
                &provider.name(),
1✔
852
                &serde_json::to_value(provider)?,
1✔
853
            ],
854
        )
855
        .await?;
1✔
856
        Ok(id)
1✔
857
    }
2✔
858

859
    async fn list_layer_providers(
1✔
860
        &self,
1✔
861
        options: LayerProviderListingOptions,
1✔
862
    ) -> Result<Vec<LayerProviderListing>> {
1✔
863
        // TODO: permission
864
        let conn = self.conn_pool.get().await?;
1✔
865

866
        let stmt = conn
1✔
867
            .prepare(
1✔
868
                "
1✔
869
            SELECT 
1✔
870
                id, 
1✔
871
                name,
1✔
872
                type_name
1✔
873
            FROM 
1✔
874
                layer_providers
1✔
875
            ORDER BY name ASC
1✔
876
            LIMIT $1 
1✔
877
            OFFSET $2;",
1✔
878
            )
1✔
879
            .await?;
1✔
880

881
        let rows = conn
1✔
882
            .query(
1✔
883
                &stmt,
1✔
884
                &[&i64::from(options.limit), &i64::from(options.offset)],
1✔
885
            )
1✔
886
            .await?;
1✔
887

888
        Ok(rows
1✔
889
            .iter()
1✔
890
            .map(|row| LayerProviderListing {
1✔
891
                id: row.get(0),
1✔
892
                name: row.get(1),
1✔
893
                description: row.get(2),
1✔
894
            })
1✔
895
            .collect())
1✔
896
    }
2✔
897

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

902
        let stmt = conn
1✔
903
            .prepare(
1✔
904
                "
1✔
905
               SELECT 
1✔
906
                   definition
1✔
907
               FROM 
1✔
908
                   layer_providers
1✔
909
               WHERE
1✔
910
                   id = $1",
1✔
911
            )
1✔
912
            .await?;
1✔
913

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

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

918
        definition.initialize().await
1✔
919
    }
2✔
920
}
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