Add schema update.

This commit is contained in:
AB
2020-12-06 13:37:54 +03:00
parent b2bb016573
commit 83407e93bb
4 changed files with 88 additions and 9 deletions

View File

@ -18,6 +18,13 @@ pub(crate) fn open() -> Result<Connection> {
Ok(db) Ok(db)
} }
pub(crate) fn update_scheme() -> Result<()> {
let conn = open()?;
conn.execute(scheme, params![])?;
info!("Scheme updated.");
Ok(())
}
pub(crate) fn get_user(id: telegram_bot::UserId) -> Result<telegram_bot::User, errors::Error> { pub(crate) fn get_user(id: telegram_bot::UserId) -> Result<telegram_bot::User, errors::Error> {
let conn = open()?; let conn = open()?;
let mut stmt = let mut stmt =
@ -263,3 +270,62 @@ pub(crate) async fn get_file(file_id: String) -> Result<(), errors::Error> {
Err(errors::Error::FileNotFound) Err(errors::Error::FileNotFound)
} }
} }
// SCHEME
static scheme: &str = "
CREATE TABLE IF NOT EXISTS sqlite_sequence(name,seq);
CREATE TABLE IF NOT EXISTS `word` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`word` TEXT UNIQUE
);
CREATE TABLE IF NOT EXISTS user (
`id` INTEGER NOT NULL UNIQUE,
`username` TEXT NOT NULL,
`first_name` INTEGER NOT NULL,
`last_name` INTEGER NOT NULL,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
CREATE TABLE IF NOT EXISTS `conf` (
`id` NUMERIC NOT NULL UNIQUE,
`title` TEXT,
`date` INTEGER NOT NULL,
PRIMARY KEY(`id`)
);
CREATE TABLE IF NOT EXISTS `file` (
`path` TEXT NOT NULL UNIQUE,
`user_id` TEXT NOT NULL,
`conf_id` TEXT NOT NULL,
PRIMARY KEY(`path`)
);
CREATE TABLE IF NOT EXISTS relations (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`word_id` INTEGER NOT NULL,
`user_id` INTEGER NOT NULL,
`conf_id` INTEGER NOT NULL,
`date` INTEGER NOT NULL, `msg_id` INTEGER NULL,
FOREIGN KEY(`word_id`) REFERENCES `word`(`id`) ON DELETE CASCADE,
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`),
FOREIGN KEY(`conf_id`) REFERENCES `conf`(`id`)
);
CREATE TABLE IF NOT EXISTS `reset` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_id` INTEGER,
`conf_id` INTEGER,
`date` INTEGER,
`relation_id` INTEGER,
FOREIGN KEY(`user_id`) REFERENCES `user`(`id`)
);
CREATE TABLE IF NOT EXISTS `alert` (
`conf_id`TEXT NOT NULL,
`user_id`TEXT NOT NULL,
`created`TEXT NOT NULL,
`time`TEXT NOT NULL,
`message`TEXT
);
CREATE TABLE IF NOT EXISTS `xxx_message` (
`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`text`TEXT UNIQUE NULL
);
";

View File

@ -42,4 +42,4 @@ impl From<io_error> for Error {
fn from(e: io_error) -> Error { fn from(e: io_error) -> Error {
return Error::IOError(e); return Error::IOError(e);
} }
} }

View File

@ -18,7 +18,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::Text { ref data, .. } => { MessageKind::Text { ref data, .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: {}", "<{}({})>[{}({})]: {}",
&message.chat.id(), &message.chat.id(),
title, title,
@ -37,7 +37,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
.. ..
} => { } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *PHOTO* {}", "<{}({})>[{}({})]: *PHOTO* {}",
&message.chat.id(), &message.chat.id(),
title, title,
@ -50,7 +50,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::Document { ref caption, .. } => { MessageKind::Document { ref caption, .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *DOCUMENT* {}", "<{}({})>[{}({})]: *DOCUMENT* {}",
&message.chat.id(), &message.chat.id(),
title, title,
@ -70,7 +70,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::Sticker { ref data, .. } => { MessageKind::Sticker { ref data, .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *STICKER*", "<{}({})>[{}({})]: *STICKER*",
&message.chat.id(), &message.chat.id(),
title, title,
@ -82,7 +82,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::Voice { .. } => { MessageKind::Voice { .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *VOICE*", "<{}({})>[{}({})]: *VOICE*",
&message.chat.id(), &message.chat.id(),
title, title,
@ -94,7 +94,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::Video { .. } => { MessageKind::Video { .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *VIDEO*", "<{}({})>[{}({})]: *VIDEO*",
&message.chat.id(), &message.chat.id(),
title, title,
@ -106,7 +106,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
MessageKind::VideoNote { .. } => { MessageKind::VideoNote { .. } => {
let title = utils::get_title(&message); let title = utils::get_title(&message);
println!( info!(
"<{}({})>[{}({})]: *VIDEO_NOTE*", "<{}({})>[{}({})]: *VIDEO_NOTE*",
&message.chat.id(), &message.chat.id(),
title, title,
@ -123,6 +123,7 @@ async fn handler(api: Api, message: Message, token: String) -> Result<(), errors
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), errors::Error> { async fn main() -> Result<(), errors::Error> {
env_logger::from_env(Env::default().default_filter_or("debug")).init(); env_logger::from_env(Env::default().default_filter_or("debug")).init();
db::update_scheme();
let token = match env::var("TELEGRAM_BOT_TOKEN") { let token = match env::var("TELEGRAM_BOT_TOKEN") {
Ok(token) => token, Ok(token) => token,
Err(_) => { Err(_) => {

View File

@ -1,6 +1,6 @@
use reqwest::Client; use reqwest::Client;
use sha1::Sha1; use sha1::Sha1;
use std::fs::File; use std::fs::{File,create_dir as fs_create_dir};
use std::io::prelude::*; use std::io::prelude::*;
use std::path::Path; use std::path::Path;
use std::{env, io}; use std::{env, io};
@ -20,6 +20,17 @@ pub(crate) fn get_title(message: &Message) -> String {
} }
} }
pub(crate) async fn create_dir(dir: &String) -> () {
info!("Going to create dir");
match fs_create_dir(dir) {
Ok(_) => info!("Dir {} created.", dir),
Err(_) => info!("Dir {} create error.", dir),
}
info!("Going to create dir");
}
pub(crate) async fn get_files( pub(crate) async fn get_files(
api: Api, api: Api,
message: Message, message: Message,
@ -35,6 +46,7 @@ pub(crate) async fn get_files(
MessageKind::Sticker { .. } => "sticker".to_string(), MessageKind::Sticker { .. } => "sticker".to_string(),
_ => "docs".to_string(), _ => "docs".to_string(),
}; };
create_dir(&file_type).await;
if let Some(files) = message.get_files() { if let Some(files) = message.get_files() {
let group_title = get_title(&message); let group_title = get_title(&message);
let author = message.from.id; let author = message.from.id;