Skip to content

Commit

Permalink
Feature/v0.1.3 (#9)
Browse files Browse the repository at this point in the history
### Added:

- πŸ§‘πŸ»β€πŸ’» Implement `ErrorKind::FromRowError`.
- πŸ§‘πŸ»β€πŸ’» Implement DB migrations with sqlx for the creation of the MySQL
test database.
- πŸ§‘πŸ»β€πŸ’» Implement ***SqlxPagination*** to generalize the implementation
of pagination methods using *sqlx*.
- πŸ§‘πŸ»β€πŸ’» Implement **SqlxPagination** for `QueryBuilder<MySQL>` to
paginate results from a SQL query into a `Page`. Only available on
***mysql-sqlx*** feature is enabled.
- πŸ§‘πŸ»β€πŸ’» Implement **SqlxPagination** for `QueryBuilder<Postgres>` to
paginate results from a SQL query into a `Page`. Only available on
***pg-sqlx*** feature is enabled.
- πŸ§‘πŸ»β€πŸ’» Implement integration test for pagination with ***mysql-sqlx***.


### Changed:

- πŸ”¨ Refactor of the pagination.rs module to create the
records_pagination.rs and sqlx_pagination.rs modules. Renaming the test
module test_sqlx_postgres_pagination.rs to test_sqlx_pagination.rs.

### Removed:

- ❌ Remove **From** **sqlx::Error** for `PaginationError`.

### Docs:

- πŸ“ Update project documentation.
  • Loading branch information
JMTamayo authored May 25, 2024
2 parents 2255ac4 + 55e86da commit 01179eb
Show file tree
Hide file tree
Showing 26 changed files with 1,211 additions and 453 deletions.
41 changes: 28 additions & 13 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,23 @@ jobs:

services:
postgres:
image: postgres:15
image: postgres:latest
env:
POSTGRES_USER: ${{ secrets.PG_DB_USER }}
POSTGRES_PASSWORD: ${{ secrets.PG_DB_PASSWORD }}
POSTGRES_DB: ${{ secrets.PG_DB_NAME }}
POSTGRES_USER: ${{ secrets.DB_USER }}
POSTGRES_PASSWORD: ${{ secrets.DB_PASSWORD }}
POSTGRES_DB: ${{ secrets.DB_NAME }}
ports:
- 5432:5432
- 5432:5432

mysql:
image: mysql:latest
env:
MYSQL_USER: ${{ secrets.DB_USER }}
MYSQL_PASSWORD: ${{ secrets.DB_PASSWORD }}
MYSQL_ROOT_PASSWORD: ${{ secrets.DB_PASSWORD }}
MYSQL_DATABASE: ${{ secrets.DB_NAME }}
ports:
- 3306:3306
steps:
- uses: actions/checkout@v2

Expand All @@ -34,12 +43,17 @@ jobs:
tool: cargo-llvm-cov

- name: Install SQLx CLI
run: cargo install sqlx-cli --no-default-features --features postgres
run: cargo install sqlx-cli --no-default-features --features postgres,mysql,sqlite

- name: Run migrations for Postgres
env:
DATABASE_URL: postgres://${{ secrets.PG_DB_USER }}:${{ secrets.PG_DB_PASSWORD }}@${{ secrets.PG_DB_HOST }}:${{ secrets.PG_DB_PORT }}/${{ secrets.PG_DB_NAME }}
run: sqlx migrate run --source page-hunter/tests/migrations/postgres
DATABASE_URL: postgres://${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.PG_DB_PORT }}/${{ secrets.DB_NAME }}
run: sqlx migrate run --source page-hunter/tests/migrations/postgres

- name: Run migrations for MySQL
env:
DATABASE_URL: mysql://${{ secrets.DB_USER }}:${{ secrets.DB_PASSWORD }}@${{ secrets.DB_HOST }}:${{ secrets.MYSQL_DB_PORT }}/${{ secrets.DB_NAME }}
run: sqlx migrate run --source page-hunter/tests/migrations/mysql

- name: Check project
uses: actions-rs/cargo@v1
Expand All @@ -48,15 +62,16 @@ jobs:
args: --all-features

- name: Check formatting
run: cargo fmt --all -- --check
run: cargo fmt --all --check

- name: Run tests with coverage for all features
env:
PG_DB_USER: ${{ secrets.PG_DB_USER }}
PG_DB_PASSWORD: ${{ secrets.PG_DB_PASSWORD }}
PG_DB_NAME: ${{ secrets.PG_DB_NAME }}
PG_DB_HOST: ${{ secrets.PG_DB_HOST }}
DB_USER: ${{ secrets.DB_USER }}
DB_PASSWORD: ${{ secrets.DB_PASSWORD }}
DB_NAME: ${{ secrets.DB_NAME }}
DB_HOST: ${{ secrets.DB_HOST }}
PG_DB_PORT: ${{ secrets.PG_DB_PORT }}
MYSQL_DB_PORT: ${{ secrets.MYSQL_DB_PORT }}
run: cargo llvm-cov --workspace --all-features --codecov --output-path codecov.json

- name: Upload coverage to Codecov
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,7 @@ venv/
.venv/

# macOS
.DS_Store
.DS_Store

# Env files
*.env
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,33 @@
pg-db-docker:
docker run --name postgres-db -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=test -e POSTGRES_USER=test -p 5432:5432 -d postgres

mysql-db-docker:
docker run --name mysql-db -e MYSQL_ROOT_PASSWORD=docker -e MYSQL_DATABASE=test -e MYSQL_USER=test -e MYSQL_PASSWORD=docker -p 3306:3306 -d mysql

install-sqlx-cli:
cargo install sqlx-cli --no-default-features --features postgres,mysql,sqlite

install-tarpaulin:
cargo install cargo-tarpaulin

install-llvm-cov:
cargo install cargo-llvm-cov

run-postgres-migrations:
sqlx migrate run --source page-hunter/tests/migrations/postgres --database-url postgres://test:docker@localhost:5432/test

revert-postgres-migrations:
sqlx migrate revert --source page-hunter/tests/migrations/postgres --database-url postgres://test:docker@localhost:5432/test

run-mysql-migrations:
sqlx migrate run --source page-hunter/tests/migrations/mysql --database-url mysql://test:docker@localhost:3306/test

revert-mysql-migrations:
sqlx migrate revert --source page-hunter/tests/migrations/mysql --database-url mysql://test:docker@localhost:3306/test

fmt-check:
cargo fmt --all --check

fmt:
cargo fmt --all

Expand Down
86 changes: 68 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ To use **page-hunter** from GitHub repository with specific version, set the dep

```ini
[dependencies]
page-hunter = {git = "https://github.com/JMTamayo/page-hunter.git", version = "0.1.2", features = ["serde"] }
page-hunter = {git = "https://github.com/JMTamayo/page-hunter.git", version = "0.1.3", features = ["serde"] }
```

You can depend on it via cargo by adding the following dependency to your `Cargo.toml` file:

```ini
[dependencies]
page-hunter = { version = "0.1.2", features = ["serde", "pg-sqlx"] }
page-hunter = { version = "0.1.3", features = ["serde", "pg-sqlx"] }
```

## CRATE FEATURES
- `serde`: Add [Serialize](https://docs.rs/serde/1.0.197/serde/trait.Serialize.html) and [Deserialize](https://docs.rs/serde/1.0.197/serde/trait.Deserialize.html) support for `Page` and `Book` based on crate [serde](https://crates.io/crates/serde/1.0.197). This feature is useful for implementing pagination models as a request or response body in REST APIs, among other implementations.
- `pg-sqlx`: Add support for pagination with [SQLx](https://crates.io/crates/sqlx) crate for PostgreSQL database. This feature is useful for paginating records from a PostgreSQL database.
- `serde`: Add [Serialize](https://docs.rs/serde/1.0.197/serde/trait.Serialize.html) and [Deserialize](https://docs.rs/serde/1.0.197/serde/trait.Deserialize.html) support for `Page` and `Book` based on [serde](https://crates.io/crates/serde/1.0.197). This feature is useful for implementing pagination models as a request or response body in REST APIs, among other implementations.
- `pg-sqlx`: Add support for pagination with [SQLx](https://crates.io/crates/sqlx) for PostgreSQL database.
- `mysql-sqlx`: Add support for pagination with [SQLx](https://crates.io/crates/sqlx) for MySQL database.

## BASIC OPERATION
The **page-hunter** library provides two main models to manage pagination:
Expand Down Expand Up @@ -151,7 +152,7 @@ On feature `serde` enabled, you can serialize and deserialize a `Book` as follow
```

#### Paginate records from a PostgreSQL database with SQLx:
If you need to paginate records from a PostgreSQL database using the [SQLx](https://crates.io/crates/sqlx) crate:
To paginate records from a PostgreSQL database:
```rust,no_run
use page_hunter::*;
use sqlx::postgres::{PgPool, Postgres};
Expand Down Expand Up @@ -180,7 +181,39 @@ If you need to paginate records from a PostgreSQL database using the [SQLx](http
query.paginate(&pool, 0, 10).await.unwrap_or_else(|error| {
panic!("Error paginating records: {:?}", error);
});
}
}
```

To paginate records from a MySQL database:
```rust,no_run
use page_hunter::*;
use sqlx::mysql::{MySqlPool, MySql};
use sqlx::{FromRow, QueryBuilder};
use uuid::Uuid;
#[tokio::main]
async fn main() {
#[derive(Clone, Debug, FromRow)]
pub struct Country {
id: Uuid,
name: String,
}
let pool: MySqlPool = MySqlPool::connect(
"mysql://username:password@localhost/db"
).await.unwrap_or_else(|error| {
panic!("Error connecting to database: {:?}", error);
});
let query: QueryBuilder<MySql> = QueryBuilder::new(
"SELECT * FROM countries"
);
let page: Page<Country> =
query.paginate(&pool, 0, 10).await.unwrap_or_else(|error| {
panic!("Error paginating records: {:?}", error);
});
}
```

## DEVELOPMENT
Expand All @@ -189,36 +222,53 @@ To test `page-hunter`, follow these recommendations:
#### Set env variables:
Create `local.env` file at workspace folder to store the required environment variables
```text
PG_DB_HOST=localhost
DB_HOST=localhost
DB_USER=test
DB_PASSWORD=docker
DB_NAME=test
PG_DB_PORT=5432
PG_DB_USER=test
PG_DB_PASSWORD=docker
PG_DB_NAME=test
MYSQL_DB_PORT=3306
```

#### Setup databases:
Run databases as Docker containers using the following commands:

##### Postgres SQL:
```bash
docker run --name postgres-db -e POSTGRES_PASSWORD=docker -e POSTGRES_DB=test -e POSTGRES_USER=test -p 5432:5432 postgres
make pg-db-docker
```

##### MySQL:
```bash
make mysql-db-docker
```

#### Run database migrations:
- Install sqlx-cli:
```bash
make install-sqlx-cli
```

##### Postgres SQL
- Install sqlx-cli for postgres:
- Run migrations:
```bash
cargo install sqlx-cli --no-default-features --features postgres
make run-postgres-migrations
```

- Export DATABASE_URL:
- Revert migrations:
```bash
export DATABASE_URL=postgres://test:docker@localhost:5432/test
make revert-postgres-migrations
```

##### MySQL
- Run migrations:
```bash
sqlx migrate run --source page-hunter/tests/migrations/postgres
make run-mysql-migrations
```

- Revert migrations:
```bash
make revert-mysql-migrations
```

#### To test:
Expand All @@ -229,7 +279,7 @@ Run databases as Docker containers using the following commands:
#### To test using tarpaulin:
- Install cargo-tarpaulin:
```bash
cargo install cargo-tarpaulin
make install-tarpaulin
```

- Run tests:
Expand All @@ -240,7 +290,7 @@ Run databases as Docker containers using the following commands:
#### To test using llvm-cov:
- Install llvm-cov:
```bash
cargo install cargo-llvm-cov
make install-llvm-cov
```

- Run tests:
Expand Down
76 changes: 50 additions & 26 deletions page-hunter/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,61 +5,85 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## πŸš€ v0.1.3 [2024-05-24]

### Added:

- πŸ§‘πŸ»β€πŸ’» Implement `ErrorKind::FromRowError`.
- πŸ§‘πŸ»β€πŸ’» Implement DB migrations with sqlx for the creation of the MySQL test database.
- πŸ§‘πŸ»β€πŸ’» Implement ***SqlxPagination*** to generalize the implementation of pagination methods using *sqlx*.
- πŸ§‘πŸ»β€πŸ’» Implement **SqlxPagination** for `QueryBuilder<MySQL>` to paginate results from a SQL query into a `Page`. Only available on ***mysql-sqlx*** feature is enabled.
- πŸ§‘πŸ»β€πŸ’» Implement **SqlxPagination** for `QueryBuilder<Postgres>` to paginate results from a SQL query into a `Page`. Only available on ***pg-sqlx*** feature is enabled.
- πŸ§‘πŸ»β€πŸ’» Implement integration test for pagination with ***mysql-sqlx***.


### Changed:

- πŸ”¨ Refactor of the pagination.rs module to create the records_pagination.rs and sqlx_pagination.rs modules. Renaming the test module test_sqlx_postgres_pagination.rs to test_sqlx_pagination.rs.

### Removed:

- ❌ Remove **From** **sqlx::Error** for `PaginationError`.

### Docs:

- πŸ“ Update project documentation.

## πŸš€ v0.1.2 [2024-05-22]

### Added:

- πŸ§‘πŸ»β€πŸ’» Implementation of `ErrorKind::DatabaseError`.
- πŸ§‘πŸ»β€πŸ’» Implementation of **From** **sqlx::Error** for `PaginationError`.
- πŸ§‘πŸ»β€πŸ’» Implement `ErrorKind::DatabaseError`.
- πŸ§‘πŸ»β€πŸ’» Implement **From** **sqlx::Error** for `PaginationError`.
- πŸ§‘πŸ»β€πŸ’» Paginate results from a SQL query into a `Page` from a PostgreSQL database using *sqlx*. Implementation of the **PgSqlxPagination** for `QueryBuilder`. Only available on ***pg-sqlx*** feature is enabled.
- πŸ§‘πŸ»β€πŸ’» Including unitary test for the **Debug** implementation for `Book`.
- πŸ§‘πŸ»β€πŸ’» Including checking project format in ci.yml.
- πŸ§‘πŸ»β€πŸ’» Implementation of DB migrations with sqlx for the creation of the postgres test database.
- πŸ§‘πŸ»β€πŸ’» Implementation of integration test for pagination with pg-sqlx in CI.
- πŸ§‘πŸ»β€πŸ’» Include unitary test for the **Debug** implementation for `Book`.
- πŸ§‘πŸ»β€πŸ’» Include checking project format in ci.yml.
- πŸ§‘πŸ»β€πŸ’» Implement DB migrations with *sqlx* for the creation of the postgres test database.
- πŸ§‘πŸ»β€πŸ’» Implement integration tests for pagination with ***pg-sqlx***.

### Changed:

- πŸ”¨ Changing the implementation of **Clone** and **Debug** using derive to implement directly in `ErrorKind` and `PaginationError`.
- πŸ”¨ Changing the implementation of **Clone** and **Debug** using derive to implement directly in `Page` and `Book`.
- πŸ”¨ Updating unit tests to get 100% coverage on the errors module.
- πŸ”¨ Change the implementation of **Clone** and **Debug** using derive to implement directly in `ErrorKind` and `PaginationError`.
- πŸ”¨ Change the implementation of **Clone** and **Debug** using derive to implement directly in `Page` and `Book`.
- πŸ”¨ Update unit tests to get 100% coverage on the errors module.

### Docs:

- πŸ“ Updating project documentation to include new implementations, utilities and development section.
- πŸ“ Update project documentation to include new implementations, utilities and development section.

## πŸš€ v0.1.1 [2024-05-18]

### Added:

- πŸ§‘πŸ»β€πŸ’» Implementation of `Book` model for binding uses.
- πŸ§‘πŸ»β€πŸ’» Implementation of **Default**, **Clone**, **Debug**, **Display** and **IntoIterator** in `Book` model for default feature.
- πŸ§‘πŸ»β€πŸ’» Implementation of **Serialize** and **Deserialize** in `Book` model for feature ***serde***.
- πŸ§‘πŸ»β€πŸ’» Implementation of `bind_records()` function for binding uses.
- πŸ§‘πŸ»β€πŸ’» Implementation of unitary tests for `bind_records()` and `Book` model.
- πŸ§‘πŸ»β€πŸ’» Implement `Book` model for binding uses.
- πŸ§‘πŸ»β€πŸ’» Implement **Default**, **Clone**, **Debug**, **Display** and **IntoIterator** in `Book` model for default feature.
- πŸ§‘πŸ»β€πŸ’» Implement **Serialize** and **Deserialize** in `Book` model for feature ***serde***.
- πŸ§‘πŸ»β€πŸ’» Implement `bind_records()` function for binding uses.
- πŸ§‘πŸ»β€πŸ’» Implement unitary tests for `bind_records()` and `Book` model.
- πŸ§‘πŸ»β€πŸ’» Codecov implementation to verify 90% of coverage in unit tests.

### Changed:

- πŸ”¨ Changing the implementation of **Serialize** trait using derive to implement directly in `Page` and `Book` models.
- πŸ”¨ Change the implementation of **Serialize** trait using derive to implement directly in `Page` and `Book` models.

### Docs:

- πŸ“ Including badges in README.md
- πŸ“ Fixing minor typos from documentation in traits implementation.
- πŸ“ Include badges in README.md
- πŸ“ Fix minor typos from documentation in traits implementation.


## πŸš€ v0.1.0 [2024-05-15]

### Added:

- πŸ§‘πŸ»β€πŸ’» Implementation of `Page` model for pagination uses.
- πŸ§‘πŸ»β€πŸ’» Implementation of **Default**, **Clone**, **Debug**, **Display** and **IntoIterator** in `Page` model for default feature.
- πŸ§‘πŸ»β€πŸ’» Implementation of **Serialize** and **Deserialize** in `Page` model for feature ***serde***.
- πŸ§‘πŸ»β€πŸ’» Implementation of `paginate_records()` function for record pagination.
- πŸ§‘πŸ»β€πŸ’» Implementation of unitary tests for `paginate_records()` and `Page` model.
- πŸ§‘πŸ»β€πŸ’» Implementation of CI workflow with GitHub actions to verify unit testing.
- πŸ§‘πŸ»β€πŸ’» Implement `Page` model for pagination uses.
- πŸ§‘πŸ»β€πŸ’» Implementat **Default**, **Clone**, **Debug**, **Display** and **IntoIterator** in `Page` model for default feature.
- πŸ§‘πŸ»β€πŸ’» Implement **Serialize** and **Deserialize** in `Page` model for feature ***serde***.
- πŸ§‘πŸ»β€πŸ’» Implement `paginate_records()` function for record pagination.
- πŸ§‘πŸ»β€πŸ’» Implement unitary tests for `paginate_records()` and `Page` model.
- πŸ§‘πŸ»β€πŸ’» Implement CI workflow with GitHub actions to verify unit testing.

### Docs:

- πŸ“ Implementation of project documentation in README.md file, functions, methods, implementations, models and library.
- πŸ“ Implementation of CHANGELOG.md file.
- πŸ“ Implement project documentation in README.md file, functions, methods, implementations, models and library.
- πŸ“ Implement CHANGELOG.md file.
Loading

0 comments on commit 01179eb

Please sign in to comment.