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 support for .gel files. #1406

Merged
merged 3 commits into from
Nov 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
8 changes: 8 additions & 0 deletions src/branding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ pub const BRANDING_CLI_CMD_ALT_FILE: &str = if cfg!(windows) {
BRANDING_CLI_CMD_ALT
};

pub const BRANDING_SCHEMA_FILE_EXT_GEL: &str = "gel";
pub const BRANDING_SCHEMA_FILE_EXT_ESDL: &str = "esdl";
pub const BRANDING_SCHEMA_FILE_EXT: &str = if cfg!(feature = "gel") {
BRANDING_SCHEMA_FILE_EXT_GEL
} else {
BRANDING_SCHEMA_FILE_EXT_ESDL
};

/// The WSL distribution name.
pub const BRANDING_WSL: &str = "EdgeDB.WSL.1";

Expand Down
22 changes: 16 additions & 6 deletions src/migrations/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use crate::migrations::prompt;
use crate::migrations::source_map::{Builder, SourceMap};
use crate::migrations::squash;
use crate::migrations::timeout;
use crate::platform::tmp_file_name;
use crate::platform::{is_legacy_schema_file, is_schema_file, tmp_file_name};
use crate::print;
use crate::print::style::Styler;
use crate::question;
Expand Down Expand Up @@ -146,8 +146,8 @@ struct SplitMigration;
struct CantResolve;

#[derive(Debug, thiserror::Error)]
#[error("cannot proceed until .esdl files are fixed")]
pub struct EsdlError;
#[error("cannot proceed until schema files are fixed")]
pub struct SchemaFileError;

