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

WIP: issue #10 #13

Merged
merged 8 commits into from
Jul 15, 2020
49 changes: 0 additions & 49 deletions .github/workflows/build.yml

This file was deleted.

74 changes: 74 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: cargo test --all

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
test:

runs-on: ${{ matrix.os }}
if: "!contains(github.event.head_commit.message, 'nogha')"

strategy:
matrix:
version: [10, 11, 12]
os: ["ubuntu-latest"]

steps:
- uses: actions/checkout@v1

- name: cargo version
run: cargo --version

# Ubuntu system dependencies required to build Postgres from sources
- name: install postgres build dependencies
run: sudo apt install -y bison flex zlib1g zlib1g-dev pkg-config libssl-dev

# install our cargo subcomment
- name: install cargo pgx
run: cd cargo-pgx && cargo install --path . --force

# test our code
- name: cargo test
run: RUST_BACKTRACE=1 CARGO_TARGET_DIR=~/cargo-target-dir cargo test --all --features pg${{ matrix.version }} --no-default-features

# make sure the examples build too
- name: build arrays example
run: |
cd pgx-examples/arrays &&
PGX_PG10_CONFIG=~/cargo-target-dir/postgresql-10/pgx-install/bin/pg_config
PGX_PG11_CONFIG=~/cargo-target-dir/postgresql-11/pgx-install/bin/pg_config
PGX_PG12_CONFIG=~/cargo-target-dir/postgresql-12/pgx-install/bin/pg_config
CARGO_TARGET_DIR=~/cargo-target-dir
cargo pgx test pg${{ matrix.version }}
- name: build errors example
run: |
cd pgx-examples/errors &&
PGX_PG10_CONFIG=~/cargo-target-dir/postgresql-10/pgx-install/bin/pg_config
PGX_PG11_CONFIG=~/cargo-target-dir/postgresql-11/pgx-install/bin/pg_config
PGX_PG12_CONFIG=~/cargo-target-dir/postgresql-12/pgx-install/bin/pg_config
CARGO_TARGET_DIR=~/cargo-target-dir
cargo pgx test pg${{ matrix.version }}
- name: build srf example
run: |
cd pgx-examples/srf &&
PGX_PG10_CONFIG=~/cargo-target-dir/postgresql-10/pgx-install/bin/pg_config
PGX_PG11_CONFIG=~/cargo-target-dir/postgresql-11/pgx-install/bin/pg_config
PGX_PG12_CONFIG=~/cargo-target-dir/postgresql-12/pgx-install/bin/pg_config
CARGO_TARGET_DIR=~/cargo-target-dir
cargo pgx test pg${{ matrix.version }}
- name: build strings example
run: |
cd pgx-examples/strings &&
PGX_PG10_CONFIG=~/cargo-target-dir/postgresql-10/pgx-install/bin/pg_config
PGX_PG11_CONFIG=~/cargo-target-dir/postgresql-11/pgx-install/bin/pg_config
PGX_PG12_CONFIG=~/cargo-target-dir/postgresql-12/pgx-install/bin/pg_config
CARGO_TARGET_DIR=~/cargo-target-dir
cargo pgx test pg${{ matrix.version }}
- name: dir listing
run: find ~/ -type f | grep -v ".git"
2 changes: 1 addition & 1 deletion cargo-pgx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ readme = "README.md"

[dependencies]
clap = { version = "2.33.1", features = [ "yaml" ] }
colored = "1.9.3"
colored = "2.0.0"
pgx-utils = { path = "../pgx-utils", version = "0.0.4" }
proc-macro2 = { version = "1.0.18", features = [ "span-locations" ] }
quote = "1.0.7"
Expand Down
81 changes: 53 additions & 28 deletions cargo-pgx/src/extension_installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
// governed by the MIT license that can be found in the LICENSE file.

use crate::property_inspector::{find_control_file, get_property};
use colored::*;
use colored::Colorize;
use pgx_utils::{get_pg_download_dir, run_pg_config};
use std::io::Write;
use std::path::PathBuf;
use std::process::{Command, Stdio};
use std::result::Result;
use std::str::FromStr;

macro_rules! exit {
() => ({ exit!("explicit panic") });
($msg:expr) => ({ exit!("{}", $msg) });
($msg:expr,) => ({ exit!($msg) });
($fmt:expr, $($arg:tt)+) => ({
eprint!("{} ", " [error]".bold().red());
eprintln!($fmt, $($arg)+);
std::process::exit(1);
});
}

