Skip to content

Commit

Permalink
database: implement migration for max_elements
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAnno committed Oct 24, 2024
1 parent a92984f commit 7aea085
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ impl PostgresMigration for CreateSubscriptionsTable {
connection_retry_count INT4,
connection_retry_interval INT4,
max_time INT4,
max_elements INT4,
max_envelope_size INT4,
enabled BOOLEAN,
read_existing_events BOOLEAN,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::Result;
use async_trait::async_trait;
use deadpool_postgres::Transaction;

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

pub(super) struct AddMaxElementsFieldInSubscriptionsTable;
migration!(
AddMaxElementsFieldInSubscriptionsTable,
13,
"add max_elements field in subscriptions table"
);

#[async_trait]
impl PostgresMigration for AddMaxElementsFieldInSubscriptionsTable {
async fn up(&self, tx: &mut Transaction) -> Result<()> {
tx.execute(
"ALTER TABLE subscriptions ADD COLUMN IF NOT EXISTS max_elements INT4;",
&[],
)
.await?;
Ok(())
}

async fn down(&self, tx: &mut Transaction) -> Result<()> {
tx.execute(
"ALTER TABLE subscriptions DROP COLUMN IF EXISTS max_elements",
&[],
)
.await?;
Ok(())
}
}
5 changes: 4 additions & 1 deletion common/src/database/schema/postgres/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use self::{
_009_alter_outputs_format::AlterOutputsFormat,
_010_add_revision_field_in_subscriptions_table::AddRevisionFieldInSubscriptionsTable,
_011_add_locale_fields_in_subscriptions_table::AddLocaleFieldsInSubscriptionsTable,
_012_alter_outputs_files_config::AlterOutputsFilesConfig
_012_alter_outputs_files_config::AlterOutputsFilesConfig,
_013_add_max_elements_field_in_subscriptions_table::AddMaxElementsFieldInSubscriptionsTable,
};

mod _001_create_subscriptions_table;
Expand All @@ -29,6 +30,7 @@ mod _009_alter_outputs_format;
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;

pub fn register_migrations(postgres_db: &mut PostgresDatabase) {
postgres_db.register_migration(Arc::new(CreateSubscriptionsTable));
Expand All @@ -43,4 +45,5 @@ pub fn register_migrations(postgres_db: &mut PostgresDatabase) {
postgres_db.register_migration(Arc::new(AddRevisionFieldInSubscriptionsTable));
postgres_db.register_migration(Arc::new(AddLocaleFieldsInSubscriptionsTable));
postgres_db.register_migration(Arc::new(AlterOutputsFilesConfig));
postgres_db.register_migration(Arc::new(AddMaxElementsFieldInSubscriptionsTable));
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ impl SQLiteMigration for CreateSubscriptionsTable {
connection_retry_count INTEGER,
connection_retry_interval INTEGER,
max_time INTEGER,
max_elements INTEGER,
max_envelope_size INTEGER,
enabled INTEGER,
read_existing_events INTEGER,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use anyhow::{anyhow, Result};
use rusqlite::Connection;

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

pub(super) struct AddMaxElementsFieldInSubscriptionsTable;
migration!(
AddMaxElementsFieldInSubscriptionsTable,
13,
"add max_elements field in subscriptions table"
);

impl SQLiteMigration for AddMaxElementsFieldInSubscriptionsTable {
fn up(&self, conn: &Connection) -> Result<()> {
conn.execute("ALTER TABLE subscriptions ADD COLUMN max_elements INTEGER", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
Ok(())
}

fn down(&self, conn: &Connection) -> Result<()> {
conn.execute("ALTER TABLE subscriptions DROP COLUMN max_elements", [])
.map_err(|err| anyhow!("SQLiteError: {}", err))?;
Ok(())
}
}
5 changes: 4 additions & 1 deletion common/src/database/schema/sqlite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use self::{
_009_alter_outputs_format::AlterOutputsFormat,
_010_add_revision_field_in_subscriptions_table::AddRevisionFieldInSubscriptionsTable,
_011_add_locale_fields_in_subscriptions_table::AddLocaleFieldsInSubscriptionsTable,
_012_alter_outputs_files_config::AlterOutputsFilesConfig
_012_alter_outputs_files_config::AlterOutputsFilesConfig,
_013_add_max_elements_field_in_subscriptions_table::AddMaxElementsFieldInSubscriptionsTable,
};

mod _001_create_subscriptions_table;
Expand All @@ -29,6 +30,7 @@ mod _009_alter_outputs_format;
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;

pub fn register_migrations(sqlite_db: &mut SQLiteDatabase) {
sqlite_db.register_migration(Arc::new(CreateSubscriptionsTable));
Expand All @@ -43,4 +45,5 @@ pub fn register_migrations(sqlite_db: &mut SQLiteDatabase) {
sqlite_db.register_migration(Arc::new(AddRevisionFieldInSubscriptionsTable));
sqlite_db.register_migration(Arc::new(AddLocaleFieldsInSubscriptionsTable));
sqlite_db.register_migration(Arc::new(AlterOutputsFilesConfig));
sqlite_db.register_migration(Arc::new(AddMaxElementsFieldInSubscriptionsTable));
}

0 comments on commit 7aea085

Please sign in to comment.