From 18bd7ce3ab3c0c539348384b841c204a033edaa5 Mon Sep 17 00:00:00 2001 From: William Desportes Date: Fri, 20 Sep 2024 00:07:01 +0200 Subject: [PATCH] Write migrations with Diesel --- snow-scanner/diesel.toml | 9 +++++ .../down.sql | 1 + .../up.sql | 11 ++++++ .../down.sql | 1 + .../up.sql | 11 ++++++ snow-scanner/src/schema.rs | 38 +++++++++++++++++++ 6 files changed, 71 insertions(+) create mode 100644 snow-scanner/diesel.toml create mode 100644 snow-scanner/migrations/2024-06-22-201500_create_scanners_table/down.sql create mode 100644 snow-scanner/migrations/2024-06-22-201500_create_scanners_table/up.sql create mode 100644 snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/down.sql create mode 100644 snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/up.sql create mode 100644 snow-scanner/src/schema.rs diff --git a/snow-scanner/diesel.toml b/snow-scanner/diesel.toml new file mode 100644 index 0000000..4006deb --- /dev/null +++ b/snow-scanner/diesel.toml @@ -0,0 +1,9 @@ +# For documentation on how to configure this file, +# see https://diesel.rs/guides/configuring-diesel-cli + +[print_schema] +file = "src/schema.rs" +custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] + +[migrations_directory] +dir = "/mnt/Dev/@wdes/security.wdes.eu/snow-scanner/migrations" diff --git a/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/down.sql b/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/down.sql new file mode 100644 index 0000000..385c331 --- /dev/null +++ b/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/down.sql @@ -0,0 +1 @@ +DROP TABLE `scanners`; diff --git a/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/up.sql b/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/up.sql new file mode 100644 index 0000000..33ffaf7 --- /dev/null +++ b/snow-scanner/migrations/2024-06-22-201500_create_scanners_table/up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS `scanners` ( + ip VARCHAR(255) NOT NULL, + ip_type TINYINT(1) UNSIGNED NOT NULL, + scanner_name VARCHAR(255) NOT NULL, + ip_ptr VARCHAR(255) NULL, + created_at DATETIME NOT NULL, + updated_at DATETIME NULL, + last_seen_at DATETIME NULL, + last_checked_at DATETIME NULL, + PRIMARY KEY (ip, ip_type) +); diff --git a/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/down.sql b/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/down.sql new file mode 100644 index 0000000..44e6112 --- /dev/null +++ b/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/down.sql @@ -0,0 +1 @@ +DROP TABLE `scan_tasks`; diff --git a/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/up.sql b/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/up.sql new file mode 100644 index 0000000..2d896a4 --- /dev/null +++ b/snow-scanner/migrations/2024-06-23-10600_create_scan_tasks_table/up.sql @@ -0,0 +1,11 @@ +CREATE TABLE IF NOT EXISTS `scan_tasks` ( + task_group_id VARCHAR(255) NOT NULL, + cidr VARCHAR(255) NOT NULL, + created_by_username VARCHAR(255) NOT NULL, + created_at DATETIME NOT NULL, + updated_at DATETIME NULL, + started_at DATETIME NULL, + still_processing_at DATETIME NULL, + ended_at DATETIME NULL, + PRIMARY KEY (task_group_id, cidr) +); diff --git a/snow-scanner/src/schema.rs b/snow-scanner/src/schema.rs new file mode 100644 index 0000000..a011a88 --- /dev/null +++ b/snow-scanner/src/schema.rs @@ -0,0 +1,38 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + scan_tasks (task_group_id, cidr) { + #[max_length = 255] + task_group_id -> Varchar, + #[max_length = 255] + cidr -> Varchar, + #[max_length = 255] + created_by_username -> Varchar, + created_at -> Datetime, + updated_at -> Nullable, + started_at -> Nullable, + still_processing_at -> Nullable, + ended_at -> Nullable, + } +} + +diesel::table! { + scanners (ip, ip_type) { + #[max_length = 255] + ip -> Varchar, + ip_type -> Unsigned, + #[max_length = 255] + scanner_name -> Varchar, + #[max_length = 255] + ip_ptr -> Nullable, + created_at -> Datetime, + updated_at -> Nullable, + last_seen_at -> Nullable, + last_checked_at -> Nullable, + } +} + +diesel::allow_tables_to_appear_in_same_query!( + scan_tasks, + scanners, +);