Skip to content

Commit

Permalink
Use domain-qualified name for extension
Browse files Browse the repository at this point in the history
An experiment to see whether the current PostgreSQL extension
installation and configuration can be abused to avoid namespace
collision between extensions with the same names. This shows that
for relocatable extensions, at least, it can be done, but it's
pretty ugly: Everything has to refer to the full extension name ---
here `github.com+theory+pg-semver` --- including file names:

*   Control file
*   SQL files
*   The `MODULERDIR` file in `Makefile` to install files into a
    directory with the long extension name
*   The `directory` entry in the control file to match `MODULERDIR`

This change doesn't actually rename all of the `sql/*.sql` files, but
updates the Makefile to generate the
`sql/github.com+theory+pg-semver--$VERSION.sql` file

Also had to update the SQL file to use `MODULE_PATHNAME` instead of
`semver` so that it can be dynamically set, and change the tests to use
`CREATE EXTENSION` to create the extension to test, rather than load the
file directly. The latter was required for PostgreSQL 9.0 and earlier,
which are no longer supported anyway.

It's possible a lot of this could be automated in the `Makefile`:
replace a short name with a longer name, move and rename files, etc.,
but it'd be kind of a pain. The alternative is to rename everything
appropriately in the repository, but that'd be ugly. Besides, changing
the extension name like this means that older versions would never be
properly upgraded.

It'd be nicer if PostgreSQL itself were updated to support some sort of
schema packaging, where everything could go into one directory named for
the repository. That's probably the smarter long-term goal.
  • Loading branch information
