Skip to content
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

Add schema.update hooks #1468

Merged
merged 3 commits into from
Feb 12, 2025
Merged
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
20 changes: 12 additions & 8 deletions src/branch/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::portable::project;
use std::fs;
use std::sync::Mutex;

#[derive(Debug)]
pub struct Context {
/// Instance name provided either with --instance or inferred from the project.
instance_name: Option<InstanceName>,
Expand All @@ -26,7 +27,7 @@ pub struct Context {
current_branch: Option<String>,

/// Project manifest cache
manifest_cache: Mutex<Option<Option<project::Context>>>,
project_ctx_cache: Mutex<Option<project::Context>>,
}

impl Context {
Expand All @@ -35,7 +36,7 @@ impl Context {
instance_name: None,
current_branch: None,
project: None,
manifest_cache: Mutex::new(None),
project_ctx_cache: Mutex::new(None),
};

// use instance name provided with --instance
Expand Down Expand Up @@ -112,15 +113,18 @@ impl Context {
}

pub async fn get_project(&self) -> anyhow::Result<Option<project::Context>> {
if let Some(mani) = &*self.manifest_cache.lock().unwrap() {
return Ok(mani.clone());
if let Some(ctx) = &*self.project_ctx_cache.lock().unwrap() {
return Ok(Some(ctx.clone()));
}

let manifest = project::load_ctx(None).await?;
let Some(location) = &self.project else {
return Ok(None);
};
let ctx = project::load_ctx_at_async(location.clone()).await?;

let mut cache_lock = self.manifest_cache.lock().unwrap();
*cache_lock = Some(manifest.clone());
Ok(manifest)
let mut cache_lock = self.project_ctx_cache.lock().unwrap();
*cache_lock = Some(ctx.clone());
Ok(Some(ctx))
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/branch/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub async fn run(

if let Some(project) = &context.get_project().await? {
hooks::on_action("branch.switch.before", project).await?;
hooks::on_action("schema.update.before", project).await?;
}

let current_branch = if let Some(mut connection) = connect_if_branch_exists(connector).await? {
Expand Down Expand Up @@ -76,6 +77,7 @@ pub async fn run(

if let Some(project) = &context.get_project().await? {
hooks::on_action("branch.switch.after", project).await?;
hooks::on_action("schema.update.after", project).await?;
}

Ok(branch::CommandResult {
Expand Down
2 changes: 2 additions & 0 deletions src/branch/wipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ pub async fn do_wipe(
) -> Result<(), anyhow::Error> {
if let Some(project) = context.get_project().await? {
hooks::on_action("branch.wipe.before", &project).await?;
hooks::on_action("schema.update.before", &project).await?;
}

let (status, _warnings) = connection.execute("RESET SCHEMA TO initial", &()).await?;
print::completion(status);

if let Some(project) = context.get_project().await? {
hooks::on_action("branch.wipe.after", &project).await?;
hooks::on_action("schema.update.after", &project).await?;
}
Ok(())
}
Expand Down
6 changes: 2 additions & 4 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,8 @@ fn get_hook<'m>(
"branch.wipe.after" => &hooks.branch.as_ref()?.wipe.as_ref()?.after,
"migration.apply.before" => &hooks.migration.as_ref()?.apply.as_ref()?.before,
"migration.apply.after" => &hooks.migration.as_ref()?.apply.as_ref()?.after,
"migration.rebase.before" => &hooks.migration.as_ref()?.rebase.as_ref()?.before,
"migration.rebase.after" => &hooks.migration.as_ref()?.rebase.as_ref()?.after,
"migration.merge.before" => &hooks.migration.as_ref()?.merge.as_ref()?.before,
"migration.merge.after" => &hooks.migration.as_ref()?.merge.as_ref()?.after,
"schema.update.before" => &hooks.schema.as_ref()?.update.as_ref()?.before,
"schema.update.after" => &hooks.schema.as_ref()?.update.as_ref()?.after,
_ => panic!("unknown action"),
};
hook.as_deref()
Expand Down
2 changes: 2 additions & 0 deletions src/migrations/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ pub async fn apply_migrations(
) -> anyhow::Result<()> {
if let Some(project) = &ctx.project {
hooks::on_action("migration.apply.before", project).await?;
hooks::on_action("schema.update.before", project).await?;
}

let old_timeout = timeout::inhibit_for_transaction(cli).await?;
Expand Down Expand Up @@ -516,6 +517,7 @@ pub async fn apply_migrations(
}?;
if let Some(project) = &ctx.project {
hooks::on_action("migration.apply.after", project).await?;
hooks::on_action("schema.update.after", project).await?;
}
Ok(())
}
Expand Down
8 changes: 6 additions & 2 deletions src/portable/project/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ pub struct Hooks {
pub project: Option<ProjectHooks>,
pub branch: Option<BranchHooks>,
pub migration: Option<MigrationHooks>,
pub schema: Option<SchemaHooks>,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
Expand All @@ -82,8 +83,11 @@ pub struct BranchHooks {
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct MigrationHooks {
pub apply: Option<Hook>,
pub rebase: Option<Hook>,
pub merge: Option<Hook>,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct SchemaHooks {
pub update: Option<Hook>,
}

#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
Expand Down
8 changes: 6 additions & 2 deletions src/portable/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,13 +316,13 @@ pub fn database_name(stash_dir: &Path) -> anyhow::Result<Option<String>> {
Ok(Some(inst.trim().into()))
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Context {
pub location: Location,
pub manifest: manifest::Manifest,
}

#[derive(Clone)]
#[derive(Debug, Clone)]
pub struct Location {
pub root: PathBuf,
pub manifest: PathBuf,
Expand Down Expand Up @@ -353,6 +353,10 @@ pub async fn load_ctx(override_dir: Option<&Path>) -> anyhow::Result<Option<Cont

#[tokio::main(flavor = "current_thread")]
pub async fn load_ctx_at(location: Location) -> anyhow::Result<Context> {
load_ctx_at_async(location).await
}

pub async fn load_ctx_at_async(location: Location) -> anyhow::Result<Context> {
let manifest = manifest::read(&location.manifest)?;
Ok(Context { location, manifest })
}
Expand Down
4 changes: 2 additions & 2 deletions tests/func/non_interactive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,12 @@ fn branch_commands() {
.arg(instance_name)
.arg("branch")
.arg("switch")
.arg("main")
.arg("test_branch_3")
.assert()
.context("switch", "switch instance branch")
.success();
assert_eq!(get_current_project_branch(), "main");
assert_eq!(get_current_instance_branch(instance_name), "main");
assert_eq!(get_current_instance_branch(instance_name), "test_branch_3");
}

#[test]
Expand Down
44 changes: 40 additions & 4 deletions tests/portable_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,9 @@ fn hooks() {
expected: &[
"project.init.after",
"migration.apply.before",
"schema.update.before",
"migration.apply.after",
"schema.update.after",
],
});

Expand All @@ -292,7 +294,12 @@ fn hooks() {
.context("branch-switch", "")
.success()
.stderr(ContainsHooks {
expected: &["branch.switch.before", "branch.switch.after"],
expected: &[
"branch.switch.before",
"schema.update.before",
"branch.switch.after",
"schema.update.after",
],
});

let branch_log = fs::read_to_string(branch_log_file).unwrap();
Expand All @@ -307,7 +314,12 @@ fn hooks() {
.context("branch-merge", "")
.success()
.stderr(ContainsHooks {
expected: &["migration.apply.before", "migration.apply.after"],
expected: &[
"migration.apply.before",
"schema.update.before",
"migration.apply.after",
"schema.update.after",
],
});

Command::new("edgedb")
Expand All @@ -320,7 +332,12 @@ fn hooks() {
.context("branch-wipe", "")
.success()
.stderr(ContainsHooks {
expected: &["branch.wipe.before", "branch.wipe.after"],
expected: &[
"branch.wipe.before",
"schema.update.before",
"branch.wipe.after",
"schema.update.after",
],
});

Command::new("edgedb")
Expand All @@ -332,11 +349,30 @@ fn hooks() {
.context("branch-switch-2", "")
.success()
.stderr(ContainsHooks {
expected: &["branch.switch.before", "branch.switch.after"],
expected: &[
"branch.switch.before",
"schema.update.before",
"branch.switch.after",
"schema.update.after",
],
});

let branch_log = fs::read_to_string(branch_log_file).unwrap();
assert_eq!(branch_log, "another\ndefault-branch-name\n");

// branch switch, but with explict --instance arg
// This should prevent hooks from being executed, since
// this action is not executed "on a project", but "on an instance".
Command::new("edgedb")
.current_dir("tests/proj/project3")
.arg("--instance=inst2")
.arg("branch")
.arg("switch")
.arg("another")
.assert()
.context("branch-switch-3", "")
.success()
.stderr(ContainsHooks { expected: &[] });
}

#[derive(Debug)]
Expand Down
2 changes: 2 additions & 0 deletions tests/proj/project3/gel.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ branch.wipe.before = "true"
branch.wipe.after = "true"
migration.apply.before = "true"
migration.apply.after = "true"
schema.update.before = "true"
schema.update.after = "true"
Loading