Skip to content

Add migrations with surrealdb-migrations #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
420 changes: 408 additions & 12 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ serde-aux = "4"
serde_json = "1"
serde_with = "3"
# DB
surrealdb = { git = "https://github.com/surrealdb/surrealdb.git", tag = "v1.0.0", features = [
surrealdb = { version = "1.0.0", features = [
"protocol-http",
"kv-mem",
] }
Expand Down Expand Up @@ -62,6 +62,7 @@ jsonwebtoken = "8.3.0"
lazy-regex = "2"
strum_macros = "0.24"
uuid = "1.3.3"
surrealdb-migrations = "1.0.0-preview.1"

[dev-dependencies]
reqwest = "0.11.18"
20 changes: 0 additions & 20 deletions db/schema.sql

This file was deleted.

Empty file added events/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions migrations/definitions/_initial.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"schemas":"DEFINE TABLE link SCHEMAFULL;\n\nDEFINE FIELD url ON TABLE link TYPE string;\nDEFINE FIELD title ON TABLE link TYPE string;\nDEFINE FIELD note ON TABLE link TYPE string;\nDEFINE FIELD user ON TABLE link TYPE record (user);\nDEFINE FIELD bookmarked_at ON TABLE link TYPE datetime DEFAULT time::now();\nDEFINE INDEX idx_user ON TABLE link COLUMNS user;\n\nDEFINE TABLE script_migration SCHEMAFULL\n PERMISSIONS\n FOR select FULL\n FOR create, update, delete NONE;\n\nDEFINE FIELD script_name ON script_migration TYPE string;\nDEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now();\n\nDEFINE TABLE token SCHEMAFULL;\n\nDEFINE FIELD token_hash ON TABLE token TYPE string;\nDEFINE FIELD name ON TABLE token TYPE string;\nDEFINE FIELD short_token ON TABLE token TYPE string;\nDEFINE FIELD user ON TABLE token TYPE record (user);\nDEFINE INDEX idx_hash ON TABLE token COLUMNS token_hash UNIQUE;\nDEFINE INDEX idx_user ON TABLE token COLUMNS user;\n\nDEFINE TABLE user SCHEMAFULL;\n\nDEFINE FIELD username ON TABLE user TYPE string;\nDEFINE FIELD password ON TABLE user TYPE string;\nDEFINE INDEX idx_username ON TABLE user COLUMNS username UNIQUE;\n","events":""}
8 changes: 8 additions & 0 deletions schemas/link.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DEFINE TABLE link SCHEMAFULL;

DEFINE FIELD url ON TABLE link TYPE string;
DEFINE FIELD title ON TABLE link TYPE string;
DEFINE FIELD note ON TABLE link TYPE string;
DEFINE FIELD user ON TABLE link TYPE record (user);
DEFINE FIELD bookmarked_at ON TABLE link TYPE datetime DEFAULT time::now();
DEFINE INDEX idx_user ON TABLE link COLUMNS user;
7 changes: 7 additions & 0 deletions schemas/script_migration.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
DEFINE TABLE script_migration SCHEMAFULL
PERMISSIONS
FOR select FULL
FOR create, update, delete NONE;

DEFINE FIELD script_name ON script_migration TYPE string;
DEFINE FIELD executed_at ON script_migration TYPE datetime DEFAULT time::now();
8 changes: 8 additions & 0 deletions schemas/token.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DEFINE TABLE token SCHEMAFULL;

DEFINE FIELD token_hash ON TABLE token TYPE string;
DEFINE FIELD name ON TABLE token TYPE string;
DEFINE FIELD short_token ON TABLE token TYPE string;
DEFINE FIELD user ON TABLE token TYPE record (user);
DEFINE INDEX idx_hash ON TABLE token COLUMNS token_hash UNIQUE;
DEFINE INDEX idx_user ON TABLE token COLUMNS user;
5 changes: 5 additions & 0 deletions schemas/user.surql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DEFINE TABLE user SCHEMAFULL;

DEFINE FIELD username ON TABLE user TYPE string;
DEFINE FIELD password ON TABLE user TYPE string;
DEFINE INDEX idx_username ON TABLE user COLUMNS username UNIQUE;
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use linkstowr::{
types::AppState,
};
use surrealdb::{engine::any::Any, opt::auth::Root, Surreal};
use surrealdb_migrations::MigrationRunner;
use tracing::info;

#[tokio::main]
Expand Down Expand Up @@ -65,6 +66,11 @@ async fn get_db(configuration: &Settings) -> Surreal<Any> {
.await
.unwrap();

MigrationRunner::new(&db)
.up()
.await
.expect("Failed to apply migrations");

info!(
"Connected to namespace: {}, database: {}",
&configuration.database.ns, &configuration.database.db
Expand Down
11 changes: 5 additions & 6 deletions tests/api_tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{fs, net::TcpListener};
use std::net::TcpListener;

use linkstowr::{
app::get_app,
Expand All @@ -13,6 +13,7 @@ use linkstowr::{
};
use serde_json::{json, Value};
use surrealdb::sql::thing;
use surrealdb_migrations::MigrationRunner;
use uuid::Uuid;

const TEST_USER_PASSWORD: &str = "password";
Expand All @@ -30,12 +31,10 @@ async fn spawn_app() -> TestApp {
db.use_ns("test").use_db("test").await.unwrap();

// Initialize schema
let schema =
fs::read_to_string("db/schema.sql").expect("Failed to read schema file from db/schema.sql");

db.query(schema)
MigrationRunner::new(&db)
.up()
.await
.expect("Failed to initialize the DB schema");
.expect("Failed to apply migrations");

// Setup env var for JWT
std::env::set_var("JWT_ENCODING_SECRET", JWT_ENCODING_SECRET);
Expand Down