allow to migrate tables

This commit is contained in:
Aleksandr Trushkin
2022-05-26 17:03:55 +03:00
parent 473953fc9c
commit fb03a110ed
4 changed files with 13 additions and 13 deletions

2
Cargo.lock generated
View File

@ -24,7 +24,7 @@ dependencies = [
[[package]]
name = "altherego"
version = "0.9.5"
version = "0.9.9"
dependencies = [
"anyhow",
"async-trait",

View File

@ -1,6 +1,6 @@
[package]
name = "altherego"
version = "0.9.5"
version = "0.9.9"
authors = ["Aleksandr Trushkin <aleksandr.trushkin@rt.ru>"]
edition = "2018"

View File

@ -6,7 +6,6 @@ mod utils;
use anyhow::Result;
use climate::SelfTemperature;
use env_logger;
use envconfig::Envconfig;
use log::{debug, info, warn};
use teloxide::{
@ -21,8 +20,8 @@ use tokio_stream::StreamExt;
use crate::repo::Storage;
const VERSION: &'static str = env!("GIT_REVISION");
const BRANCH: &'static str = env!("GIT_BRANCH");
const VERSION: &str = env!("GIT_REVISION");
const BRANCH: &str = env!("GIT_BRANCH");
#[tokio::main]
async fn main() {
@ -60,9 +59,10 @@ async fn run() -> anyhow::Result<()> {
let settings = Settings::init_from_env().expect("reading config values");
let bot = teloxide::Bot::new(&settings.telegram_token).auto_send();
let migrate = std::env::args().any(|v| v == "migrate");
let repo_config = repo::SqliteConfig {
source: settings.db_source,
migrate,
..Default::default()
};
@ -73,7 +73,7 @@ async fn run() -> anyhow::Result<()> {
let climate_client = climate::Client::new(&settings.climate_dsn);
let self_temp_client = {
let splitted: Vec<&str> = settings.hosttemp_cmd.split(" ").into_iter().collect();
let splitted: Vec<&str> = settings.hosttemp_cmd.split(' ').into_iter().collect();
let (cmd, arg) = (splitted[0], splitted[1]);
climate::SelfTemperature::new(cmd, arg)
@ -211,10 +211,7 @@ async fn handle_version(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
Ok(())
}
async fn handle_help(
bot: AutoSend<Bot>,
msg: Message,
) -> Result<()> {
async fn handle_help(bot: AutoSend<Bot>, msg: Message) -> Result<()> {
let chat_id = msg.chat.id;
bot.send_message(chat_id, Command::descriptions().to_string())

View File

@ -15,6 +15,7 @@ pub struct SqliteConfig {
pub source: String,
pub timeout: std::time::Duration,
pub max_conns: u32,
pub migrate: bool,
}
impl Default for SqliteConfig {
@ -23,6 +24,7 @@ impl Default for SqliteConfig {
source: ":memory:".to_string(),
timeout: std::time::Duration::from_secs(10),
max_conns: 2,
migrate: false,
}
}
}
@ -54,7 +56,9 @@ impl SqliteRepo {
.connect_with(opts)
.await?;
// sqlx::migrate!("./db").run(&pool).await?;
if config.migrate {
sqlx::migrate!("./db").run(&pool).await?;
}
sqlx::query("pragma temp_store = memory;")
.execute(&pool)
@ -147,7 +151,6 @@ impl Storage for SqliteRepo {
}
}
async fn get_user(&self, user_id: i64) -> Result<Option<UserDB>> {
let result: std::result::Result<UserDB, sqlx::Error> = sqlx::query_as!(
UserDB,