Skip to content

Commit

Permalink
Isolate the Redis to Postgres data migration tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
bakar-io committed Mar 29, 2024
1 parent 200107b commit 27cf74f
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 75 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ COPY ./backend .
# Copy the frontend build
COPY --from=builder /frontend/dist ./ui

HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --start-interval=1s --retries=3 CMD [ "curl", "-f", "http://localhost:8000/health" ]

ENTRYPOINT [ "uvicorn", "app.server:app", "--host", "0.0.0.0" ]
35 changes: 1 addition & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,40 +174,7 @@ Navigate to [http://localhost:5173/](http://localhost:5173/) and enjoy!

## Migrating data from Redis to Postgres

OpenGPTs previously used Redis for data persistence, but has since switched to Postgres. If you have data in Redis that you would like to migrate to Postgres, you can use the following steps:

### With Docker

Add Postgres' environment variables to the `.env` file (by following `.env.example`). Then, start the services with `docker compose` and run the following command to migrate data from Redis to Postgres:

```shell
docker exec -it opengpts-backend make redis_to_postgres
```

### Without Docker

Make sure the following environment variables are set:

```shell
export POSTGRES_HOST=...
export POSTGRES_PORT=...
export POSTGRES_DB=...
export POSTGRES_USER=...
export POSTGRES_PASSWORD=...
export REDIS_URL=...
```

Install [golang-migrate](https://github.com/golang-migrate/migrate) on your machine if you haven't already and run database schema migrations:

```shell
make migrate
```

Finally, run the following command to migrate data from Redis to Postgres:

```shell
make redis_to_postgres
```
Refer to this [guide](tools/redis_to_postgres/README.md) for migrating data from Redis to Postgres.

## Features

Expand Down
3 changes: 0 additions & 3 deletions backend/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ start:
migrate:
migrate -database postgres://$(POSTGRES_USER):$(POSTGRES_PASSWORD)@$(POSTGRES_HOST):$(POSTGRES_PORT)/$(POSTGRES_DB)?sslmode=disable -path ./migrations up

redis_to_postgres:
poetry run python redis_to_postgres.py

test:
# We need to update handling of env variables for tests
YDC_API_KEY=placeholder OPENAI_API_KEY=placeholder poetry run pytest $(TEST_FILE)
Expand Down
20 changes: 1 addition & 19 deletions backend/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ pgvector = "^0.2.5"
psycopg2-binary = "^2.9.9"
asyncpg = "^0.29.0"
langchain-core = "^0.1.33"
redis = "5.0.1"

[tool.poetry.group.dev.dependencies]
uvicorn = "^0.23.2"
Expand Down
8 changes: 0 additions & 8 deletions docker-compose-prod.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
version: "3"

services:
redis:
container_name: opengpts-redis
image: redis/redis-stack-server:latest
ports:
- "6379:6379"
volumes:
- ./redis-volume:/data
postgres:
image: pgvector/pgvector:pg16
healthcheck:
Expand Down Expand Up @@ -44,4 +37,3 @@ services:
- .env
environment:
POSTGRES_HOST: "postgres"
REDIS_URL: "redis://opengpts-redis:6379"
8 changes: 0 additions & 8 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
version: "3"

services:
redis:
container_name: opengpts-redis
image: redis/redis-stack-server:latest
ports:
- "6379:6379"
volumes:
- ./redis-volume:/data
postgres:
image: pgvector/pgvector:pg16
healthcheck:
Expand Down Expand Up @@ -47,7 +40,6 @@ services:
- ./backend:/backend
environment:
POSTGRES_HOST: "postgres"
REDIS_URL: "redis://opengpts-redis:6379"
command:
- --reload
frontend:
Expand Down
8 changes: 8 additions & 0 deletions tools/redis_to_postgres/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FROM langchain/open-gpts:latest

RUN poetry add redis==5.0.1

COPY migrate_data.py .

# Run database schema migrations and then migrate data
ENTRYPOINT sh -c "make migrate && python migrate_data.py"
9 changes: 9 additions & 0 deletions tools/redis_to_postgres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OpenGPTs previously used Redis for data persistence, but has since switched to Postgres. If you have data in Redis that you would like to migrate to Postgres, follow the instructions below.

Navigate to the `tools/redis_to_postgres` directory and ensure that the environment variables in the docker-compose file are set correctly for your Redis and Postgres instances. Then, run the following command to perform the migration:

```shell
docker compose up --build --abort-on-container-exit
```

This will run database schema migrations for Postgres and then copy data from Redis to Postgres. Eventually all containers will be stopped.
24 changes: 24 additions & 0 deletions tools/redis_to_postgres/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
version: "3"

services:
redis:
container_name: opengpts-redis
image: redis/redis-stack-server:latest
ports:
- "6380:6379"
volumes:
- ./../../redis-volume:/data
data-migrator:
build:
context: .
depends_on:
- redis
network_mode: "host"
environment:
REDIS_URL: "redis://localhost:6380"
POSTGRES_HOST: "localhost"
POSTGRES_PORT: 5433
POSTGRES_DB: postgres
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
OPENAI_API_KEY: ...
File renamed without changes.

0 comments on commit 27cf74f

Please sign in to comment.