GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing simonwillison.net — sqlite-utils 2.0: real upserts
Sponsored by: Greptile — Review code faster with Greptile’s new PR inbox. It puts everything you need to understand a change and merge it on one screen. Try it for free
sqlite-utils 2.0: real upserts30th December 2019 I just released version 2.0 of my sqlite-utils library/CLI tool to PyPI. It’s 2.0 because this is my first backwards compatibility breaking version of the library. I was hoping to avoid this for as long as possible: given semantic versioning, my opinion is that the highest quality libraries are the 1.x versions with the highest sub-version. Version 1.253 means a library managed to ship 253 new features without once breaking compatibility! For sqlite-utils I needed the clean break, because I made a big mistake in my definition of “upsert”. An upsert is a database operation which says “Update this record with these new values. If the record does not exist, create it.” This is actually a documented feature of SQLite version 3.24.0 and higher. I’ve seen the term used for other databases such as PostgreSQL as well. The problem is: when I first implemented the So in sqlite-utils 1.x, the following code:
Executes then following SQL:
It turns out this isn’t how upsert should be expected to work. This query will entirely replace any existing record. But... an upsert should update the record in place. Crucially, it should leave any columns not referenced in the upsert alone. My implementation over-wrote the entire row, deleting any missing values. So if I run this Python code:
The existing record for id=1 should have its age updated, but the name should stay the same. I thought hard about whether I could keep the existing, incorrect While this is technically a breaking change, when I look at the code I have already written that uses sqlite-utils it turns out all of my uses of If I’m lucky, other users of sqlite-utils will be unaffected by this behavior change either. So hopefully it will be a painless upgrade. If you do need to keep the old behavior, I’ve made it available as a
I did think about introducing a new piece of terminology for this— Using this for Niche MuseumsThe project that made me realize I needed this upsert change was Niche Museums. Niche Museums works by building a SQLite database file from a museums.yaml file. The YAML file contains the data for all 81 museums on the site. To add or edit a listing, I edit that file—sometimes in Visual Code on my Mac, sometimes via the GitHub web interface and often by using the Working Copy git client on my iPhone. Creating the database is done using my yaml-to-sqlite tool:
The build process then runs a few extra scripts to annotate the records with extra data. annotate_timestamps.py iterates through the full git history and uses it to figure out when each record was first created or last updated. annotate_nominatim.py uses the OpenStreetMap Nominatim API to reverse geocode the latitude and longitude of each museum, adding extra columns for the city, state, country and all kinds of other interesting geographical details. I’m not doing much with this OSM data yet—mainly using it to include the OSM country and city name in the full-text search index—but I hope to use it for faceting and other navigation improvements in the future (and maybe location-based breadcrumbs). Here’s the problem though: every time I deploy the site, I run those scripts. Nominatim asks you not to hit their API more than once a second, so I have a It would be great to avoid geocoding museums that have already been geocoded. Properly implemented I haven’t made these changes yet, but my plan is to update the build script so that instead of starting from a blank database each time, it downloads the previously built version of the database and runs an upsert against it with the data from the YAML. This will overwrite all of the data that I might have updated in that file, but leave the That script can then just run against records for which Other improvements this weekI added a few new features to Niche Museums this week. First, I added simple photo galleries to the listings pages, using CSS columns. This means I can add more than one photo to a museum! I’ve done this to a few now, including going back to the listing for the Ilfracombe Museum to add a whole bunch of extra photos. I added photos and press links to the Atom feed for the site. Since the Atom feed is defined using a custom SQL query and datasette-atom, achieving this involved building a truly diabolical SQL query (making extensive use of the SQLite json1 extension). I fixed an issue with the search engine where unexpected punctuation could cause an FTS syntax error in SQLite. I’ve extracted that into a fix for a Datasette issue as well. The site search engine also now attempts to geocode the user’s query and offers a link to the corresponding lat/lon radius search for that point. Try a search for new orleans to see what that looks like—the implementation (entirely client-side, calling the Nominatim API) is here. Here are the museums I added this week:
Posted 30th December 2019 at 6:48 am · Follow me on Mastodon, Bluesky, Twitter or subscribe to my newsletter
More recent articles
This is sqlite-utils 2.0: real upserts by Simon Willison, posted on 30th December 2019. museums 145 projects 568 sql 117 sqlite 885 upsert 3 weeknotes 193 sqlite-utils 237Next: Building a sitemap.xml with a one-off Datasette plugin Previous: Weeknotes: Datasette 0.33 Monthly briefingSponsor me for $10/month and get a curated email digest of the month's most important LLM developments. Pay me to send you less! Sponsor & subscribe |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |