tursodatabase / turso-client-php

Turso + PHP - libSQL Extension for PHP
https://turso.tech/php
MIT License
48 stars 4 forks source link

[Feature Request]: Support to Thread Safe builds #6

Open harrysbaraini opened 3 months ago

harrysbaraini commented 3 months ago

Description

Hello, first of all, thank you for such great work.

I'm trying to get it working with FrankenPHP (that uses thread safe builds) but Turso php client seems to be built with PHP NTS. For that reason, I get PHP Warning: PHP Startup: Unable to load dynamic library error.

I cloned this repo locally but I have almost zero knowledge on rust. Guidance or support to it would be appreciated. I'm evaluating Turso for a new enterprise application, but I'd like to stitch with Franken too for its Mercury feature as well.

Thank you.

Describe the solution you'd like

I'm on a Macbook M2. I'm updated Dockerfile to use php:8.3-zts as the base docker image, but when running cross build ... I get this error: could not create home directory: '/.rustup': Permission denied (os error 13). I tried setting up RUSTUP_HOME on host but it didn't work as well.

Describe alternatives you've considered

No response

darkterminal commented 3 months ago

Hi @harrysbaraini, thank you for using this extension. What's file you add in your php.ini is a .dylib or .so file?

I never used frankenphp, can you provide a more detailed explanation of how you did the configuration?

FYI: this extension is not using PDO driver.

harrysbaraini commented 3 months ago

I am on phone now, so sorry for not much detail. I followed the instructions in Readme and added the so extension to my ini.

As soon as I get back to my laptop I'll give a bit more detail.

darkterminal commented 3 months ago

Oki...

harrysbaraini commented 3 months ago

@darkterminal I just have a simple Dockerfile, where I download the compiled extension and add it to php.ini.

FROM composer
FROM dunglas/frankenphp:1.2-php8.3

COPY --from=composer /usr/bin/composer /usr/bin/composer

RUN install-php-extensions \
    intl \
    pdo_mysql \
    pdo_pgsql \
    zip \
    redis \
    pcntl \
    posix

RUN apt update && \
    apt install -yq ca-certificates wget unzip npm && \
    rm -rf /var/cache/apt/* /tmp/*

RUN wget https://github.com/tursodatabase/turso-client-php/releases/download/turso-php-extension-v1.2.6/libsql_php-turso-php-extension-v1.2.6-php-8.3-x86_64-unknown-linux-gnu.tar.gz \
    && tar -xf libsql_php-turso-php-extension-v1.2.6-php-8.3-x86_64-unknown-linux-gnu.tar.gz \
    && mv libsql_php-turso-php-extension-v1.2.6-php-8.3-x86_64-unknown-linux-gnu/liblibsql_php.so $(php -i | grep ^extension_dir | cut -d " " -f 5) \
    && echo "extension=liblibsql_php.so" > /usr/local/etc/php/conf.d/liblibsql_php.ini \
    && rm -rf libsql_php-turso-php-extension-v1.2.6-php-8.3-x86_64-unknown-linux-gnu \
    && rm -f libsql_php-turso-php-extension-v1.2.6-php-8.3-x86_64-unknown-linux-gnu.tar.gz

WORKDIR /app

php -i output:

Warning: PHP Startup: libsql_php: Unable to initialize module
Module compiled with build ID=API20230831,NTS
PHP    compiled with build ID=API20230831,TS
These options need to match
 in Unknown on line 0
[PHP Modules]
Core
ctype
curl
[...]

Meanwhile, I'll try to compile FrankenPHP without thread-safety so I can evaluate Turso. It will work but with some drawbacks (https://github.com/dunglas/frankenphp/issues/601#issuecomment-1964769437).

I'll still keep trying to compile this library locally, and if works I can happily open a PR.

darkterminal commented 3 months ago

Thank you for your report! I will try to compile this using PHP NTS. But you can also create PR if you want.

harrysbaraini commented 3 months ago

It looks like it's already compiled as NTS, in this case it should be compiled with Thread-Sagety enabled I think.

Thank you!

harrysbaraini commented 3 months ago

I tried to find some guidance in README or a Contributing.md but didn't find it. Could you briefly explain how do you compile?

I have rust and cross installed in my macbook. I then run cross build --release --target x86_64-unknown-linux-gnu but I get could not create home directory: '/.rustup': Permission denied (os error 13. RUSTUP_HOME is set accordingly to $HOME/.rustup.

darkterminal commented 3 months ago

You can compile following this Actions command: https://github.com/tursodatabase/turso-client-php/blob/main/.github/workflows/cross-compile.yml#L138-L140

About the error: https://stackoverflow.com/a/67830648

andersonpem commented 1 month ago

FrankenPHP gives me:

447.3  TURSO CLIENT PHP SUCCESSFULLY INSTALLED!
447.3  To get extension class autocompletion you need to modify your IDE Settings
447.3  in this case VSCode Settings:
447.3  - Open your VSCode setting (cmd/ctrl+,) then search "intelephense.stubs"
447.3  - add this: /root/.turso-client-php value on the lists
447.3  Thank you for using Turso Database!
447.3 
447.3 + echo extension=/root/.turso-client-php/liblibsql_php.so
447.3 + php -m
447.3 + grep -q libsql
447.3 PHP Warning:  PHP Startup: libsql_php: Unable to initialize module
447.3 Module compiled with build ID=API20230831,NTS
447.3 PHP    compiled with build ID=API20230831,TS
447.3 These options need to match
447.3  in Unknown on line 0

PHP compiled for FrankenPHP is Thread Safe :)

darkterminal commented 1 month ago

@andersonpem It seems that the error generated occurs because the PHP version used is different from the downloaded binary.

Btw, I never used FrankenPHP before.

andersonpem commented 1 month ago

For setting up the shivammathur/setup-php@v2 with thread safe PHP, go:

jobs:
  run:
    runs-on: ubuntu-latest
    name: Setup PHP TS
    steps:
    - name: Setup PHP
      uses: shivammathur/setup-php@v2
      with:
        php-version: '8.3'
      env:
        phpts: ts # specify ts or nts