Skip to main content

Use SQLite to manage your configs

Project description

sqlconfig: manage your config files with sqlite

The problem

Your app probably has a lot of configuration in git. Storing it as files in a git repo has a lot of advantages, including:

  • Diffing
  • Rollbacks
  • Blame
  • Branching

However, flat files in a repo can get unwieldy:

  • Different files need to be kept in sync with each other
  • Bulk operations are challenging
  • It's easy to add invalid data
  • They're disorganized

sqlconfig gives you all the advantages of config files stored in version control, with the power, flexibility and safety of SQL.

The solution

Model your config as a SQLite database. sqlconfig can deterministically turn that database into diffable flat files that live in the repo and back again.

Tutorial

Install sqlconfig. You need Python 3.

$ pip install sqlconfig

Next, design your config in SQLite. For this example, we'll use Twitch's unfairly ridiculued spam system.

$ sqlconfig --shell --dir example_config --overwrite
Running shell in read-write mode.
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> create table keyword_categories (id integer primary key, name text not null unique);
sqlite> create table keywords (id integer primary key, category_id integer not null references keyword_categories(id), keyword text unique);
sqlite> insert into keyword_categories (name) values ("spam"),("hate"),("false_positives");
sqlite> insert into keywords (category_id, keyword) values (1, "viagra"),(1, "nigerian prince"),(2, "suck"),(2, "jerk"),(3, "fanny hands lane");

We can see that sqlconfig has created files on disk:

$ ls example_config/
keyword_categories.json  keywords.json  schema.sql

Note that the JSON files are printed deterministically and in a format that's easy for git diff to work with.

Note one of the advantages of using SQL is that it's harder to write a bad config. For example, if we delete a keyword category without deleting all the keywords in it, we get an error.

$ sqlconfig --shell --dir example_config --overwrite
Running shell in read-write mode.
SQLite version 3.31.1 2020-01-27 19:55:54
Enter ".help" for usage hints.
sqlite> delete from keyword_categories where id=1;
sqlite>
error: 2 rows failed foreign key integrity checks. Run "pragma foreign_key_check" in the sqlite shell for more information.

Reading your config

It's recommended to put your schema.sql and .json files in a git repository and to review changes to them as part of your normal code review or pull request process. Through CI, you can publish this repo to your configuration management system of choice.

If you don't want to read the files directly, you can also read them with SQL. Either via the command line:

$ sqlconfig --shell --dir example_config -- -header -csv 'select keyword from keywords where category_id=1'
Running shell in read-only mode. Pass --overwrite to save your changes
keyword
viagra
"nigerian prince"

or by exporting a SQLite database to disk with --load and reading it from your application or another tool.

$ sqlconfig --load --db config.sqlite --dir example_config
$ sqlite3 config.sqlite -header -csv 'select keyword from keywords where category_id=1'
keyword
viagra
"nigerian prince"

Best practices

It's best to put the schema.sql and <table>.json files in a git repo, and use a CI job to push the config to whatever system should serve it. Before doing so, it's a good idea to use sqlconfig to validate the configuration:

$ sqlconfig --check --dir example_config/
OK

API

You can use sqlconfig.lib.load(db, dir) and sqlconfig.lib.dump(db, dir) to programmatically perform these operations. Read the code if you have any questions :)

Project details


Release history Release notifications | RSS feed

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlconfig-0.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

sqlconfig-0.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file sqlconfig-0.1.tar.gz.

File metadata

  • Download URL: sqlconfig-0.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

File hashes

Hashes for sqlconfig-0.1.tar.gz
Algorithm Hash digest
SHA256 70afd6e67aa54bc20fdd988be9e90eba339b4b1129a9299a9e1ccdd43cdebe41
MD5 50d910c4d5e2be73e129080edaae7aa5
BLAKE2b-256 8d3ee5fbbed87d9f7a9d5306b35f7ff06e751113b6e928fae380aa0af2b3b2fa

See more details on using hashes here.

File details

Details for the file sqlconfig-0.1-py3-none-any.whl.

File metadata

  • Download URL: sqlconfig-0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.8.10

File hashes

Hashes for sqlconfig-0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa3a854fe0af0facdaac0b215c812715fef8362efaed4dcc02d1f90dbb64cdc1
MD5 2df04fb19f76345896cb4a72129fdf2e
BLAKE2b-256 feea4e2220aae986b1657f2bc1a508c9e3b19f93ad255f72fa4a118168609142

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page