Skip to content

Commit

Permalink
fix bugs with file content filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeebbbbrrrr committed Nov 9, 2021
1 parent b922202 commit 6807f18
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions cargo-pgx/src/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ pub(crate) fn install_extension(
let mut dest = base_directory.clone();
dest.push(&extdir);
dest.push(&control_file);
copy_file(control_file, dest, "control file");
copy_file(&control_file, &dest, "control file", true);
}

{
let mut dest = base_directory.clone();
dest.push(&pkgdir);
dest.push(format!("{}.so", extname));
copy_file(shlibpath, dest, "shared library");
copy_file(&shlibpath, &dest, "shared library", false);
}

if !no_schema || !get_target_sql_file(&extdir, &base_directory).exists() {
Expand All @@ -64,7 +64,7 @@ pub(crate) fn install_extension(
Ok(())
}

fn copy_file(src: PathBuf, dest: PathBuf, msg: &str) {
fn copy_file(src: &PathBuf, dest: &PathBuf, msg: &str, do_filter: bool) {
if !dest.parent().unwrap().exists() {
handle_result!(
std::fs::create_dir_all(dest.parent().unwrap()),
Expand All @@ -82,17 +82,24 @@ fn copy_file(src: PathBuf, dest: PathBuf, msg: &str) {
format_display_path(&dest)
);

// we want to filter the contents of each sql file
let input = handle_result!(
std::fs::read_to_string(&src),
format!("failed to read `{}`", src.display())
);
let input = filter_contents(input);
if do_filter {
// we want to filter the contents of the file we're to copy
let input = handle_result!(
std::fs::read_to_string(&src),
format!("failed to read `{}`", src.display())
);
let input = filter_contents(input);

handle_result!(
std::fs::write(&src, &input),
format!("failed writing `{}` to `{}`", src.display(), dest.display())
);
handle_result!(
std::fs::write(&dest, &input),
format!("failed writing `{}` to `{}`", src.display(), dest.display())
);
} else {
handle_result!(
std::fs::copy(&src, &dest),
format!("failed copying `{}` to `{}`", src.display(), dest.display())
);
}
}

pub(crate) fn build_extension(major_version: u16, is_release: bool, additional_features: &[&str]) {
Expand Down Expand Up @@ -167,9 +174,7 @@ fn copy_sql_files(
false,
true,
)?;
let written = std::fs::read_to_string(&dest).unwrap();
let written = filter_contents(written);
std::fs::write(&dest, written).unwrap();
copy_file(&dest, &dest, "extension schema file", true);

// now copy all the version upgrade files too
if let Ok(dir) = std::fs::read_dir("sql/") {
Expand All @@ -182,7 +187,7 @@ fn copy_sql_files(
dest.push(extdir);
dest.push(filename);

copy_file(sql.path(), dest, "extension schema file");
copy_file(&sql.path(), &dest, "extension schema upgrade file", true);
}
}
}
Expand Down

0 comments on commit 6807f18

Please sign in to comment.