mirror of
https://github.com/flibusta-apps/telegram_files_cache_server.git
synced 2026-03-02 22:55:19 +01:00
Run DB migrations at startup and add cached_files
This commit is contained in:
@@ -54,4 +54,4 @@ teloxide = { version = "0.17.0", features = ["macros", "webhooks-axum", "cache-m
|
|||||||
|
|
||||||
moka = { version = "0.12.10", features = ["future"] }
|
moka = { version = "0.12.10", features = ["future"] }
|
||||||
|
|
||||||
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "macros"] }
|
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "macros", "migrate"] }
|
||||||
|
|||||||
54
migrations/20260116094605_create_cached_files_table.sql
Normal file
54
migrations/20260116094605_create_cached_files_table.sql
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
-- Create cached_files table with all indexes and constraints
|
||||||
|
-- This migration is idempotent and safe to run on existing databases
|
||||||
|
|
||||||
|
-- Create table if not exists
|
||||||
|
CREATE TABLE IF NOT EXISTS cached_files (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('cached_files_id_seq'::regclass),
|
||||||
|
object_id INTEGER NOT NULL,
|
||||||
|
object_type VARCHAR(8) NOT NULL,
|
||||||
|
message_id BIGINT NOT NULL,
|
||||||
|
chat_id BIGINT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
-- Create sequence if not exists (for compatibility)
|
||||||
|
CREATE SEQUENCE IF NOT EXISTS cached_files_id_seq;
|
||||||
|
|
||||||
|
-- Ensure the sequence is owned by the table column
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_depend
|
||||||
|
WHERE refobjid = 'cached_files'::regclass
|
||||||
|
AND objid = 'cached_files_id_seq'::regclass
|
||||||
|
) THEN
|
||||||
|
ALTER SEQUENCE cached_files_id_seq OWNED BY cached_files.id;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Create indexes if they don't exist
|
||||||
|
CREATE UNIQUE INDEX IF NOT EXISTS ix_cached_files_message_id ON cached_files (message_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_cached_files_object_id ON cached_files (object_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS ix_cached_files_object_type ON cached_files (object_type);
|
||||||
|
|
||||||
|
-- Create unique constraints if they don't exist
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'uc_cached_files_message_id_chat_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE cached_files
|
||||||
|
ADD CONSTRAINT uc_cached_files_message_id_chat_id UNIQUE (message_id, chat_id);
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'uc_cached_files_object_id_object_type'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE cached_files
|
||||||
|
ADD CONSTRAINT uc_cached_files_object_id_object_type UNIQUE (object_id, object_type);
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
@@ -19,3 +19,7 @@ pub async fn get_pg_pool() -> PgPool {
|
|||||||
.await
|
.await
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn run_migrations(pool: &PgPool) -> Result<(), sqlx::migrate::MigrateError> {
|
||||||
|
sqlx::migrate!("./migrations").run(pool).await
|
||||||
|
}
|
||||||
|
|||||||
12
src/main.rs
12
src/main.rs
@@ -12,7 +12,7 @@ use std::{net::SocketAddr, str::FromStr};
|
|||||||
use tracing::info;
|
use tracing::info;
|
||||||
use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt};
|
use tracing_subscriber::{filter, layer::SubscriberExt, util::SubscriberInitExt};
|
||||||
|
|
||||||
use crate::views::get_router;
|
use crate::{db::run_migrations, views::get_router};
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@@ -40,6 +40,16 @@ async fn main() {
|
|||||||
|
|
||||||
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
let addr = SocketAddr::from(([0, 0, 0, 0], 8080));
|
||||||
|
|
||||||
|
// Get database pool
|
||||||
|
let pool = db::get_pg_pool().await;
|
||||||
|
|
||||||
|
// Run migrations
|
||||||
|
info!("Running database migrations...");
|
||||||
|
run_migrations(&pool)
|
||||||
|
.await
|
||||||
|
.expect("Failed to run database migrations");
|
||||||
|
info!("Database migrations completed successfully");
|
||||||
|
|
||||||
let app = get_router().await;
|
let app = get_router().await;
|
||||||
|
|
||||||
info!("Start webserver...");
|
info!("Start webserver...");
|
||||||
|
|||||||
Reference in New Issue
Block a user