From 91fefdb83d945c3fc8f788cdf3ee5dd48229a631 Mon Sep 17 00:00:00 2001 From: calle <93376500+callebtc@users.noreply.github.com> Date: Wed, 3 Aug 2022 11:27:08 +0200 Subject: [PATCH] migration for extensions (#835) --- docs/devs/extensions.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/devs/extensions.md b/docs/devs/extensions.md index 0bdbb060e..98c5d4dc2 100644 --- a/docs/devs/extensions.md +++ b/docs/devs/extensions.md @@ -40,3 +40,29 @@ $ ./venv/bin/pip install Dependencies need to be added to `pyproject.toml` and `requirements.txt`, then tested by running on `venv` and `poetry`. `nix` compatability can be tested with `nix build .#checks.x86_64-linux.vmTest`. + +SQLite to PostgreSQL migration +----------------------- + +LNbits currently supports SQLite and PostgreSQL databases. There is a migration script `tools/conv.py` that helps users migrate from SQLite to PostgreSQL. This script also copies all extension databases to the new backend. Unfortunately, it is not automatic (yet) which is why a new extension **must** add its migration to this script in order for all GitHub checks to pass. It is rather easy to add a migration though, just copy/paste one of the examples and replace the column names with the ones found in your extension `migrations.py`. The next step is to add a mock SQLite database with a few lines of sample data to `tests/data/mock_data.zip`. + +### Adding migration to `conv.py` + +Here is an example block from the `subdomains` exteion: + +```python +elif schema == "subdomain": + # SUBDOMAIN + res = sq.execute("SELECT * FROM subdomain;") + q = f""" + INSERT INTO subdomains.subdomain (id, domain, email, subdomain, ip, wallet, sats, duration, paid, record_type, time) + VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s::boolean, %s, to_timestamp(%s)); + """ + insert_to_pg(q, res.fetchall()) +``` + +Note how boolean columns must use `%s::boolean` and timestamps use `to_timestamp(%s)`. If your extension uses amounts (like the column `sats` above) it should use a PostgreSQL column of type `int8` or `numeric` (aka `BIGINT`). SQLite doesn't know the difference. + +### Adding mock data to `mock_data.zip` + +`mock_data.zip` contains a few lines of sample SQLite data and is used in automated GitHub test to see whether your migration in `conv.py` works. Run your extension and save a few lines of data into a SQLite `your_extension.db` file. Unzip `tests/data/mock_data.zip`, add `your_extension.db` and zip it again. Add the updated `mock_data.zip` to your PR. \ No newline at end of file