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

Branches: dont require projects unless explicitly needed #1259

Merged
merged 4 commits into from
Mar 25, 2024
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
52 changes: 36 additions & 16 deletions src/branch/context.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,61 @@
use std::fs;
use crate::portable::config::Config;
use std::path::{Path, PathBuf};
use crate::connect::Connection;
use crate::credentials;
use crate::portable::options::InstanceName;
use crate::portable::project::{instance_name, stash_path};

pub struct Context {
pub project_config: Config,
pub project_branch: String,
pub branch: String,
pub project_config: Option<Config>,
pub branch: Option<String>,


project_dir: PathBuf,
project_dir: Option<PathBuf>,
}

impl Context {
pub async fn new(project_dir: &Path) -> anyhow::Result<Context> {
let project_config = crate::portable::config::read(&project_dir.join("edgedb.toml"))?;
let project_branch = project_config.edgedb.branch.clone();
pub async fn new(project_dir: Option<&PathBuf>) -> anyhow::Result<Context> {
let project_config = match project_dir {
Some(path) => Some(crate::portable::config::read(&path.join("edgedb.toml"))?),
None => None
};

let path = credentials::path(&get_instance_name(project_dir)?)?;
let credentials = credentials::read(&path).await?;
let credentials = match project_dir {
Some(path) => Some(credentials::read(&credentials::path(&get_instance_name(&path)?)?).await?),
None => None
};

Ok(Context {
project_config,
branch: credentials.database.unwrap_or(project_branch.clone()),
project_branch,
project_dir: PathBuf::from(project_dir),
branch: credentials.and_then(|v| v.database),
project_dir: project_dir.map(PathBuf::from)
})
}

pub fn get_instance_name(&self) -> anyhow::Result<String> {
get_instance_name(&self.project_dir)
pub async fn get_default_branch_name(&self, connection: &mut Connection) -> anyhow::Result<&str> {
let version = connection.get_version().await?.specific();

if version.major >= 5 {
return Ok("main")
}

Ok("edgedb")
}

pub fn get_instance_name(&self) -> anyhow::Result<Option<String>> {
Ok(match &self.project_dir {
Some(dir) => Some(get_instance_name(&dir)?),
None => None
})
}

pub async fn update_branch(&self, branch: &String) -> anyhow::Result<()> {
let path = credentials::path(&self.get_instance_name()?)?;
let instance_name = match self.get_instance_name()? {
Some(i) => i,
None => return Ok(())
};

let path = credentials::path(&instance_name)?;

let mut credentials = credentials::read(&path).await?;

Expand Down
6 changes: 5 additions & 1 deletion src/branch/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ pub async fn main(
) -> anyhow::Result<()> {
eprintln!("Creating branch '{}'...", options.name);

create_branch(connection, &options.name, options.from.as_ref().unwrap_or(&context.branch), options.empty, options.copy_data).await?;
let from = options.from.clone().unwrap_or(async {
anyhow::Ok(context.get_default_branch_name(connection).await?.to_string())
}.await?);

create_branch(connection, &options.name, &from, options.empty, options.copy_data).await?;
Ok(())
}

Expand Down
12 changes: 7 additions & 5 deletions src/branch/drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ pub async fn main(
context: &Context,
connection: &mut Connection,
) -> anyhow::Result<()> {
if context.branch == options.branch {
anyhow::bail!(
"Dropping the currently active branch is not supported, please switch to a \
different branch to drop this one with `edgedb branch switch <branch>`"
);
if let Some(current_branch) = &context.branch {
if current_branch == &options.branch {
anyhow::bail!(
"Dropping the currently active branch is not supported, please switch to a \
different branch to drop this one with `edgedb branch switch <branch>`"
);
}
}

if !options.non_interactive {
Expand Down
4 changes: 1 addition & 3 deletions src/branch/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ pub async fn main(
.await?;

for branch in branches {
if context.branch == branch {
if context.branch.as_ref() == Some(&branch) {
println!("{} - Current", branch.green());
} else if context.project_config.edgedb.branch == branch {
println!("{} - Project default", branch.blue());
} else {
println!("{}", branch);
}
Expand Down
4 changes: 2 additions & 2 deletions src/branch/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub async fn branch_main(options: &Options, cmd: &BranchCommand) -> anyhow::Resu
}

async fn create_context() -> anyhow::Result<Context> {
let project_dir = get_project_dir(None, true).await?.expect("Missing project");
Context::new(&project_dir).await
let project_dir = get_project_dir(None, true).await?;
Context::new(project_dir.as_ref()).await
}

pub async fn verify_server_can_use_branches(connection: &mut Connection) -> anyhow::Result<()> {
Expand Down
11 changes: 9 additions & 2 deletions src/branch/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ use crate::migrations::merge::{apply_merge_migration_files, get_merge_migrations
use crate::options::Options;

pub async fn main(options: &Merge, context: &Context, source_connection: &mut Connection, cli_opts: &Options) -> anyhow::Result<()> {
if options.target_branch == context.branch {
if context.project_config.is_none() {
anyhow::bail!("Merge must be used within a project");
}

let current_branch = context.branch.as_ref().unwrap();
let project_config = context.project_config.as_ref().unwrap();

if &options.target_branch == current_branch {
anyhow::bail!("Cannot merge the current branch into its self");
}

Expand All @@ -18,7 +25,7 @@ pub async fn main(options: &Merge, context: &Context, source_connection: &mut Co
None => anyhow::bail!("The branch '{}' doesn't exist", options.target_branch)
};

let migration_context = migrations::Context::for_project(&context.project_config)?;
let migration_context = migrations::Context::for_project(project_config)?;
let mut merge_migrations = get_merge_migrations(source_connection, &mut target_connection).await?;

eprintln!("Merging {} migration(s) into '{}'...", merge_migrations.target_migrations.len(), source_connection.database());
Expand Down
19 changes: 13 additions & 6 deletions src/branch/rebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ use crate::{migrations, print};
use crate::branch::connections::get_connection_to_modify;

pub async fn main(options: &Rebase, context: &Context, source_connection: &mut Connection, cli_opts: &Options) -> anyhow::Result<()> {
if options.target_branch == context.branch {
if context.project_config.is_none() {
anyhow::bail!("Rebase must be used within a project");
}

let current_branch = context.branch.as_ref().unwrap();

if &options.target_branch == current_branch {
anyhow::bail!("Cannot rebase the current branch on top of itself");
}

Expand All @@ -36,21 +42,22 @@ pub async fn main(options: &Rebase, context: &Context, source_connection: &mut C

async fn rebase(branch: &String, source_connection: &mut Connection, target_connection: &mut Connection, context: &Context, cli_opts: &Options, apply_migrations: bool) -> anyhow::Result<()> {
let mut migrations = get_diverging_migrations(source_connection, target_connection).await?;

let current_branch = context.branch.as_ref().unwrap();
let project_config = context.project_config.as_ref().unwrap();
migrations.print_status();

let migration_context = migrations::Context::for_project(&context.project_config)?;
let migration_context = migrations::Context::for_project(project_config)?;
do_rebase(&mut migrations, &migration_context).await?;

if apply_migrations {
write_rebased_migration_files(&migrations, &migration_context, target_connection).await?;
}

// drop source branch
eprintln!("\nReplacing '{}' with rebased version...", &context.branch);
let status = target_connection.execute(&format!("drop branch {} force", edgeql_parser::helpers::quote_name(&context.branch)), &()).await?;
eprintln!("\nReplacing '{}' with rebased version...", current_branch);
let status = target_connection.execute(&format!("drop branch {} force", edgeql_parser::helpers::quote_name(current_branch)), &()).await?;
print::completion(status);
rename_temp_to_source(branch, &context.branch, cli_opts, target_connection).await?;
rename_temp_to_source(branch, current_branch, cli_opts, target_connection).await?;

eprintln!("Done!");
anyhow::Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/branch/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub async fn main(
connection: &mut Connection,
cli_opts: &Options,
) -> anyhow::Result<()> {
if options.old_name == context.branch {
let mut modify_connection = get_connection_to_modify(&context.branch, &cli_opts, connection).await?;
if Some(&options.old_name) == context.branch.as_ref() || connection.database() == options.old_name {
let mut modify_connection = get_connection_to_modify(&options.old_name, &cli_opts, connection).await?;
rename(&mut modify_connection.connection, &options).await?;
modify_connection.clean().await?;
context.update_branch(&options.new_name).await?;
Expand Down
12 changes: 9 additions & 3 deletions src/branch/switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ pub async fn main(
context: &Context,
connector: &mut Connector,
) -> anyhow::Result<()> {
if context.branch == options.branch {
if context.branch.is_none() {
anyhow::bail!("Cannot switch branches: No project found");
}

let current_branch = context.branch.as_ref().unwrap();

if current_branch == &options.branch {
anyhow::bail!("Already on '{}'", options.branch);
}

Expand All @@ -29,7 +35,7 @@ pub async fn main(
if !branches.contains(&options.branch) {
if options.create {
eprintln!("Creating '{}'...", &options.branch);
create_branch(&mut connection, &options.branch, options.from.as_ref().unwrap_or(&context.branch), options.empty, options.copy_data).await?;
create_branch(&mut connection, &options.branch, options.from.as_ref().unwrap_or(current_branch), options.empty, options.copy_data).await?;
} else {
anyhow::bail!("Branch '{}' doesn't exists", options.branch)
}
Expand All @@ -47,7 +53,7 @@ pub async fn main(

eprintln!(
"Switching from '{}' to '{}'",
context.branch, options.branch
current_branch, options.branch
);

context.update_branch(&options.branch).await?;
Expand Down
4 changes: 2 additions & 2 deletions src/migrations/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::migrations::{Context, migrate, migration};
use crate::migrations::create::{MigrationKey, MigrationToText, write_migration};
use crate::migrations::db_migration::{DBMigration, read_all};
use crate::migrations::migration::MigrationFile;
use crate::migrations::rebase::{fix_migration_ids};
use crate::print;



pub struct MergeMigrations {
pub base_migrations: IndexMap<String, MergeMigration>,
Expand Down
8 changes: 0 additions & 8 deletions src/portable/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ pub struct SrcConfig {
pub struct SrcEdgedb {
#[serde(default)]
pub server_version: Option<toml::Spanned<Query>>,
#[serde(default)]
pub branch: Option<Spanned<String>>,
#[serde(flatten)]
pub extra: BTreeMap<String, toml::Value>,
}
Expand All @@ -50,7 +48,6 @@ pub struct Config {
#[derive(Debug)]
pub struct Edgedb {
pub server_version: Query,
pub branch: String,
}

#[derive(Debug)]
Expand Down Expand Up @@ -92,11 +89,6 @@ pub fn read(path: &Path) -> anyhow::Result<Config> {
channel: Channel::Stable,
version: None,
}),
branch: val
.edgedb
.branch
.map(|x| x.into_inner())
.unwrap_or("main".to_string()),
},
project: Project {
schema_dir: val
Expand Down
Loading