Skip to content

Commit

Permalink
refactor: extract migrations into directory (#1035)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsxiaoys authored Dec 14, 2023
1 parent c0269e0 commit db421a5
Show file tree
Hide file tree
Showing 11 changed files with 68 additions and 60 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE registration_token;
Original file line number Diff line number Diff line change
@@ -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`)
);
1 change: 1 addition & 0 deletions ee/tabby-webserver/migrations/02-users-table/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE users;
12 changes: 12 additions & 0 deletions ee/tabby-webserver/migrations/02-users-table/up.sql
Original file line number Diff line number Diff line change
@@ -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`)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE invitations;
8 changes: 8 additions & 0 deletions ee/tabby-webserver/migrations/03-invitations-table/up.sql
Original file line number Diff line number Diff line change
@@ -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`)
);
1 change: 1 addition & 0 deletions ee/tabby-webserver/migrations/04-refresh-tokens/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE refresh_tokens;
8 changes: 8 additions & 0 deletions ee/tabby-webserver/migrations/04-refresh-tokens/up.sql
Original file line number Diff line number Diff line change
@@ -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`)
);
65 changes: 6 additions & 59 deletions ee/tabby-webserver/src/service/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<PathBuf> {
Expand Down

0 comments on commit db421a5

Please sign in to comment.