theory committed Jan 29, 2024
1 parent 88b3abd commit c21646c
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ tmp/
*.o
regression.diffs
regression.out
/sql/semver--?.??.?.sql
/sql/github.com+theory+pg-semver--?.??.?.sql
/semver-*
/latest-changes.md
/src/*.bc
Expand Down
4 changes: 2 additions & 2 deletions META.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "semver",
"name": "github.com+theory+pg-semver",
"abstract": "A semantic version data type",
"description": "A Postgres data type for the Semantic Version format with support for btree and hash indexing.",
"version": "0.32.1",
Expand All @@ -11,7 +11,7 @@
],
"license": "postgresql",
"provides": {
"semver": {
"github.com+theory+pg-semver": {
"abstract": "A semantic version data type",
"file": "sql/semver.sql",
"docfile": "doc/semver.mmd",
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
NAME = semver
EXTENSION = $(shell grep -m 1 '"name":' META.json | \
sed -e 's/[[:space:]]*"name":[[:space:]]*"\([^"]*\)",/\1/')
EXTVERSION = $(shell grep -m 1 '[[:space:]]\{8\}"version":' META.json | \
sed -e 's/[[:space:]]*"version":[[:space:]]*"\([^"]*\)",\{0,1\}/\1/')
DISTVERSION = $(shell grep -m 1 '[[:space:]]\{3\}"version":' META.json | \
sed -e 's/[[:space:]]*"version":[[:space:]]*"\([^"]*\)",\{0,1\}/\1/')

MODULEDIR = semver
MODULEDIR = $(EXTENSION)
DATA = $(wildcard sql/*.sql)
DOCS = $(wildcard doc/*.mmd)
TESTS = $(wildcard test/sql/*.sql)
Expand All @@ -25,7 +26,7 @@ include $(PGXS)

all: sql/$(EXTENSION)--$(EXTVERSION).sql

sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(EXTENSION).sql
sql/$(EXTENSION)--$(EXTVERSION).sql: sql/$(NAME).sql
cp $< $@

.PHONY: results
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
semver 0.32.0
=============

Namespace Experiment
--------------------

Experimental branch to try naming an extension with a source code repository URL
similar to Go packages.

[![PGXN version](https://badge.fury.io/pg/semver.svg)](https://badge.fury.io/pg/semver)
[![Build Status](https://github.com/theory/pg-semver/workflows/CI/badge.svg)](https://github.com/theory/pg-semver/actions)

Expand Down
6 changes: 6 additions & 0 deletions github.com+theory+pg-semver.control
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# github.com+theory+pg-semver extension
comment = 'Semantic version data type'
default_version = '0.32.1'
directory = 'github.com+theory+pg-semver'
module_pathname = '$libdir/github.com+theory+pg-semver'
relocatable = true
7 changes: 0 additions & 7 deletions semver.control

This file was deleted.

44 changes: 22 additions & 22 deletions sql/semver.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,22 @@ CREATE TYPE semver;
--
CREATE OR REPLACE FUNCTION semver_in(cstring)
RETURNS semver
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION semver_out(semver)
RETURNS cstring
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION semver_recv(internal)
RETURNS semver
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION semver_send(semver)
RETURNS bytea
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

--
Expand All @@ -67,7 +67,7 @@ CREATE TYPE semver (

CREATE OR REPLACE FUNCTION to_semver(text)
RETURNS semver
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

--
Expand All @@ -76,12 +76,12 @@ CREATE OR REPLACE FUNCTION to_semver(text)

CREATE OR REPLACE FUNCTION semver(text)
RETURNS semver
AS 'semver', 'text_to_semver'
AS 'MODULE_PATHNAME', 'text_to_semver'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION text(semver)
RETURNS text
AS 'semver', 'semver_to_text'
AS 'MODULE_PATHNAME', 'semver_to_text'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION semver(numeric)
Expand Down Expand Up @@ -127,7 +127,7 @@ CREATE CAST (bigint AS semver) WITH FUNCTION semver(bigint);

CREATE OR REPLACE FUNCTION semver_eq(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR = (
Expand All @@ -143,7 +143,7 @@ CREATE OPERATOR = (

CREATE OR REPLACE FUNCTION semver_ne(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR <> (
Expand All @@ -157,7 +157,7 @@ CREATE OPERATOR <> (

CREATE OR REPLACE FUNCTION semver_le(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR <= (
Expand All @@ -169,7 +169,7 @@ CREATE OPERATOR <= (

CREATE OR REPLACE FUNCTION semver_lt(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR < (
Expand All @@ -181,7 +181,7 @@ CREATE OPERATOR < (

CREATE OR REPLACE FUNCTION semver_ge(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR >= (
Expand All @@ -193,7 +193,7 @@ CREATE OPERATOR >= (

CREATE OR REPLACE FUNCTION semver_gt(semver, semver)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OPERATOR > (
Expand All @@ -209,12 +209,12 @@ CREATE OPERATOR > (

CREATE OR REPLACE FUNCTION semver_cmp(semver, semver)
RETURNS int4
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION hash_semver(semver)
RETURNS int4
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

--
Expand Down Expand Up @@ -245,7 +245,7 @@ DEFAULT FOR TYPE semver USING hash AS

CREATE OR REPLACE FUNCTION semver_smaller(semver, semver)
RETURNS semver
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE AGGREGATE min(semver) (
Expand All @@ -256,7 +256,7 @@ CREATE AGGREGATE min(semver) (

CREATE OR REPLACE FUNCTION semver_larger(semver, semver)
RETURNS semver
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE AGGREGATE max(semver) (
Expand All @@ -271,7 +271,7 @@ CREATE AGGREGATE max(semver) (

CREATE OR REPLACE FUNCTION is_semver(text)
RETURNS bool
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

--
Expand All @@ -280,22 +280,22 @@ CREATE OR REPLACE FUNCTION is_semver(text)

CREATE OR REPLACE FUNCTION get_semver_major(semver)
RETURNS int4
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION get_semver_minor(semver)
RETURNS int4
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION get_semver_patch(semver)
RETURNS int4
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE OR REPLACE FUNCTION get_semver_prerelease(semver)
RETURNS text
AS 'semver'
AS 'MODULE_PATHNAME'
LANGUAGE C STRICT IMMUTABLE;

CREATE TYPE semverrange AS RANGE (SUBTYPE = semver);
File renamed without changes.
2 changes: 1 addition & 1 deletion test/sql/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
BEGIN;

\i test/pgtap-core.sql
\i sql/semver.sql
CREATE EXTENSION "github.com+theory+pg-semver";

SELECT plan(334);
--SELECT * FROM no_plan();
Expand Down
2 changes: 1 addition & 1 deletion test/sql/corpus.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ BEGIN;
-- Test the SemVer corpus from https://regex101.com/r/Ly7O1x/3/.

\i test/pgtap-core.sql
\i sql/semver.sql
CREATE EXTENSION "github.com+theory+pg-semver";

SELECT plan(71);
--SELECT * FROM no_plan();
Expand Down

0 comments on commit c21646c

Please sign in to comment.