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

Initial plugin design #1

Closed
simonw opened this issue Feb 6, 2024 · 6 comments
Closed

Initial plugin design #1

simonw opened this issue Feb 6, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@simonw
Copy link
Collaborator

simonw commented Feb 6, 2024

This will use the new track_events() plugin hook to write events to a database.

Can't release an alpha of this until Datasette 1.0a8 is out.

@simonw simonw added the enhancement New feature or request label Feb 6, 2024
@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

Got the most basic version working:

CleanShot 2024-02-08 at 09 01 49@2x

simonw added a commit that referenced this issue Feb 8, 2024
simonw added a commit that referenced this issue Feb 8, 2024
simonw added a commit that referenced this issue Feb 8, 2024
An alpha because it depends on a Datasette alpha release.

Refs #1
@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

Surprising test failure:

    def get_database(self, name=None, route=None):
        if route is not None:
            matches = [db for db in self.databases.values() if db.route == route]
            if not matches:
                raise KeyError
            return matches[0]
        if name is None:
            name = [key for key in self.databases.keys()][0]
>       return self.databases[name]
E       KeyError: '_internal'

Works fine on my laptop!

simonw added a commit that referenced this issue Feb 8, 2024
@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

Relevant code in the test:

datasette = Datasette(
memory=True,
config=config,
)
db = datasette.add_memory_database("demo")
await datasette.invoke_startup()
# Drop table if it exists
await db.execute_write("drop table if exists new_table")

Could it be that the plugin hook is executing and trying to create a table in _internal before the Datasette code that populates _internal has run?

@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

That looks likely:

https://github.com/simonw/datasette/blob/569aacd39bcc5529b2f463c89c616e3ada21c560/datasette/app.py#L475-L479

    async def _refresh_schemas(self):
        internal_db = self.get_internal_database()
        if not self.internal_db_created:
            await init_internal_db(internal_db)
            self.internal_db_created = True

And refresh_schemas() is called by the various view functions. So if a view hasn't been processed yet the database won't exist!

@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

Actually no it isn't that at all - you need to call datasette.get_internal_database() instead of get_database("_internal").

The only reason it worked on my local machine is that I had installed the https://github.com/datasette/datasette-visible-internal-db plugin as a debugging aid!

simonw added a commit that referenced this issue Feb 8, 2024
@simonw
Copy link
Collaborator Author

simonw commented Feb 8, 2024

@simonw simonw closed this as completed Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant