feat: migrator binary

This commit is contained in:
Aravinth Manivannan 2022-04-12 17:34:25 +05:30
parent b39ce2098a
commit 5af8888936
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
6 changed files with 2396 additions and 104 deletions

979
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -36,7 +36,20 @@ version = "1"
features = ["tokio-runtime", "dns-over-tls", "dns-over-rustls"]
version = "0.21.1"
#[workspace]
#exclude = ["db/migrator"]
#members = [".", "db/db-core", "db/db-sqlx-sqlite"]
[dependencies.db-core]
path = "./db/db-core"
#[dependencies.db-sqlx-postgres]
#path = "./db/db-sqlx-postgres"
#
[dependencies.db-sqlx-sqlite]
path = "./db/db-sqlx-sqlite"
[dependencies.sqlx]
features = ["runtime-actix-rustls", "uuid", "postgres", "time", "offline", "sqlite"]
version = "0.5.11"
[workspace]
exclude = ["db/migrator"]
members = [".", "db/db-core", "db/db-sqlx-sqlite"]
# "db/db-sqlx-postgres"

2
db/migrator/.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/target
.env

1416
db/migrator/Cargo.lock generated Normal file

File diff suppressed because it is too large Load diff

18
db/migrator/Cargo.toml Normal file
View file

@ -0,0 +1,18 @@
[package]
name = "migrator"
version = "0.1.0"
edition = "2021"
homepage = "https://github.com/forgeflux-org/starchart"
repository = "https://github.com/forgeflux-org/starchart"
documentation = "https://github.con/forgeflux-org/starchart"
readme = "https://github.com/forgeflux-org/starchart/blob/master/README.md"
license = "AGPLv3 or later version"
authors = ["Aravinth Manivannan <realaravinth@batsense.net>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[workspace]
[dependencies]
sqlx = { version = "0.5.11", features = [ "runtime-actix-rustls", "sqlite", "postgres", "time", "offline" ] }
actix-rt = "2"

66
db/migrator/src/main.rs Normal file
View file

@ -0,0 +1,66 @@
/*
* ForgeFlux StarChart - A federated software forge spider
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use std::env;
use sqlx::migrate::MigrateDatabase;
//use sqlx::postgres::PgPoolOptions;
use sqlx::sqlite::SqlitePoolOptions;
use sqlx::Sqlite;
#[cfg(not(tarpaulin_include))]
#[actix_rt::main]
async fn main() {
//TODO featuregate sqlite and postgres
// postgres_migrate().await;
sqlite_migrate().await;
}
//async fn postgres_migrate() {
// let db_url = env::var("POSTGRES_DATABASE_URL").expect("set POSTGRES_DATABASE_URL env var");
// let db = PgPoolOptions::new()
// .max_connections(2)
// .connect(&db_url)
// .await
// .expect("Unable to form database pool");
//
// todo!("unimplemented");
//
//// sqlx::migrate!("../db-sqlx-postgres/migrations/")
//// .run(&db)
//// .await
//// .unwrap();
//}
async fn sqlite_migrate() {
let db_url = env::var("SQLITE_DATABASE_URL").expect("Set SQLITE_DATABASE_URL env var");
if !matches!(Sqlite::database_exists(&db_url).await, Ok(true)) {
Sqlite::create_database(&db_url).await.unwrap();
}
let db = SqlitePoolOptions::new()
.max_connections(2)
.connect(&db_url)
.await
.expect("Unable to form database pool");
sqlx::migrate!("../db-sqlx-sqlite/migrations/")
.run(&db)
.await
.unwrap();
}