Skip to content

Commit

Permalink
Merge branch 'development' of github.com:hotosm/fmtm into feat-data-f…
Browse files Browse the repository at this point in the history
…ilter
  • Loading branch information
sujanadh committed Oct 9, 2023
2 parents 3b0ee0e + e4331db commit f066b22
Show file tree
Hide file tree
Showing 23 changed files with 1,429 additions and 2,581 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build_and_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ jobs:
needs: [extract-vars]
with:
context: src/backend
dockerfile: src/backend/Dockerfile
build_target: prod
image_tags: |
"ghcr.io/hotosm/fmtm/backend:${{ needs.extract-vars.outputs.api_version }}-${{ github.ref_name }}"
Expand All @@ -48,7 +47,7 @@ jobs:
needs: [extract-vars]
with:
context: src/frontend
dockerfile: src/frontend/prod.dockerfile
dockerfile: prod.dockerfile
build_target: prod
image_tags: |
"ghcr.io/hotosm/fmtm/frontend:${{ needs.extract-vars.outputs.frontend_main_version }}-${{ github.ref_name }}"
Expand Down
1 change: 0 additions & 1 deletion .github/workflows/build_ci_img.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
needs: [extract-vars]
with:
context: src/backend
dockerfile: src/backend/Dockerfile
build_target: ci
image_tags: |
"ghcr.io/hotosm/fmtm/backend:${{ needs.extract-vars.outputs.api_version }}-ci-${{ github.ref_name }}"
Expand Down
2 changes: 0 additions & 2 deletions .github/workflows/build_odk_imgs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ jobs:
uses: hotosm/gh-workflows/.github/workflows/image_build.yml@main
with:
context: odkcentral/api
dockerfile: odkcentral/api/Dockerfile
image_tags: |
"ghcr.io/hotosm/fmtm/odkcentral:${{ vars.ODK_CENTRAL_VERSION }}"
"ghcr.io/hotosm/fmtm/odkcentral:latest"
Expand All @@ -27,7 +26,6 @@ jobs:
uses: hotosm/gh-workflows/.github/workflows/image_build.yml@main
with:
context: odkcentral/proxy
dockerfile: odkcentral/proxy/Dockerfile
image_tags: |
"ghcr.io/hotosm/fmtm/odkcentral-proxy:${{ vars.ODK_CENTRAL_VERSION }}"
"ghcr.io/hotosm/fmtm/odkcentral-proxy:latest"
4 changes: 2 additions & 2 deletions .github/workflows/r-extract_vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ jobs:

- name: Extract api version
id: extract_api_version
working-directory: src/backend
run: |
cd src/backend
API_VERSION=$(python -c 'from app.__version__ import __version__; print(__version__)')
echo "api_version=${API_VERSION}" >> $GITHUB_OUTPUT
- name: Extract frontend versions
id: extract_frontend_version
working-directory: src/frontend
run: |
cd src/frontend
FRONTEND_MAIN_VERSION=$(jq -r '.version' package.json)
echo "frontend_main_version=${FRONTEND_MAIN_VERSION}" >> $GITHUB_OUTPUT
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/r-pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,5 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Run pytest
run: |
cd src/backend
pytest
working-directory: src/backend
run: pytest
4 changes: 2 additions & 2 deletions docker-compose.deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ services:
- fmtm_tiles:/opt/tiles
depends_on:
- fmtm-db
- migrations
- traefik
env_file:
- .env
Expand All @@ -109,13 +110,12 @@ services:
image: "ghcr.io/hotosm/fmtm/backend:${API_VERSION}-${GIT_BRANCH}"
container_name: fmtm_migrations
depends_on:
- api
- fmtm-db
env_file:
- .env
networks:
- fmtm-net
entrypoint: ["/migrate-entrypoint.sh"]
command: ["alembic", "upgrade", "head"]
restart: "on-failure:3"

ui:
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.noodk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ services:
- ./src/backend/app:/opt/app
depends_on:
- fmtm-db
- migrations
env_file:
- .env
ports:
Expand All @@ -71,13 +72,12 @@ services:
image: "ghcr.io/hotosm/fmtm/backend:${API_VERSION}-${GIT_BRANCH}"
container_name: fmtm_migrations
depends_on:
- api
- fmtm-db
env_file:
- .env
networks:
- fmtm-net
entrypoint: ["/migrate-entrypoint.sh"]
command: ["alembic", "upgrade", "head"]
restart: "on-failure:3"

ui:
Expand Down
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ services:
# - ../osm-fieldwork/osm_fieldwork:/home/appuser/.local/lib/python3.10/site-packages/osm_fieldwork
depends_on:
- fmtm-db
- migrations
- central-proxy
env_file:
- .env
Expand All @@ -78,13 +79,12 @@ services:
image: "ghcr.io/hotosm/fmtm/backend:debug"
container_name: fmtm_migrations
depends_on:
- api
- fmtm-db
env_file:
- .env
networks:
- fmtm-dev
entrypoint: ["/migrate-entrypoint.sh"]
command: ["alembic", "upgrade", "head"]
restart: "on-failure:3"

ui:
Expand Down
200 changes: 137 additions & 63 deletions docs/dev/Database-Tips.md
Original file line number Diff line number Diff line change
@@ -1,122 +1,196 @@
# Access the database (psql)
# Database Tips

**Option 1** (when the docker container is running) use this command to access it through the local psql using the below command:
## Access the database (psql)

