feat: ⚗️ migrations

This commit is contained in:
ThatOneCalculator 2022-04-03 21:55:29 -07:00
parent fa78dd4246
commit 35004d8f0f
5 changed files with 50 additions and 8 deletions

39
backend/Cargo.lock generated
View File

@ -276,10 +276,12 @@ dependencies = [
"actix-web",
"badge-maker",
"chrono",
"include_dir",
"nanoid",
"serde",
"serde_json",
"sqlx",
"sqlx-pg-migrate",
]
[[package]]
@ -749,6 +751,12 @@ dependencies = [
"wasi",
]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]]
name = "governor"
version = "0.4.2"
@ -871,6 +879,26 @@ dependencies = [
"unicode-normalization",
]
[[package]]
name = "include_dir"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "482a2e29200b7eed25d7fdbd14423326760b7f6658d21a4cf12d55a50713c69f"
dependencies = [
"glob",
"include_dir_macros",
]
[[package]]
name = "include_dir_macros"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e074c19deab2501407c91ba1860fa3d6820bfde307db6d8cb851b55a10be89b"
dependencies = [
"proc-macro2",
"quote",
]
[[package]]
name = "indexmap"
version = "1.8.0"
@ -1616,6 +1644,17 @@ dependencies = [
"url",
]
[[package]]
name = "sqlx-pg-migrate"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c953f3c114842c806461afc90a68da2fad7072d302cfcf69bdc31c893f6c899b"
dependencies = [
"include_dir",
"sqlx",
"thiserror",
]
[[package]]
name = "sqlx-rt"
version = "0.5.11"

View File

@ -17,6 +17,8 @@ actix-governor = "0.3.0"
badge-maker = "0.2.1"
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "chrono"] }
include_dir = "0.7.2"
sqlx-pg-migrate = "1.0"
chrono = { version = "0.4.19", features = ["serde"] }

View File

@ -7,8 +7,6 @@ CREATE TABLE IF NOT EXISTS pastes (
"created_at" TIMESTAMP WITHOUT TIME ZONE DEFAULT(NOW() AT TIME ZONE 'utc')
);
-- ALTER TABLE pastes ADD COLUMN single_view BOOLEAN DEFAULT false;
CREATE OR REPLACE FUNCTION deleteExpiredPastes() RETURNS trigger AS $pastes_expire$ BEGIN
DELETE FROM pastes
WHERE "expires_at" IS NOT NULL

View File

@ -0,0 +1 @@
ALTER TABLE pastes ADD COLUMN single_view BOOLEAN DEFAULT false;

View File

@ -12,9 +12,13 @@ use actix_web::{
};
use config::Config;
use include_dir::include_dir;
use sqlx::{postgres::PgPoolOptions, PgPool};
use sqlx_pg_migrate::migrate;
use crate::routes::{get_paste, get_stats, get_total_pastes_badge, get_version_badge, new_paste, get_raw_paste};
use crate::routes::{
get_paste, get_raw_paste, get_stats, get_total_pastes_badge, get_version_badge, new_paste,
};
#[derive(Clone)]
pub struct AppState {
@ -25,25 +29,23 @@ pub struct AppState {
#[actix_rt::main]
async fn main() -> io::Result<()> {
let config = config::load(PathBuf::from("config.json"));
let db_uri = &config.databases.postgres_uri.to_string();
let pool = PgPoolOptions::new()
.max_connections(100)
.connect(&config.databases.postgres_uri)
.connect(db_uri)
.await
.expect("Failed to connect to database");
let address = format!(
"{}:{}",
config.server.backend_host, config.server.backend_port
);
let paste_governor = GovernorConfigBuilder::default()
.per_second(config.ratelimits.seconds_in_between_pastes)
.burst_size(config.ratelimits.allowed_pastes_before_ratelimit)
.finish()
.unwrap();
let state = AppState { config, pool };
migrate(db_uri, &include_dir!("migrations")).await;
println!("🚀 zer0bin is running on {address}");
HttpServer::new(move || {