Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
sqlite-utils-fast-fks
Advanced tools
Fast foreign key addition for sqlite-utils.
SQLite does not yet have a built-in method for adding foreign key constraints to an existing table.
There are two workarounds for this limitation:
PRAGMA writable_schema = 1
, then directly modify the schema stored in the sqlite_master
table for that table. Then increment the schema_version
, set writable_schema = 0
again and run a vacuum against the database.For tables with large numbers of rows that second option is a lot faster, as you don't need to create an entirely new copy of all of the data.
Prior to version 3.35 sqlite-utils implemented the latter pattern as part of its table.add_foreign_key()
and db.add_foreign_keys()
methods.
It turned out these caused table sqlite_master may not be modified
errors on some Python installations, primarily on macOS where the ability to modify the sqlite_master
table is sometimes disabled by default.
This plugin brings the same functionality back again. You can use this if you want fast foreign key addition and you know that your platform does not suffer from the table sqlite_master may not be modified
error.
Install this plugin in the same environment as sqlite-utils.
sqlite-utils install sqlite-utils-fast-fks
Or install using pip
:
pip install sqlite-utils-fast-fks
To add foreign keys in Python code, use the add_foreign_keys(db, foreign_keys)
function. Here's an example:
from sqlite_utils_fast_fks import add_foreign_keys
from sqlite_utils import Database
db = Database("my_database.db")
db["country"].insert_all([{"id": 1, "name": "United Kingdom"}])
db["continent"].insert_all([{"id": 1, "name": "Europe"}])
db["places"].insert(
{
"id": 1,
"name": "London",
"country_id": 1,
"continent_id": 1,
}
)
# Now modify that places table to have two foreign keys:
add_foreign_keys(
db,
[
("places", "country_id", "country", "id"),
("places", "continent_id", "continent", "id"),
],
)
The foreign_keys
argument is a list of tuples, each containing four values:
When installed as a sqlite-utils plugin, this library adds a new sqlite-utils fast-fks
command. It can be used like this:
sqlite-utils fast-fks my_database.db places country_id country id
The command takes a path to a database, then the table name, the column name, the other table name and the other column name.
You can specify multiple foreign keys to add at once by repeating the last four arguments:
sqlite-utils fast-fks my_database.db \
places country_id country id \
places continent_id continent id
To set up this plugin locally, first checkout the code. Then create a new virtual environment:
cd sqlite-utils-fast-fks
python3 -m venv venv
source venv/bin/activate
Now install the dependencies and test dependencies:
pip install -e '.[test]'
To run the tests:
pytest
FAQs
Fast foreign key addition for sqlite-utils
We found that sqlite-utils-fast-fks demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.