Skip to content

Commit

Permalink
Support arbitrary versions of Postgres, and add PG13 support (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeebbbbrrrr authored Nov 15, 2020
1 parent b019d26 commit 8354093
Show file tree
Hide file tree
Showing 80 changed files with 210,676 additions and 78,632 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

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

steps:
Expand All @@ -33,14 +33,14 @@ jobs:
~/.cargo/git
~/.cargo/cargo-pgx
target
key: ${{ matrix.os }}-cargo-${{ hashFiles('~/work/pgx/pgx/**/Cargo.toml') }}
key: ${{ matrix.os }}-${{ matrix.version }}-cargo-${{ hashFiles('~/work/pgx/pgx/**/Cargo.toml') }}

- name: cache cargo pgx init
id: cargo-pgx-init-cache
uses: actions/cache@v2
with:
path: ~/.pgx/
key: ${{ matrix.os }}-pgx-init-${{ hashFiles('~/work/pgx/pgx/**/*.h') }}
key: ${{ matrix.os }}-${{ matrix.version }}-pgx-init-${{ hashFiles('~/work/pgx/pgx/**/*.h') }}

- name: cargo version
run: cargo --version
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

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

steps:
Expand All @@ -33,14 +33,14 @@ jobs:
~/.cargo/git
~/.cargo/cargo-pgx
target
key: ${{ matrix.os }}-cargo-${{ hashFiles('~/work/pgx/pgx/**/Cargo.toml') }}
key: ${{ matrix.os }}-${{ matrix.version }}-cargo-${{ hashFiles('~/work/pgx/pgx/**/Cargo.toml') }}

- name: cache cargo pgx init
id: cargo-pgx-init-cache
uses: actions/cache@v2
with:
path: ~/.pgx/
key: ${{ matrix.os }}-pgx-init-${{ hashFiles('~/work/pgx/pgx/**/*.h') }}
key: ${{ matrix.os }}-${{ matrix.version }}-pgx-init-${{ hashFiles('~/work/pgx/pgx/**/*.h') }}

- name: cargo version
run: cargo --version
Expand Down
11 changes: 6 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "pgx-parent"
version = "0.0.18"
version = "0.1.1"
authors = ["ZomboDB, LLC <[email protected]>"]
edition = "2018"
license = "MIT"
Expand Down Expand Up @@ -33,13 +33,14 @@ exclude = [

[features]
default = [ ]
pg10 = [ "pgx/pg10", "pgx-pg-sys/pg10" ]
pg11 = [ "pgx/pg11", "pgx-pg-sys/pg11" ]
pg12 = [ "pgx/pg12", "pgx-pg-sys/pg12" ]
pg10 = [ "pgx/pg10", "pgx-pg-sys/pg10", "pgx-tests/pg10" ]
pg11 = [ "pgx/pg11", "pgx-pg-sys/pg11", "pgx-tests/pg11" ]
pg12 = [ "pgx/pg12", "pgx-pg-sys/pg12", "pgx-tests/pg12" ]
pg13 = [ "pgx/pg13", "pgx-pg-sys/pg13", "pgx-tests/pg13" ]
pg_test = [ "pgx-tests/pg_test" ]

[package.metadata.docs.rs]
features = ["pg12"]
features = ["pg13"]
no-default-features = true

[dependencies]
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

`pgx` is a framework for developing PostgreSQL extensions in Rust and strives to be as idiomatic and safe as possible.

`pgx` supports Postgres v10, v11, and v12.
`pgx` supports Postgres v10, v11, v12, and v13.

Feel free to join our [Discord Server](https://discord.gg/hPb93Y9).

Expand All @@ -29,8 +29,8 @@ Feel free to join our [Discord Server](https://discord.gg/hPb93Y9).
- Create installation packages for your extension via `cargo pgx package`

#### Target Multiple Postgres Versions
- Support Postgres 10, 11, and 12, from the same codebase
- Postgres Rust bindings are organized into `common.rs` and `pgXX_specific.rs` modules
- Support Postgres v10, v11, v12, and v13 from the same codebase
- Postgres Rust bindings are organized into `pgXX.rs` modules
- Use Rust feature gating to use version-specific APIs
- Seamlessly test against all versions

Expand Down Expand Up @@ -138,10 +138,12 @@ Next, `pgx` needs to be initialized. You only need to do this once.
$ cargo pgx init
```

The `init` command downloads Postgres versions 10, 11, 12, and compiles them to `~/.pgx/`. These installations
The `init` command downloads Postgres versions v10, v11, v12, v13 and compiles them to `~/.pgx/`. These installations
are needed by `pgx` not only for auto-generating Rust bindings from each version's header files, but also for `pgx`'s
test framework.

See the documentation for [`cargo-pgx`](cargo-pgx/README.md) for details on how to limit the required postgres versions.


### Create a new extension

Expand All @@ -153,7 +155,7 @@ $ cargo pgx new my_extension

```shell script
$ cd my_extension
$ cargo pgx run pg12 # or pg10 or pg11
$ cargo pgx run pg13 # or pg10 or pg11 or pg12
```

`cargo pgx run` compiles the extension to a shared library, copies it to the specified Postgres installation (in `~/.pgx/`),
Expand Down
4 changes: 2 additions & 2 deletions cargo-pgx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cargo-pgx"
version = "0.0.18"
version = "0.1.1"
authors = ["ZomboDB, LLC <[email protected]>"]
edition = "2018"
license = "MIT"
Expand All @@ -15,7 +15,7 @@ clap = { version = "2.33.3", features = [ "yaml" ] }
colored = "2.0.0"
env_proxy = "0.4.1"
num_cpus = "1.13.0"
pgx-utils = { path = "../pgx-utils", version = "^0.0.18"}
pgx-utils = { path = "../pgx-utils", version = "^0.1.1"}
proc-macro2 = { version = "1.0.24", features = [ "span-locations" ] }
quote = "1.0.7"
rayon = "1.5.0"
Expand Down
43 changes: 31 additions & 12 deletions cargo-pgx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,20 @@ SUBCOMMANDS:

`cargo pgx init` is required to be run once to properly configure the `pgx` development environment.

As shown by the screenshot above, it downloads Postgres v10, 11, 12, configures them, compiles them, and installs them to `~/.pgx/`. Other `pgx` command such as `run` and `test` will fully manage and otherwise use these Postgres installations for you.
As shown by the screenshot above, it downloads Postgres v10, v11, v12, v13, configures them, compiles them, and installs them to `~/.pgx/`. Other `pgx` commands such as `run` and `test` will fully manage and otherwise use these Postgres installations for you.

`pgx` is designed to support Postgres v10, 11, 12 in such a way that during development, you'll know if you're trying to use a Postgres API that isn't common across all three versions. It's also designed to make testing your extension against these versions easy. This is why it requires you have three fully compiled and installed versions of Postgres during development.
`pgx` is designed to support Postgres v10, v11, v12, v13 in such a way that during development, you'll know if you're trying to use a Postgres API that isn't common across all three versions. It's also designed to make testing your extension against these versions easy. This is why it requires you have three fully compiled and installed versions of Postgres during development.

If you want to use your operating system's package manager to install Postgres, `cargo pgx init` has 3 optional arguments that allow you to specify where they're installed (see below).
If you want to use your operating system's package manager to install Postgres, `cargo pgx init` has optional arguments that allow you to specify where they're installed (see below).

What you're telling `cargo pgx init` is the full path to `pg_config` for each version.

For any version you specify, `cargo pgx init` will forego downloading/compiling/installing it. `pgx` will then use that locally-installed version just as it uses any version it downloads/compiles/installs itself.

However, if the unless the "path to pg_config" is the literal string `download`, the `pgx` will download and compile that version of Postgres for you.

When the various `--pgXX` options are specified, these are they **only** versions of Postgres that `pgx` will manage for you.

You'll also want to make sure you have the "postgresql-server-dev" package installed for each version you want to manage yourself.

Once complete, `cargo pgx init` also creates a configuration file (`~/.pgx/config.toml`) that describes where to find each version's `pg_config` tool.
Expand All @@ -86,9 +90,24 @@ FLAGS:
-V, --version Prints version information

OPTIONS:
--pg10 <PG10_PG_CONFIG> if installed locally, the path to PG10's 'pg_config' tool
--pg11 <PG11_PG_CONFIG> if installed locally, the path to PG11's 'pg_config' tool
--pg12 <PG12_PG_CONFIG> if installed locally, the path to PG12's 'pg_config' tool
initize pgx development environment for the first time

USAGE:
cargo pgx init [OPTIONS]

FLAGS:
-h, --help Prints help information
-V, --version Prints version information

OPTIONS:
--pg10 <PG10_PG_CONFIG> if installed locally, the path to PG10's 'pg_config' tool, or 'download' to have pgx
download/compile/install it
--pg11 <PG11_PG_CONFIG> if installed locally, the path to PG11's 'pg_config' tool, or 'download' to have pgx
download/compile/install it
--pg12 <PG12_PG_CONFIG> if installed locally, the path to PG12's 'pg_config' tool, or 'download' to have pgx
download/compile/install it
--pg13 <PG13_PG_CONFIG> if installed locally, the path to PG13's 'pg_config' tool, or 'download' to have pgx
download/compile/install it
```
## Creating a new Extension
Expand Down Expand Up @@ -136,7 +155,7 @@ Once started, you can connect to them using `psql` (if you have it on your $PATH
![run](run.png)
`cargo pgx run <pg10 | pg11 | pg12>` is the primary interface into compiling and interactively testing/using your extension during development.
`cargo pgx run <pg10 | pg11 | pg12 | pg13>` is the primary interface into compiling and interactively testing/using your extension during development.
The very first time you execute `cargo pgx run pgXX`, it needs to compile not only your extension, but pgx itself, along with all its dependencies. Depending on your computer, this could take a bit of time (`pgx` is nearly 200k lines of Rust when counting the generated bindings for Postgres). Afterwards, however (as seen in the above screenshot), it's fairly fast.
Expand All @@ -162,7 +181,7 @@ FLAGS:
-V, --version Prints version information
ARGS:
<PG_VERSION> Do you want to run against Postgres 'pg10', 'pg11', pg12'?
<PG_VERSION> Do you want to run against Postgres 'pg10', 'pg11', 'pg12', 'pg13'?
<DBNAME> The database to connect to (and create if the first time). Defaults to a database with the same
name as the current extension name
```
Expand All @@ -172,7 +191,7 @@ ARGS:
![connect](connect.png)
If you'd simply like to connect to a managed version of Postgres without re-compiling and installing
your extension, use `cargo pgx connect <pg10 | pg11 | pg12>`.
your extension, use `cargo pgx connect <pg10 | pg11 | pg12 | pg13>`.
This command will use the default database named for your extension, or you can specify another
database name as the final argument.
Expand All @@ -192,7 +211,7 @@ FLAGS:
-V, --version Prints version information
ARGS:
<PG_VERSION> Do you want to run against Postgres 'pg10', 'pg11', pg12'?
<PG_VERSION> Do you want to run against Postgres 'pg10', 'pg11', 'pg12', 'pg13'?
<DBNAME> The database to connect to (and create if the first time). Defaults to a database with the same
name as the current extension name
```
Expand Down Expand Up @@ -227,7 +246,7 @@ $ cargo pgx install --help
![test](test.png)
`cargo pgx test [pg10 | pg11 | pg12]` runs your `#[test]` and `#[pg_test]` annotated functions using cargo's test system.
`cargo pgx test [pg10 | pg11 | pg12 | pg13]` runs your `#[test]` and `#[pg_test]` annotated functions using cargo's test system.
During the testing process, `pgx` starts a tempory instance of Postgres with its `PGDATA` directory in `./target/pgx-test-data-PGVER/`. This Postgres instance is stopped as soon as the test framework has finished.
Expand All @@ -252,7 +271,7 @@ FLAGS:
-V, --version Prints version information
ARGS:
<PG_VERSION> Do you want to test for Postgres 'pg10', 'pg11', pg12', or 'all' (default)?
<PG_VERSION> Do you want to test for Postgres 'pg10', 'pg11', 'pg12', 'pg13', or 'all' (default)?
```
## Building an Installation Package
Expand Down
22 changes: 14 additions & 8 deletions cargo-pgx/src/cli.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,41 @@ subcommands:
value_name: PG10_PG_CONFIG
takes_value: true
required: false
help: if installed locally, the path to PG10's 'pg_config' tool
help: if installed locally, the path to PG10's 'pg_config' tool, or 'download' to have pgx download/compile/install it
- pg11:
long: pg11
value_name: PG11_PG_CONFIG
takes_value: true
required: false
help: if installed locally, the path to PG11's 'pg_config' tool
help: if installed locally, the path to PG11's 'pg_config' tool, or 'download' to have pgx download/compile/install it
- pg12:
long: pg12
value_name: PG12_PG_CONFIG
takes_value: true
required: false
help: if installed locally, the path to PG12's 'pg_config' tool
help: if installed locally, the path to PG12's 'pg_config' tool, or 'download' to have pgx download/compile/install it
- pg13:
long: pg13
value_name: PG13_PG_CONFIG
takes_value: true
required: false
help: if installed locally, the path to PG13's 'pg_config' tool, or 'download' to have pgx download/compile/install it
- start:
about: start a pgx-managed Postgres instance
args:
- pg_version:
value_name: PG_VERSION
takes_value: true
required: true
help: the Postgres version to start ('pg10', 'pg11', pg12', or 'all')
help: the Postgres version to start ('pg10', 'pg11', 'pg12', 'pg13', or 'all')
- stop:
about: stop a pgx-managed Postgres instance
args:
- pg_version:
value_name: PG_VERSION
takes_value: true
required: true
help: the Postgres version to stop ('pg10', 'pg11', pg12', or 'all')
help: the Postgres version to stop ('pg10', 'pg11', 'pg12', 'pg13', or 'all')
- status:
about: is a pgx-managed Postgres instance running?
args:
Expand Down Expand Up @@ -87,7 +93,7 @@ subcommands:
value_name: PG_VERSION
takes_value: true
required: true
help: Do you want to run against Postgres 'pg10', 'pg11', pg12'?
help: Do you want to run against Postgres 'pg10', 'pg11', 'pg12', 'pg13'?
- dbname:
value_name: DBNAME
takes_value: true
Expand All @@ -103,7 +109,7 @@ subcommands:
value_name: PG_VERSION
takes_value: true
required: true
help: Do you want to run against Postgres 'pg10', 'pg11', pg12'?
help: Do you want to run against Postgres 'pg10', 'pg11', 'pg12', 'pg13'?
- dbname:
value_name: DBNAME
takes_value: true
Expand All @@ -114,7 +120,7 @@ subcommands:
- pg_version:
value_name: PG_VERSION
takes_value: true
help: Do you want to test for Postgres 'pg10', 'pg11', pg12', or 'all' (default)?
help: Do you want to test for Postgres 'pg10', 'pg11', 'pg12', 'pg13', or 'all' (default)?
- release:
short: r
long: release
Expand Down
17 changes: 6 additions & 11 deletions cargo-pgx/src/commands/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,15 @@
use crate::commands::run::exec_psql;
use crate::commands::start::start_postgres;
use colored::Colorize;
use pgx_utils::{createdb, BASE_POSTGRES_PORT_NO};
use pgx_utils::createdb;
use pgx_utils::pg_config::PgConfig;

pub(crate) fn connect_psql(major_version: u16, dbname: &str) {
pub(crate) fn connect_psql(pg_config: &PgConfig, dbname: &str) -> Result<(), std::io::Error> {
// restart postgres
start_postgres(major_version);
start_postgres(pg_config)?;

// create the named database
if !createdb(
major_version,
"localhost",
BASE_POSTGRES_PORT_NO + major_version,
dbname,
true,
) {
if !createdb(pg_config, dbname, false, true)? {
println!(
"{} existing database {}",
" Re-using".bold().cyan(),
Expand All @@ -26,5 +21,5 @@ pub(crate) fn connect_psql(major_version: u16, dbname: &str) {
}

// run psql
exec_psql(major_version, dbname);
exec_psql(pg_config, dbname)
}
4 changes: 2 additions & 2 deletions cargo-pgx/src/commands/get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ pub fn get_property(name: &str) -> Option<String> {

pub(crate) fn find_control_file() -> (PathBuf, String) {
for f in handle_result!(
"cannot open current directory for reading",
std::fs::read_dir(".")
std::fs::read_dir("."),
"cannot open current directory for reading"
) {
if f.is_ok() {
if let Ok(f) = f {
Expand Down
Loading

0 comments on commit 8354093

Please sign in to comment.