Skip to content

Commit

Permalink
database: rename princs_filter to client_filter
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAnno committed Dec 5, 2024
1 parent 2ff60b9 commit 45031ab
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
10 changes: 5 additions & 5 deletions common/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,10 @@ fn row_to_subscription(row: &Row) -> Result<SubscriptionData> {
let max_time: i32 = row.try_get("max_time")?;
let max_elements: Option<i32> = row.try_get("max_elements")?;

let client_filter_op: Option<String> = row.try_get("princs_filter_op")?;
let client_filter_op: Option<String> = row.try_get("client_filter_op")?;

let client_filter = match client_filter_op {
Some(op) => Some(ClientFilter::from(op, row.try_get("princs_filter_value")?)?),
Some(op) => Some(ClientFilter::from(op, row.try_get("client_filter_value")?)?),
None => None
};

Expand Down Expand Up @@ -638,7 +638,7 @@ impl Database for PostgresDatabase {
r#"INSERT INTO subscriptions (uuid, version, revision, name, uri, query,
heartbeat_interval, connection_retry_count, connection_retry_interval,
max_time, max_elements, max_envelope_size, enabled, read_existing_events, content_format,
ignore_channel_error, princs_filter_op, princs_filter_value, outputs, locale,
ignore_channel_error, client_filter_op, client_filter_value, outputs, locale,
data_locale)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21)
ON CONFLICT (uuid) DO UPDATE SET
Expand All @@ -657,8 +657,8 @@ impl Database for PostgresDatabase {
read_existing_events = excluded.read_existing_events,
content_format = excluded.content_format,
ignore_channel_error = excluded.ignore_channel_error,
princs_filter_op = excluded.princs_filter_op,
princs_filter_value = excluded.princs_filter_value,
client_filter_op = excluded.client_filter_op,
client_filter_value = excluded.client_filter_value,
outputs = excluded.outputs,
locale = excluded.locale,
data_locale = excluded.data_locale"#,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
use anyhow::Result;
use async_trait::async_trait;
use deadpool_postgres::Transaction;

use crate::{database::postgres::PostgresMigration, migration};

pub(super) struct AlterClientFilterInSubscriptionsTable;
migration!(
AlterClientFilterInSubscriptionsTable,
14,
"renames fields and adds filter type and flags to subscriptions table"
);

#[async_trait]
impl PostgresMigration for AlterClientFilterInSubscriptionsTable {
async fn up(&self, tx: &mut Transaction) -> Result<()> {
tx.execute("ALTER TABLE subscriptions RENAME COLUMN princs_filter_op TO client_filter_op", &[]).await?;
tx.execute("ALTER TABLE subscriptions RENAME COLUMN princs_filter_value TO client_filter_value", &[]).await?;
Ok(())
}

async fn down(&self, tx: &mut Transaction) -> Result<()> {
tx.execute("ALTER TABLE subscriptions RENAME COLUMN client_filter_op TO princs_filter_op", &[]).await?;
tx.execute("ALTER TABLE subscriptions RENAME COLUMN client_filter_value TO princs_filter_value", &[]).await?;
Ok(())
}
}
3 changes: 3 additions & 0 deletions common/src/database/schema/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use self::{
_011_add_locale_fields_in_subscriptions_table::AddLocaleFieldsInSubscriptionsTable,
_012_alter_outputs_files_config::AlterOutputsFilesConfig,
_013_add_max_elements_field_in_subscriptions_table::AddMaxElementsFieldInSubscriptionsTable,
_014_alter_client_filter_in_subscriptions::AlterClientFilterInSubscriptionsTable,
};

mod _001_create_subscriptions_table;
Expand All @@ -31,6 +32,7 @@ mod _010_add_revision_field_in_subscriptions_table;
mod _011_add_locale_fields_in_subscriptions_table;
mod _012_alter_outputs_files_config;
mod _013_add_max_elements_field_in_subscriptions_table;
mod _014_alter_client_filter_in_subscriptions;

pub fn register_migrations(postgres_db: &mut PostgresDatabase) {
postgres_db.register_migration(Arc::new(CreateSubscriptionsTable));
Expand All @@ -46,4 +48,5 @@ pub fn register_migrations(postgres_db: &mut PostgresDatabase) {
postgres_db.register_migration(Arc::new(AddLocaleFieldsInSubscriptionsTable));
postgres_db.register_migration(Arc::new(AlterOutputsFilesConfig));
postgres_db.register_migration(Arc::new(AddMaxElementsFieldInSubscriptionsTable));
postgres_db.register_migration(Arc::new(AlterClientFilterInSubscriptionsTable));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use anyhow::{anyhow, Result};
use rusqlite::Connection;

use crate::database::sqlite::SQLiteMigration;
use crate::migration;

pub(super) struct AlterClientFilterInSubscriptionsTable;
migration!(
AlterClientFilterInSubscriptionsTable,
14,
"renames fields and adds filter type and flags to subscriptions table"
);

impl SQLiteMigration for AlterClientFilterInSubscriptionsTable {
fn up(&self, conn: &Connection) -> Result<()> {
conn.execute("ALTER TABLE subscriptions RENAME COLUMN princs_filter_op TO client_filter_op", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
conn.execute("ALTER TABLE subscriptions RENAME COLUMN princs_filter_value TO client_filter_value", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
Ok(())
}

fn down(&self, conn: &Connection) -> Result<()> {
conn.execute("ALTER TABLE subscriptions RENAME COLUMN client_filter_op TO princs_filter_op", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
conn.execute("ALTER TABLE subscriptions RENAME COLUMN client_filter_value TO princs_filter_value", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
Ok(())
}
}
3 changes: 3 additions & 0 deletions common/src/database/schema/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use self::{
_011_add_locale_fields_in_subscriptions_table::AddLocaleFieldsInSubscriptionsTable,
_012_alter_outputs_files_config::AlterOutputsFilesConfig,
_013_add_max_elements_field_in_subscriptions_table::AddMaxElementsFieldInSubscriptionsTable,
_014_alter_client_filter_in_subscriptions::AlterClientFilterInSubscriptionsTable,
};

mod _001_create_subscriptions_table;
Expand All @@ -31,6 +32,7 @@ mod _010_add_revision_field_in_subscriptions_table;
mod _011_add_locale_fields_in_subscriptions_table;
mod _012_alter_outputs_files_config;
mod _013_add_max_elements_field_in_subscriptions_table;
mod _014_alter_client_filter_in_subscriptions;

pub fn register_migrations(sqlite_db: &mut SQLiteDatabase) {
sqlite_db.register_migration(Arc::new(CreateSubscriptionsTable));
Expand All @@ -46,4 +48,5 @@ pub fn register_migrations(sqlite_db: &mut SQLiteDatabase) {
sqlite_db.register_migration(Arc::new(AddLocaleFieldsInSubscriptionsTable));
sqlite_db.register_migration(Arc::new(AlterOutputsFilesConfig));
sqlite_db.register_migration(Arc::new(AddMaxElementsFieldInSubscriptionsTable));
sqlite_db.register_migration(Arc::new(AlterClientFilterInSubscriptionsTable));
}
16 changes: 8 additions & 8 deletions common/src/database/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,10 @@ fn row_to_subscription(row: &Row) -> Result<SubscriptionData> {

let content_format = ContentFormat::from_str(row.get::<&str, String>("content_format")?.as_ref())?;

let client_filter_op: Option<String> = row.get("princs_filter_op")?;
let client_filter_op: Option<String> = row.get("client_filter_op")?;

let client_filter = match client_filter_op {
Some(op) => Some(ClientFilter::from(op, row.get("princs_filter_value")?)?),
Some(op) => Some(ClientFilter::from(op, row.get("client_filter_value")?)?),
None => None
};

Expand Down Expand Up @@ -564,12 +564,12 @@ impl Database for SQLiteDatabase {
r#"INSERT INTO subscriptions (uuid, version, revision, name, uri, query,
heartbeat_interval, connection_retry_count, connection_retry_interval,
max_time, max_elements, max_envelope_size, enabled, read_existing_events, content_format,
ignore_channel_error, princs_filter_op, princs_filter_value, outputs, locale,
ignore_channel_error, client_filter_op, client_filter_value, outputs, locale,
data_locale)
VALUES (:uuid, :version, :revision, :name, :uri, :query,
:heartbeat_interval, :connection_retry_count, :connection_retry_interval,
:max_time, :max_elements, :max_envelope_size, :enabled, :read_existing_events, :content_format,
:ignore_channel_error, :princs_filter_op, :princs_filter_value, :outputs,
:ignore_channel_error, :client_filter_op, :client_filter_value, :outputs,
:locale, :data_locale)
ON CONFLICT (uuid) DO UPDATE SET
version = excluded.version,
Expand All @@ -587,8 +587,8 @@ impl Database for SQLiteDatabase {
read_existing_events = excluded.read_existing_events,
content_format = excluded.content_format,
ignore_channel_error = excluded.ignore_channel_error,
princs_filter_op = excluded.princs_filter_op,
princs_filter_value = excluded.princs_filter_value,
client_filter_op = excluded.client_filter_op,
client_filter_value = excluded.client_filter_value,
outputs = excluded.outputs,
locale = excluded.locale,
data_locale = excluded.data_locale"#,
Expand All @@ -609,8 +609,8 @@ impl Database for SQLiteDatabase {
":read_existing_events": subscription.read_existing_events(),
":content_format": subscription.content_format().to_string(),
":ignore_channel_error": subscription.ignore_channel_error(),
":princs_filter_op": client_filter_op,
":princs_filter_value": client_filter_value,
":client_filter_op": client_filter_op,
":client_filter_value": client_filter_value,
":outputs": serde_json::to_string(subscription.outputs())?,
":locale": subscription.locale(),
":data_locale": subscription.data_locale(),
Expand Down

0 comments on commit 45031ab

Please sign in to comment.