pub(crate) fn install_extension(is_release: bool) -> Result<(), std::io::Error> {
let (control_file, extname) = find_control_file()?;
let target_dir = get_target_dir();

if &std::env::var("PGX_NO_BUILD").unwrap_or_default() != "true" {
eprintln!("building extension");
build_extension(is_release, target_dir.to_str().unwrap())?;
} else {
eprintln!(
Expand All @@ -22,30 +35,44 @@ pub(crate) fn install_extension(is_release: bool) -> Result<(), std::io::Error>
);
}

eprintln!();
eprintln!("installing extension");
let pkgdir = get_pkglibdir();
let extdir = get_extensiondir();
let (libpath, libfile) =
find_library_file(&target_dir.display().to_string(), &extname, is_release)?;

let src = control_file.clone();
let dest = format!("{}/{}", extdir, control_file);
if let Err(e) = std::fs::copy(src, dest) {
panic!(
if let Err(e) = std::fs::copy(&src, &dest) {
exit!(
"failed copying control file ({}) to {}: {}",
control_file, extdir, e
control_file,
extdir,
e
);
}
println!(
"{} control file to: {}",
" Copying".bold().green(),
dest
);

let src = format!("{}/{}", libpath, libfile);
let dest = format!("{}/{}.so", pkgdir, extname);
println!("Copying {} to {}", src, dest);
if let Err(e) = std::fs::copy(src, dest) {
panic!("failed copying library ({}) to {}: {}", libfile, pkgdir, e);
if let Err(e) = std::fs::copy(&src, &dest) {
exit!("failed copying library ({}) to {}: {}", libfile, pkgdir, e);
}
println!(
"{} shared library to: {}",
" Copying".bold().green(),
dest
);

crate::generate_schema()?;
copy_sql_files(&extdir, &extname)?;

println!("{} installing {}", " Finished".bold().green(), extname);
Ok(())
}

Expand All @@ -69,6 +96,7 @@ fn build_extension(is_release: bool, target_dir: &str) -> Result<(), std::io::Er

let mut process = command
.env("CARGO_TARGET_DIR", target_dir.to_string())
.env("PG_DOWNLOAD_TARGET_DIR", get_pg_download_dir())
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.spawn()?;
Expand All @@ -86,6 +114,11 @@ fn copy_sql_files(extdir: &str, extname: &str) -> Result<(), std::io::Error> {
let target_filename =
PathBuf::from_str(&format!("{}/{}--{}.sql", extdir, extname, get_version())).unwrap();
let mut sql = std::fs::File::create(&target_filename).unwrap();
println!(
"{} {}",
" Writing".bold().green(),
target_filename.display()
);

// write each sql file from load-order.txt to the version.sql file
for file in load_order {
Expand Down Expand Up @@ -118,7 +151,7 @@ fn copy_sql_files(extdir: &str, extname: &str) -> Result<(), std::io::Error> {
let dest = format!("{}/{}", extdir, filename);

if let Err(e) = std::fs::copy(f.path(), &dest) {
panic!("failed copying SQL {} to {}: {}", filename, dest, e)
exit!("failed copying SQL {} to {}: {}", filename, dest, e)
}
}
}
Expand All @@ -138,6 +171,10 @@ fn find_library_file(
format!("{}/debug", target_dir)
});

if !path.exists() {
exit!("target directory does not exist: {}", path.display());
}

for f in std::fs::read_dir(&path)? {
if let Ok(f) = f {
let filename = f.file_name().into_string().unwrap();
Expand All @@ -153,44 +190,32 @@ fn find_library_file(
}
}

panic!("couldn't find library file in: {}", target_dir);
exit!("couldn't find library file in: {}", target_dir);
}
fn get_version() -> String {
match get_property("default_version") {
Some(v) => v,
None => panic!("couldn't determine version number"),
None => exit!("couldn't determine version number"),
}
}

fn get_target_dir() -> PathBuf {
PathBuf::from(std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| {
let target_dir = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| {
let mut out_dir = PathBuf::from(std::env::current_dir().unwrap());
out_dir.push("target");
out_dir.display().to_string()
}))
});

PathBuf::from(&format!("{}/pgx-install", target_dir))
}

fn get_pkglibdir() -> String {
run_pg_config("--pkglibdir")
run_pg_config(&None, "--pkglibdir")
}

fn get_extensiondir() -> String {
let mut dir = run_pg_config("--sharedir");
let mut dir = run_pg_config(&None, "--sharedir");

dir.push_str("/extension");
dir
}

fn run_pg_config(arg: &str) -> String {
let pg_config = std::env::var("PG_CONFIG").unwrap_or_else(|_| "pg_config".to_string());
let output = Command::new(pg_config).arg(arg).output();

match output {
Ok(output) => String::from_utf8(output.stdout).unwrap().trim().to_string(),

Err(e) => {
eprintln!("{}: Problem running pg_config: {}", "error".bold().red(), e);
std::process::exit(1);
}
}
}
15 changes: 12 additions & 3 deletions cargo-pgx/src/test_runner.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2020 ZomboDB, LLC <zombodb@gmail.com>. All rights reserved. Use of this source code is
// governed by the MIT license that can be found in the LICENSE file.


use std::process::{Command, Stdio};

pub fn test_extension(version: &str) -> Result<(), std::io::Error> {
Expand All @@ -12,17 +11,27 @@ pub fn test_extension(version: &str) -> Result<(), std::io::Error> {
};

for version in versions {
let cargo_target_dir = std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| {
format!(
"{}/target",
std::env::current_dir()
.expect("couldn't detect current directory")
.display()
)
});
let pgx_target_dir = format!("{}/pgx-test-{}", cargo_target_dir, version);

let result = Command::new("cargo")
.stdout(Stdio::inherit())
.stderr(Stdio::inherit())
.arg("test")
.arg("--target-dir")
.arg(std::env::var("CARGO_TARGET_DIR").unwrap_or_else(|_| "target".to_string()))
.arg("--all")
.arg("--features")
.arg(version)
.arg("--no-default-features")
.env("RUST_BACKTRACE", "1")
.env("CARGO_TARGET_DIR", pgx_target_dir)
.env("PG_DOWNLOAD_TARGET_DIR", cargo_target_dir)
.status();

if result.is_err() {
Expand Down
2 changes: 1 addition & 1 deletion pgx-examples/strings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pg_module_magic!();

#[pg_extern]
fn return_static() -> &'static str {
"This is a static string"
"This is a static string xxx"
}

#[pg_extern]
Expand Down
2 changes: 1 addition & 1 deletion pgx-pg-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pgx-macros = { path = "../pgx-macros/", version="0.0.4" }
bindgen = "=0.54.0"
build-deps = "0.1.4"
num_cpus = "1.13.0"
clang = "0.24.0"
pgx-utils = { path = "../pgx-utils/", version="0.0.4" }
proc-macro2 = "1.0.18"
quote = "1.0.7"
rayon = "1.3.1"
Expand Down
Loading