This guide explains how to work with Mathesar's code. Be sure to also see our Contributor Guide to learn about our collaboration workflow.
Mathesar is built using:
- PostgreSQL for the data storage
- Python for the backend
- Django for the web application
- SQLAlchemy to talk to the database
- Django REST Framework for the API
- Svelte and TypeScript for the frontend
Note: If you are developing on Windows, first install WSL. Then do all of your development work from within the WSL shell, including running commands like
git clone
anddocker compose
.
-
Ensure that you have Docker installed.
-
Clone the repository and
cd
into it. -
Copy the .env file by running the following command in the repository's root directory:
cp .env.example .env
-
From the repository's root directory, run:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up dev-service
Note: You may need to run docker
sudo
depending on how you have it installed.You should now have a web server and database server running.
-
Login at http://localhost:8000/ with the following credentials:
- username:
admin
- password:
password
- username:
-
Keep Docker running while making your code changes. The app will update automatically with your new code. Please refer to our Troubleshooting guide if you are experiencing any issues.
Before getting started with your code changes, read our Contributor guide to understand our processes for handling issues and and PRs.
-
Using a CSV File (limited visibility of features):
For sample table data, you can create a new table in the UI using the
patents.csv
file found in/mathesar/tests/data
. -
Using Mathesar Data Playground (recommended):
-
Clone the
mathesar-data-playground
repo:git clone https://github.com/mathesar-foundation/mathesar-data-playground.git
-
Load the data from sql by running:
sudo docker exec -i mathesar_dev_db bash -c 'psql -U mathesar' < /path/to/your/cloned/repo/mathesar-data-playground/realistic_library_simulation/simulation_runs/simulation_run_20230106_00.sql
sudo docker exec -i mathesar_dev_db bash -c 'psql -U mathesar' < /path/to/your/cloned/repo/mathesar-data-playground/realistic_library_simulation/simulation_runs/simulation_run_20230106_00_checkouts.sql
-
Sync these changes from the UI.
-
See our API guide for more information on API usage and development.
- The
db
directory contains low-level code for interacting with the database. - The
mathesar
directory contains the Django application and API, which we sometimes refer to as the "service layer".
We use pytest for our backend tests.
-
Run all python backend tests:
docker exec mathesar_service_dev pytest mathesar/ db/
-
Run a specific python test, by name:
docker exec mathesar_service_dev pytest -k "test_name"
-
See the pytest documentation, or run pytest with the
--help
flag to learn about more options for running tests. -
Run all SQL tests:
docker exec mathesar_dev_db /bin/bash sql/run_tests.sh
- All the front end code is in the
mathesar_ui
directory. - If you are modifying front end code, read more the Front end development guide.
To lint the front end and back end code at the same time, run the lint.sh
script from the root of the repository. The script requires that the Python virtual environment with flake8
be activated and that Node modules be installed in mathesar_ui/
. Alternatively, ESLint and Flake8 should be installed globally on the system.
./lint.sh
-
By default, the script lints both Python and Node.js (if changes are staged), but this can be overridden with the
-p
and-n
flags respectively../lint.sh -p false
-
You may wish to symlink the script as a pre-commit hook to lint your changes before committing.
ln -s ../../lint.sh .git/hooks/pre-commit
If you want to use Mathesar with a preexisting Postgres DB, modify the DATABASES.mathesar_tables
entry of the config/settings.py
file with appropriate connection details before installing the Mathesar types and functions by running install.py
as described in the previous step.
Sometimes you may need to rebuild your Docker images after pulling new code changes or switching branches. Do so via:
docker compose -f docker-compose.yml -f docker-compose.dev.yml up dev-service --force-recreate --build dev-service
Our repo contains two separate i18n flows: one for the server-rendered UI from Django, and another for the client-rendered UI handled by Svelte.
We use the i18n features provided by Django. Refer the Django docs on how to translate strings.
If you make code changes to the UI strings in Django templates, follow these steps to ensure your changes are properly translated.
-
Regenerate the English-language django.po file:
docker exec mathesar_service_dev python manage.py makemessages -l en -i "mathesar_ui" -i "docs"
Note:
Only generate the
.po
file for English. Do not update other languages usingmakemessages
. They will be pulled from our translation service provider when the translation process is complete. -
Commit the changes to
django.po
along with your code changes.
Django uses gettext, which require the .po
files to be compiled into a more efficient form before using in production.
-
Compile the Django messages:
docker exec mathesar_service_dev python manage.py compilemessages
This will produce files with
.mo
extension for each of the.po
files. -
Test the app locally with different languages.
- We use svelte-i18n, which internally uses format-js for handling i18n.
- The source translation file is en/dict.json.
- To handle pluralization and other complexities, the source translation strings may utilize a special syntax called JSON with ICU Plurals (a subset of the ICU format).
- After making changes to your code, ensure that the source
/en/dict.json
file contains new translation strings, if any. - Do not update other translation files. They will be pulled from our translation service provider when the translation process is complete.
- We use Transifex for managing our translation process.
- You'll need to be a member of the Mathesar organization in Transifex, inorder to work with translations. Please reach out to us for information on how to join.
(We're currently working on a workflow for translators. This section will be updated once we have a clear set of instructions to follow.)
- We have automated sync between Transifex and the
develop
branch, via the GitHub integration feature provided by Transifex. - The configuration for it is specified in the
.tx/integration.yml
file, and within the Transifex admin panel. - Refer Transfiex documentation for more information.
If you'd like to manually push or pull translations, follow the instructions in this section.
Warning
Only push and pull translations on the
develop
branch. Do not do it for other branches since this will overwrite the existing resources within Transifex.
-
Install the Transifex cli tool,
tx
, if you haven't already.curl -o- https://raw.githubusercontent.com/transifex/cli/master/install.sh | bash
It can be installed in your host machine or on the docker container.
-
Push the updated source translation files:
TX_TOKEN=<transifex_api_token> tx push -s
-
Pull the translations from Transifex:
TX_TOKEN=<transifex_api_token> tx pull -f
-
Commit and push the changes to our repo.
-
If you need to do some work within the container you can open a bash shell via:
docker exec -it mathesar_service_dev bash
-
To open a PostgreSQL psql terminal for the data in Mathesar:
docker exec -it mathesar_dev_db psql -U mathesar
- Running Script in powershell is disabled by default in windows , you have to change permission to run scripts Official Docs
If you happen to clone the git repository outside of WSL, then you fix it by running these commands from within WSL.
git config --global core.autocrlf input
sudo apt-get install -y dos2unix
sudo find . -type f -exec dos2unix {} \;
These commands install the dos2unix
utility, which converts text files from the DOS/Microsoft Windows format (with CRLF line endings) to the Unix/Linux format (with LF line endings). Next, the find utility is used to locate all files (-type f) in the current directory (.) and its subdirectories, and the dos2unix command is then executed on each of them (-exec dos2unix ;).
Hot module replacement for front end code does not work when the project is present on a windows filesystem and WSL is used to run docker. Read more.
If you you see the following error after attempting to start Docker, then the port used by Postgres is already in use on your host machine.
ERROR: for db Cannot start server db: driver failed programming external connectivity on endpoint mathesar_dev_db (70c521f468cf2bd54014f089f0051ba28e2514667): Error starting userland proxy: listen tcp4 0.0.0.0:5432: bind: address already in use.
-
First stop Postgres on your host machine.
sudo service postgresql stop
-
Then try starting Mathesar via Docker again.
Note that you'll need to manually start your Postgres server on your host machine again if you want to continue working on other projects which rely on that. And the next time you restart your machine, you'll probably need to stop Postgres again before you can begin working on Mathesar.
Upon starting Mathesar, you may notice errors similar to:
mathesar_service_dev | psycopg.errors.InvalidFunctionDefinition: cannot change name of input parameter "tab_id"
mathesar_service_dev | HINT: Use DROP FUNCTION msar.drop_table(oid,boolean,boolean) first.
In this case, it's probable that a function parameter name was changed in the develop
branch at some point. To fix this, you must drop the msar
and __msar
schemas from the PostgreSQL database you're using for development using either psql
or a different client. After doing this, simply stop and start Mathesar using the appropriate docker compose
commands.