-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cef4f5f
commit 4c6cecd
Showing
8 changed files
with
2,481 additions
and
183 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use anyhow::{bail, Result}; | ||
use async_trait::async_trait; | ||
|
||
#[async_trait] | ||
pub trait DatabaseDriver { | ||
async fn execute(self, content: &str) -> Result<()>; | ||
} | ||
|
||
pub fn new(db_url: &str) -> Result<impl DatabaseDriver> { | ||
if db_url.starts_with("libsql") { | ||
// TODO: return LibSQL database client | ||
} | ||
|
||
bail!("No matching database") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::database::DatabaseDriver; | ||
use anyhow::{bail, Result}; | ||
use async_trait::async_trait; | ||
use libsql_client::{ | ||
http::{Client, InnerClient}, | ||
Config, | ||
}; | ||
|
||
struct LibSQLDriver { | ||
db: Client, | ||
} | ||
|
||
impl LibSQLDriver { | ||
fn new(db_url: &str, token: &str) -> Result<LibSQLDriver> { | ||
let inner_client = InnerClient::Default; | ||
let config = Config::new(db_url)?.with_auth_token(token); | ||
|
||
let client = match Client::from_config(inner_client, config) { | ||
Ok(c) => c, | ||
Err(err) => bail!("{:?}", err), | ||
}; | ||
|
||
Ok(LibSQLDriver { db: client }) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl DatabaseDriver for LibSQLDriver { | ||
// execute query with the provided database | ||
async fn execute(self, content: &str) -> Result<()> { | ||
match self.db.execute(content).await { | ||
Ok(..) => Ok(()), | ||
Err(err) => bail!("{:?}", err), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use crate::config::migration_folder; | ||
use anyhow::{bail, Result}; | ||
use std::fs; | ||
use std::path::PathBuf; | ||
|
||
fn get_paths(folder: &PathBuf, ending: &str) -> Vec<PathBuf> { | ||
let entries = fs::read_dir(folder).unwrap(); | ||
|
||
let mut migration_files = vec![]; | ||
|
||
for entry in entries { | ||
let entry = entry.unwrap(); | ||
let path = entry.path(); | ||
|
||
if path.extension().unwrap().to_str().unwrap() == format!("{ending}.sql") { | ||
migration_files.push(path); | ||
} | ||
} | ||
|
||
migration_files | ||
} | ||
|
||
pub fn up() -> Result<()> { | ||
let folder = migration_folder(); | ||
let path = PathBuf::from(&folder); | ||
let files = get_paths(&path, "up"); | ||
|
||
if files.len() == 0 { | ||
bail!( | ||
"Didn't find any files ending with .up.sql at {}. Does the path exist?", | ||
folder | ||
); | ||
} | ||
|
||
for f in files {} | ||
|
||
Ok(()) | ||
} | ||
|