feat(tools): cleanup scripts and Makefile
This commit is contained in:
@@ -1,78 +1,70 @@
|
|||||||
# Makefile for pg_morbac PostgreSQL Extension
|
# Makefile for pg_morbac PostgreSQL Extension
|
||||||
# Multi-OrBAC: Organization-Based Access Control
|
# Multi-OrBAC: Organization-Based Access Control
|
||||||
|
|
||||||
EXTENSION = pg_morbac
|
PROJECT_FILENAME = pg_morbac
|
||||||
|
|
||||||
# Read version from control file
|
# Read version from control file using centralized script
|
||||||
EXTVERSION = $(shell grep default_version $(EXTENSION).control | sed "s/default_version = '\(.*\)'/\1/")
|
PROJECT_VERSION = $(shell ./tools/get_version.sh $(PROJECT_FILENAME).control)
|
||||||
|
|
||||||
# For development, work on source files in src/
|
# For development, work on source files in src/
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
DEV_SQL = $(EXTENSION).sql
|
OUTPUT_DEV_FILENAME = $(PROJECT_FILENAME).sql
|
||||||
DATA = $(EXTENSION)--$(EXTVERSION).sql
|
OUTPUT_RELEASE_FILENAME = $(PROJECT_FILENAME)--$(PROJECT_VERSION).sql
|
||||||
DOCS = README.md
|
|
||||||
|
|
||||||
# PostgreSQL extension build
|
# PostgreSQL Extension build
|
||||||
PG_CONFIG = pg_config
|
PG_CONFIG = pg_config
|
||||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
||||||
include $(PGXS)
|
include $(PGXS)
|
||||||
|
|
||||||
# Custom targets
|
# Custom targets
|
||||||
|
|
||||||
# Build versioned SQL file from source files
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build:
|
build:
|
||||||
@echo "Building release $(EXTVERSION) from source files..."
|
@./tools/build.sh $(SRC_DIR) $(OUTPUT_DEV_FILENAME)
|
||||||
@./tools/build.sh
|
@if [ ! -f $(OUTPUT_DEV_FILENAME) ]; then \
|
||||||
@if [ ! -f $(DEV_SQL) ]; then \
|
echo "Error: Build failed" >&2; \
|
||||||
echo "Error: Build failed"; \
|
|
||||||
exit 1; \
|
exit 1; \
|
||||||
fi
|
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
|
.PHONY: install
|
||||||
install: build
|
install: build
|
||||||
@echo "Installing pg_morbac extension..."
|
@cp $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
|
||||||
$(MAKE) -f Makefile install
|
@./tools/install.sh $(PROJECT_FILENAME) $(PROJECT_VERSION)
|
||||||
|
|
||||||
# Run tests with development file in temporary database
|
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: build
|
test: build
|
||||||
@echo "Testing with development file..."
|
@dropdb morbac_test 2>/dev/null || true
|
||||||
-dropdb morbac_test 2>/dev/null || true
|
@createdb morbac_test
|
||||||
createdb morbac_test
|
@./tools/test.sh morbac_test $(OUTPUT_DEV_FILENAME)
|
||||||
@./tools/test.sh morbac_test
|
@dropdb morbac_test
|
||||||
dropdb morbac_test
|
|
||||||
@echo "Test completed."
|
|
||||||
|
|
||||||
# Uninstall extension
|
# Uninstall extension from PostgreSQL
|
||||||
.PHONY: uninstall
|
.PHONY: uninstall
|
||||||
uninstall:
|
uninstall:
|
||||||
@echo "Uninstalling pg_morbac extension..."
|
@echo "Uninstalling $(PROJECT_FILENAME)..."
|
||||||
$(MAKE) -f Makefile uninstall
|
@./tools/uninstall.sh $(PROJECT_FILENAME)
|
||||||
|
|
||||||
|
# Clean up generated files
|
||||||
|
.PHONY: clean
|
||||||
|
clean:
|
||||||
|
@rm -f $(OUTPUT_DEV_FILENAME) $(OUTPUT_RELEASE_FILENAME)
|
||||||
|
|
||||||
# Show help
|
# Show help
|
||||||
.PHONY: help
|
.PHONY: help
|
||||||
help:
|
help:
|
||||||
@echo "pg_morbac Makefile targets:"
|
@echo "pg_morbac Makefile targets:"
|
||||||
@echo ""
|
@echo " make build - Build $(OUTPUT_DEV_FILENAME) from src/"
|
||||||
@echo "Development:"
|
@echo " make release - Build versioned $(PROJECT_FILENAME)--X.Y.Z.sql from src/"
|
||||||
@echo " Edit source files in src/ directory"
|
|
||||||
@echo ""
|
|
||||||
@echo " make build - Build versioned pg_morbac--X.Y.Z.sql from src/"
|
|
||||||
@echo " make test - Build and run test suite"
|
@echo " make test - Build and run test suite"
|
||||||
@echo " make install - Build and install extension to PostgreSQL"
|
@echo " make install - Build and install $(PROJECT_FILENAME) to PostgreSQL"
|
||||||
@echo " make uninstall - Uninstall extension from PostgreSQL"
|
@echo " make uninstall - Uninstall $(PROJECT_FILENAME) from PostgreSQL"
|
||||||
|
@echo " make clean - Clean up generated files"
|
||||||
@echo " make help - Show this help"
|
@echo " make help - Show this help"
|
||||||
@echo ""
|
@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
@@ -1,28 +1,21 @@
|
|||||||
#!/bin/bash
|
#!/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"
|
set -e # Exit immediately if a command exits with a non-zero status
|
||||||
OUTPUT="pg_morbac.sql"
|
|
||||||
|
|
||||||
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
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove old output file
|
|
||||||
rm -f "$OUTPUT"
|
rm -f "$OUTPUT"
|
||||||
|
|
||||||
# Define build order (dependencies matter)
|
|
||||||
BUILD_ORDER=(
|
BUILD_ORDER=(
|
||||||
"header.sql"
|
"header.sql"
|
||||||
"types.sql"
|
"types.sql"
|
||||||
@@ -50,17 +43,15 @@ BUILD_ORDER=(
|
|||||||
|
|
||||||
file_count=0
|
file_count=0
|
||||||
|
|
||||||
# Build in specified order
|
|
||||||
for filename in "${BUILD_ORDER[@]}"; do
|
for filename in "${BUILD_ORDER[@]}"; do
|
||||||
filepath="$SRC_DIR/$filename"
|
filepath="$SRC_DIR/$filename"
|
||||||
if [ -f "$filepath" ]; then
|
if [ -f "$filepath" ]; then
|
||||||
echo " Adding: $filename"
|
|
||||||
cat "$filepath" >> "$OUTPUT"
|
cat "$filepath" >> "$OUTPUT"
|
||||||
echo "" >> "$OUTPUT"
|
echo "" >> "$OUTPUT"
|
||||||
((file_count++))
|
((file_count++))
|
||||||
else
|
else
|
||||||
echo " Warning: $filename not found, skipping"
|
echo "Warning: $filename not found, skipping" >&2
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo "Built $OUTPUT successfully ($file_count source files)"
|
echo "Built $OUTPUT ($file_count files)"
|
||||||
|
|||||||
Executable
+22
@@ -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
@@ -1,87 +1,51 @@
|
|||||||
#!/bin/bash
|
#!/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 "=================================="
|
set -e # Exit immediately if a command exits with a non-zero status
|
||||||
echo "pg_morbac Extension Installer"
|
|
||||||
echo "=================================="
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# Check if running as root or with sudo
|
PROJECT_FILENAME="${1:-pg_morbac}"
|
||||||
if [ "$EUID" -ne 0 ] && [ -z "$SUDO_USER" ]; then
|
PROJECT_VERSION="${2}"
|
||||||
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
|
|
||||||
|
|
||||||
# Check if pg_config is available
|
|
||||||
if ! command -v pg_config &> /dev/null; then
|
if ! command -v pg_config &> /dev/null; then
|
||||||
echo "Error: pg_config not found in PATH"
|
echo "Error: pg_config not found" >&2
|
||||||
echo "Please install PostgreSQL development packages:"
|
echo "Install: apt install postgresql-server-dev-all (Debian/Ubuntu), yum install postgresql-devel (RHEL/CentOS), or brew install postgresql (macOS)" >&2
|
||||||
echo " Ubuntu/Debian: sudo apt-get install postgresql-server-dev-all"
|
|
||||||
echo " RedHat/CentOS: sudo yum install postgresql-devel"
|
|
||||||
echo " macOS: brew install postgresql"
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Get PostgreSQL extension directory
|
|
||||||
EXTDIR=$(pg_config --sharedir)/extension
|
EXTDIR=$(pg_config --sharedir)/extension
|
||||||
|
|
||||||
echo "PostgreSQL extension directory: $EXTDIR"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# Check if directory exists
|
|
||||||
if [ ! -d "$EXTDIR" ]; then
|
if [ ! -d "$EXTDIR" ]; then
|
||||||
echo "Error: Extension directory does not exist: $EXTDIR"
|
echo "Error: extension directory not found: $EXTDIR" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Detect version from control file
|
CONTROL_FILE="${PROJECT_FILENAME}.control"
|
||||||
CONTROL_FILE="pg_morbac.control"
|
|
||||||
if [ ! -f "$CONTROL_FILE" ]; then
|
if [ ! -f "$CONTROL_FILE" ]; then
|
||||||
echo "Error: $CONTROL_FILE not found"
|
echo "Error: control file not found: $CONTROL_FILE" >&2
|
||||||
echo "Please run this script from the pg_morbac directory"
|
echo "Run this script from the project root directory" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
VERSION=$(grep "default_version" "$CONTROL_FILE" | sed "s/default_version = '\(.*\)'/\1/")
|
if [ -z "$PROJECT_VERSION" ]; then
|
||||||
if [ -z "$VERSION" ]; then
|
SCRIPT_DIR=$(dirname "$0")
|
||||||
echo "Error: Could not detect version from $CONTROL_FILE"
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
SQL_FILE="pg_morbac--${VERSION}.sql"
|
SQL_FILE="${PROJECT_FILENAME}--${PROJECT_VERSION}.sql"
|
||||||
|
|
||||||
echo "Detected version: $VERSION"
|
|
||||||
echo ""
|
|
||||||
|
|
||||||
# Check if versioned SQL file exists
|
|
||||||
if [ ! -f "$SQL_FILE" ]; then
|
if [ ! -f "$SQL_FILE" ]; then
|
||||||
echo "Error: $SQL_FILE not found"
|
echo "Error: versioned SQL file not found: $SQL_FILE" >&2
|
||||||
echo "Please run 'make build' first to create the versioned SQL file"
|
echo "Run 'make build' to generate the required file" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Copy extension files
|
echo "Installing ${PROJECT_FILENAME} v${PROJECT_VERSION} to ${EXTDIR}"
|
||||||
echo "Installing extension files..."
|
cp "$CONTROL_FILE" "$EXTDIR/"
|
||||||
cp -v pg_morbac.control "$EXTDIR/"
|
mv "$SQL_FILE" "$EXTDIR/"
|
||||||
cp -v "$SQL_FILE" "$EXTDIR/"
|
|
||||||
|
|
||||||
echo ""
|
echo "Installation complete. Enable with: CREATE EXTENSION ${PROJECT_FILENAME};"
|
||||||
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 ""
|
|
||||||
|
|||||||
+30
-30
@@ -1,48 +1,48 @@
|
|||||||
#!/bin/bash
|
#!/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}"
|
DB="${1:-morbac_test}"
|
||||||
|
SQL_FILE="${2:-pg_morbac.sql}"
|
||||||
|
|
||||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
TEST_DIR="$PROJECT_DIR/tests"
|
TEST_DIR="$PROJECT_DIR/tests"
|
||||||
|
|
||||||
echo "======================================"
|
if [ ! -f "$PROJECT_DIR/$SQL_FILE" ]; then
|
||||||
echo "pg_morbac Extension Test Suite"
|
echo "Error: SQL file not found: $SQL_FILE" >&2
|
||||||
echo "======================================"
|
echo "Run 'make build' to generate the required file" >&2
|
||||||
echo "Database: $DB"
|
exit 1
|
||||||
echo ""
|
fi
|
||||||
|
|
||||||
# Install extension
|
if [ ! -d "$TEST_DIR" ]; then
|
||||||
echo "=== Installing Extension ==="
|
echo "Error: test directory not found: $TEST_DIR" >&2
|
||||||
psql -d "$DB" -f "$PROJECT_DIR/pg_morbac.sql"
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Load common setup
|
if ! psql -d "$DB" -c '\q' 2>/dev/null; then
|
||||||
echo ""
|
echo "Error: cannot connect to database: $DB" >&2
|
||||||
echo "=== Loading Test Setup ==="
|
echo "Ensure the database exists and is accessible" >&2
|
||||||
psql -d "$DB" -f "$TEST_DIR/00_setup.sql"
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
# Run all test files in order
|
echo "Testing $SQL_FILE on database $DB"
|
||||||
echo ""
|
|
||||||
echo "=== Running Tests ==="
|
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
|
for test_file in "$TEST_DIR"/[0-9][0-9]_*.sql; do
|
||||||
if [ -f "$test_file" ]; then
|
if [ -f "$test_file" ]; then
|
||||||
test_name=$(basename "$test_file" .sql)
|
test_name=$(basename "$test_file" .sql)
|
||||||
echo ""
|
echo "Running: $test_name"
|
||||||
echo "--- Running: $test_name ---"
|
psql -d "$DB" -f "$test_file" -q
|
||||||
psql -d "$DB" -f "$test_file"
|
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
echo ""
|
echo "All tests completed"
|
||||||
echo "======================================"
|
|
||||||
echo "All Tests Completed"
|
|
||||||
echo "======================================"
|
|
||||||
|
|||||||
Executable
+45
@@ -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;"
|
||||||
Reference in New Issue
Block a user