mirror of
https://github.com/chirpstack/chirpstack.git
synced 2024-12-18 20:57:55 +00:00
Add cmd to migrate ds from Redis to Pg.
This commit is contained in:
parent
8a986d04ce
commit
411cd681a9
56
chirpstack/src/cmd/migrate_ds_to_pg.rs
Normal file
56
chirpstack/src/cmd/migrate_ds_to_pg.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use anyhow::Result;
|
||||
use diesel::prelude::*;
|
||||
use diesel_async::RunQueryDsl;
|
||||
use tracing::{debug, info};
|
||||
|
||||
use crate::storage::{self, device_session, error::Error, get_async_db_conn, schema::device};
|
||||
use lrwn::{DevAddr, EUI64};
|
||||
|
||||
pub async fn run() -> Result<()> {
|
||||
storage::setup().await?;
|
||||
|
||||
info!("Migrating device-sessions from Redis to PostgreSQL");
|
||||
info!("Getting DevEUIs from PostgreSQL without device-session");
|
||||
|
||||
let dev_euis: Vec<EUI64> = device::dsl::device
|
||||
.select(device::dsl::dev_eui)
|
||||
.filter(device::dsl::device_session.is_null())
|
||||
.load(&mut get_async_db_conn().await?)
|
||||
.await?;
|
||||
|
||||
info!(
|
||||
"There are {} devices in PostgreSQL without device-session set",
|
||||
dev_euis.len()
|
||||
);
|
||||
|
||||
for dev_eui in &dev_euis {
|
||||
debug!(dev_eui = %dev_eui, "Migrating device-session");
|
||||
|
||||
let ds = match device_session::get(dev_eui).await {
|
||||
Ok(v) => v,
|
||||
Err(e) => match e {
|
||||
Error::NotFound(_) => {
|
||||
debug!(dev_eui = %dev_eui, "Device does not have a device-session");
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
return Err(anyhow::Error::new(e));
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
storage::device::partial_update(
|
||||
*dev_eui,
|
||||
&storage::device::DeviceChangeset {
|
||||
dev_addr: Some(Some(DevAddr::from_slice(&ds.dev_addr)?)),
|
||||
device_session: Some(Some(ds)),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
|
||||
debug!(dev_eui = %dev_eui, "Device-session migrated");
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
@ -1,5 +1,6 @@
|
||||
pub mod configfile;
|
||||
pub mod create_api_key;
|
||||
pub mod import_legacy_lorawan_devices_repository;
|
||||
pub mod migrate_ds_to_pg;
|
||||
pub mod print_ds;
|
||||
pub mod root;
|
||||
|
@ -78,6 +78,9 @@ enum Commands {
|
||||
#[arg(short, long, value_name = "NAME")]
|
||||
name: String,
|
||||
},
|
||||
|
||||
/// Migrate device-sessions from Redis to PostgreSQL.
|
||||
MigrateDeviceSessionsToPostgres {},
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
@ -116,6 +119,7 @@ async fn main() -> Result<()> {
|
||||
.unwrap()
|
||||
}
|
||||
Some(Commands::CreateApiKey { name }) => cmd::create_api_key::run(name).await?,
|
||||
Some(Commands::MigrateDeviceSessionsToPostgres {}) => cmd::migrate_ds_to_pg::run().await?,
|
||||
None => cmd::root::run().await?,
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user