impl FutureMigration {
fn new(key: MigrationKey, descr: CurrentMigration) -> Self {
Expand Down Expand Up @@ -272,17 +272,27 @@ async fn gen_start_migration(ctx: &Context) -> anyhow::Result<(String, SourceMap
};

let mut paths: Vec<PathBuf> = Vec::new();
let mut has_legacy_paths: bool = false;
while let Some(item) = dir.next_entry().await? {
let fname = item.file_name();
let lossy_name = fname.to_string_lossy();
if !lossy_name.starts_with('.')
&& lossy_name.ends_with(".esdl")
&& is_schema_file(&lossy_name)
&& item.file_type().await?.is_file()
{
paths.push(item.path())
paths.push(item.path());
if is_legacy_schema_file(&lossy_name) {
has_legacy_paths = true;
}
}
}

if has_legacy_paths {
print::warn!(
"Legacy schema file extension '.esdl' detected. Consider renaming them to '.gel'."
);
}

paths.sort();

for path in paths {
Expand All @@ -301,7 +311,7 @@ pub async fn execute_start_migration(ctx: &Context, cli: &mut Connection) -> any
Ok(_) => Ok(()),
Err(e) if e.is::<QueryError>() => {
print_migration_error(&e, &source_map)?;
Err(EsdlError)?
Err(SchemaFileError)?
}
Err(e) => Err(e)?,
}
Expand Down
4 changes: 2 additions & 2 deletions src/migrations/upgrade_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::branding::BRANDING_CLI_CMD;
use crate::commands::{ExitCode, Options};
use crate::connect::Connection;
use crate::migrations::context::Context;
use crate::migrations::create::{execute_start_migration, EsdlError};
use crate::migrations::create::{execute_start_migration, SchemaFileError};
use crate::migrations::edb::{execute, execute_if_connected};
use crate::migrations::migrate::{apply_migration, ApplyMigrationError};
use crate::migrations::migration;
Expand Down Expand Up @@ -245,7 +245,7 @@ async fn single_check(ctx: &Context, cli: &mut Connection) -> anyhow::Result<Che
Ok(()) => {
execute(cli, "ABORT MIGRATION", None).await?;
}
Err(e) if e.is::<EsdlError>() => {
Err(e) if e.is::<SchemaFileError>() => {
print::warn!(
"Schema incompatibilities found. \
Please fix the errors above to proceed.",
Expand Down
13 changes: 12 additions & 1 deletion src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ use std::path::{Path, PathBuf};
use anyhow::Context;
use fn_error_context::context;

use crate::branding::BRANDING_CLI_CMD_FILE;
use crate::branding::{
BRANDING_CLI_CMD_FILE, BRANDING_SCHEMA_FILE_EXT_ESDL, BRANDING_SCHEMA_FILE_EXT_GEL,
};
use crate::cli::env::Env;

#[cfg(windows)]
Expand Down Expand Up @@ -134,6 +136,15 @@ pub fn binary_path() -> anyhow::Result<PathBuf> {
Ok(dir.join(BRANDING_CLI_CMD_FILE))
}

pub fn is_legacy_schema_file(filename: &str) -> bool {
filename.ends_with(&format!(".{BRANDING_SCHEMA_FILE_EXT_ESDL}"))
}

pub fn is_schema_file(filename: &str) -> bool {
filename.ends_with(&format!(".{BRANDING_SCHEMA_FILE_EXT_ESDL}"))
|| filename.ends_with(&format!(".{BRANDING_SCHEMA_FILE_EXT_GEL}"))
}

pub fn data_dir() -> anyhow::Result<PathBuf> {
Ok(dirs::data_dir()
.ok_or_else(|| anyhow::anyhow!("Can't determine data directory"))?
Expand Down
24 changes: 13 additions & 11 deletions src/portable/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ use edgedb_tokio::Builder;
use edgeql_parser::helpers::quote_name;

use crate::branding::BRANDING_CLOUD;
use crate::branding::{BRANDING, BRANDING_CLI_CMD, CONFIG_FILE_DISPLAY_NAME};
use crate::branding::{
BRANDING, BRANDING_CLI_CMD, BRANDING_SCHEMA_FILE_EXT, CONFIG_FILE_DISPLAY_NAME,
};
use crate::cloud;
use crate::cloud::client::CloudClient;
use crate::commands::ExitCode;
Expand All @@ -29,7 +31,7 @@ use crate::credentials;
use crate::migrations;
use crate::options::CloudOptions;
use crate::platform::{bytes_to_path, path_bytes};
use crate::platform::{config_dir, symlink_dir, tmp_file_path};
use crate::platform::{config_dir, is_schema_file, symlink_dir, tmp_file_path};
use crate::portable::config;
use crate::portable::control;
use crate::portable::create;
Expand All @@ -49,13 +51,13 @@ use crate::print::{self, msg, Highlight};
use crate::question;
use crate::table;

const DEFAULT_ESDL: &str = "\
const DEFAULT_SCHEMA: &str = "\
module default {\n\
\n\
}\n\
";

const FUTURES_ESDL: &str = "\
const FUTURES_SCHEMA: &str = "\
# Disable the application of access policies within access policies\n\
# themselves. This behavior will become the default in EdgeDB 3.0.\n\
# See: https://www.edgedb.com/docs/reference/ddl/access_policies#nonrecursive\n\
Expand Down Expand Up @@ -1402,12 +1404,12 @@ fn find_schema_files(path: &Path) -> anyhow::Result<bool> {
};
for item in dir {
let entry = item?;
let is_esdl = entry
let is_schema_file = entry
.file_name()
.to_str()
.map(|x| x.ends_with(".esdl"))
.map(is_schema_file)
.unwrap_or(false);
if is_esdl {
if is_schema_file {
return Ok(true);
}
}
Expand Down Expand Up @@ -1436,17 +1438,17 @@ fn print_initialized(name: &str, dir_option: &Option<PathBuf>) {
fn write_schema_default(dir: &Path, version: &Query) -> anyhow::Result<()> {
fs::create_dir_all(dir)?;
fs::create_dir_all(dir.join("migrations"))?;
let default = dir.join("default.esdl");
let default = dir.join(format!("default.{BRANDING_SCHEMA_FILE_EXT}"));
let tmp = tmp_file_path(&default);
fs::remove_file(&tmp).ok();
fs::write(&tmp, DEFAULT_ESDL)?;
fs::write(&tmp, DEFAULT_SCHEMA)?;
fs::rename(&tmp, &default)?;

if version.is_nonrecursive_access_policies_needed() {
let futures = dir.join("futures.esdl");
let futures = dir.join(format!("futures.{BRANDING_SCHEMA_FILE_EXT}"));
let tmp = tmp_file_path(&futures);
fs::remove_file(&tmp).ok();
fs::write(&tmp, FUTURES_ESDL)?;
fs::write(&tmp, FUTURES_SCHEMA)?;
fs::rename(&tmp, &futures)?;
};
Ok(())
Expand Down
49 changes: 36 additions & 13 deletions tests/func/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,18 @@ fn error() {
.arg("empty_err")
.assert()
.success();
let err = if SERVER.0.version_major >= 4 {
let err = if SERVER.0.version_major >= 6 {
r###"error: Unexpected keyword 'CREATE'
┌─ tests/migrations/db1/error/bad.esdl:3:9
3 │ create property text -> str;
│ ^^^^^^ Use a different identifier or quote the name with backticks: `create`
= This name is a reserved keyword and cannot be used as an identifier

edgedb error: cannot proceed until schema files are fixed
"###
} else if SERVER.0.version_major >= 4 {
r###"error: Unexpected keyword 'CREATE'
┌─ tests/migrations/db1/error/bad.esdl:3:9
Expand Down Expand Up @@ -1071,6 +1082,29 @@ fn input_required() {

#[test]
fn eof_err() {
let err = if SERVER.0.version_major >= 6 {
r###"error: Missing '{'
┌─ tests/migrations/db_eof_err/default.esdl:9:19
9 │ alias default::Foo
│ ╭──────────────────^
10 │ │
│ ╰^ error

edgedb error: cannot proceed until schema files are fixed
"###
} else {
r###"error: Missing '{'
┌─ tests/migrations/db_eof_err/default.esdl:9:19
9 │ alias default::Foo
│ ╭──────────────────^
10 │ │
│ ╰^ error

edgedb error: cannot proceed until .esdl files are fixed
"###
};
SERVER
.admin_cmd()
.arg("database")
Expand All @@ -1087,18 +1121,7 @@ fn eof_err() {
.env("NO_COLOR", "1")
.assert()
.code(1)
.stderr(ends_with(
r###"error: Missing '{'
┌─ tests/migrations/db_eof_err/default.esdl:9:19
9 │ alias default::Foo
│ ╭──────────────────^
10 │ │
│ ╰^ error

edgedb error: cannot proceed until .esdl files are fixed
"###,
));
.stderr(ends_with(err));
}

#[test]
Expand Down
Loading