From 45031abf7a46fc5fd2ac8d9f6b2cdf36ccdb8539 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A1szl=C3=B3=20V=C3=A1rady?= Date: Thu, 5 Dec 2024 13:37:17 +0100 Subject: [PATCH] database: rename princs_filter to client_filter --- common/src/database/postgres.rs | 10 +++---- ...14_alter_client_filter_in_subscriptions.rs | 27 +++++++++++++++++ common/src/database/schema/postgres/mod.rs | 3 ++ ...14_alter_client_filter_in_subscriptions.rs | 30 +++++++++++++++++++ common/src/database/schema/sqlite/mod.rs | 3 ++ common/src/database/sqlite.rs | 16 +++++----- 6 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 common/src/database/schema/postgres/_014_alter_client_filter_in_subscriptions.rs create mode 100644 common/src/database/schema/sqlite/_014_alter_client_filter_in_subscriptions.rs diff --git a/common/src/database/postgres.rs b/common/src/database/postgres.rs index acac2de..9cc55be 100644 --- a/common/src/database/postgres.rs +++ b/common/src/database/postgres.rs @@ -213,10 +213,10 @@ fn row_to_subscription(row: &Row) -> Result { let max_time: i32 = row.try_get("max_time")?; let max_elements: Option = row.try_get("max_elements")?; - let client_filter_op: Option = row.try_get("princs_filter_op")?; + let client_filter_op: Option = 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 }; @@ -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 @@ -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"#, diff --git a/common/src/database/schema/postgres/_014_alter_client_filter_in_subscriptions.rs b/common/src/database/schema/postgres/_014_alter_client_filter_in_subscriptions.rs new file mode 100644 index 0000000..1e97d32 --- /dev/null +++ b/common/src/database/schema/postgres/_014_alter_client_filter_in_subscriptions.rs @@ -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(()) + } +} diff --git a/common/src/database/schema/postgres/mod.rs b/common/src/database/schema/postgres/mod.rs index 5b12f95..1c3cbe3 100644 --- a/common/src/database/schema/postgres/mod.rs +++ b/common/src/database/schema/postgres/mod.rs @@ -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; @@ -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)); @@ -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)); } diff --git a/common/src/database/schema/sqlite/_014_alter_client_filter_in_subscriptions.rs b/common/src/database/schema/sqlite/_014_alter_client_filter_in_subscriptions.rs new file mode 100644 index 0000000..d7c7bf3 --- /dev/null +++ b/common/src/database/schema/sqlite/_014_alter_client_filter_in_subscriptions.rs @@ -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(()) + } +} diff --git a/common/src/database/schema/sqlite/mod.rs b/common/src/database/schema/sqlite/mod.rs index c49935c..5bad7be 100644 --- a/common/src/database/schema/sqlite/mod.rs +++ b/common/src/database/schema/sqlite/mod.rs @@ -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; @@ -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)); @@ -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)); } diff --git a/common/src/database/sqlite.rs b/common/src/database/sqlite.rs index 3e12909..96a46bc 100644 --- a/common/src/database/sqlite.rs +++ b/common/src/database/sqlite.rs @@ -166,10 +166,10 @@ fn row_to_subscription(row: &Row) -> Result { let content_format = ContentFormat::from_str(row.get::<&str, String>("content_format")?.as_ref())?; - let client_filter_op: Option = row.get("princs_filter_op")?; + let client_filter_op: Option = 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 }; @@ -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, @@ -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"#, @@ -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(),