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

Expose CLI API programmatically #948

Closed
demurgos opened this issue Dec 30, 2020 · 6 comments
Closed

Expose CLI API programmatically #948

demurgos opened this issue Dec 30, 2020 · 6 comments

Comments

@demurgos
Copy link
Contributor

I would like to avoid installing the CLI manually and would prefer to use it from a build.rs file. This way I can define it as a build dependency and have it in my Cargo.lock. It would also be one less command to have to install manually.

The current implementation only exposes the Opt struct and run function. Would it be possible to expose more functions?

@abonander
Copy link
Collaborator

What functions do you need to run from the CLI?

You can setup and run migrations manually: https://docs.rs/sqlx/0.4.2/sqlx/migrate/struct.Migrator.html
Or embed them to run on startup: https://docs.rs/sqlx/0.4.2/sqlx/macro.migrate.html

Creating and dropping databases can be done in SQL, that's all sqlx db [create, drop] do.

@demurgos
Copy link
Contributor Author

I am interested in the prepare command.

I am using the offline feature. I have a directory with my SQL scripts to handle schema migrations (I do not use the SQLx migration tool).
My goal is that anyone should be able to compile the project, even if they don't have any configured database.
Only those who edit the SQL files should need a database. I would like to detect changes to the SQL scripts and refresh the sqlx-data.json files when a change is found.

Using the prepare function programmatically currently requires me to use env::set_var and env::set_current_dir before calling run from my build.rs.

@abonander
Copy link
Collaborator

You'll probably be interested in #941 then as it aims to make cargo sqlx prepare redundant by not requiring a merging step. That should be part of the 0.5 release targeted for Q1 of next year.

@demurgos
Copy link
Contributor Author

Thank you for the link, I will follow this MR then.

I still think that it would be nice if any feature provided by the CLI was also available through an API.

@mehcode
Copy link
Member

mehcode commented Dec 30, 2020

I still think that it would be nice if any feature provided by the CLI was also available through an API.

From what I understand – apart from prepare (which is actually mostly impossible to provide an API for in the library) – everything should be possible with the API of SQLx.

Most of sqlx-cli is minuscule, see the implementation of the database sub-command: https://github.com/launchbadge/sqlx/blob/master/sqlx-cli/src/database.rs

@demurgos
Copy link
Contributor Author

I confirm that everything is possible with the API. The wording in the issue may be overly general when I was mostly interested in prepare. I was able to use it in my code using the current API and it seems that there PRs to make this subcommand less relevant.

You may close this issue if you feel that it is not relevant.

@mehcode mehcode closed this as completed Jan 1, 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

3 participants