Run SQLx migrations on startup

This commit is contained in:
2026-01-16 10:39:51 +01:00
parent fc2c8b3452
commit 26b23948ec
7 changed files with 99 additions and 3 deletions

View File

@@ -34,4 +34,4 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter"]}
sentry-tracing = "0.42.0" sentry-tracing = "0.42.0"
tower-http = { version = "0.6.2", features = ["trace"] } tower-http = { version = "0.6.2", features = ["trace"] }
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "macros", "chrono"] } sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "macros", "chrono", "migrate"] }

View File

@@ -0,0 +1,12 @@
-- Create user_settings table
CREATE TABLE IF NOT EXISTS user_settings (
id SERIAL PRIMARY KEY,
user_id BIGINT NOT NULL UNIQUE,
last_name VARCHAR(64) NOT NULL,
first_name VARCHAR(64) NOT NULL,
username VARCHAR(32) NOT NULL,
source VARCHAR(32) NOT NULL
);
-- Create unique index on user_id (if not exists from UNIQUE constraint)
CREATE UNIQUE INDEX IF NOT EXISTS user_settings_user_id_key ON user_settings(user_id);

View File

@@ -0,0 +1,9 @@
-- Create languages table
CREATE TABLE IF NOT EXISTS languages (
id SERIAL PRIMARY KEY,
label VARCHAR(16) NOT NULL,
code VARCHAR(4) NOT NULL UNIQUE
);
-- Create unique index on code (if not exists from UNIQUE constraint)
CREATE UNIQUE INDEX IF NOT EXISTS languages_code_key ON languages(code);

View File

@@ -0,0 +1,22 @@
-- Create user_activity table
CREATE TABLE IF NOT EXISTS user_activity (
id SERIAL PRIMARY KEY,
"user" INTEGER NOT NULL UNIQUE,
updated TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
-- Create unique index on user (if not exists from UNIQUE constraint)
CREATE UNIQUE INDEX IF NOT EXISTS user_activity_user_key ON user_activity("user");
-- Add foreign key constraint if it doesn't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'fk_user_activity_user_settings_id_user'
) THEN
ALTER TABLE user_activity
ADD CONSTRAINT fk_user_activity_user_settings_id_user
FOREIGN KEY ("user") REFERENCES user_settings(id);
END IF;
END $$;

View File

@@ -0,0 +1,33 @@
-- Create users_languages table
CREATE TABLE IF NOT EXISTS users_languages (
id SERIAL PRIMARY KEY,
language INTEGER NOT NULL,
"user" INTEGER NOT NULL
);
-- Add foreign key constraints if they don't exist
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'fk_users_languages_languages_language_id'
) THEN
ALTER TABLE users_languages
ADD CONSTRAINT fk_users_languages_languages_language_id
FOREIGN KEY (language) REFERENCES languages(id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'fk_users_languages_user_settings_user_id'
) THEN
ALTER TABLE users_languages
ADD CONSTRAINT fk_users_languages_user_settings_user_id
FOREIGN KEY ("user") REFERENCES user_settings(id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;

View File

@@ -0,0 +1,9 @@
-- Create chat_donate_notifications table
CREATE TABLE IF NOT EXISTS chat_donate_notifications (
id BIGSERIAL PRIMARY KEY,
chat_id BIGINT NOT NULL UNIQUE,
sended TIMESTAMP WITHOUT TIME ZONE NOT NULL
);
-- Create unique index on chat_id (if not exists from UNIQUE constraint)
CREATE UNIQUE INDEX IF NOT EXISTS chat_donate_notifications_chat_id_key ON chat_donate_notifications(chat_id);

View File

@@ -1,6 +1,7 @@
use crate::config::CONFIG; use crate::config::CONFIG;
use sqlx::{postgres::PgPoolOptions, PgPool}; use sqlx::{postgres::PgPoolOptions, PgPool};
use tracing::info;
pub async fn get_postgres_pool() -> PgPool { pub async fn get_postgres_pool() -> PgPool {
let database_url: String = format!( let database_url: String = format!(
@@ -12,10 +13,20 @@ pub async fn get_postgres_pool() -> PgPool {
CONFIG.postgres_db CONFIG.postgres_db
); );
PgPoolOptions::new() let pool = PgPoolOptions::new()
.max_connections(10) .max_connections(10)
.acquire_timeout(std::time::Duration::from_secs(300)) .acquire_timeout(std::time::Duration::from_secs(300))
.connect(&database_url) .connect(&database_url)
.await .await
.unwrap() .unwrap();
// Run migrations
info!("Running database migrations...");
sqlx::migrate!("./migrations")
.run(&pool)
.await
.expect("Failed to run migrations");
info!("Database migrations completed successfully");
pool
} }