Skip to content

Commit

Permalink
fix sqlx
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jan 19, 2025
1 parent cdf311a commit f2b6417
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 321 deletions.
39 changes: 0 additions & 39 deletions template/_base/src/config/db.rs

This file was deleted.

104 changes: 0 additions & 104 deletions template/_base/src/config/mod.rs.liquid

This file was deleted.

158 changes: 0 additions & 158 deletions template/_base/src/main.rs.liquid

This file was deleted.

8 changes: 8 additions & 0 deletions templates/classic/_base/config.toml.liquid
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
listen_addr = "127.0.0.1:8008"

[db]
{%- if db_type == "postgres" %}
url = "postgres://postgres:[email protected]:5432/{{project_name}}"
{%- endif %}
{%- if db_type == "sqlite" %}
url = "sqlite:data/{{db_lib}}.sqlite"
{%- endif %}
{%- if db_type == "mysql" %}
url = "mysql://root:root@localhost/{{project_name}}"
{%- endif %}
pool_size = 1

[jwt]
Expand Down
14 changes: 11 additions & 3 deletions templates/classic/_base/src/main.rs.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ use std::time::Duration;

{%- if db_lib == "diesel" %}
use diesel::r2d2;
use scheduled_thread_pool::ScheduledThreadPool;
{%- elsif db_lib == "sqlx" %}
use sqlx::SqlitePool;
{%- endif %}
use dotenvy::dotenv;
use figment::providers::{Env, Format, Toml};
Expand All @@ -15,7 +18,6 @@ use salvo::catcher::Catcher;
use salvo::conn::rustls::{Keycert, RustlsConfig};
use salvo::prelude::*;
use salvo::server::ServerHandle;
use scheduled_thread_pool::ScheduledThreadPool;
use serde::Serialize;
use tokio::signal;
use tracing::info;
Expand Down Expand Up @@ -90,9 +92,15 @@ async fn main() {
crate::db::DIESEL_POOL
.set(db_primary)
.expect("diesel pool should be set");
{%- endif %}

crate::db::migrate();
{%- elsif db_lib == "sqlx" %}
let sqlx_pool = SqlitePool::connect(&config::get().db.url)
.await
.expect("Database connection failed.");
crate::db::SQLX_POOL
.set(sqlx_pool)
.expect("diesel pool should be set");
{%- endif %}

let _guard = config.log.guard();
tracing::info!("log level: {}", &config.log.filter_level);
Expand Down
2 changes: 1 addition & 1 deletion templates/classic/diesel/.env.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
DATABASE_URL=postgresql://postgres:root@localhost/{{project_name}}
{%- endif %}
{%- if db_type == "sqlite" %}
DATABASE_URL="sqlite:data/{{project_name}}.sqlite"
DATABASE_URL="sqlite:data/diesel.sqlite"
{%- endif %}
{%- if db_type == "mysql" %}
DATABASE_URL="mysql://root:root@localhost/{{project_name}}"
Expand Down
2 changes: 1 addition & 1 deletion templates/classic/sqlx/.env.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
DATABASE_URL=postgresql://postgres:root@localhost/{{project_name}}
{%- endif %}
{%- if db_type == "sqlite" %}
DATABASE_URL="sqlite:data/{{project_name}}.sqlite"
DATABASE_URL="sqlite:data/sqlx.sqlite"
{%- endif %}
{%- if db_type == "mysql" %}
DATABASE_URL="mysql://root:root@localhost/{{project_name}}"
Expand Down
Loading

0 comments on commit f2b6417

Please sign in to comment.