-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lutra): pull database schema into source (#4182)
- Loading branch information
1 parent
784895d
commit 72b408a
Showing
15 changed files
with
280 additions
and
117 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
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
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,17 @@ | ||
use std::path::Path; | ||
|
||
use anyhow::Result; | ||
|
||
use crate::project::DatabaseModule; | ||
|
||
pub fn open(db: &DatabaseModule, project_root: &Path) -> Result<rusqlite::Connection> { | ||
// convert relative to absolute path | ||
let mut sqlite_file_abs = project_root.to_path_buf(); | ||
sqlite_file_abs.push(&db.connection_params.file_relative); | ||
let sqlite_file_abs = sqlite_file_abs.as_os_str().to_str().unwrap(); | ||
|
||
// init SQLite | ||
let sqlite_conn = rusqlite::Connection::open(sqlite_file_abs)?; | ||
|
||
Ok(sqlite_conn) | ||
} |
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,41 @@ | ||
use std::fs; | ||
|
||
use anyhow::Result; | ||
use prqlc::Error; | ||
|
||
use crate::ProjectCompiled; | ||
|
||
/// Edit a source file such that the source of the declaration with id `decl_id` is now `new_source`. | ||
pub fn edit_source_file( | ||
project: &ProjectCompiled, | ||
decl_id: usize, | ||
new_source: String, | ||
) -> Result<()> { | ||
let span = project.root_module.span_map.get(&decl_id); | ||
let Some(span) = span else { | ||
// TODO: bad error message, we should not mention decl ids | ||
return Err(Error::new_simple(format!( | ||
"cannot find where declaration {decl_id} came from" | ||
)) | ||
.into()); | ||
}; | ||
|
||
// retrieve file path, relative to project root | ||
// this is safe, because the source_id must exist, right? It was created during parsing. | ||
let file_path = project.sources.get_path(span.source_id).unwrap().clone(); | ||
|
||
// find original source | ||
let mut source = project.sources.sources.get(&file_path).unwrap().clone(); | ||
|
||
// replace the text | ||
source.replace_range(span.start..span.end, &new_source); | ||
|
||
// reconstruct full path of the file | ||
// sources are loaded from file-system, so root path must always exist | ||
let mut file_path_full = project.sources.root.clone().unwrap(); | ||
file_path_full.extend(&file_path); | ||
|
||
// write the new file contents | ||
fs::write(file_path_full, &source)?; | ||
Ok(()) | ||
} |
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
Oops, something went wrong.