Skip to content

Conversation

asg017
Copy link
Collaborator

@asg017 asg017 commented Aug 19, 2022

Closes #1784

The --load-extension flag can now accept an optional "entrypoint" value, to specify which entrypoint SQLite should load from the given extension.

# would load default entrypoint like before
datasette data.db --load-extension ext

# loads the extensions with the "sqlite3_foo_init" entrpoint
datasette data.db --load-extension ext:sqlite3_foo_init

# loads the extensions with the "sqlite3_bar_init" entrpoint
datasette data.db --load-extension ext:sqlite3_bar_init

For testing, I added a small SQLite extension in C at tests/ext.c. If compiled, then pytest will run the unit tests in test_load_extensions.pyto verify that Datasette loads in extensions correctly (and loads the correct entrypoints). Compiling the extension requires a C compiler, I compiled it on my Mac with:

gcc ext.c -I path/to/sqlite -fPIC -shared -o ext.dylib

Where path/to/sqlite is a directory that contains the SQLite amalgamation header files.

Re documentation: I added a bit to the help text for --load-extension (which I believe should auto-add to documentation?), and the existing extension documentation is spatialite specific. Let me know if a new extensions documentation page would be helpful!

@simonw
Copy link
Owner

simonw commented Aug 20, 2022

This looks great!

You need to run cog -r docs/*.rst to update the docs, then add those to the PR.

@simonw
Copy link
Owner

simonw commented Aug 20, 2022

If you cherry-pick in the fix from #1778 it should fix the Read the Docs failure.

@simonw
Copy link
Owner

simonw commented Aug 20, 2022

If you wanted to be really ambitious about documentation a page that explained what it takes to actually run SQLite extensions in Python would be great. I'm still trying to figure out the best way to do that in macOS myself.

@simonw
Copy link
Owner

simonw commented Aug 20, 2022

I don't think we should block landing this PR on that though.

@asg017 asg017 force-pushed the add-load-extension-entrypoint branch from 036c097 to 5a2a05f Compare August 21, 2022 16:12
@asg017
Copy link
Collaborator Author

asg017 commented Aug 21, 2022

Rebased, Read the docs failure should now now fixed

Re docs - ya that's a pretty ambitious page, I'm still not 100% sure what the best practices are/should be... Would be happy to make that page in a future PR

@simonw
Copy link
Owner

simonw commented Aug 21, 2022

Any tips on how I can get the GitHub Action test.yml workflow to compile this so that the test runs there?

I imagine I need an extra step before

- name: Run tests

Neatest thing would be to cache the compiled file between actions runs (in the Actions cache) so we don't have to add the SQLite build dependencies on most runs.

@asg017
Copy link
Collaborator Author

asg017 commented Aug 23, 2022

@simonw to build the extension on ubuntu, you can run:

apt-get update && apt-get install libsqlite3-dev gcc
gcc ext.c -fPIC -shared -o ext.so

I'm not the best with Actions, but if you set the cache key to ext.c, run those two commands to download dependencies + compile to ext.so, then the unit test should pick it up and run it correctly. Let me know if you want me to update the PR with that added

@simonw
Copy link
Owner

simonw commented Aug 23, 2022

I'm going to merge as-is and then figure out the GitHub Actions bit separately. Thanks!

@simonw simonw merged commit 1d64c9a into simonw:main Aug 23, 2022
simonw added a commit that referenced this pull request Aug 23, 2022
simonw added a commit that referenced this pull request Aug 23, 2022
simonw added a commit that referenced this pull request Aug 23, 2022
simonw added a commit that referenced this pull request Aug 23, 2022
@simonw
Copy link
Owner

simonw commented Aug 23, 2022

Looks like it's not actually necessary to apt-get install anything extra in order to compile that test extension:

image

simonw added a commit that referenced this pull request Aug 24, 2022
* Run the --load-extension test, refs #1789
* Ran cog, refs #1789
simonw added a commit that referenced this pull request Sep 26, 2022
@simonw simonw mentioned this pull request Sep 26, 2022
@simonw simonw mentioned this pull request Oct 27, 2022
simonw added a commit that referenced this pull request Oct 27, 2022
simonw added a commit that referenced this pull request Oct 28, 2022
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

Successfully merging this pull request may close these issues.

Include "entrypoint" option on --load-extension?
2 participants