-
-
Notifications
You must be signed in to change notification settings - Fork 25
Implement Add Feature #217
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,167 @@ | ||
use std::{collections::HashSet, path::PathBuf}; | ||
|
||
use sqlx::{types::chrono::Utc, PgConnection}; | ||
use tokio::fs; | ||
|
||
use crate::{ | ||
models::{Payload, UpdatePath}, | ||
util::{extension_versions, update_paths}, | ||
}; | ||
|
||
pub async fn add( | ||
payload: &Payload, | ||
output_path: &PathBuf, | ||
mut conn: PgConnection, | ||
) -> anyhow::Result<()> { | ||
let existing_versions = extension_versions(&mut conn, &payload.metadata.extension_name).await?; | ||
let mut installed_extension_once = !existing_versions.is_empty(); | ||
let mut versions_installed_now = HashSet::new(); | ||
|
||
let timestamp = Utc::now().format("%Y%m%d%H%M%S"); | ||
let mut migration_content = String::new(); | ||
|
||
// Header with metadata | ||
migration_content.push_str("-- Migration generated by dbdev add at:"); | ||
migration_content.push_str(&Utc::now().format("%Y-%m-%d %H:%M:%S").to_string()); | ||
migration_content.push('\n'); | ||
|
||
migration_content.push_str("-- Extension: "); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push('\n'); | ||
|
||
migration_content.push_str("-- Default version:"); | ||
migration_content.push_str(&payload.metadata.default_version); | ||
migration_content.push('\n'); | ||
|
||
if let Some(comment) = &payload.metadata.comment { | ||
migration_content.push_str("-- Comment:"); | ||
migration_content.push_str(&comment); | ||
migration_content.push('\n'); | ||
} | ||
|
||
// Add prerequisites first | ||
if !&payload.metadata.requires.is_empty() { | ||
migration_content.push_str("-- Install prerequisites\n"); | ||
for req in &payload.metadata.requires { | ||
migration_content.push_str("CREATE EXTENSION IF NOT EXISTS "); | ||
migration_content.push_str(req); | ||
migration_content.push_str(";\n"); | ||
} | ||
migration_content.push('\n'); | ||
} | ||
|
||
for install_file in &payload.install_files { | ||
if !existing_versions.contains(&install_file.version) { | ||
if installed_extension_once { | ||
// For subsequent versions | ||
migration_content.push_str("-- Installing version "); | ||
migration_content.push_str(&install_file.version); | ||
migration_content.push('\n'); | ||
|
||
migration_content.push_str("SELECT pgtle.install_extension_version_sql('"); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str("', '"); | ||
migration_content.push_str(&install_file.version); | ||
migration_content.push_str("', $SQL$"); | ||
migration_content.push_str(&install_file.body); | ||
migration_content.push_str("$SQL$);\n\n"); | ||
|
||
versions_installed_now.insert(install_file.version.clone()); | ||
} else { | ||
// For initial installation | ||
migration_content.push_str("-- Initial installation of version:"); | ||
migration_content.push_str(&install_file.version); | ||
migration_content.push('\n'); | ||
|
||
let requirements = payload | ||
.metadata | ||
.requires | ||
.iter() | ||
.map(|r| format!("'{}'", r)) | ||
.collect::<Vec<_>>() | ||
.join(","); | ||
|
||
migration_content.push_str("SELECT pgtle.install_extension('"); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str("', '"); | ||
migration_content.push_str(&install_file.version); | ||
migration_content.push_str("', $COMMENT$"); | ||
migration_content.push_str(&payload.metadata.comment.as_deref().unwrap_or("")); | ||
migration_content.push_str("$COMMENT$, $SQL$"); | ||
migration_content.push_str(&install_file.body); | ||
migration_content.push_str("$SQL$, ARRAY["); | ||
migration_content.push_str(&requirements); | ||
migration_content.push_str("]::text[] );\n\n"); | ||
|
||
versions_installed_now.insert(install_file.version.clone()); | ||
installed_extension_once = true; | ||
} | ||
} | ||
} | ||
|
||
let existing_update_paths = | ||
match update_paths(&mut conn, &payload.metadata.extension_name).await { | ||
Ok(paths) => paths, | ||
Err(_) => HashSet::new(), | ||
}; | ||
|
||
for upgrade_file in &payload.upgrade_files { | ||
let update_path = UpdatePath { | ||
source: upgrade_file.from_version.clone(), | ||
target: upgrade_file.to_version.clone(), | ||
}; | ||
if !existing_update_paths.contains(&update_path) { | ||
migration_content.push_str("SELECT pgtle.install_update_path('"); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str("', '"); | ||
migration_content.push_str(&upgrade_file.from_version); | ||
migration_content.push_str("', '"); | ||
migration_content.push_str(&upgrade_file.to_version); | ||
migration_content.push_str("', $SQL$"); | ||
migration_content.push_str(&upgrade_file.body); | ||
migration_content.push_str("$SQL$);\n\n"); | ||
} | ||
} | ||
|
||
// Create the extension | ||
migration_content.push_str("-- Create the extension\n"); | ||
match &payload.metadata.schema { | ||
Some(schema) => { | ||
migration_content.push_str("CREATE EXTENSION IF NOT EXISTS "); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str(" SCHEMA "); | ||
migration_content.push_str(schema); | ||
migration_content.push_str(";\n"); | ||
} | ||
None => { | ||
migration_content.push_str("CREATE EXTENSION IF NOT EXISTS "); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str(";\n"); | ||
} | ||
} | ||
|
||
// Set default version | ||
migration_content.push_str("-- Setting default version to:"); | ||
migration_content.push_str(&payload.metadata.default_version); | ||
migration_content.push('\n'); | ||
|
||
migration_content.push_str("SELECT pgtle.set_default_version('"); | ||
migration_content.push_str(&payload.metadata.extension_name); | ||
migration_content.push_str("', '"); | ||
migration_content.push_str(&payload.metadata.default_version); | ||
migration_content.push_str("');\n"); | ||
|
||
// Write to file | ||
let mut filename = String::new(); | ||
filename.push_str(×tamp.to_string()); | ||
filename.push('_'); | ||
filename.push_str(&payload.metadata.extension_name); | ||
filename.push_str("_install.sql"); | ||
|
||
let file_path = output_path.join(filename); | ||
|
||
fs::write(&file_path, migration_content).await?; | ||
|
||
println!("Generated migration file at: {}", file_path.display()); | ||
Ok(()) | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod add; | ||
pub mod install; | ||
pub mod list; | ||
pub mod login; | ||
pub mod publish; | ||
pub mod uninstall; | ||
pub mod list; |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.