Skip to content

Setup according to examples with extensions results in 401 error #183

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

Open
flusterIO opened this issue May 27, 2025 · 4 comments
Open

Setup according to examples with extensions results in 401 error #183

flusterIO opened this issue May 27, 2025 · 4 comments
Labels
question Further information is requested

Comments

@flusterIO
Copy link

What steps will reproduce the bug? (please provide code snippet if relevant)

pub async fn initialize_database<T: Runtime>(app: &AppHandle<T>) {
    let uri = get_database_uri();
    if let Some(db_path) = get_database_path() {
        let exists = std::fs::exists(&db_path).is_ok_and(|x| x);
        if !exists {
            let mkdir_res = std::fs::create_dir_all(&db_path);
            if mkdir_res.is_err() {
                error!("Creating the fluster managed database was unsuccessful.");
                println!("Creating the fluster managed database was unsuccessful.");
            }
        }
        let db_res = get_database().await;
        let mut db = db_res.lock().await;

        let db_exists = db.database_exists("fluster").await.is_ok_and(|x| x);
        if !exists || !db_exists {
            // This is only setup once when the application is initially launched.
            info!("Initializing database...");
            let setup_res = db.setup().await;
            if setup_res.is_err() {
                /// ----- Error occurs here ----
                print!("Error in db.setup - {:?}", setup_res.err());
                error!("An error occurred while attempting to initialize Fluster's database.");
                println!("An error occurred while attempting to initialize Fluster's database.");
            }
            info!("Installing vector related database dependencies...");
            let pg_vector_install_res = postgresql_extensions::install(
                db.settings(),
                "portal-corp",
                "pgvector_compiled",
                &VersionReq::parse("=0.16.12")
                    .expect("Failed to parse version provided to pg-vector."),
            )
            .await;
            if pg_vector_install_res.is_err() {
                println!(
                    "Error while installing pg-vector: {:?}",
                    pg_vector_install_res.err()
                );
            }
            let start_res = db.start().await;
            if start_res.is_err() {
                println!("Error when starting database: {:?}", start_res.err());
            }

            // Database needs to be created before the pool is created to avoid a 'database not
            // found' error.
            let create_res = db.create_database("fluster").await;
            if create_res.is_err() {
                log::error!("Failed to create the fluster database.");
                println!("Failed to create the fluster database.");
            }
            let pool_res = PgPoolOptions::new().max_connections(5).connect(&uri).await;
            if pool_res.is_ok() {
                let pool = pool_res.unwrap();
                let res = sqlx::migrate!("./migrations").run(&pool).await;
                if res.is_err() {
                    println!("Error here: {:?}", res.err());
                    log::error!("Failed to initialize database.");
                    println!("Failed to initialize database.");
                } else {
                    log::info!("Successfully initialized database.");
                    println!("Creating Fluster database...");
                    let stop_res = db.stop().await;
                    if stop_res.is_err() {
                        println!("Error when stopping database: {:?}", stop_res.err())
                    }
                    pool.close().await;
                }
            } else {
                log::error!("Failed to initialize database.");
                println!("Failed to initialize database.");
                println!("Error down here: {:?}", pool_res.err());
            }
        }
    } else {
        // TODO: Send an event to the front end here.
        // on_error.send(FlusterError::FailToConnect);
        log::error!("Failed to establish a local database connection. If you are using a common operating system and still encounter this error, please file an issue on Github.");
    }
    let res = app.emit("set-db-uri", SetDbConnectionUri { uri });
    if res.is_err() {
        log::error!("An error occurred while emitting the SetDbConnectionUri event.");
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn initializes_database() {
        let app = tauri::test::mock_app();
        let handle = app.handle();
        initialize_database(handle).await;
    }
}

What happens?

I just migrated from pg_embed to this library for the extensions support, which was foolish on my part to even start without having this taken care of right away because it's so crucial to the app, but now that I've migrated to this library I can't run the setup function.

Of course I've cleaned everything so it can start with a fresh slate, but it returns a 401 permissions error still every time

What did you expect to happen instead?

To setup the initial database by downloading the postgres binary.

Information about your environment

  • postgresql_embedded version: [REQUIRED] (e.g. "0.18.3")
  • Database version: [REQUIRED] (e.g. "16.4.0")
  • Operating system: [REQUIRED] MacOS latest

Also, I am working on a public network at the local library, but I really don't have a way around that. I've tried this with 3 different networks now, and wasn't able to get past this 401 error.

@brianheineman
Copy link
Contributor

brianheineman commented May 27, 2025

Hello @flusterIO, have you tried to manually download the binary from the postgres_binaries release? If you are able to manually download the correct version for you platform, one workaround would be to manually extract the contents. Alternatively, have you tried disabling the default features and using the zonky feature? This feature will attempt to download the archives from the same location that pg_embed was using (maven central).

@brianheineman brianheineman added the question Further information is requested label May 27, 2025
@flusterIO
Copy link
Author

Oh shoot I saw that zonky option and didn't think to look into what it did. I was in the middle of doing like 12 things at once, but it sounds like that's what I needed. Will that cause any issues with pg-vector, if I'm using a different binary?

@brianheineman
Copy link
Contributor

@flusterIO I would expect the extension to work with either binary; caveat emptor: I have not tested it with the zonky binaries. Let me know if you run into any issues and we can try to figure out how to get things working for you.

@flusterIO
Copy link
Author

Man I appreciate you getting back to me so fast. I'm insansely rushed with this but I know you got your own stuff to do too. The zonky feature worked great and all tests passed, so we're good to go. You can expect me to bug you again in the next day or two if I can't get the pg-vector working, but I have some other stuff I need to knock out between known and then.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants