Skip to content

Commit 5077b98

Browse files
authored
Merge pull request #2 from MacPaw/develop
Release
2 parents fb2e66b + 663c516 commit 5077b98

14 files changed

+559
-2
lines changed

.github/workflows/ci.yaml

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main, develop ]
7+
8+
jobs:
9+
run:
10+
runs-on: ubuntu-latest
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
php:
15+
- '8.3'
16+
- '8.4'
17+
coverage: ['none']
18+
doctrine-orm-versions:
19+
- '^2.17'
20+
- '^3.0'
21+
symfony-versions:
22+
- '6.4.*'
23+
- '7.0.*'
24+
include:
25+
- description: 'Log Code Coverage'
26+
php: '8.3'
27+
symfony-versions: '^7.0'
28+
doctrine-orm-versions: '^3.0'
29+
coverage: xdebug
30+
31+
name: PHP ${{ matrix.php }} Symfony ${{ matrix.symfony-versions }} Doctrine ${{ matrix.doctrine-orm-versions }} ${{ matrix.description }}
32+
steps:
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
36+
- uses: actions/cache@v4
37+
with:
38+
path: ~/.composer/cache/files
39+
key: ${{ matrix.php }}-${{ matrix.symfony-versions }}
40+
41+
- name: Setup PHP
42+
uses: shivammathur/setup-php@v2
43+
with:
44+
php-version: ${{ matrix.php }}
45+
coverage: xdebug
46+
47+
- name: Add PHPUnit matcher
48+
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
49+
50+
- name: Set composer cache directory
51+
id: composer-cache
52+
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
53+
54+
- name: Cache composer
55+
uses: actions/cache@v4
56+
with:
57+
path: ${{ steps.composer-cache.outputs.dir }}
58+
key: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer-${{ hashFiles('composer.json') }}
59+
restore-keys: ${{ runner.os }}-${{ matrix.php }}-${{ matrix.symfony-versions }}-composer
60+
61+
- name: Update Symfony version
62+
if: matrix.symfony-versions != ''
63+
run: |
64+
composer require symfony/framework-bundle:${{ matrix.symfony-versions }} --no-update --no-scripts
65+
composer require doctrine/orm:${{ matrix.doctrine-orm-versions }} --no-update --no-scripts
66+
composer require --dev symfony/yaml:${{ matrix.symfony-versions }} --no-update --no-scripts
67+
68+
- name: Install dependencies
69+
run: composer install
70+
71+
- name: Run PHPUnit tests
72+
run: vendor/bin/phpunit
73+
if: matrix.coverage == 'none'
74+
75+
- name: PHPUnit tests and Log Code coverage
76+
run: vendor/bin/phpunit --coverage-clover=coverage.xml
77+
if: matrix.coverage == 'xdebug'
78+
79+
- name: Upload coverage reports to Codecov
80+
if: matrix.coverage == 'xdebug'
81+
uses: codecov/[email protected]
82+
with:
83+
token: ${{ secrets.CODECOV_TOKEN }}

.github/workflows/security.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
on:
2+
pull_request:
3+
push:
4+
branches: [ main, develop ]
5+
6+
jobs:
7+
security-checker:
8+
name: Security checker
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
14+
- name: Setup PHP
15+
uses: shivammathur/setup-php@v2
16+
17+
- name: Install dependencies
18+
run: composer install --no-progress --no-interaction --prefer-dist
19+
20+
- name: Download local-php-security-checker
21+
run: curl -s -L -o local-php-security-checker https://github.com/fabpot/local-php-security-checker/releases/download/v1.0.0/local-php-security-checker_1.0.0_linux_amd64
22+
23+
- name: Run local-php-security-checker
24+
run: chmod +x local-php-security-checker && ./local-php-security-checker
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: Code style and static analysis
2+
3+
on:
4+
pull_request:
5+
push:
6+
branches: [ main, develop ]
7+
8+
jobs:
9+
php-cs-fixer:
10+
name: PHP-CS-Fixer
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Checkout
14+
uses: actions/checkout@v2
15+
16+
- name: Setup PHP
17+
uses: shivammathur/setup-php@v2
18+
19+
- name: Install dependencies
20+
run: composer install --no-progress --no-interaction --prefer-dist
21+
22+
- name: Run script
23+
run: vendor/bin/phpcs
24+
25+
phpstan:
26+
name: PHPStan
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Checkout
30+
uses: actions/checkout@v2
31+
32+
- name: Setup PHP
33+
uses: shivammathur/setup-php@v2
34+
35+
- name: Install dependencies
36+
run: composer install --no-progress --no-interaction --prefer-dist
37+
38+
- name: Run script
39+
run: vendor/bin/phpstan analyse
40+
41+
composer-validate:
42+
name: Composer validate
43+
runs-on: ubuntu-latest
44+
steps:
45+
- name: Checkout
46+
uses: actions/checkout@v2
47+
48+
- name: Setup PHP
49+
uses: shivammathur/setup-php@v2
50+
51+
- name: Install dependencies
52+
run: composer install --no-progress --no-interaction --prefer-dist
53+
54+
- name: Run script
55+
run: composer composer-validate

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.idea
2+
/vendor/
3+
/composer.lock
4+
/.phpcs-cache
5+
6+
###> phpunit/phpunit ###
7+
/phpunit.xml
8+
.phpunit.result.cache
9+
###< phpunit/phpunit ###

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,71 @@
1-
# postgres-schema-bundle
2-
Postgres schema bundle
1+
# Postgres Schema Bundle
2+
The **Postgres Schema Bundle** provides seamless multi-tenant schema support for PostgreSQL within Symfony applications. It automatically switches PostgreSQL `search_path` based on the current request context and ensures proper schema resolution across Doctrine and Messenger.## Installation
3+
4+
## Features
5+
6+
- Automatically sets PostgreSQL `search_path` from request headers.
7+
- Validates that the schema exists in the database.
8+
- Works only if the configured database driver is PostgreSQL.
9+
- Integrates with [Schema Context Bundle](https://github.com/macpaw/schema-context-bundle).
10+
- Compatible with Symfony Messenger and Doctrine ORM.
11+
12+
## Installation
13+
Use Composer to install the bundle:
14+
```
15+
composer require macpaw/postgres-schema-bundle
16+
```
17+
18+
### Applications that don't use Symfony Flex
19+
Enable the bundle by adding it to the list of registered bundles in ```config/bundles.php```
20+
21+
```
22+
// config/bundles.php
23+
<?php
24+
25+
return [
26+
Macpaw\SchemaContextBundle\SchemaContextBundle::class => ['all' => true],
27+
Macpaw\SchemaContextBundle\PostgresSchemaBundle::class => ['all' => true],
28+
// ...
29+
];
30+
```
31+
32+
## Configuration
33+
34+
You must tell Doctrine to use the SchemaConnection class as its DBAL connection class:
35+
36+
```yaml
37+
# config/packages/doctrine.yaml
38+
doctrine:
39+
dbal:
40+
connections:
41+
default:
42+
wrapper_class: Macpaw\PostgresSchemaBundle\Doctrine\SchemaConnection
43+
```
44+
Make sure you configure the context bundle properly:
45+
46+
See https://github.com/MacPaw/schema-context-bundle/blob/develop/README.md
47+
48+
```yaml
49+
schema_context:
50+
app_name: '%env(APP_NAME)%' # Application name
51+
header_name: 'X-Tenant' # Request header to extract schema name
52+
default_schema: 'public' # Default schema to fallback to
53+
allowed_app_names: ['develop', 'staging', 'test'] # App names where schema context is allowed to change
54+
```
55+
56+
## How it Works
57+
* A request comes in with a header like X-Tenant-Id: tenant123.
58+
* The SchemaRequestListener sets this schema in the context.
59+
* When Doctrine connects to PostgreSQL, it sets the search_path to the specified schema.
60+
* If the schema does not exist or DB is not PostgreSQL, an exception is thrown.
61+
62+
## Testing
63+
To run tests:
64+
```bash
65+
vendor/bin/phpunit
66+
```
67+
## Contributing
68+
Feel free to open issues and submit pull requests.
69+
70+
## License
71+
This bundle is released under the MIT license.

SECURITY.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Security Policy
2+
3+
## Reporting Security Issues
4+
If you believe you have found a security vulnerability in any MacPaw-owned repository, please report it to us through coordinated disclosure.
5+
6+
Please do not report security vulnerabilities through public GitHub issues, discussions, or pull requests.
7+
8+
Instead, please send an email to security[@]macpaw.com.
9+
10+
Please include as much of the information listed below as you can to help us better understand and resolve the issue:
11+
12+
- The type of issue (e.g., buffer overflow, SQL injection, or cross-site scripting)
13+
- Full paths of source file(s) related to the manifestation of the issue
14+
- The location of the affected source code (tag/branch/commit or direct URL)
15+
- Any special configuration required to reproduce the issue
16+
- Step-by-step instructions to reproduce the issue
17+
- Proof-of-concept or exploit code (if possible)
18+
- Impact of the issue, including how an attacker might exploit the issue
19+
20+
This information will help us triage your report more quickly.
21+
22+
## Policy
23+
See MacPaw's [Vulnerability Disclosure Policy](https://macpaw.com/vulnerability-disclosure-policy)

composer.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"name": "macpaw/postgres-schema-bundle",
3+
"description": "A Symfony bundle to add schema wrapper for postgres",
4+
"type": "symfony-bundle",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"Macpaw\\PostgresSchemaBundle\\": "src/"
9+
}
10+
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Macpaw\\PostgresSchemaBundle\\Tests\\": "tests/"
14+
}
15+
},
16+
"require": {
17+
"php": ">=8.3",
18+
"doctrine/orm": "^2.17 || ^3.0",
19+
"symfony/doctrine-bridge": "^6.4 || ^7.0",
20+
"doctrine/dbal": "^3.4",
21+
"macpaw/schema-context-bundle": "^1.0"
22+
},
23+
"require-dev": {
24+
"phpstan/phpstan": "^1.10",
25+
"phpunit/phpunit": "^10.0",
26+
"squizlabs/php_codesniffer": "3.7.*"
27+
},
28+
"config": {
29+
"allow-plugins": {
30+
"dealerdirect/phpcodesniffer-composer-installer": true
31+
}
32+
},
33+
"scripts": {
34+
"composer-validate": [
35+
"composer validate"
36+
],
37+
"cs": [
38+
"vendor/bin/phpcs"
39+
],
40+
"cs-fix": [
41+
"vendor/bin/phpcbf"
42+
],
43+
"phpstan": [
44+
"vendor/bin/phpstan analyse"
45+
],
46+
"phpunit": [
47+
"vendor/bin/phpunit"
48+
]
49+
}
50+
}

phpcs.xml.dist

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="vendor/squizlabs/php_codesniffer/phpcs.xsd">
4+
5+
<arg name="basepath" value="."/>
6+
<arg name="cache" value=".phpcs-cache"/>
7+
<arg name="colors"/>
8+
<arg value="p"/>
9+
<arg name="extensions" value="php"/>
10+
<arg name="tab-width" value="4"/>
11+
<arg name="report-width" value="120"/>
12+
13+
<rule ref="PSR12" />
14+
15+
<file>src/</file>
16+
<file>tests/</file>
17+
18+
<rule ref="Squiz.PHP.LowercasePHPFunctions"/>
19+
<rule ref="Generic.PHP.RequireStrictTypes"/>
20+
<rule ref="Squiz.Arrays.ArrayBracketSpacing"/>
21+
<rule ref="Generic.Arrays.DisallowLongArraySyntax.Found"></rule>
22+
<rule ref="Squiz.Commenting.FunctionComment.SpacingAfterParamType"></rule>
23+
24+
<rule ref="Generic.PHP.ForbiddenFunctions">
25+
<properties>
26+
<property name="forbiddenFunctions" type="array" value="eval=>NULL,dd=>NULL,die=>NULL,var_dump=>NULL,dump=>NULL,sizeof=>count,delete=>unset,print=>echo,echo=>NULL,print_r=>NULL,create_function=>NULL"/>
27+
</properties>
28+
</rule>
29+
<rule ref="Squiz.WhiteSpace.FunctionSpacing">
30+
<properties>
31+
<property name="spacing" value="1" />
32+
<property name="spacingBeforeFirst" value="0" />
33+
<property name="spacingAfterLast" value="0" />
34+
</properties>
35+
</rule>
36+
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">
37+
<properties>
38+
<property name="ignoreBlankLines" value="false" />
39+
</properties>
40+
</rule>
41+
</ruleset>

phpstan.neon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
parameters:
2+
level: max
3+
paths:
4+
- src

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0"?>
2+
<phpunit
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
7+
>
8+
<testsuites>
9+
<testsuite name="Symfony Messenger Bundle Test Suite">
10+
<directory>./tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
<coverage>
14+
<report>
15+
<clover outputFile="clover.xml"/>
16+
</report>
17+
</coverage>
18+
<source>
19+
<include>
20+
<directory>./src</directory>
21+
</include>
22+
</source>
23+
</phpunit>

0 commit comments

Comments
 (0)