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

query macro with multiple databases #224

Closed
glademiller opened this issue Apr 3, 2020 · 3 comments
Closed

query macro with multiple databases #224

glademiller opened this issue Apr 3, 2020 · 3 comments
Labels
duplicate This issue or pull request already exists

Comments

@glademiller
Copy link

I have a utility I am writing that will be connecting to multiple databases most MySQL and one Postgres and read data out and store the data in another format in another database. Looking at documentation and the source code it doesn't seem obvious if there is a way to support this scenario. The only possibility that comes to mind is to setup a workspace and somehow dynamically change the environment variable when building separate library packages. Would that be the only approach right now or am I missing something?

Thank you.

@mehcode
Copy link
Member

mehcode commented Apr 3, 2020

That is correct.

See here: #121 (comment)

We do want to support this. We were thinking a decent approach would be to generate a set of macros with a higher level macro.. e.g.,

mod one { sqlx::database!(env!("ONE_DATABASE_URL")); }
mod two { sqlx::database!(env!("TWO_DATABASE_URL")); }

one::query!("...") // mysql://...
two::query!("...") // postgres://...

@YpeKingma
Copy link

My code consists of 3 applications (and a library) in a single workspace, and one of the apps has a postgresql db and I'd like to add db's to to the other 2 apps. The apps run on different machines and will have different databases.

I have just setup the first app to use offline compilation by using cargo sqxl prepare to generate the query .json files in a .sqlx/ directory in the workspace directory.

I also tried cargo sqlx prepare in the package directory of the first app, and it works nicely, generating the .sqlx/ directory in the directory of the first app, but these .json files are not used for compiling the workspace.

Above here, this was mentioned "The only possibility that comes to mind is to setup a workspace and somehow dynamically change the environment variable when building separate library packages".
I could do that statically by using cargo sqlx prepare for the 3 apps in their package directories.

The question is: suppose I copy the .sqlx/*.json files from the apps's package directories into the .sqx/ directory of the workspace,
would that work for offline compilation of the workspace?

Looking at the .json content this might work when the hash in the .json file name is independent of the directory location of the generated .json files.

@YpeKingma
Copy link

Just tried using cargo sqlx prepare in the workspace directory and the package subdirectory. The output of this command makes me hopeful:

ls -l ../.sqlx .sqlx

../.sqlx:
totaal 20
-rw-rw-r-- 1 ype ype  807 sep 14 14:01 query-55399dac2ef15727fda8614334ff9c267050bf6d57533fae0a4aee2c21c162dd.json
-rw-rw-r-- 1 ype ype 1364 sep 14 14:01 query-7cdd1afddea1745cd811f9309ebd035fd752cc65d9f171cb73be9516669a7841.json
-rw-rw-r-- 1 ype ype  379 sep 14 14:01 query-8feebceef1be281fed110e49db32969d682932024794d09c2dbffad5f6ce127c.json
-rw-rw-r-- 1 ype ype 1001 sep 14 14:01 query-9d7f583306896207c978454792f21c48714ac2853da2a3eb6c5c800f47a4925d.json
-rw-rw-r-- 1 ype ype  574 sep 14 14:01 query-c7360eaf7fd21ef91c3b26f859732317810c3e1d171bc0cec94621ed972207c1.json

.sqlx:
totaal 20
-rw------- 1 ype ype  807 sep 14 20:26 query-55399dac2ef15727fda8614334ff9c267050bf6d57533fae0a4aee2c21c162dd.json
-rw------- 1 ype ype 1364 sep 14 20:26 query-7cdd1afddea1745cd811f9309ebd035fd752cc65d9f171cb73be9516669a7841.json
-rw------- 1 ype ype  379 sep 14 20:26 query-8feebceef1be281fed110e49db32969d682932024794d09c2dbffad5f6ce127c.json
-rw------- 1 ype ype 1001 sep 14 20:26 query-9d7f583306896207c978454792f21c48714ac2853da2a3eb6c5c800f47a4925d.json
-rw------- 1 ype ype  574 sep 14 20:26 query-c7360eaf7fd21ef91c3b26f859732317810c3e1d171bc0cec94621ed972207c1.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
duplicate This issue or pull request already exists
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants