Skip to content

Commit c32660b

Browse files
fix(services): default permissions for providers existing before migration 0020 (#1072)
* Set default permissions for providers existing before migration 0020 * Sqlfluff * Add default roles to migration test data * Add all default data to migration0015 snapshot * Sqlfluff * Use role IDs from Role struct * Replace migration 0015 with test-only snapshot creation * Fix tests --------- Co-authored-by: Johannes Drönner <[email protected]>
1 parent 005c23b commit c32660b

10 files changed

+274
-117
lines changed

services/src/contexts/migrations/current_schema.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::{
22
all_migrations,
33
database_migration::{DatabaseVersion, Migration},
44
};
5+
use crate::permissions::Role;
56
use crate::{
67
error::Result,
78
layers::{
@@ -11,11 +12,6 @@ use crate::{
1112
use async_trait::async_trait;
1213
use pwhash::bcrypt;
1314
use tokio_postgres::Transaction;
14-
use uuid::Uuid;
15-
16-
const ADMIN_ROLE_ID: Uuid = Uuid::from_u128(0xd532_8854_6190_4af9_ad69_4e74_b096_1ac9);
17-
const REGISTERED_USER_ROLE_ID: Uuid = Uuid::from_u128(0x4e80_81b6_8aa6_4275_af0c_2fa2_da55_7d28);
18-
const ANONYMOUS_USER_ROLE_ID: Uuid = Uuid::from_u128(0xfd8e_87bf_515c_4f36_8da6_1a53_702f_f102);
1915

2016
const ADMIN_QUOTA: i64 = 9_223_372_036_854_775_807; // max postgres `bigint` value
2117

@@ -131,9 +127,9 @@ impl CurrentSchemaMigration {
131127
;
132128
",
133129
&[
134-
&ADMIN_ROLE_ID,
135-
&REGISTERED_USER_ROLE_ID,
136-
&ANONYMOUS_USER_ROLE_ID,
130+
&Role::admin_role_id().0,
131+
&Role::registered_user_role_id().0,
132+
&Role::anonymous_role_id().0,
137133
],
138134
)
139135
.await?;
@@ -157,7 +153,7 @@ impl CurrentSchemaMigration {
157153
);
158154
",
159155
&[
160-
&ADMIN_ROLE_ID,
156+
&Role::admin_role_id().0,
161157
&user_config.admin_email,
162158
&bcrypt::hash(user_config.admin_password)
163159
.expect("Admin password hash should be valid"),
@@ -176,7 +172,7 @@ impl CurrentSchemaMigration {
176172
$1
177173
);
178174
",
179-
&[&ADMIN_ROLE_ID],
175+
&[&Role::admin_role_id().0],
180176
)
181177
.await?;
182178

@@ -194,9 +190,9 @@ impl CurrentSchemaMigration {
194190
;
195191
",
196192
&[
197-
&ADMIN_ROLE_ID,
198-
&REGISTERED_USER_ROLE_ID,
199-
&ANONYMOUS_USER_ROLE_ID,
193+
&Role::admin_role_id().0,
194+
&Role::registered_user_role_id().0,
195+
&Role::anonymous_role_id().0,
200196
&INTERNAL_LAYER_DB_ROOT_COLLECTION_ID,
201197
&UNSORTED_COLLECTION_ID,
202198
],

services/src/contexts/migrations/database_migration.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,14 @@ where
189189
}
190190

191191
#[cfg(test)]
192-
mod tests {
192+
pub mod tests {
193193
use super::*;
194194
use crate::{
195195
config::get_config_element,
196196
contexts::PostgresDb,
197197
contexts::{
198198
SessionId,
199-
migrations::{CurrentSchemaMigration, Migration0015LogQuota, all_migrations},
199+
migrations::{CurrentSchemaMigration, all_migrations},
200200
},
201201
permissions::RoleId,
202202
projects::{ProjectDb, ProjectListOptions},
@@ -208,6 +208,22 @@ mod tests {
208208
use geoengine_datatypes::{primitives::DateTime, test_data};
209209
use tokio_postgres::NoTls;
210210

211+
pub async fn create_migration_0015_snapshot<Tls>(
212+
connection: &mut PooledConnection<'_, PostgresConnectionManager<Tls>>,
213+
) -> Result<()>
214+
where
215+
Tls: MakeTlsConnect<Socket> + Clone + Send + Sync + 'static,
216+
<Tls as MakeTlsConnect<Socket>>::Stream: Send + Sync,
217+
<Tls as MakeTlsConnect<Socket>>::TlsConnect: Send,
218+
<<Tls as MakeTlsConnect<Socket>>::TlsConnect as TlsConnect<Socket>>::Future: Send,
219+
{
220+
connection
221+
.batch_execute(include_str!("migration_0015_snapshot.sql"))
222+
.await?;
223+
224+
Ok(())
225+
}
226+
211227
#[tokio::test]
212228
#[serial_test::parallel]
213229
async fn it_migrates() -> Result<()> {
@@ -216,7 +232,7 @@ mod tests {
216232
#[async_trait]
217233
impl Migration for TestMigration {
218234
fn prev_version(&self) -> Option<DatabaseVersion> {
219-
Some(Migration0015LogQuota.version())
235+
Some("0015_log_quota".to_string())
220236
}
221237

222238
fn version(&self) -> DatabaseVersion {
@@ -256,11 +272,8 @@ mod tests {
256272
}
257273
}
258274

259-
let migrations: Vec<Box<dyn Migration>> = vec![
260-
Box::new(Migration0015LogQuota),
261-
Box::new(TestMigration),
262-
Box::new(FollowUpMigration),
263-
];
275+
let migrations: Vec<Box<dyn Migration>> =
276+
vec![Box::new(TestMigration), Box::new(FollowUpMigration)];
264277

265278
let postgres_config = get_config_element::<crate::config::Postgres>()?;
266279
let db_config = DatabaseConnectionConfig::from(postgres_config);
@@ -270,6 +283,8 @@ mod tests {
270283

271284
let mut conn = pool.get().await?;
272285

286+
create_migration_0015_snapshot(&mut conn).await?;
287+
273288
migrate_database(&mut conn, &migrations).await?;
274289

275290
let stmt = conn.prepare("SELECT * FROM mock;").await?;
@@ -296,6 +311,8 @@ mod tests {
296311

297312
let mut conn = pool.get().await?;
298313

314+
create_migration_0015_snapshot(&mut conn).await?;
315+
299316
migrate_database(&mut conn, &all_migrations()).await?;
300317

301318
Ok(())
@@ -338,7 +355,7 @@ mod tests {
338355
let mut conn = pool.get().await?;
339356

340357
// initial schema
341-
migrate_database(&mut conn, &all_migrations()[0..1]).await?;
358+
create_migration_0015_snapshot(&mut conn).await?;
342359

343360
// insert test data on initial schema
344361
let test_data_sql = std::fs::read_to_string(test_data!("migrations/test_data.sql"))?;

services/src/contexts/migrations/migration_0015_log_quota.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

services/src/contexts/migrations/migration_0015_snapshot.sql

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,3 +1180,113 @@ CREATE TABLE quota_log (
11801180
);
11811181

11821182
CREATE INDEX ON quota_log (user_id, timestamp, computation_id);
1183+
1184+
1185+
-- Data
1186+
1187+
INSERT INTO geoengine (clear_database_on_start, database_version) VALUES (
1188+
FALSE, '0015_log_quota'
1189+
);
1190+
1191+
INSERT INTO layer_collections (
1192+
id,
1193+
name,
1194+
description,
1195+
properties
1196+
)
1197+
VALUES (
1198+
'05102bb3-a855-4a37-8a8a-30026a91fef1',
1199+
'Layers',
1200+
'All available Geo Engine layers',
1201+
ARRAY[]::"PropertyType" []
1202+
);
1203+
1204+
INSERT INTO layer_collections (
1205+
id,
1206+
name,
1207+
description,
1208+
properties
1209+
)
1210+
VALUES (
1211+
'ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f',
1212+
'Unsorted',
1213+
'Unsorted Layers',
1214+
ARRAY[]::"PropertyType" []
1215+
);
1216+
1217+
INSERT INTO collection_children (
1218+
parent,
1219+
child
1220+
)
1221+
VALUES (
1222+
'05102bb3-a855-4a37-8a8a-30026a91fef1',
1223+
'ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f'
1224+
);
1225+
1226+
INSERT INTO roles (id, name)
1227+
VALUES ('d5328854-6190-4af9-ad69-4e74b0961ac9', 'admin'),
1228+
('4e8081b6-8aa6-4275-af0c-2fa2da557d28', 'user'),
1229+
(
1230+
'fd8e87bf-515c-4f36-8da6-1a53702ff102',
1231+
'anonymous'
1232+
);
1233+
1234+
1235+
INSERT INTO users (
1236+
id,
1237+
email,
1238+
password_hash,
1239+
real_name,
1240+
quota_available,
1241+
active
1242+
)
1243+
VALUES (
1244+
'd5328854-6190-4af9-ad69-4e74b0961ac9',
1245+
'admin@localhost',
1246+
'$2a$12$leX8lZRDZS6JDf/6m.0QU.xBpAglyTZyAzMcgGF5swFd1CBHn1eHC',
1247+
'admin',
1248+
9223372036854775807,
1249+
TRUE
1250+
);
1251+
1252+
INSERT INTO user_roles (
1253+
user_id,
1254+
role_id
1255+
)
1256+
VALUES (
1257+
'd5328854-6190-4af9-ad69-4e74b0961ac9',
1258+
'd5328854-6190-4af9-ad69-4e74b0961ac9'
1259+
);
1260+
1261+
INSERT INTO permissions
1262+
(role_id, layer_collection_id, permission)
1263+
VALUES (
1264+
'd5328854-6190-4af9-ad69-4e74b0961ac9',
1265+
'05102bb3-a855-4a37-8a8a-30026a91fef1',
1266+
'Owner'
1267+
),
1268+
(
1269+
'4e8081b6-8aa6-4275-af0c-2fa2da557d28',
1270+
'05102bb3-a855-4a37-8a8a-30026a91fef1',
1271+
'Read'
1272+
),
1273+
(
1274+
'fd8e87bf-515c-4f36-8da6-1a53702ff102',
1275+
'05102bb3-a855-4a37-8a8a-30026a91fef1',
1276+
'Read'
1277+
),
1278+
(
1279+
'd5328854-6190-4af9-ad69-4e74b0961ac9',
1280+
'ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f',
1281+
'Owner'
1282+
),
1283+
(
1284+
'4e8081b6-8aa6-4275-af0c-2fa2da557d28',
1285+
'ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f',
1286+
'Read'
1287+
),
1288+
(
1289+
'fd8e87bf-515c-4f36-8da6-1a53702ff102',
1290+
'ffb2dd9e-f5ad-427c-b7f1-c9a0c7a0ae3f',
1291+
'Read'
1292+
);

services/src/contexts/migrations/migration_0016_merge_providers.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use super::{
2-
Migration0015LogQuota,
3-
database_migration::{DatabaseVersion, Migration},
4-
};
1+
use super::database_migration::{DatabaseVersion, Migration};
52
use crate::error::Result;
63
use async_trait::async_trait;
74
use tokio_postgres::Transaction;
@@ -12,7 +9,7 @@ pub struct Migration0016MergeProviders;
129
#[async_trait]
1310
impl Migration for Migration0016MergeProviders {
1411
fn prev_version(&self) -> Option<DatabaseVersion> {
15-
Some(Migration0015LogQuota.version())
12+
Some("0015_log_quota".into())
1613
}
1714

1815
fn version(&self) -> DatabaseVersion {
@@ -29,6 +26,7 @@ impl Migration for Migration0016MergeProviders {
2926
#[cfg(test)]
3027
mod tests {
3128
use crate::contexts::migrations::all_migrations;
29+
use crate::contexts::migrations::database_migration::tests::create_migration_0015_snapshot;
3230
use crate::util::postgres::DatabaseConnectionConfig;
3331
use crate::{config::get_config_element, contexts::migrate_database};
3432
use bb8_postgres::{PostgresConnectionManager, bb8::Pool};
@@ -45,9 +43,7 @@ mod tests {
4543
let mut conn = pool.get().await.unwrap();
4644

4745
// initial schema
48-
migrate_database(&mut conn, &all_migrations()[0..1])
49-
.await
50-
.unwrap();
46+
create_migration_0015_snapshot(&mut conn).await.unwrap();
5147

5248
// insert test data on initial schema
5349
assert_eq!(
@@ -58,7 +54,7 @@ mod tests {
5854
);
5955

6056
// perform this migration
61-
migrate_database(&mut conn, &all_migrations()[1..=1])
57+
migrate_database(&mut conn, &all_migrations()[..=0])
6258
.await
6359
.unwrap();
6460

services/src/contexts/migrations/migration_0018_wildlive_connector.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ impl Migration for Migration0018WildliveConnector {
3030
#[cfg(test)]
3131
mod tests {
3232
use super::*;
33-
use crate::contexts::migrations::{
34-
Migration0015LogQuota, Migration0016MergeProviders, migrations_by_range,
35-
};
33+
use crate::contexts::migrations::database_migration::tests::create_migration_0015_snapshot;
34+
use crate::contexts::migrations::{Migration0016MergeProviders, migrations_by_range};
3635
use crate::util::postgres::DatabaseConnectionConfig;
3736
use crate::{config::get_config_element, contexts::migrate_database};
3837
use bb8_postgres::{PostgresConnectionManager, bb8::Pool};
@@ -49,15 +48,7 @@ mod tests {
4948
let mut conn = pool.get().await.unwrap();
5049

5150
// initial schema
52-
migrate_database(
53-
&mut conn,
54-
&migrations_by_range(
55-
&Migration0015LogQuota.version(),
56-
&Migration0015LogQuota.version(),
57-
),
58-
)
59-
.await
60-
.unwrap();
51+
create_migration_0015_snapshot(&mut conn).await.unwrap();
6152

6253
// insert test data on initial schema
6354
assert_eq!(

0 commit comments

Comments
 (0)