Skip to content

Commit

Permalink
Fixing CI and Add DB Migrations
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony M. Bonafide <AnthonyMBonafide@gmail.com>
  • Loading branch information
AnthonyMBonafide committed Mar 22, 2024
1 parent 9f43209 commit 46a65a8
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 25 deletions.
1 change: 1 addition & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DATABASE_URL="postgres://postgres:password@127.0.0.1:5432/newsletter"
27 changes: 27 additions & 0 deletions .github/workflows/msrv.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@ on: [push, pull_request]
name: MSRV

jobs:
# Label of the container job
container-job:
# Containers must run in Linux based operating systems
runs-on: ubuntu-latest
# Docker Hub image that `container-job` executes in
container: node:10.18-jessie

# Service containers to run with `container-job`
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: passowrd
POSTGRES_USERNAME: postgres
POSTGRES_DB: newsletter
# Set health checks to wait until postgres has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
check:
name: Check
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion migrations/20240321030443_create_subscription_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
CREATE TABLE subscriptions(
id UUID NOT NULL,
PRIMARY KEY(id),
emil TEXT NOT NULL UNIQUE,
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL,
subscribed_at TIMESTAMPTZ NOT NULL
);
27 changes: 16 additions & 11 deletions src/configuration.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
#[derive(serde::Deserialize)]
pub struct Settings{
pub database: DatabaseSettings,
pub struct Settings {
pub database: DatabaseSettings,
pub application_port: u16,
}

#[derive(serde::Deserialize)]
pub struct DatabaseSettings{
pub struct DatabaseSettings {
pub username: String,
pub password:String,
pub password: String,
pub port: u16,
pub host: String,
pub database_name:String,
pub database_name: String,
}

impl DatabaseSettings {
pub fn connection_string(&self) -> String {
format!("postgres://{}:{}@{}:{}/{}", self.username, self.password, self.host, self.password, self.database_name)
pub fn connection_string(&self) -> String {
format!(
"postgres://{}:{}@{}:{}/{}",
self.username, self.password, self.host, self.port, self.database_name
)
}
}


pub fn get_configuration() -> Result<Settings, config::ConfigError>{
pub fn get_configuration() -> Result<Settings, config::ConfigError> {
let settings = config::Config::builder()
.add_source(config::File::new("configuration.yaml", config::FileFormat::Yaml))
.build()?;
.add_source(config::File::new(
"configuration.yaml",
config::FileFormat::Yaml,
))
.build()?;

settings.try_deserialize()
}
4 changes: 1 addition & 3 deletions src/routes/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

use actix_web::HttpResponse;

pub async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}


2 changes: 0 additions & 2 deletions src/routes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

mod health_check;
mod subscriptions;

pub use health_check::*;
pub use subscriptions::*;

5 changes: 1 addition & 4 deletions src/routes/subscriptions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::net::TcpListener;
use actix_web::{web, HttpResponse};

use actix_web::{dev::Server, web, App, HttpResponse, HttpServer};
#[derive(serde::Deserialize, serde::Serialize)]
pub struct FormData {
name: String,
Expand All @@ -10,5 +9,3 @@ pub struct FormData {
pub async fn subscribe(_form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}


4 changes: 2 additions & 2 deletions src/startup.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::net::TcpListener;
use actix_web::{dev::Server, web, App, HttpServer};
use crate::routes::{health_check, subscribe};
use actix_web::{dev::Server, web, App, HttpServer};
use std::net::TcpListener;

pub fn run(tcp_listener: TcpListener) -> Result<Server, std::io::Error> {
let server = HttpServer::new(|| {
Expand Down
19 changes: 17 additions & 2 deletions tests/health_check.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use sqlx::{Connection, PgConnection, Row};
use std::net::TcpListener;

use zero2prod::run;
use zero2prod::{get_configuration, run};

#[tokio::test]
async fn health_check_works() {
Expand Down Expand Up @@ -32,8 +33,22 @@ async fn subscribe_returns_200_for_valid_form_data() {
.await
.expect("Failed to execute request");

assert_eq!(200, response.status().as_u16())
assert_eq!(200, response.status().as_u16());

let config = get_configuration().expect("Failed to get configuration");
let mut connection = PgConnection::connect(&config.database.connection_string())
.await
.expect("Failed to connect to database");

let r = sqlx::query!("SELECT id, name, email, subscribed_at FROM subscriptions")
.fetch_one(&mut connection)
.await
.expect("Failed to execute query");

assert_eq!("le guin", r.name);
assert_eq!("le_guin@gmail.com", r.email);
}

#[tokio::test]
async fn subscribe_returns_400_for_missing_form_data() {
let address = spawn_app();
Expand Down

0 comments on commit 46a65a8

Please sign in to comment.