Skip to content

Commit

Permalink
rework encoding Uuid for mysql
Browse files Browse the repository at this point in the history
  • Loading branch information
pxp9 committed Apr 17, 2024
1 parent ea85604 commit 94fd332
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*/

CREATE TABLE fang_tasks (
id VARCHAR(36) DEFAULT (uuid()) PRIMARY KEY,
id BINARY(16) PRIMARY KEY,
metadata VARCHAR(2048) NOT NULL,
error_message VARCHAR(2048),
state ENUM('new', 'in_progress', 'failed', 'finished', 'retried') NOT NULL DEFAULT 'new',
Expand Down
47 changes: 6 additions & 41 deletions fang/src/asynk/backend_sqlx/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ pub(super) struct BackendSqlXMySQL {}

impl<'a> FromRow<'a, MySqlRow> for Task {
fn from_row(row: &'a MySqlRow) -> Result<Self, sqlx::Error> {
let uuid_as_text: &str = row.get("id");

let id = Uuid::parse_str(uuid_as_text).unwrap();
let id: Uuid = row.get("id");

let raw: &str = row.get("metadata"); // will work if database cast json to string
let raw = raw.replace('\\', "");
Expand Down Expand Up @@ -87,8 +85,6 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {
params: QueryParams<'_>,
) -> Result<Task, AsyncQueueError> {
let uuid = Uuid::new_v4();
let mut buffer = Uuid::encode_buffer();
let uuid_as_str: &str = uuid.as_hyphenated().encode_lower(&mut buffer);

let scheduled_at = params.scheduled_at.unwrap();

Expand All @@ -97,7 +93,7 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {

let affected_rows = Into::<MySqlQueryResult>::into(
sqlx::query(query)
.bind(uuid_as_str)
.bind(&uuid)
.bind(metadata_str)
.bind(task_type)
.bind(scheduled_at)
Expand Down Expand Up @@ -136,14 +132,11 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {

let uuid = params.uuid.unwrap();

let mut buffer = Uuid::encode_buffer();
let uuid_as_text = uuid.as_hyphenated().encode_lower(&mut buffer);

let affected_rows = Into::<MySqlQueryResult>::into(
sqlx::query(query)
.bind(state_str)
.bind(updated_at)
.bind(&*uuid_as_text)
.bind(&uuid)
.execute(pool)
.await?,
)
Expand Down Expand Up @@ -174,8 +167,6 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {
params: QueryParams<'_>,
) -> Result<Task, AsyncQueueError> {
let uuid = Uuid::new_v4();
let mut buffer = Uuid::encode_buffer();
let uuid_as_str: &str = uuid.as_hyphenated().encode_lower(&mut buffer);

let metadata = params.metadata.unwrap();

Expand All @@ -189,7 +180,7 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {

let affected_rows = Into::<MySqlQueryResult>::into(
sqlx::query(query)
.bind(uuid_as_str)
.bind(&uuid)
.bind(metadata_str)
.bind(task_type)
.bind(uniq_hash)
Expand Down Expand Up @@ -227,17 +218,14 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {

let id = params.task.unwrap().id;

let mut buffer = Uuid::encode_buffer();
let uuid_as_text = id.as_hyphenated().encode_lower(&mut buffer);

let error_message = params.error_message.unwrap();

let affected_rows = Into::<MySqlQueryResult>::into(
sqlx::query(query)
.bind(<&str>::from(FangTaskState::Failed))
.bind(error_message)
.bind(updated_at)
.bind(&*uuid_as_text)
.bind(&id)
.execute(pool)
.await?,
)
Expand Down Expand Up @@ -275,9 +263,6 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {

let uuid = params.task.unwrap().id;

let mut buffer = Uuid::encode_buffer();
let uuid_as_text = uuid.as_hyphenated().encode_lower(&mut buffer);

let error = params.error_message.unwrap();

let affected_rows = Into::<MySqlQueryResult>::into(
Expand All @@ -286,7 +271,7 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {
.bind(retries)
.bind(scheduled_at)
.bind(now)
.bind(&*uuid_as_text)
.bind(&uuid)
.execute(pool)
.await?,
)
Expand Down Expand Up @@ -330,26 +315,6 @@ impl FangQueryable<MySql> for BackendSqlXMySQL {
}
}
}

async fn find_task_by_id(
query: &str,
pool: &Pool<MySql>,
params: QueryParams<'_>,
) -> Result<Task, AsyncQueueError> {
let mut buffer = Uuid::encode_buffer();
let uuid_as_text = params
.uuid
.unwrap()
.as_hyphenated()
.encode_lower(&mut buffer);

let task: Task = sqlx::query_as(query)
.bind(&*uuid_as_text)
.fetch_one(pool)
.await?;

Ok(task)
}
}

impl BackendSqlXMySQL {
Expand Down
4 changes: 2 additions & 2 deletions fang/src/blocking/mysql_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ diesel::table! {
use super::sql_types::FangTasksStateEnum;

fang_tasks (id) {
#[max_length = 36]
id -> Varchar,
#[max_length = 16]
id -> Binary,
#[max_length = 2048]
metadata -> Varchar,
#[max_length = 2048]
Expand Down

0 comments on commit 94fd332

Please sign in to comment.