diff --git a/packages/backend/native-utils/migration/Cargo.toml b/packages/backend/native-utils/migration/Cargo.toml index 0b4b2b2e7f..3813eccd80 100644 --- a/packages/backend/native-utils/migration/Cargo.toml +++ b/packages/backend/native-utils/migration/Cargo.toml @@ -19,6 +19,8 @@ native-utils = { path = "../", optional = true } indicatif = { version = "0.17.4", features = ["tokio"] } tokio = { version = "1.28.2", features = ["full"] } futures = "0.3.28" +serde_yaml = "0.9.21" +serde = { version = "1.0.163", features = ["derive"] } [dependencies.sea-orm-migration] version = "0.11.0" diff --git a/packages/backend/native-utils/migration/src/main.rs b/packages/backend/native-utils/migration/src/main.rs index 31a72026ee..a845a41ff4 100644 --- a/packages/backend/native-utils/migration/src/main.rs +++ b/packages/backend/native-utils/migration/src/main.rs @@ -1,11 +1,45 @@ +use serde::Deserialize; +use std::env; +use std::fs; + use sea_orm_migration::prelude::*; +#[cfg(feature = "convert")] mod vec_to_json; #[async_std::main] async fn main() { + let cwd = env::current_dir().unwrap(); + let yml = fs::File::open(cwd.join("../../.config/default.yml")) + .expect("Unable to read '.config/default.yml'"); + let config: Config = serde_yaml::from_reader(yml).expect("Unable to parse"); + + env::set_var( + "DATABASE_URL", + format!( + "postgres://{}:{}@{}:{}/{}", + config.db.user, config.db.pass, config.db.host, config.db.port, config.db.db + ), + ); + cli::run_cli(migration::Migrator).await; #[cfg(feature = "convert")] vec_to_json::convert().await; } + +#[derive(Debug, PartialEq, Deserialize)] +#[serde(rename = "camelCase")] +pub struct Config { + pub db: DbConfig, +} + +#[derive(Debug, PartialEq, Deserialize)] +#[serde(rename = "camelCase")] +pub struct DbConfig { + pub host: String, + pub port: u32, + pub db: String, + pub user: String, + pub pass: String, +} diff --git a/packages/backend/native-utils/migration/src/vec_to_json.rs b/packages/backend/native-utils/migration/src/vec_to_json.rs index c4a6d9b073..104357a496 100644 --- a/packages/backend/native-utils/migration/src/vec_to_json.rs +++ b/packages/backend/native-utils/migration/src/vec_to_json.rs @@ -1,5 +1,3 @@ -#![cfg(feature = "convert")] - use indicatif::{MultiProgress, ProgressBar, ProgressStyle}; use native_utils::model::entity::newtype::{I32Vec, StringVec}; use sea_orm_migration::{ diff --git a/packages/backend/package.json b/packages/backend/package.json index 020257dd82..ca916576b3 100644 --- a/packages/backend/package.json +++ b/packages/backend/package.json @@ -6,8 +6,12 @@ "scripts": { "start": "pnpm node ./built/index.js", "start:test": "NODE_ENV=test pnpm node ./built/index.js", - "migrate": "typeorm migration:run -d ormconfig.js", - "revertmigration": "typeorm migration:revert -d ormconfig.js", + "migrate": "pnpm run migrate:typeorm && pnpm run migrate:cargo", + "migrate:typeorm": "typeorm migration:run -d ormconfig.js", + "migrate:cargo": "cargo run --manifest-path native-utils/migration/Cargo.toml -- up", + "revertmigration": "pnpm run revertmigration:cargo && pnpm run revertmigration:typeorm", + "revertmigration:typeorm": "typeorm migration:revert -d ormconfig.js", + "revertmigration:cargo": "cargo run --manifest-path native-utils/migration/Cargo.toml -- down", "check:connect": "node ./check_connect.js", "build": "pnpm swc src -d built -D", "watch": "pnpm swc src -d built -D -w",