Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQLITE compilation stuck on 0% CPU Usage #1245

Closed
Selyatin opened this issue May 24, 2021 · 6 comments
Closed

SQLITE compilation stuck on 0% CPU Usage #1245

Selyatin opened this issue May 24, 2021 · 6 comments

Comments

@Selyatin
Copy link

Selyatin commented May 24, 2021

I've a DB client written with the query! and query_as! macros and these cause my project to stall/get stuck at the last compilation step, it's stuck on the SQL check step of those 2 macros and it worked fine up until I did cargo update.

use super::features::{
    moderation::{
        Warning
    }
};
use sqlx::{
    Sqlite,
    SqlitePool,
    sqlite::SqlitePoolOptions,
    migrate::MigrateDatabase
};

#[derive(Debug)]
pub struct DB {
    pool: SqlitePool
}

impl DB {
    pub async fn new(url: impl AsRef<str>) -> Result<Self, String> {
        let url = url.as_ref();
        
        match Sqlite::database_exists(url).await {
            Ok(exists) => {
                if !exists {
                    if let Err(err) = Sqlite::create_database(url).await {
                        return Err(err.to_string());
                    }
                }
            },
            Err(err) => return Err(err.to_string())
        };

        let pool = match SqlitePoolOptions::new().max_connections(5).connect(url).await{
            Ok(pool) => pool,
            Err(err) => return Err(err.to_string())
        };
        
        // Run Migrations Here
        if let Err(err) = sqlx::migrate!().run(&pool).await {
            return Err(err.to_string());
        }


        Ok(Self {
            pool
        })
    }
    
    pub async fn find_warnings(&self, user_id: i64) -> Option<Vec<Warning>>{
        match sqlx::query_as!(Warning, "SELECT * FROM warnings WHERE user_id = ?", user_id).fetch_all(&self.pool).await {
            Ok(warnings) => Some(warnings),
            Err(_) => None
        }
    }

    pub async fn insert_warning(&self, warning: impl Into<Warning>) -> Result<(), String> {
        let warning = warning.into();
        
        if let Err(err) = sqlx::query!(
            "INSERT INTO warnings(user_id, description) VALUES(?, ?)",
            warning.user_id,
            warning.description
        ).execute(&self.pool).await {
            return Err(err.to_string());
        }

        Ok(())
    }

    pub async fn delete_warnings(&self, user_id: i64) -> Result<(), String> {
        match sqlx::query!("DELETE FROM warnings WHERE user_id = ?", user_id).execute(&self.pool).await {
            Ok(_) => Ok(()),
            Err(err) => return Err(err.to_string())
        }
    }
}

Any help would be highly appreciated!

The machines that this code was tested on:

Asus Zephyrus G14 with Ubuntu 21.04
MacBook Air M1 Base Model
Both with the 1.52 rustc compiler
sqlx 0.5.4

@Selyatin Selyatin changed the title MacOS SQLITE compilation stuck on 0% CPU Usage SQLITE compilation stuck on 0% CPU Usage May 24, 2021
@ac0v
Copy link

ac0v commented May 24, 2021

I think this issue ca be solved by #1242

@bowlofeggs
Copy link

I see this with PostgreSQL on Linux, so I think it may not be limited to sqlite.

@ac0v
Copy link

ac0v commented May 24, 2021

I think this issue happens only if you use a .env File. I had the issue with sqlite and mariadb. The PR I mentioned should solve it.

@mplanchard
Copy link

mplanchard commented May 24, 2021

Also seeing this issue w/potsgresql (ubuntu & debian) after upgrading to 0.5.4 (after downgrading from 0.5.3 to 0.5.1 over the weekend due to #1236). Downgrading again to 0.5.1 (sqlx = "=0.5.1", sqlx-core = "=0.5.1") seems to resolve the issue for the time being.

@toshokan
Copy link
Contributor

toshokan commented May 24, 2021

@Selyatin You can test if #1242 would fix your issue by temporarily testing against sqlx = { git = "https://github.com/toshokan/sqlx", ...}

Nevermind, it just got merged :)

@JesusGuzmanJr
Copy link

JesusGuzmanJr commented May 24, 2021

@toshokan
I'm not op but I experienced a similar bug yesterday when I upgraded my sqlx version. I can confirm your fix fixes the issue for me. Thank you!

@mehcode mehcode closed this as completed May 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants