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

add --stdin option to db:import #934

Open
ChojinDSL opened this issue Aug 22, 2017 · 4 comments
Open

add --stdin option to db:import #934

ChojinDSL opened this issue Aug 22, 2017 · 4 comments

Comments

@ChojinDSL
Copy link

Would it be possible to add the ability to import a DB via a pipe?

db:import always seems to expect a filename. db:dump has an --stdout parameter.

Is there a specific reason why there is no --stdin for db:import?

@ktomk
Copy link
Collaborator

ktomk commented Aug 23, 2017

I think this is technically possible but requires an implementation. Any interest in taking care of this?

@pocallaghan
Copy link
Contributor

I believe it would be more complex to achieve a stdin option than the stdout feature. The import is performed by using exec, delegating to mysql and simply using file redirection, easy enough to achieve since we just pass the filename into the command being passed. To achieve stdout it therefore just needs to not redirect / capture the output, again achieved by simply not redirecting the output in the sub process. To do stdin you'd need to proxy the piped data from PHP to the child process. AFAIK this isn't possible with exec and would require the usage of something like proc_open instead.

Swapping the entire import routine to use proc_open could potentially be considered a BC break, since environments may have proc_open blocked, but not exec. Could potentially fork based on the --stdin flag, meaning proc_open is only a dependency if using the feature, but I think that may require proxying that flag a few models deep. Any thoughts?

@ktomk
Copy link
Collaborator

ktomk commented Jan 13, 2018

I quickly checked, exec is immune to STDIN:

$ echo "hello" | php -r 'exec("cat");' 

However with passthru it worked:

$ echo "hello" | php -r 'passthru("cat");'
hello

I would guess that for large inputs passthru then should be used.

You can use some of the stream handles directly with proc_open, IIRC you must take STDIN and perhaps also opening php://stdin and passing the handle might work. Those from other PHP stream wrappers normally do not work, but proc_open tells in the error message so it's quick to check for experiments.

b/c break for exec/passthru/proc_open. Good question. Normally proc_open is available on CLI, as much as the others, but then, it depends on the system this is running. I would just give it a try, especially in case of passthru.

For implementation one might consider to use the special filename "-" for standard input and output.

@pocallaghan
Copy link
Contributor

Took an initial stab at this, wasn't sure exactly how you'd want it to fit into the command, so let me know what you think. I was surprised that in my initial test, it seems that exec actually did have access to the STDIN data. I can add to the readme.rst if you're happy with the approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants