๐ ๏ธ Command-Line Tools
dev.py runs the administration tasks: starting the server, managing users and invitations, maintaining the database, backing up the instance. Each section says where to run the command and when it needs the server stopped.
Where to run the commands
| Installation | Where | Command form |
|---|---|---|
| On the host | Terminal on the server, in the LibreFolio folder (the project root), Pipenv environment | pipenv run ./dev.py <command> โ or ./dev.py <command> after pipenv shell |
| Docker, server running | Terminal on the host, in the folder of docker-compose.yml |
docker compose exec librefolio python dev.py <command> |
| Docker, server stopped | Same terminal | docker compose stop librefolio, then docker compose run --rm librefolio python dev.py <command> (one-off container), then docker compose start librefolio |
- There is no
pipenv runin the container: the image installs the dependencies globally. docker compose execruns the command as root. From a source checkout,./dev.py docker exec <command>runs it as the server's user instead (details).- The commands
user,db current,db check,db currencies,backup create,backup inspect,backup restore,backup decryptandbackup pendingwork in the image (Back Up with Docker).db upgradeanddb downgradeneed the server stopped, so the third form (Apply Migrations). Development commands such astestare not part of the image.
๐ This guide is in several pages
Besides this page: CLI Admin Tools: Backups.
๐ฅ๏ธ Start the Server
On the host, in the server's terminal, LibreFolio folder. In Docker the server is the container's main process: docker compose up -d starts it.
# Standard start, one worker
pipenv run ./dev.py server
# Size the workers to the CPUs (`auto` and `0` do the same)
pipenv run ./dev.py server --workers auto
# Or set the number of workers
pipenv run ./dev.py server --workers 4
# Listen on another port (default: PORT from .env, else 6040)
pipenv run ./dev.py server --port 8080
# Kill whatever already holds the port, then start
pipenv run ./dev.py server --force
The server answers on the chosen port (PORT from .env, else 6040).
- ๐งฎ More workers serve more requests at once: use them on any machine with more than one CPU.
autostarts \(\max(1,\ 2\,(n-1))\) workers on \(n\) CPUs. - โณ At the first start, or when they are out of date, the server builds the web interface and this documentation: it takes a few minutes. If the interface does not build, the server does not start.
- ๐ A restart signs everybody out, unless
JWT_SECRETis set in.env(Configuration). - โน๏ธ Ctrl+C stops the server cleanly:
dev.pygives it the shutdown budget plus 2 seconds (10 seconds by default), then ends it; a second Ctrl+C ends it at once. A SIGTERM sent todev.pyalone, by a process supervisor for example, gets the same wait:dev.pypasses it on to the server.
โ๏ธ Other server options โ rarely needed
| Option | What it does |
|---|---|
--host HOST |
Address to listen on (default: HOST from the environment or .env, else 0.0.0.0) |
--data-dir PATH |
Another data directory for this run, instead of LIBREFOLIO_DATA_DIR |
--no-scheduler |
Start without the scheduled price and FX rate syncs |
--rebuild, -r |
Rebuild the web interface even when it looks up to date |
--debug, -d |
DEBUG logs, a debug build of the web interface and the API documentation pages (/api/v1/docs, /api/v1/redoc), otherwise off |
Short forms: -w for --workers, -p for --port, -f for --force. --test, --coverage and --no-reload are development options: pipenv run ./dev.py server --help lists them all.
๐ค Manage Users
On the host or in Docker with the server running (first or second form of the table above): these commands write straight to the database, so they also work while the server is running.
In the app, administrators manage accounts and invitations in Settings โบ Administration โบ Users (Users & Invitations). The command line is for a server without a browser, or to go past the user limit.
โ Create and List Users
# Create an administrator account
pipenv run ./dev.py user create <username> <email> <password>
# Create a regular account
pipenv run ./dev.py user create <username> <email> <password> --role user
# List all users: ID, username, email, active, administrator
pipenv run ./dev.py user list
user list prints, for each account: ID, username, email, active, administrator.
- ๐
user createcreates an administrator; with--role user, a regular account. People can also sign up on their own: Register here on the login page while Registration is Open (on a new instance, also until the first account exists), or with an invitation link (Who Can Sign Up). - ๐ Password: same rules as in the app and the API. At least 8 characters, with an upper-case letter, a lower-case letter, a digit and a symbol. Letters and digits of any alphabet count (
ร,รฉ); a symbol is any character that is neither a letter, a number nor a space (!,_,โฌ). An invalid password changes nothing: the command printsPassword does not meet security requirements:and one line per rule missed, starting with the rule's code (digit: the password must contain a digit), then exits with1. - ๐ค Username: 3 to 50 characters, without
@(at sign-in, anything containing@is read as an email address). Spaces, accented letters,.,_,-,+are fine. Errors, each with exit1and changing nothing:A username cannot contain @: the sign-in reads it as an email address;A username must be 3 to 50 characters long;Username already taken;Email already registeredif another account has that address, or the address is another account's username (user set-email), in any capitals. - ๐ At the user limit,
user createwarnsThe instance is at its limit (LIBREFOLIO_MAX_USERS: 5 of 5 places taken): the command line creates the account anyway, then creates the account: the command line is how you go past the limit.
โ๏ธ Invite Someone
# An invitation link for a regular account
pipenv run ./dev.py user invite
# An invitation for an administrator, with a note of whom it is for
pipenv run ./dev.py user invite --admin --email anna@example.com
The command prints a link that works once, for 7 days: send it to the person, who opens it and creates their own account with the username, email and password they choose (what they see).
- ๐
--admin: the new account is an administrator; without it, a regular one. - โ๏ธ
--email: only a note of whom the invitation is for; the person can sign up with another address. - ๐ With
LIBREFOLIO_PUBLIC_URLset, the link is complete (https://librefolio.example.com/invite#token=โฆ). Without it, the command prints the path only,/invite#token=โฆ, and reminds you to put the address of the server in front of it (http://192.168.1.10:6040). If the variable is set but is not anhttp://orhttps://address, the reminder saysLIBREFOLIO_PUBLIC_URL is not valid (โฆ), with the reason. This holds for every link on this page. - ๐ The secret part comes after
#: browsers never send it to the server, so it does not end up in the server or proxy logs. Send the link whole. - ๐ช Invitations work with Registration Open or By invitation. With Closed, the command stops with
Registration is disabled: invitations cannot be createdand exits with1; the links already sent do not work either. - ๐ An invitation takes a place of the user limit until it is used, revoked or expired. At the limit, the command warns, then creates the invitation anyway.
- โณ No command cancels an invitation: a link nobody used expires after 7 days. Sooner, Revoke in the Users tab, or through the API:
GET /api/v1/users/account-linkslists every account link with its state,DELETE /api/v1/users/account-links/<id>revokes an open one.
๐ Reset a Password or Lock an Account
# Set a new password (same rules as above)
pipenv run ./dev.py user reset <username> <new_password>
# Lock an account out, then let it back in
pipenv run ./dev.py user deactivate <username>
pipenv run ./dev.py user activate <username>
reset changes the password at once; deactivate refuses the account from its next request.
- โฑ๏ธ
resetends the open sessions: every device is signed out at its next request. To lock someone out for good,deactivate: activating the account again does not bring its old sessions back. - ๐
resetalso cancels any reset link still open for the account, whoever created it. - ๐
deactivaterefuses the last active administrator:User '<username>' is the last active administrator: promote or activate another administrator first, exit1. - ๐ At the user limit,
activateof a deactivated account warnsโฆ the command line reactivates the account anyway, then reactivates it. No warning for an account that already holds its place: one waiting for its email with a live link, or the initial administrator waiting for its setup link. - ๐ฌ With email verification on,
activatelets an account waiting for its email in at once anddeactivateends the wait: either way its verification links stop working (Email Verification in Email). - ๐ On the initial administrator still waiting for its setup link,
resetandactivateset it up without the link and end the wait for good: its setup links are invalidated, the one ofLIBREFOLIO_ADMIN_BOOTSTRAP_TOKENincluded, and neitheruser setup-linknor a newLIBREFOLIO_ADMIN_BOOTSTRAP_TOKENgives it another one.activatealone leaves an active account whose password nobody knows, untilresetor a reset link gives it one;resetalone leaves it inactive, until you step in withactivate. - ๐ซ On that same waiting administrator,
deactivatesets nothing up: it cancels the setup link, as Cancel setup link does in Users & Invitations. It printsโ User '<username>' deactivated; every setup link of the account is invalidated, the one ofLIBREFOLIO_ADMIN_BOOTSTRAP_TOKENincluded, and the account keeps waiting:user setup-linkgives it a new one, and so does a newLIBREFOLIO_ADMIN_BOOTSTRAP_TOKENat the next start. The same token, at a restart, does not: the instance has already seen it. - ๐ฌ The app's Forgot Password? screen shows this command for both installations: for Docker the line
docker compose exec librefolio sh -c 'exec gosu โฆ' sh user reset โฆ(the one ofdocker exec, run as the server's user, not root); for the host./dev.py user reset โฆ, afterpipenv shellor withpipenv runin front. When the instance can email a reset link, the screen shows only the email form, without the commands (Forgotten Passwords).
๐ Send a Password-Reset Link
The command prints a link: the person opens it and chooses the new password themselves, instead of receiving the one you chose with user reset (what they see).
- โฑ๏ธ The link works once, for 30 minutes. A new link for the same account cancels the earlier ones; a change of the account's password or email โ in the user's Profile, or with
user resetoruser set-emailโ cancels them all. - ๐ช Once the new password is set, every signed-in device is signed out, as with
user reset. Creating the link signs nobody out. - ๐ Complete link with
LIBREFOLIO_PUBLIC_URL, the path only (/reset-password#token=โฆ) without it (as for invitations).
๐ A New Setup Link for the Initial Administrator
The command prints a new setup link for the administrator created from LIBREFOLIO_ADMIN_EMAIL at the first start (The Initial Administrator), as long as nobody has used theirs: they open it, choose the password and are signed in. Use it when the link has expired, got lost or was cancelled.
- โฑ๏ธ The link works once, for 72 hours. The earlier setup links are invalidated, the one of
LIBREFOLIO_ADMIN_BOOTSTRAP_TOKENincluded. - ๐ซ After a cancellation โ Cancel setup link in Users & Invitations, or
user deactivateon the waiting administrator โ the administrator still waits: this command gives it a new link. - ๐ฎ After a new address for the waiting administrator โ
LIBREFOLIO_ADMIN_EMAILchanged before a restart, oruser set-emailโ its links, sent to the old address, are invalidated: this command gives it a link for the new one. The same address with other capitals of the letters A-Z is no change: the link keeps working. - ๐ Set up another way โ
user activate, or a password fromuser resetor from a reset link โ the administrator waits no more: its links are invalidated for good and the command stops with the error below. - ๐ Complete link with
LIBREFOLIO_PUBLIC_URL, the path only (/setup#token=โฆ) without it (as for invitations). - ๐ง The command only prints the link: it sends no email.
- โ Without a waiting administrator, the command stops with
No administrator account is waiting for its setup linkand exits with1. The same if the administrator completes the setup while the command runs: the command checks again just before it issues the link and gives none to an account already set up.
๐ง Change the Email Address of an Account
The same change as a new email in the user's Profile, without their password: for someone who cannot change their address themselves, or to separate two accounts that share one (below).
- ๐ค An address belongs to one account only, whatever its capitals:
Anna@example.comandanna@example.comare the same address. Only the letters A-Z are compared this way, not accented ones:รmile@example.comandรฉmile@example.comare two different addresses. - ๐ช A new address signs the account out of every device, cancels every password-reset link and every pending email-change link, and is recorded as not verified; the account can still sign in. It does not touch the password. The same address with other capitals only changes how it is written: nobody is signed out and it stays verified.
- ๐ On the initial administrator still waiting for its setup link, the new address also invalidates that link: the account still waits, and
user setup-linkgives it a new one. WhileLIBREFOLIO_ADMIN_EMAILis set, each start of the server gives the waiting account that address again: change the variable as well, or the next start brings the old address back and invalidates the new link too. - โ Refused, with exit
1and changing nothing: an address another account already has, in any capitals (Email already registered); an address that is another account's username, in any capitals (same error: an account created before version 1.3 may have an@in its username, Usernames with an @, and if that name became someone's address, the sign-in, which reads an address first, would no longer find that account); an invalid address (Not an email address); an unknown username (User '<username>' not found). The account's own username is no obstacle: it can become its address.
๐ Grant or Remove Administrator Rights
A demotion signs the account out of every device.
- ๐
demoterefuses the last active administrator:User '<username>' is the last active administrator: promote another user first, exit1. - ๐
demotealso refuses the initial administrator still waiting for its setup link, which keeps its role until then:User '<username>' is the initial administrator waiting for its setup: it keeps its role until then; deactivate it to cancel its setup link, or delete it from Settings โบ Administration โบ Users, exit1, changing nothing. To cancel its link:user deactivate(above). To remove the account: delete it in Users & Invitations, with no demotion first. The command line deletes no account.
๐งญ Restart the Guides of a User
# Every guide, the Welcome setup included
pipenv run ./dev.py user reset-onboarding <username>
# One guide only
pipenv run ./dev.py user reset-onboarding <username> --flow intro_tour
The next time the user opens LibreFolio, the Welcome setup and the guides start again, as on their first visit; the user's data and preferences stay. --flow restarts one guide only: pipenv run ./dev.py user reset-onboarding --help lists the names (welcome, intro_tour, import_guideโฆ).
๐๏ธ Maintain the Database
โฌ๏ธ Apply Migrations
Every start of the server applies the pending migrations by itself: you rarely need to do it by hand.
On the host, in the server's terminal, LibreFolio folder, with the server stopped: db upgrade refuses to run while the server answers on the configured port. db current, db check and db currencies change nothing and also run with the server up.
# Apply pending migrations
pipenv run ./dev.py db upgrade
# Show the migration the database is at
pipenv run ./dev.py db current
# Look for missing CHECK constraints: changes nothing, exits with 1 if any
pipenv run ./dev.py db check
# List the currency codes that are not ISO codes: changes nothing, exits with 1 if any
pipenv run ./dev.py db currencies
# Upgrade and check another database file, such as a copy
pipenv run ./dev.py db upgrade /path/to/copy/app.db
pipenv run ./dev.py db check /path/to/copy/app.db
db upgrade brings the database to the latest migration; db current prints the one it is at.
- ๐ Without a path, the commands use the configured database. A path names another SQLite file: a relative one starts at the project root, whatever folder you run the command from. The file must exist, except for
db upgrade, which creates it (folder included) and brings it up to date. - ๐ณ In Docker, the path is inside the container:
LibreFolio-data/is/app/backend/data/prod-docker, the database issqlite/app.dbin it.db current,db checkanddb currencies: server running,docker compose exec librefolio python dev.py db current <path>.db upgradeanddb downgrade: server stopped (docker compose stop librefolio), then a one-off container,docker compose run --rm librefolio python dev.py db upgrade <path>(details).
๐ค Two Accounts with the Same Email Address
From version 1.3, an email address belongs to one account only, whatever its capitals. If an earlier database has two accounts with addresses that differ only in capitals (Anna@example.com and anna@example.com), the upgrade stops at the migration that checks this and the server does not start. The earlier migrations stay applied, no account is changed, the next start picks up from there. The log (or the output of db upgrade) names the accounts by id and username, never by address; a ; separates one group from the next:
Email addresses must be unique regardless of case (release 1.3), and these accounts share one apart from upper and lower case: #3 'anna', #8 'anna_work'. Give all but one account of each group another address with โฆ
LibreFolio never merges two accounts: give all but one account of each group another address, then start LibreFolio again.
In the server's terminal, LibreFolio folder:
In the folder of docker-compose.yml. The server is the container's main process, so the container does not stay up and docker compose exec is no use: use a one-off container.
๐ฉน Post-Migration Fixes
Right after the migrations, every start runs the post-migration fixes: repairs that a migration cannot make. Usually there is nothing to do. In the log (the server output, also saved in logs/) you may read:
| Log line | Meaning |
|---|---|
โ
Post-migration fixes applied and verified |
A repair was made. On a large database that start is slower, once. |
โ ๏ธ Post-migration fix failed |
Database left as it was, server started normally. The warning gives the error and the copy of the database kept next to the original until you delete it (app.db.pre-autoincrement-20261008T101500Z.bak). The fix is tried again at the next start. |
๐ฉบ Post-migration fixes skipped |
The copy could not be made, or the database fails SQLite's integrity check. Nothing changed. |
๐ Foreign key violations already in the database, left as they are |
The database holds references to rows that no longer exist, listed in the warning. The fixes leave them as they are and still run: a fix fails only on a broken reference it would add itself. |
The empty file app.db.post-migration.lock, next to the database, makes the runs take turns when several workers start together: leave it in place.
The fixes:
autoincrement: the id of a deleted user, broker, asset, transaction, FX conversion route or asset event is never given out again. While converting, it deletes the report folders of brokers that no longer exist, so that a new broker does not inherit them.orphan_pair_halves, once: repairs the transfers that a broker deletion left without their other half on the remaining brokers.- ๐ Securities received from the deleted broker โ opening position at the transfer cost, from the transfer date. That broker's capital counts them at that cost, not at their market value on arrival, so its gain also includes what they gained before the transfer. Without a recorded cost they stay without one and the app reports an acquisition without a purchase value: enter the cost in the transaction.
- ๐ถ Cash transfers with the deleted broker โ deposits or withdrawals: capital and gain do not change.
- ๐ค Securities sent to the deleted broker: right after the fixes, at the same start, each transfer becomes an adjustment that takes the securities out of the sending broker, without a value: only the quantity goes down, and the cost counts as a realized loss on that broker. One log line each (
Lone transfer leg converted to an ADJUSTMENT-out without a value). If the database fails SQLite's quick check, they stay as they were (Lone transfer legs left as they are: the database failed its quick check). If the whole step fails (Lone transfer legs could not be converted; starting normally), nothing is changed and the server starts anyway.
event_types, once: repairs the asset events with a type LibreFolio does not know, such asdividendordividendowritten by hand in a CSV. Until now such an event made the whole list of the asset's events fail to load, so none was shown.- โ
A type that differs only in letter case or spaces (
dividend,Split) is corrected. - ๐๏ธ An event whose type is still unknown is deleted. A linked transaction stays, without its event: link it again to the right event if you need to.
- โ
A type that differs only in letter case or spaces (
โจ๏ธ Run the fixes by hand โ to preview them or retry a failed one
On the host, in the server's terminal, LibreFolio folder, with the server stopped (the script does not check it). In Docker: Post-migration fixes with the server stopped.
# Preview: report what would be fixed, change nothing
pipenv run python -m backend.app.db.post_migration --dry-run
# Apply the fixes
pipenv run python -m backend.app.db.post_migration
The script prints each fix as clean (nothing to do), would_apply (dry run), applied or failed, with the orphan broker folders, a kept copy and any error, then one line for each transfer of securities sent to a deleted broker (Lone transfer leg โฆ); it exits with 1 when something failed. --db PATH and --data-dir PATH point it at another database or another data directory.
To see what orphan_pair_halves will do on your database, run --dry-run before the first start of the new version: it prints Fix orphan_pair_halves: would_apply when there is something to repair, and a warning lists the transactions by id, grouped by conversion (TRANSFER->ADJUSTMENT), with those that will stay without a cost and the transfers of securities sent to a deleted broker; these get one line each after the fixes (would become an ADJUSTMENT-out without a value). The script prints on the console what the server would write to the log: the warning and, on a real run, one line per transaction changed (with LOG_LEVEL at INFO, the default). It writes no log file: keep its output if you want a record of the repair.
๐ How the fixes work inside: Database Schema โ Post-migration fixes.
๐ฑ Currency Codes That Are Not ISO Codes
From version 1.3, LibreFolio saves a currency only as an ISO 4217 alphabetic code (EUR, USD). Earlier versions also accepted the name or the numeric code and saved it as written: Euro, 978. Such a value still reads, but an edit that saves its row again is refused until the code is corrected, and its amounts are not summed with those of the ISO code: 978 and EUR make two balances. A restored backup can bring such values back.
Shortly after every start, the log warns when the database holds some; LibreFolio does not change them by itself:
Stored currency codes that are not ISO 4217 codes: an edit of their rows is refused until they are corrected (./dev.py db currencies lists them)
On the host or in Docker with the server running (docker compose exec librefolio python dev.py db currencies):
# Where each value is stored, how many rows hold it, and the ISO code it stands for
pipenv run ./dev.py db currencies
# Another database file, such as a copy
pipenv run ./dev.py db currencies /path/to/copy/app.db
The command lists the values without changing anything.
- ๐ Each line names the place (
transactions.currency,assets.currency, the steps of an FX route, a provider's settings, a user's base currencyโฆ), the value, how many rows hold it and the ISO code it stands for:978isEUR,US DOLLARisUSD. - โ A dash: no single code. A name two currencies share (
Bolรญvar Soberano:VEDorVES), a value that is not a currency, or a code that counts cents rather than units (GBX,ZAC,ILA), never to be simply renamed. - ๐ค A code in lower case or with spaces (
eur) is listed too: the right currency, in a form that exact comparisons miss. - ๐งฑ A value saved as raw bytes or as text that is not valid UTF-8, which only a damaged or hand-made database holds, appears in SQLite's notation (
X'455552') with a dash: it is never a code. - ๐ฆ Exit
0when every code is ISO,1when it lists some,2when it cannot read the database.
๐ง Add Missing Global Settings
On the host or in Docker with the server running:
Adds the missing Global Settings with their default values, as every start does, but without starting the server; it never changes a value already set.
๐งน Reset the Database
On the host, in the server's terminal, LibreFolio folder, with the server stopped:
All data will be lost
db create-clean deletes the database and creates an empty one. Like db upgrade, it refuses to run while the server is up. Uploaded files and broker reports stay on disk: Database Initialization & Reset.
๐ Full Command Tree
# Every command, by category
pipenv run ./dev.py --help
# The options of one command
pipenv run ./dev.py server --help
๐ฉโ๐ป Developer and documentation commands
- Frontend:
pipenv run ./dev.py front build,front dev,front checkโ Frontend Development - Testing:
pipenv run ./dev.py test allโ Test Walkthrough - API Client:
pipenv run ./dev.py api syncโ API Overview - i18n:
pipenv run ./dev.py i18n auditโ Internationalization - Documentation:
pipenv run ./dev.py mkdocs deploypublishes this documentation to GitHub Pages;pipenv run ./dev.py mkdocs galleryregenerates the screenshots with Playwright on a test server (--no-populatekeeps the current test data).
The full developer toolkit: Developer Workflow Guide.
๐ Related
- ๐ CLI Admin Tools: Backups โ Back Up the Instance