diff --git a/fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql b/fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql index 50912b7..2fce8fb 100644 --- a/fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql +++ b/fang/mysql_migrations/migrations/2023-08-17-102017_create_fang_tasks/up.sql @@ -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', diff --git a/fang/src/asynk/backend_sqlx/mysql.rs b/fang/src/asynk/backend_sqlx/mysql.rs index 32a2d21..f787081 100644 --- a/fang/src/asynk/backend_sqlx/mysql.rs +++ b/fang/src/asynk/backend_sqlx/mysql.rs @@ -35,9 +35,7 @@ pub(super) struct BackendSqlXMySQL {} impl<'a> FromRow<'a, MySqlRow> for Task { fn from_row(row: &'a MySqlRow) -> Result { - 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('\\', ""); @@ -87,8 +85,6 @@ impl FangQueryable for BackendSqlXMySQL { params: QueryParams<'_>, ) -> Result { 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(); @@ -97,7 +93,7 @@ impl FangQueryable for BackendSqlXMySQL { let affected_rows = Into::::into( sqlx::query(query) - .bind(uuid_as_str) + .bind(&uuid) .bind(metadata_str) .bind(task_type) .bind(scheduled_at) @@ -136,14 +132,11 @@ impl FangQueryable 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::::into( sqlx::query(query) .bind(state_str) .bind(updated_at) - .bind(&*uuid_as_text) + .bind(&uuid) .execute(pool) .await?, ) @@ -174,8 +167,6 @@ impl FangQueryable for BackendSqlXMySQL { params: QueryParams<'_>, ) -> Result { 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(); @@ -189,7 +180,7 @@ impl FangQueryable for BackendSqlXMySQL { let affected_rows = Into::::into( sqlx::query(query) - .bind(uuid_as_str) + .bind(&uuid) .bind(metadata_str) .bind(task_type) .bind(uniq_hash) @@ -227,9 +218,6 @@ impl FangQueryable 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::::into( @@ -237,7 +225,7 @@ impl FangQueryable for BackendSqlXMySQL { .bind(<&str>::from(FangTaskState::Failed)) .bind(error_message) .bind(updated_at) - .bind(&*uuid_as_text) + .bind(&id) .execute(pool) .await?, ) @@ -275,9 +263,6 @@ impl FangQueryable 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::::into( @@ -286,7 +271,7 @@ impl FangQueryable for BackendSqlXMySQL { .bind(retries) .bind(scheduled_at) .bind(now) - .bind(&*uuid_as_text) + .bind(&uuid) .execute(pool) .await?, ) @@ -330,26 +315,6 @@ impl FangQueryable for BackendSqlXMySQL { } } } - - async fn find_task_by_id( - query: &str, - pool: &Pool, - params: QueryParams<'_>, - ) -> Result { - 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 { diff --git a/fang/src/blocking/mysql_schema.rs b/fang/src/blocking/mysql_schema.rs index c8a9d60..e510d8a 100644 --- a/fang/src/blocking/mysql_schema.rs +++ b/fang/src/blocking/mysql_schema.rs @@ -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]