From 9e4b3c6a1fdeda247ce2792bcdcd8a433aae0ac4 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Sat, 3 Jun 2023 00:27:00 -0400 Subject: [PATCH] call seaorm migrations after typeorm --- .../backend/native-utils/migration/Cargo.toml | 2 ++ .../native-utils/migration/src/main.rs | 34 +++++++++++++++++++ .../native-utils/migration/src/vec_to_json.rs | 2 -- packages/backend/package.json | 8 +++-- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/packages/backend/native-utils/migration/Cargo.toml b/packages/backend/native-utils/migration/Cargo.toml index 0b4b2b2e7..3813eccd8 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 31a72026e..a845a41ff 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 c4a6d9b07..104357a49 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 020257dd8..ca916576b 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",