Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlx-migrations #199

Merged
merged 7 commits into from
Apr 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion trunk/registry/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
FROM rust:1.68.0 as builder
COPY sqlx-data.json Cargo.toml Cargo.lock ./
COPY src ./src
COPY migrations ./migrations/
COPY registry-s3 ./registry-s3
RUN cargo build && \
RUN cargo build --release && \
cargo clean -p trunk-registry
RUN cargo install --path .

Expand Down
16 changes: 16 additions & 0 deletions trunk/registry/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
DATABASE_URL := "postgresql://postgres:postgres@0.0.0.0:5432/postgres"

format:
cargo +nightly fmt --all
DATABASE_URL=${DATABASE_URL} cargo clippy
cargo sqlx prepare

run-migrations:
sqlx migrate run

run:
RUST_LOG=debug \
DATABASE_URL=${DATABASE_URL} \
RUST_BACKTRACE=full \
RUST_LOG=debug \
cargo run

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE extensions (
CREATE TABLE IF NOT EXISTS extensions (
id BIGSERIAL PRIMARY KEY,
name VARCHAR,
updated_at TIMESTAMP,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
CREATE TABLE versions (
CREATE TABLE IF NOT EXISTS versions (
id BIGSERIAL PRIMARY KEY,
extension_id INT4,
num VARCHAR,
Expand Down
4 changes: 4 additions & 0 deletions trunk/registry/migrations/20230402002617_versions.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ALTER TABLE versions
ADD CONSTRAINT fk_extension_id
FOREIGN KEY (extension_id)
REFERENCES extensions(id);
14 changes: 13 additions & 1 deletion trunk/registry/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
use actix_cors::Cors;
use actix_web::{web, App, HttpServer};
use trunk_registry::connect;
use trunk_registry::{config, download, publish, routes};

#[actix_web::main]
async fn main() -> std::io::Result<()> {
env_logger::init();
// load configurations from environment
let cfg = config::Config::default();

let conn = connect(&cfg.database_url)
.await
.expect("error connecting to database");

// run database migrations
sqlx::migrate!()
.run(&conn)
.await
.expect("error running migrations");
Comment on lines +12 to +20
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


HttpServer::new(move || {
let cors = Cors::permissive();
App::new()
.wrap(cors)
.app_data(web::Data::new(cfg.clone()))
.app_data(web::Data::new(conn.clone()))
.service(routes::running)
.service(routes::get_all_extensions)
.service(publish::publish)
Expand Down
8 changes: 2 additions & 6 deletions trunk/registry/src/publish.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Functionality related to publishing a new extension or version of an extension.

use crate::config::Config;
use crate::connect;
use crate::errors::ExtensionRegistryError;
use crate::views::extension_publish::ExtensionUpload;
use actix_web::{error, post, web, HttpResponse};
use futures::StreamExt;
use sqlx::{Pool, Postgres};

const MAX_SIZE: usize = 262_144; // max payload size is 256k

Expand All @@ -15,7 +14,7 @@ const MAX_SIZE: usize = 262_144; // max payload size is 256k

#[post("/extensions/new")]
pub async fn publish(
cfg: web::Data<Config>,
conn: web::Data<Pool<Postgres>>,
mut payload: web::Payload,
) -> Result<HttpResponse, ExtensionRegistryError> {
// Get request body
Expand All @@ -34,9 +33,6 @@ pub async fn publish(
// Deserialize body
let new_extension = serde_json::from_slice::<ExtensionUpload>(&body)?;

// Set database conn
let conn = connect(&cfg.database_url).await?;

// Create a transaction on the database, if there are no errors,
// commit the transactions to record a new or updated extension.
let mut tx = conn.begin().await?;
Expand Down
8 changes: 3 additions & 5 deletions trunk/registry/src/routes.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::config::Config;
use crate::connect;
use crate::errors::ExtensionRegistryError;
use actix_web::{get, web, HttpResponse, Responder};
use serde_json::{json, Value};
use sqlx::{Pool, Postgres};

#[get("/")]
pub async fn running() -> impl Responder {
Expand All @@ -11,11 +10,10 @@ pub async fn running() -> impl Responder {

#[get("/extensions/all")]
pub async fn get_all_extensions(
cfg: web::Data<Config>,
conn: web::Data<Pool<Postgres>>,
) -> Result<HttpResponse, ExtensionRegistryError> {
let mut extensions: Vec<Value> = Vec::new();
// Set database conn
let conn = connect(&cfg.database_url).await?;

// Create a transaction on the database, if there are no errors,
// commit the transactions to record a new or updated extension.
let mut tx = conn.begin().await?;
Expand Down
38 changes: 0 additions & 38 deletions trunk/registry/src/schema.rs

This file was deleted.