Skip to content

Commit

Permalink
feat: add missing logic for postgressql integration (#246)
Browse files Browse the repository at this point in the history
* refactor sqlite to postgres

* add missing file

* address PR comments

* add changes

* add changes

* try make ci/cd work
  • Loading branch information
jorgeantonio21 authored Nov 23, 2024
1 parent 19fe7aa commit e8fbd64
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ sui_config_path = "/root/.sui/sui_config/client.yaml"
sui_keystore_path = "/root/.sui/sui_config/sui.keystore"

[atoma-state]
database_url = "sqlite:///app/data/atoma.db"
database_url = "postgres://<POSTGRES_USER>:<POSTGRES_PASSWORD>@localhost:5432/<POSTGRES_DB>"
```

4. Create required directories
Expand Down Expand Up @@ -166,7 +166,7 @@ The deployment consists of two main services:
- HuggingFace cache: `~/.cache/huggingface:/root/.cache/huggingface`
- Sui configuration: `~/.sui/sui_config:/root/.sui/sui_config`
- Logs: `./logs:/app/logs`
- SQLite database: `./data:/app/data`
- PostgreSQL database: `./data:/app/data`

#### Managing the Deployment

Expand Down Expand Up @@ -252,6 +252,28 @@ sudo ufw allow 50000/tcp
- Ensure Sui configuration files have appropriate permissions
- Keep keystore file secure and never commit to version control

### Testing

Since the `AtomaStateManager` instance relies on a PostgreSQL database, we need to have a local instance running to run the tests. You can spawn one using the `docker-compose.test.yaml` file:

```bash
docker compose -f docker-compose.test.yaml up --build -d
```

It might be necessary that you clean up the database before or after running the tests. You can do so by running:

```bash
docker compose -f docker-compose.test.yaml down
```

and remove the specific postgres volumes:

```bash
docker system prune -af --volumes
```

Notice that by running the above commands you will lose all the data stored in the database.

### Manual deployment

#### 1. Installing Rust
Expand Down Expand Up @@ -305,7 +327,7 @@ The application uses a TOML configuration file with the following sections:

##### `[atoma-state]`

- `database_url`: SQLite database connection URL
- `database_url`: PostgreSQL database connection URL

##### Example Configuration

Expand All @@ -332,7 +354,9 @@ sui_config_path = "<PATH_TO_SUI_CONFIG>" # Example: "~/.sui/sui_config/client.ya
sui_keystore_path = "<PATH_TO_SUI_KEYSTORE>" # Example: "~/.sui/sui_config/sui.keystore" (default)

[atoma-state]
database_url = "sqlite:///<PATH_TO_DATABASE>"
# Path inside the container
# Replace the placeholder values with the ones for your local environment (in the .env file)
database_url = "postgres://<POSTGRES_USER>:<POSTGRES_PASSWORD>@localhost:5432/<POSTGRES_DB>"
```

#### 4. Running the Atoma Node
Expand Down
5 changes: 3 additions & 2 deletions atoma-service/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ mod middleware {
AtomaStateManager,
};
use atoma_sui::events::AtomaEvent;
use atoma_utils::test::POSTGRES_TEST_DB_URL;
use axum::{
body::Body, extract::Request, http::StatusCode, response::Response, routing::post, Router,
};
Expand Down Expand Up @@ -85,7 +86,7 @@ mod middleware {
}

async fn truncate_tables() {
let db = PgPool::connect("postgres://atoma:atoma@localhost:5432/atoma")
let db = PgPool::connect(POSTGRES_TEST_DB_URL)
.await
.expect("Failed to connect to database");
sqlx::query(
Expand Down Expand Up @@ -113,7 +114,7 @@ mod middleware {
let (_event_subscriber_sender, event_subscriber_receiver) = flume::unbounded();
let (state_manager_sender, state_manager_receiver) = flume::unbounded();
let state_manager = AtomaStateManager::new_from_url(
"postgres://atoma:atoma@localhost:5432/atoma",
POSTGRES_TEST_DB_URL,
event_subscriber_receiver,
state_manager_receiver,
)
Expand Down
1 change: 1 addition & 0 deletions atoma-state/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tracing = { workspace = true }

[dev-dependencies]
serial_test = { workspace = true }
atoma-utils = { workspace = true }
3 changes: 2 additions & 1 deletion atoma-state/src/state_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,9 +2104,10 @@ pub enum AtomaStateManagerError {
#[cfg(test)]
mod tests {
use super::*;
use atoma_utils::test::POSTGRES_TEST_DB_URL;

async fn setup_test_db() -> AtomaState {
AtomaState::new_from_url("postgres://atoma:atoma@localhost:5432/atoma")
AtomaState::new_from_url(POSTGRES_TEST_DB_URL)
.await
.unwrap()
}
Expand Down
4 changes: 4 additions & 0 deletions atoma-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,7 @@ where
res.map_err(Into::into)
})
}

pub mod test {
pub const POSTGRES_TEST_DB_URL: &str = "postgres://atoma:atoma@localhost:5432/atoma";
}
4 changes: 3 additions & 1 deletion config.example.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ sui_keystore_path = "/root/.sui/sui_config/sui.keystore"
cursor_path = "./cursor.toml" # Path to the Sui events cursor file

[atoma_state]
database_url = "sqlite:///app/data/atoma.db" # Path inside the container
# Path inside the container
# Replace the placeholder values with the ones for your local environment (in the .env file)
database_url = "postgres://<POSTGRES_USER>:<POSTGRES_PASSWORD>@localhost:5432/<POSTGRES_DB>"

[atoma_daemon]
service_bind_address = "0.0.0.0:3001"
Expand Down

0 comments on commit e8fbd64

Please sign in to comment.