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

programmatically execute mysql commands #766

Closed
mohit2152sharma opened this issue Dec 1, 2023 · 7 comments
Closed

programmatically execute mysql commands #766

mohit2152sharma opened this issue Dec 1, 2023 · 7 comments

Comments

@mohit2152sharma
Copy link

Pretty much the title, is it possible to execute mysql commands programmatically. I think pscale shell database branch should do that but when I try to run a command, it throws an error saying, shell command is only possible in interactive mode.

@mscoutermarsh
Copy link
Member

Try setting env var PSCALE_ALLOW_NONINTERACTIVE_SHELL=1 and should allow you to run it.

@mohit2152sharma
Copy link
Author

thanks a lot, this solves it. But now I get different error, saying that database and branch do not exist in the org, even though they do exist and I am able to run the same command normally.

@mscoutermarsh
Copy link
Member

I believe you'll need to authenticate with a service token. Which you can create by going to your organization settings. And adding connect_branch or connect_production_branch permissions for the database.

Set the following env vars once you have the token.

PLANETSCALE_SERVICE_TOKEN_ID
PLANETSCALE_SERVICE_TOKEN

@mohit2152sharma
Copy link
Author

Gave all the permissions and set up the enviroment variables as well, although I am passing them in the command itself using --service-token and --serivce-token-id.

Here's how I was doing:

  1. I have a file with multiple commands
  2. I read the file and split into individual commands
  3. I create a command pscale shell db branch --service-token $serviceToken --service-token-id $serviceTokenId <<EOF\n{the multiline command}\nEOF
  4. I then run a subprocess.run() command and pass the above command to it.

Anyway, now I am doing differently, using mysql-connector-python and executing command using this module and it's running successfully. I can close this comment, but would still like to know, I am doing anything wrong.

@mscoutermarsh
Copy link
Member

I believe you tried this, but just to confirm. The command works when run directly? Not from python?

Are you getting the same error when the service token is passed?

@mohit2152sharma
Copy link
Author

Yes, it works. see for example:

❯ pscale shell saral develop --service-token $PLANETSCALE_SERVICE_TOKEN --service-token-id $PLANETSCALE_SERVICE_TOKEN_ID <<EOF
show databases;
EOF
+--------------------+
| Database           |
+--------------------+
| saral              |
| information_schema |
| mysql              |
| sys                |
| performance_schema |
+--------------------+

@disco-infinex
Copy link

disco-infinex commented Oct 3, 2024

How is the PSCALE_ALLOW_NONINTERACTIVE_SHELL env var not mentioned anywhere in the docs, error message or blog post? The cli error message basically denies non-interactive use is even possible:

$ pscale shell db_name main < some_raw_commands.sql
Error: pscale shell only works in interactive mode

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

4 participants