From db421a5b206573334f2af8a755228fce29379bf5 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Thu, 14 Dec 2023 10:59:08 +0800 Subject: [PATCH] refactor: extract migrations into directory (#1035) --- Cargo.lock | 21 ++++++ ee/tabby-webserver/Cargo.toml | 3 +- .../01-registration-token-table/down.sql | 1 + .../01-registration-token-table/up.sql | 7 ++ .../migrations/02-users-table/down.sql | 1 + .../migrations/02-users-table/up.sql | 12 ++++ .../migrations/03-invitations-table/down.sql | 1 + .../migrations/03-invitations-table/up.sql | 8 +++ .../migrations/04-refresh-tokens/down.sql | 1 + .../migrations/04-refresh-tokens/up.sql | 8 +++ ee/tabby-webserver/src/service/db/mod.rs | 65 ++----------------- 11 files changed, 68 insertions(+), 60 deletions(-) create mode 100644 ee/tabby-webserver/migrations/01-registration-token-table/down.sql create mode 100644 ee/tabby-webserver/migrations/01-registration-token-table/up.sql create mode 100644 ee/tabby-webserver/migrations/02-users-table/down.sql create mode 100644 ee/tabby-webserver/migrations/02-users-table/up.sql create mode 100644 ee/tabby-webserver/migrations/03-invitations-table/down.sql create mode 100644 ee/tabby-webserver/migrations/03-invitations-table/up.sql create mode 100644 ee/tabby-webserver/migrations/04-refresh-tokens/down.sql create mode 100644 ee/tabby-webserver/migrations/04-refresh-tokens/up.sql diff --git a/Cargo.lock b/Cargo.lock index 9fd4dc01f1e2..dbfe0d9770a9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1577,6 +1577,25 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "include_dir" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "18762faeff7122e89e0857b02f7ce6fcc0d101d5e9ad2ad7846cc01d61b7f19e" +dependencies = [ + "include_dir_macros", +] + +[[package]] +name = "include_dir_macros" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b139284b5cf57ecfa712bcc66950bb635b31aff41c188e8a4cfc758eca374a3f" +dependencies = [ + "proc-macro2", + "quote", +] + [[package]] name = "indexmap" version = "1.9.3" @@ -3011,6 +3030,7 @@ version = "1.1.0-beta.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5767f8cb28e54d1ed745f072b72c6e68bfa6179fabb4cd15bdb8575858e301d" dependencies = [ + "include_dir", "log", "rusqlite", "tokio", @@ -3746,6 +3766,7 @@ dependencies = [ "chrono", "futures", "hyper", + "include_dir", "jsonwebtoken", "juniper", "juniper-axum", diff --git a/ee/tabby-webserver/Cargo.toml b/ee/tabby-webserver/Cargo.toml index 85e7a0d83e4b..41ab6101e10a 100644 --- a/ee/tabby-webserver/Cargo.toml +++ b/ee/tabby-webserver/Cargo.toml @@ -14,6 +14,7 @@ bincode = "1.3.3" chrono = "0.4" futures.workspace = true hyper = { workspace = true, features=["client"]} +include_dir = "0.7.3" jsonwebtoken = "9.1.0" juniper.workspace = true juniper-axum = { path = "../../crates/juniper-axum" } @@ -22,7 +23,7 @@ mime_guess = "2.0.4" pin-project = "1.1.3" rusqlite = { version = "0.30.0", features = ["bundled", "chrono"] } # `alpha-async-tokio-rusqlite` is only available from 1.1.0-alpha.2, will bump up version when it's stable -rusqlite_migration = { version = "1.1.0-beta.1", features = ["alpha-async-tokio-rusqlite"] } +rusqlite_migration = { version = "1.1.0-beta.1", features = ["alpha-async-tokio-rusqlite", "from-directory"] } rust-embed = "8.0.0" serde.workspace = true tabby-common = { path = "../../crates/tabby-common" } diff --git a/ee/tabby-webserver/migrations/01-registration-token-table/down.sql b/ee/tabby-webserver/migrations/01-registration-token-table/down.sql new file mode 100644 index 000000000000..a7291f659e9a --- /dev/null +++ b/ee/tabby-webserver/migrations/01-registration-token-table/down.sql @@ -0,0 +1 @@ +DROP TABLE registration_token; \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/01-registration-token-table/up.sql b/ee/tabby-webserver/migrations/01-registration-token-table/up.sql new file mode 100644 index 000000000000..0d8f8ba37600 --- /dev/null +++ b/ee/tabby-webserver/migrations/01-registration-token-table/up.sql @@ -0,0 +1,7 @@ +CREATE TABLE registration_token ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + token VARCHAR(255) NOT NULL, + created_at TIMESTAMP DEFAULT (DATETIME('now')), + updated_at TIMESTAMP DEFAULT (DATETIME('now')), + CONSTRAINT `idx_token` UNIQUE (`token`) +); \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/02-users-table/down.sql b/ee/tabby-webserver/migrations/02-users-table/down.sql new file mode 100644 index 000000000000..441087ad7e15 --- /dev/null +++ b/ee/tabby-webserver/migrations/02-users-table/down.sql @@ -0,0 +1 @@ +DROP TABLE users; \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/02-users-table/up.sql b/ee/tabby-webserver/migrations/02-users-table/up.sql new file mode 100644 index 000000000000..c2f30c93767b --- /dev/null +++ b/ee/tabby-webserver/migrations/02-users-table/up.sql @@ -0,0 +1,12 @@ +CREATE TABLE users ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + email VARCHAR(150) NOT NULL COLLATE NOCASE, + password_encrypted VARCHAR(128) NOT NULL, + is_admin BOOLEAN NOT NULL DEFAULT 0, + created_at TIMESTAMP DEFAULT (DATETIME('now')), + updated_at TIMESTAMP DEFAULT (DATETIME('now')), + auth_token VARCHAR(128) NOT NULL, + + CONSTRAINT `idx_email` UNIQUE (`email`) + CONSTRAINT `idx_auth_token` UNIQUE (`auth_token`) +); \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/03-invitations-table/down.sql b/ee/tabby-webserver/migrations/03-invitations-table/down.sql new file mode 100644 index 000000000000..af3776cf781c --- /dev/null +++ b/ee/tabby-webserver/migrations/03-invitations-table/down.sql @@ -0,0 +1 @@ +DROP TABLE invitations; \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/03-invitations-table/up.sql b/ee/tabby-webserver/migrations/03-invitations-table/up.sql new file mode 100644 index 000000000000..5dc20f3fb2e8 --- /dev/null +++ b/ee/tabby-webserver/migrations/03-invitations-table/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE invitations ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + email VARCHAR(150) NOT NULL COLLATE NOCASE, + code VARCHAR(36) NOT NULL, + created_at TIMESTAMP DEFAULT (DATETIME('now')), + CONSTRAINT `idx_email` UNIQUE (`email`) + CONSTRAINT `idx_code` UNIQUE (`code`) +); \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/04-refresh-tokens/down.sql b/ee/tabby-webserver/migrations/04-refresh-tokens/down.sql new file mode 100644 index 000000000000..b9cc2d0678df --- /dev/null +++ b/ee/tabby-webserver/migrations/04-refresh-tokens/down.sql @@ -0,0 +1 @@ +DROP TABLE refresh_tokens; \ No newline at end of file diff --git a/ee/tabby-webserver/migrations/04-refresh-tokens/up.sql b/ee/tabby-webserver/migrations/04-refresh-tokens/up.sql new file mode 100644 index 000000000000..c7cf9bf5f9b2 --- /dev/null +++ b/ee/tabby-webserver/migrations/04-refresh-tokens/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE refresh_tokens ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + user_id INTEGER NOT NULL, + token VARCHAR(255) NOT NULL COLLATE NOCASE, + expires_at TIMESTAMP NOT NULL, + created_at TIMESTAMP DEFAULT (DATETIME('now')), + CONSTRAINT `idx_token` UNIQUE (`token`) +); \ No newline at end of file diff --git a/ee/tabby-webserver/src/service/db/mod.rs b/ee/tabby-webserver/src/service/db/mod.rs index 6a4e38161c51..f8086b1a3f82 100644 --- a/ee/tabby-webserver/src/service/db/mod.rs +++ b/ee/tabby-webserver/src/service/db/mod.rs @@ -5,73 +5,20 @@ mod users; use std::{path::PathBuf, sync::Arc}; use anyhow::Result; +use include_dir::{include_dir, Dir}; use lazy_static::lazy_static; use rusqlite::params; -use rusqlite_migration::{AsyncMigrations, M}; +use rusqlite_migration::AsyncMigrations; use tabby_common::path::tabby_root; use tokio_rusqlite::Connection; use crate::service::cron::run_offline_job; +static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); + lazy_static! { - static ref MIGRATIONS: AsyncMigrations = AsyncMigrations::new(vec![ - M::up( - r#" - CREATE TABLE registration_token ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - token VARCHAR(255) NOT NULL, - created_at TIMESTAMP DEFAULT (DATETIME('now')), - updated_at TIMESTAMP DEFAULT (DATETIME('now')), - CONSTRAINT `idx_token` UNIQUE (`token`) - ); - "# - ) - .down("DROP TABLE registration_token"), - // ==== Above migrations released in 0.6.0 ==== - M::up( - r#" - CREATE TABLE users ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - email VARCHAR(150) NOT NULL COLLATE NOCASE, - password_encrypted VARCHAR(128) NOT NULL, - is_admin BOOLEAN NOT NULL DEFAULT 0, - created_at TIMESTAMP DEFAULT (DATETIME('now')), - updated_at TIMESTAMP DEFAULT (DATETIME('now')), - auth_token VARCHAR(128) NOT NULL, - - CONSTRAINT `idx_email` UNIQUE (`email`) - CONSTRAINT `idx_auth_token` UNIQUE (`auth_token`) - ); - "# - ) - .down("DROP TABLE users"), - M::up( - r#" - CREATE TABLE invitations ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - email VARCHAR(150) NOT NULL COLLATE NOCASE, - code VARCHAR(36) NOT NULL, - created_at TIMESTAMP DEFAULT (DATETIME('now')), - CONSTRAINT `idx_email` UNIQUE (`email`) - CONSTRAINT `idx_code` UNIQUE (`code`) - ); - "# - ) - .down("DROP TABLE invitations"), - M::up( - r#" - CREATE TABLE refresh_tokens ( - id INTEGER PRIMARY KEY AUTOINCREMENT, - user_id INTEGER NOT NULL, - token VARCHAR(255) NOT NULL COLLATE NOCASE, - expires_at TIMESTAMP NOT NULL, - created_at TIMESTAMP DEFAULT (DATETIME('now')), - CONSTRAINT `idx_token` UNIQUE (`token`) - ); - "# - ) - .down("DROP TABLE refresh_tokens"), - ]); + static ref MIGRATIONS: AsyncMigrations = + AsyncMigrations::from_directory(&MIGRATIONS_DIR).unwrap(); } async fn db_path() -> Result {