feat(tools): cleanup scripts and Makefile

This commit is contained in:
2026-02-22 20:33:04 +01:00
parent 85f53eff31
commit 5957ac4789
7 changed files with 166 additions and 152 deletions
Vendored
BIN
View File
Binary file not shown.
+35 -43
View File
@@ -1,78 +1,70 @@
# Makefile for pg_morbac PostgreSQL Extension
# Multi-OrBAC: Organization-Based Access Control
EXTENSION = pg_morbac
PROJECT_FILENAME = pg_morbac
# Read version from control file
EXTVERSION = $(shell grep default_version $(EXTENSION).control | sed "s/default_version = '\(.*\)'/\1/")
# Read version from control file using centralized script
PROJECT_VERSION = $(shell ./tools/get_version.sh $(PROJECT_FILENAME).control)
# For development, work on source files in src/
SRC_DIR = src
DEV_SQL = $(EXTENSION).sql
DATA = $(EXTENSION)--$(EXTVERSION).sql
DOCS = README.md
OUTPUT_DEV_FILENAME = $(PROJECT_FILENAME).sql
OUTPUT_RELEASE_FILENAME = $(PROJECT_FILENAME)--$(PROJECT_VERSION).sql
# PostgreSQL extension build
# PostgreSQL Extension build
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)
# Custom targets
# Build versioned SQL file from source files
.PHONY: build
build:
@echo "Building release $(EXTVERSION) from source files..."
@./tools/build.sh
@if [ ! -f $(DEV_SQL) ]; then \
echo "Error: Build failed"; \
@./tools/build.sh $(SRC_DIR) $(OUTPUT_DEV_FILENAME)
@if [ ! -f $(OUTPUT_DEV_FILENAME) ]; then \
echo "Error: Build failed" >&2; \
exit 1; \
fi
cp $(DEV_SQL) $(DATA)
@echo "Created $(DATA)"
@echo "Ready to install or tag: git tag v$(EXTVERSION)"
# Install extension in PostgreSQL (creates versioned file if needed)
# Rename development file to versioned release file
.PHONY: release
release: build
mv $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
@echo "Release $(PROJECT_VERSION) built at $(OUTPUT_RELEASE_FILENAME)."
# Build and install extension in PostgreSQL
.PHONY: install
install: build
@echo "Installing pg_morbac extension..."
$(MAKE) -f Makefile install
@cp $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
@./tools/install.sh $(PROJECT_FILENAME) $(PROJECT_VERSION)
# Run tests with development file in temporary database
.PHONY: test
test: build
@echo "Testing with development file..."
-dropdb morbac_test 2>/dev/null || true
createdb morbac_test
@./tools/test.sh morbac_test
dropdb morbac_test
@echo "Test completed."
@dropdb morbac_test 2>/dev/null || true
@createdb morbac_test
@./tools/test.sh morbac_test $(OUTPUT_DEV_FILENAME)
@dropdb morbac_test
# Uninstall extension
# Uninstall extension from PostgreSQL
.PHONY: uninstall
uninstall:
@echo "Uninstalling pg_morbac extension..."
$(MAKE) -f Makefile uninstall
@echo "Uninstalling $(PROJECT_FILENAME)..."
@./tools/uninstall.sh $(PROJECT_FILENAME)
# Clean up generated files
.PHONY: clean
clean:
@rm -f $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
# Show help
.PHONY: help
help:
@echo "pg_morbac Makefile targets:"
@echo ""
@echo "Development:"
@echo " Edit source files in src/ directory"
@echo ""
@echo " make build - Build versioned pg_morbac--X.Y.Z.sql from src/"
@echo " make build - Build $(OUTPUT_DEV_FILENAME) from src/"
@echo " make release - Build versioned $(PROJECT_FILENAME)--X.Y.Z.sql from src/"
@echo " make test - Build and run test suite"
@echo " make install - Build and install extension to PostgreSQL"
@echo " make uninstall - Uninstall extension from PostgreSQL"
@echo " make install - Build and install $(PROJECT_FILENAME) to PostgreSQL"
@echo " make uninstall - Uninstall $(PROJECT_FILENAME) from PostgreSQL"
@echo " make clean - Clean up generated files"
@echo " make help - Show this help"
@echo ""
@echo "Workflow:"
@echo " 1. Edit source files in src/"
@echo " 2. make test (build and test changes)"
@echo " 3. Update version in pg_morbac.control"
@echo " 4. make build (builds versioned SQL)"
@echo " 5. git tag vX.Y.Z && git push --tags"
@echo ""
@echo ""
+9 -18
View File
@@ -1,28 +1,21 @@
#!/bin/bash
# =============================================================================
# Build Script for pg_morbac Extension
# =============================================================================
# Concatenates SQL source files in src/ into a single pg_morbac.sql file
# Order: header first, then specific order for dependencies, footer last
# =============================================================================
set -e
# build.sh - Build the PostgreSQL extension SQL file from source components
# Usage: ./build.sh [source_directory] [output_file]
SRC_DIR="src"
OUTPUT="pg_morbac.sql"
set -e # Exit immediately if a command exits with a non-zero status
echo "Building pg_morbac.sql from source files..."
SRC_DIR="${1:-src}"
OUTPUT="${2:-pg_morbac.sql}"
# Check if src directory exists
if [ ! -d "$SRC_DIR" ]; then
echo "Error: $SRC_DIR directory not found"
echo "Error: source directory not found: $SRC_DIR" >&2
echo "Ensure the source directory exists in the project root" >&2
exit 1
fi
# Remove old output file
rm -f "$OUTPUT"
# Define build order (dependencies matter)
BUILD_ORDER=(
"header.sql"
"types.sql"
@@ -50,17 +43,15 @@ BUILD_ORDER=(
file_count=0
# Build in specified order
for filename in "${BUILD_ORDER[@]}"; do
filepath="$SRC_DIR/$filename"
if [ -f "$filepath" ]; then
echo " Adding: $filename"
cat "$filepath" >> "$OUTPUT"
echo "" >> "$OUTPUT"
((file_count++))
else
echo " Warning: $filename not found, skipping"
echo "Warning: $filename not found, skipping" >&2
fi
done
echo "Built $OUTPUT successfully ($file_count source files)"
echo "Built $OUTPUT ($file_count files)"
+22
View File
@@ -0,0 +1,22 @@
#!/bin/bash
# get_version.sh - Extract version from PostgreSQL extension control file
# Usage: ./get_version.sh [control_file]
set -e # Exit immediately if a command exits with a non-zero status
CONTROL_FILE="${1:-pg_morbac.control}"
if [ ! -f "$CONTROL_FILE" ]; then
echo "Error: control file not found: $CONTROL_FILE" >&2
exit 1
fi
VERSION=$(grep "default_version" "$CONTROL_FILE" | sed "s/default_version = '\(.*\)'/\1/")
if [ -z "$VERSION" ]; then
echo "Error: could not extract version from $CONTROL_FILE" >&2
exit 1
fi
echo "$VERSION"
+25 -61
View File
@@ -1,87 +1,51 @@
#!/bin/bash
# Installation script for pg_morbac extension
set -e
# install.sh - Install the PostgreSQL extension to the system's extension directory
# Usage: ./install.sh [project_name] [version]
echo "=================================="
echo "pg_morbac Extension Installer"
echo "=================================="
echo ""
set -e # Exit immediately if a command exits with a non-zero status
# Check if running as root or with sudo
if [ "$EUID" -ne 0 ] && [ -z "$SUDO_USER" ]; then
echo "This script needs to copy files to PostgreSQL extension directory."
echo "Please run with sudo or as root."
echo ""
echo "Usage: sudo ./install.sh"
exit 1
fi
PROJECT_FILENAME="${1:-pg_morbac}"
PROJECT_VERSION="${2}"
# Check if pg_config is available
if ! command -v pg_config &> /dev/null; then
echo "Error: pg_config not found in PATH"
echo "Please install PostgreSQL development packages:"
echo " Ubuntu/Debian: sudo apt-get install postgresql-server-dev-all"
echo " RedHat/CentOS: sudo yum install postgresql-devel"
echo " macOS: brew install postgresql"
echo "Error: pg_config not found" >&2
echo "Install: apt install postgresql-server-dev-all (Debian/Ubuntu), yum install postgresql-devel (RHEL/CentOS), or brew install postgresql (macOS)" >&2
exit 1
fi
# Get PostgreSQL extension directory
EXTDIR=$(pg_config --sharedir)/extension
echo "PostgreSQL extension directory: $EXTDIR"
echo ""
# Check if directory exists
if [ ! -d "$EXTDIR" ]; then
echo "Error: Extension directory does not exist: $EXTDIR"
echo "Error: extension directory not found: $EXTDIR" >&2
exit 1
fi
# Detect version from control file
CONTROL_FILE="pg_morbac.control"
CONTROL_FILE="${PROJECT_FILENAME}.control"
if [ ! -f "$CONTROL_FILE" ]; then
echo "Error: $CONTROL_FILE not found"
echo "Please run this script from the pg_morbac directory"
echo "Error: control file not found: $CONTROL_FILE" >&2
echo "Run this script from the project root directory" >&2
exit 1
fi
VERSION=$(grep "default_version" "$CONTROL_FILE" | sed "s/default_version = '\(.*\)'/\1/")
if [ -z "$VERSION" ]; then
echo "Error: Could not detect version from $CONTROL_FILE"
if [ -z "$PROJECT_VERSION" ]; then
SCRIPT_DIR=$(dirname "$0")
PROJECT_VERSION=$("$SCRIPT_DIR/get_version.sh" "$CONTROL_FILE")
fi
if [ -z "$PROJECT_VERSION" ]; then
echo "Error: could not detect version from $CONTROL_FILE" >&2
exit 1
fi
SQL_FILE="pg_morbac--${VERSION}.sql"
echo "Detected version: $VERSION"
echo ""
# Check if versioned SQL file exists
SQL_FILE="${PROJECT_FILENAME}--${PROJECT_VERSION}.sql"
if [ ! -f "$SQL_FILE" ]; then
echo "Error: $SQL_FILE not found"
echo "Please run 'make build' first to create the versioned SQL file"
echo "Error: versioned SQL file not found: $SQL_FILE" >&2
echo "Run 'make build' to generate the required file" >&2
exit 1
fi
# Copy extension files
echo "Installing extension files..."
cp -v pg_morbac.control "$EXTDIR/"
cp -v "$SQL_FILE" "$EXTDIR/"
echo "Installing ${PROJECT_FILENAME} v${PROJECT_VERSION} to ${EXTDIR}"
cp "$CONTROL_FILE" "$EXTDIR/"
mv "$SQL_FILE" "$EXTDIR/"
echo ""
echo "=================================="
echo "Installation complete!"
echo "=================================="
echo ""
echo "To use the extension in your database:"
echo ""
echo " psql -d your_database -c 'CREATE EXTENSION pg_morbac;'"
echo ""
echo "To run tests:"
echo ""
echo " make test"
echo ""
echo "For more information, see README.md"
echo ""
echo "Installation complete. Enable with: CREATE EXTENSION ${PROJECT_FILENAME};"
+30 -30
View File
@@ -1,48 +1,48 @@
#!/bin/bash
# =============================================================================
# Test Runner for pg_morbac Extension
# =============================================================================
# Runs all tests in order, loading setup data and executing each test file.
# Usage: ./tools/test.sh [database_name]
# =============================================================================
set -e
# test.sh - Run tests against the PostgreSQL extension using psql
# Usage: ./test.sh [database_name] [sql_file]
set -e # Exit immediately if a command exits with a non-zero status
# Database name (default: morbac_test)
DB="${1:-morbac_test}"
SQL_FILE="${2:-pg_morbac.sql}"
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
TEST_DIR="$PROJECT_DIR/tests"
echo "======================================"
echo "pg_morbac Extension Test Suite"
echo "======================================"
echo "Database: $DB"
echo ""
if [ ! -f "$PROJECT_DIR/$SQL_FILE" ]; then
echo "Error: SQL file not found: $SQL_FILE" >&2
echo "Run 'make build' to generate the required file" >&2
exit 1
fi
# Install extension
echo "=== Installing Extension ==="
psql -d "$DB" -f "$PROJECT_DIR/pg_morbac.sql"
if [ ! -d "$TEST_DIR" ]; then
echo "Error: test directory not found: $TEST_DIR" >&2
exit 1
fi
# Load common setup
echo ""
echo "=== Loading Test Setup ==="
psql -d "$DB" -f "$TEST_DIR/00_setup.sql"
if ! psql -d "$DB" -c '\q' 2>/dev/null; then
echo "Error: cannot connect to database: $DB" >&2
echo "Ensure the database exists and is accessible" >&2
exit 1
fi
# Run all test files in order
echo ""
echo "=== Running Tests ==="
echo "Testing $SQL_FILE on database $DB"
psql -d "$DB" -f "$PROJECT_DIR/$SQL_FILE" -q
if [ -f "$TEST_DIR/00_setup.sql" ]; then
psql -d "$DB" -f "$TEST_DIR/00_setup.sql" -q
fi
for test_file in "$TEST_DIR"/[0-9][0-9]_*.sql; do
if [ -f "$test_file" ]; then
test_name=$(basename "$test_file" .sql)
echo ""
echo "--- Running: $test_name ---"
psql -d "$DB" -f "$test_file"
echo "Running: $test_name"
psql -d "$DB" -f "$test_file" -q
fi
done
echo ""
echo "======================================"
echo "All Tests Completed"
echo "======================================"
echo "All tests completed"
+45
View File
@@ -0,0 +1,45 @@
#!/bin/bash
# uninstall.sh - Uninstall the PostgreSQL extension from the system's extension directory
# Usage: ./uninstall.sh [project_name]
set -e # Exit immediately if a command exits with a non-zero status
PROJECT_FILENAME="${1:-pg_morbac}"
if ! command -v pg_config &> /dev/null; then
echo "Error: pg_config not found" >&2
echo "Install: apt install postgresql-server-dev-all (Debian/Ubuntu), yum install postgresql-devel (RHEL/CentOS), or brew install postgresql (macOS)" >&2
exit 1
fi
EXTDIR=$(pg_config --sharedir)/extension
if [ ! -d "$EXTDIR" ]; then
echo "Error: extension directory not found: $EXTDIR" >&2
exit 1
fi
CONTROL_FILE="$EXTDIR/${PROJECT_FILENAME}.control"
if [ ! -f "$CONTROL_FILE" ]; then
echo "Warning: control file not found: $CONTROL_FILE" >&2
echo "Extension may not be installed" >&2
fi
echo "Uninstalling ${PROJECT_FILENAME} from ${EXTDIR}"
# Remove control file
if [ -f "$CONTROL_FILE" ]; then
rm -f "$CONTROL_FILE"
echo "Removed: ${PROJECT_FILENAME}.control"
fi
# Remove all SQL files (versioned and upgrade scripts)
SQL_FILES_COUNT=$(find "$EXTDIR" -name "${PROJECT_FILENAME}--*.sql" -type f 2>/dev/null | wc -l)
if [ "$SQL_FILES_COUNT" -gt 0 ]; then
find "$EXTDIR" -name "${PROJECT_FILENAME}--*.sql" -type f -exec rm -f {} \;
echo "Removed: ${SQL_FILES_COUNT} SQL file(s)"
else
echo "No SQL files found to remove"
fi
echo "Uninstallation complete. Drop the extension from databases with: DROP EXTENSION IF EXISTS ${PROJECT_FILENAME} CASCADE;"