`psql -d fmtm -U fmtm -h localhost`
### Option 1

**Option 2** (when running the database in Docker) use this command to access the
PostgreSQL shell inside the fmtm-db-1 container and interact with the fmtm database
using the psql command-line interface:
Access the database container using psql on your local machine:

`docker exec -it fmtm-db-1 psql -U fmtm fmtm`
```bash
psql -d fmtm -U fmtm -h localhost
```

And then connect to the database using this command :
### Option 2

`\c fmtm`
Access a PostgreSQL shell inside the fmtm_db container:

## To access the fmtm database using psql, follow the instructions below
```bash
docker exec -it fmtm_db psql -U fmtm fmtm
```

### A few helpful psql commands
And then connect to the database using this command:

- Open a terminal window and run the following command:
```bash
\c fmtm
```

docker exec -it fmtm-db-1 psql -U fmtm fmtm

This will open the psql command-line interface and connect you to the fmtm database.

- Once connected to the fmtm database, you can switch to a different database using the command:

\c dbname

Replace "dbname" with the name of the database you want to switch to. forexample `\c fmtm`
## A few helpful psql commands

- You can list all the databases using the command:

\l
```bash
\l
```

- To list all the schemas of the currently connected database, use the command:

\dn
```bash
\dn
```

- To list all the functions in the current database, use the command:

\df
```bash
\df
```

- To list all the views in the current database, use the command:

\dv
```bash
\dv
```

- To list all the users and roles, use the command:

\du
```bash
\du
```

- To list all the tables in the current database, use the command:

\dt
```bash
\dt
```

- To describe a table, use the command:

\d table_name
```bash
\d table_name
```

Replace "table_name" with the name of the table you want to describe.
Replace "table_name" with the name of the table you want to describe.

- To execute the last command again, use the command:

\g
```bash
\g
```

- To view your command history, use the command:

\s
```bash
\s
```

- To save your command history to a file, use the command:

\s filename
```bash
\s filename
```

Replace "filename" with the name of the file you want to save the command history to.
Replace "filename" with the name of the file you
want to save the command history to.

- To execute commands from a file, use the command:

\i filename
```bash
\i filename
```

Replace "filename" with the name of the file containing the commands you want to execute.
Replace "filename" with the name of the file
containing the commands you want to execute.

- To view a list of all psql commands, use the command:

\?
```bash
\?
```

- To view help for a specific command, use the command:

\h command_name
```bash
\h command_name
```

Replace "command_name" with the name of the command you want help with.
Replace "command_name" with the name of the command you want help with.

- To exit psql, use the command:

\q
```bash
\q
```

**Note:** If you make a change, don't forget to commit the change!

# Migrations

Migrations are a way to manage changes to the database schema over time. We haven't yet implemented migrations in fmtm, but if you need to drop all tables, you can use the following commands while connected to the fmtm database:

If you need to drop all tables, connect to fmtm and...

drop table mapping_issue_categories cascade;
drop table organisation_managers cascade;
drop table organisations cascade;
drop table project_allowed_users cascade;
drop table project_chat cascade;
drop table project_info cascade;
drop table project_teams cascade;
drop table projects cascade;
drop table task_history cascade;
drop table task_invalidation_history cascade;
drop table task_mapping_issues cascade;
drop table tasks cascade;
drop table teams cascade;
drop table user_licenses cascade;
drop table users cascade;
drop table x_form cascade;

**Note:** Remember to use caution when dropping tables, as this will permanently delete all data in those tables. If you make any changes to the database, be sure to commit them to ensure that they are saved.
## Migrations

- Migrations are a way to manage changes to the database schema over time.
- They are handled automatically by a management script when FMTM starts up.
- Individual SQL migration scripts are placed in the `src/backend/migrations` dir.
- These should be idempotent, i.e. can run over and over without causing errors.
- There should also be a commented out SQL script for how to revert the migration.
- Scripts should be named sequentially,
i.e. the first is 001-some-migration.sql,
then they increment by one.
- Example `000-remove-user-password.sql`:

```bash
-- ## Migration to remove password field from public.users (replaced with OSM OAuth)


-- ## Apply Migration
-- Start a transaction
BEGIN;
-- Drop the 'password' column if it exists
ALTER TABLE IF EXISTS public.users
DROP COLUMN IF EXISTS password;
-- Commit the transaction
COMMIT;


-- ## Revert Migration (comment above, uncomment below)
-- -- Start a transaction
-- BEGIN;
-- -- Add the 'password' column back if it doesn't exist
-- ALTER TABLE public.users
-- ADD COLUMN IF NOT EXISTS password character varying;
-- -- Commit the transaction
-- COMMIT;
```
- When the docker compose stack starts,
an additional container starts up and runs a bash script once.
- The script generates a _table_ called `migrations`,
which simply tracks the script name and execution date.
- The `migrations` _directory_ is scanned for new files,
and if there is no record in the database of being applied,
the migration is applied.
### Running Migrations Manually
If for any reason you need to run migrations manually,
there are a few options:
#### Restart the migrations container
```bash
docker compose restart migrations
```
#### Run the migration script in docker
This runs inside the backend container:
```bash
docker compose exec api bash /migrate-entrypoint.sh`
```
#### Run the migration script directly
Make sure you have the 4 env vars for the database
connection set on your machine,
then run the migration script directly:
```bash
bash src/backend/migrate-entrypoint.sh
```
Loading

0 comments on commit f066b22

Please sign in to comment.