escape database password as it may contain special characters

This commit is contained in:
Namekuji 2023-06-05 11:59:45 -04:00
parent e1279b4abe
commit cf3f566e06
No known key found for this signature in database
GPG Key ID: B541BD6E646CABC7
3 changed files with 13 additions and 6 deletions

View File

@ -13,7 +13,6 @@ default = []
convert = ["dep:native-utils"] convert = ["dep:native-utils"]
[dependencies] [dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
serde_json = "1.0.96" serde_json = "1.0.96"
native-utils = { path = "../", optional = true } native-utils = { path = "../", optional = true }
indicatif = { version = "0.17.4", features = ["tokio"] } indicatif = { version = "0.17.4", features = ["tokio"] }
@ -21,6 +20,7 @@ tokio = { version = "1.28.2", features = ["full"] }
futures = "0.3.28" futures = "0.3.28"
serde_yaml = "0.9.21" serde_yaml = "0.9.21"
serde = { version = "1.0.163", features = ["derive"] } serde = { version = "1.0.163", features = ["derive"] }
urlencoding = "2.1.2"
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]
version = "0.11.0" version = "0.11.0"

View File

@ -1,24 +1,29 @@
use serde::Deserialize; use serde::Deserialize;
use std::env; use std::env;
use std::fs; use std::fs;
use urlencoding::encode;
use sea_orm_migration::prelude::*; use sea_orm_migration::prelude::*;
#[cfg(feature = "convert")] #[cfg(feature = "convert")]
mod vec_to_json; mod vec_to_json;
#[async_std::main] #[tokio::main]
async fn main() { async fn main() {
let cwd = env::current_dir().unwrap(); let cwd = env::current_dir().unwrap();
let yml = fs::File::open(cwd.join("../../.config/default.yml")) let yml = fs::File::open(cwd.join("../../.config/default.yml"))
.expect("Unable to read '.config/default.yml'"); .expect("Failed to open '.config/default.yml'");
let config: Config = serde_yaml::from_reader(yml).expect("Unable to parse"); let config: Config = serde_yaml::from_reader(yml).expect("Failed to parse yaml");
env::set_var( env::set_var(
"DATABASE_URL", "DATABASE_URL",
format!( format!(
"postgres://{}:{}@{}:{}/{}", "postgres://{}:{}@{}:{}/{}",
config.db.user, config.db.pass, config.db.host, config.db.port, config.db.db config.db.user,
encode(&config.db.pass),
config.db.host,
config.db.port,
config.db.db,
), ),
); );

View File

@ -222,7 +222,9 @@ export const db = new DataSource({
export async function initDb(force = false) { export async function initDb(force = false) {
await nativeInitDatabase( await nativeInitDatabase(
`postgres://${config.db.user}:${config.db.pass}@${config.db.host}:${config.db.port}/${config.db.db}`, `postgres://${config.db.user}:${encodeURIComponent(config.db.pass)}@${
config.db.host
}:${config.db.port}/${config.db.db}`,
); );
if (force) { if (force) {
if (db.isInitialized) { if (db.isInitialized) {