Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Standalone #152

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/.phpunit.result.cache
/.php-cs-fixer.cache
/composer.lock
/composer.phar
/phpstan.neon
/phpunit.xml
/vendor/
test_db*
test_db*
39 changes: 39 additions & 0 deletions bin/compile
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/usr/bin/bash

ROOTPATH="`dirname $0`"
ROOTPATH="`dirname $ROOTPATH`"
COMPOSER="$ROOTPATH/composer.phar"

echo " ==> Using composer $COMPOSER"
echo " ==> Will generate $ROOTPATH/db-tools.phar"

# Make a backup of the composer.json file.
cp "$ROOTPATH/composer.json" "$ROOTPATH/composer.json.dist"

if [ ! -e "$COMPOSER" ]; then
echo " ==> Download composer in $COMPOSER"
wget --quiet https://getcomposer.org/download/latest-stable/composer.phar -o "$COMPOSER"
fi

# Prepare composer, install without depdendencies.
echo " ==> Prepare environment"
rm -rf "$ROOTPATH/composer.lock"
rm -rf "$ROOTPATH/vendor"

# Install PHAR only tooling.
echo " ==> Require compile-only dependencies"
php "$COMPOSER" -n require --no-audit composer/pcre:'^3.1' seld/phar-utils:'^1.2'
php "$COMPOSER" -n -q config autoloader-suffix DbToolsPhar
php "$COMPOSER" -n install --no-dev
php "$COMPOSER" -n config autoloader-suffix --unset

# Compile PHAR file
echo " ==> Running compilation"
php -d phar.readonly=0 bin/compile.php
chmod +x "$ROOTPATH/db-tools.phar"

# Clean up environment
echo " ==> Cleaning up environment"
cp "$ROOTPATH/composer.json.dist" "$ROOTPATH/composer.json"
rm -rf "$ROOTPATH/composer.lock" "$ROOTPATH/composer.json.dist"
php "$COMPOSER" -n -q install
57 changes: 57 additions & 0 deletions bin/compile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle;

use MakinaCorpus\DbToolsBundle\Bridge\Standalone\PharCompiler;

/**
* Please run before running this:
* $ composer config autoloader-suffix DbToolsPhar
* $ composer install --no-dev
* $ composer config autoloader-suffix --unset
* $ php -d phar.readonly=0 bin/compile.php
*/

(static function (): void {
$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
];

$autoloaderFound = false;
foreach ($autoloadFiles as $autoloadFile) {
if (!\file_exists($autoloadFile)) {
continue;
}
require_once $autoloadFile;
$autoloaderFound = true;
}

if (!$autoloaderFound) {
if (\extension_loaded('phar') && \Phar::running() !== '') {
\fwrite(STDERR, 'The PHAR was built without dependencies!' . \PHP_EOL);
exit(1);
}
\fwrite(STDERR, 'vendor/autoload.php could not be found. Did you run `composer install`?' . \PHP_EOL);
exit(1);
}

$cwd = \getcwd();
\assert(\is_string($cwd));
\chdir(__DIR__.'/../');
$ts = \rtrim(\exec('git log -n1 --pretty=%ct HEAD'));
if (!\is_numeric($ts)) {
echo 'Could not detect date using "git log -n1 --pretty=%ct HEAD"'.\PHP_EOL;
exit(1);
}
\chdir($cwd);

\error_reporting(-1);
\ini_set('display_errors', '1');

$compiler = new PharCompiler();
$compiler->compile();
exit(1);
})();
8 changes: 8 additions & 0 deletions bin/db-tools
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env php
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle;

require __DIR__ . '/db-tools.php';
34 changes: 34 additions & 0 deletions bin/db-tools.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle;

use MakinaCorpus\DbToolsBundle\Bridge\Standalone\Bootstrap;

(static function (): void {
$autoloadFiles = [
__DIR__ . '/../vendor/autoload.php',
__DIR__ . '/../../../autoload.php',
];

$autoloaderFound = false;
foreach ($autoloadFiles as $autoloadFile) {
if (!\file_exists($autoloadFile)) {
continue;
}
require_once $autoloadFile;
$autoloaderFound = true;
}

if (!$autoloaderFound) {
if (\extension_loaded('phar') && \Phar::running() !== '') {
\fwrite(STDERR, 'The PHAR was built without dependencies!' . \PHP_EOL);
exit(1);
}
\fwrite(STDERR, 'vendor/autoload.php could not be found. Did you run `composer install`?' . \PHP_EOL);
exit(1);
}

Bootstrap::run();
})();
17 changes: 14 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,37 @@
"prefer-stable": true,
"require": {
"php": ">=8.1",
"doctrine/doctrine-bundle": "^2.10.0",
"makinacorpus/query-builder": "^1.6.1",
"psr/log": "^3.0",
"symfony/config": "^6.0|^7.0",
"symfony/console": "^6.0|^7.0",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/filesystem": "^6.0|^7.0",
"symfony/finder": "^6.0|^7.0",
"symfony/options-resolver": "^6.0|^7.0",
"symfony/password-hasher": "^6.0|^7.0",
"symfony/process": "^6.0|^7.0",
"symfony/yaml": "^6.0|^7.0"
},
"require-dev": {
"doctrine/doctrine-bundle": "^2.10.0",
"doctrine/orm": "^2.15|^3.0",
"friendsofphp/php-cs-fixer": "^3.34",
"phpstan/phpstan": "^1.10",
"phpunit/phpunit": "^10.4",
"symfony/dependency-injection": "^6.0|^7.0",
"symfony/framework-bundle": "^6.0|^7.0",
"symfony/password-hasher": "^6.0|^7.0",
"symfony/validator": "^6.3|^7.0"
},
"suggest": {
"symfony/password-hasher": "In order to use the password hash anonymizer"
},
"conflict": {
"composer/pcre": "<3.1|>=4.0",
"doctrine/dbal": "<3.0|>=5.0",
"doctrine/orm": "<2.15|>=4.0",
"seld/phar-utils": "<1.2|>=2.0",
"symfony/password-hasher": "<6.0|>=8.0"
pounard marked this conversation as resolved.
Show resolved Hide resolved
},
"autoload": {
"psr-4": {
"MakinaCorpus\\DbToolsBundle\\" : "src/"
Expand Down
2 changes: 1 addition & 1 deletion config/anonymizations.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ user:
anonymizer: email
options: {domain: 'toto.com'}
# Or not
email: email
#email: email
level:
anonymizer: string
options: {sample: ['none', 'bad', 'good', 'expert']}
Expand Down
137 changes: 137 additions & 0 deletions config/db_tools.standalone.sample.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# This configuration file is an example for standalone usage.
#
# The given parameters you will find in this file must not be set in a Symfony
# application context, and will be ignored if so.
#
# All other configuration you can find in the ./packages/db_tools.yaml file
# can be added in this file, you must simply omit the 'db_tools:' top level
# node.

# Working directory is the path to which all relative file references will
# be relative to. If none set, the path will be this file directory instead.
workdir: /var/www/my_project/

# Database connections.
# One line per connection, a single database URL, all options as query
# parameters. Connections will be made using makincorpus/query-builder
# which will raise exceptions when invalid options are found.
# There is less configuration amplitude than using doctrine/dbal in
# Symfony, yet it should be enough in most case.
pounard marked this conversation as resolved.
Show resolved Hide resolved
# In case any options or specific behaviour is missing, please file
# an issue at https://github.com/makinacorpus/php-query-builder/issues
connections:
connection_one: "pgsql://username:password@hostname:port?version=16.0&other_option=..."
connection_two: "mysql://username:password@hostname:port?version=8.1&other_option=..."

pounard marked this conversation as resolved.
Show resolved Hide resolved
# If you have a single connection, you can use this syntax. In this case
# the connection name will be "default".
# connections: "pgsql://username:password@hostname:port?version=16.0&other_option=..."

# You can explicitely set which will be default connection in use when
# none providen in the command line options. If you omit this configuration
# value, then the first one in the list will be used.
#default_connection: connection_one

# Using the DbToolsBundle standalone, you must provide at least
# a root directory for backups.
storage:
# Path can be relative or absolute, Relative paths are relative to the
# workdir option if specified, or from this configuration file directory
# otherwise.
# If none provided, the default will be the following one.
root_dir: ./var/db_tools
# Filename strategies. You may specify one strategy for each doctrine
# connection. Keys are doctrine connection names. Values are strategy
# names, "default" (or null) or omitting the connection will use the
# default implementation.
# If you created and registered a custom one into the container as a
# service, you may simply set the service identifier. If no service
# exists, and your implementation does not require parameters, simply
# set the class name.
#filename_strategy:
# Backup filename strategy.
# "default" is an alias of "datetime"
#default: default
# "datetime" implementation is ROOT_DIR/YYYY/MM/<connection-name>-<datestamp>.<ext>"
#other_connection_strategy: datetime
# Example of using a service name:
#yet_another_connection: app.db_tools.filename.custom_strategy
# Or a classe name:
#another_one: App\DbTools\Storage\MyCustomStrategy

# When old backups are considered obsolete
# (Use relative date/time formats : https://www.php.net/manual/en/datetime.formats.relative.php)
#backup_expiration_age: '6 months ago' # default '3 months ago'

# Timeout for backups.
# backup_timeout: 1200 # default 600

# Timeout for restores.
# restore_timeout: 2400 # default 1800

# List here tables (per connection) you don't want in your backups
#excluded_tables:
#default: ['table1', 'table2']

# Specify here paths to binaries, only if the system can't find them by himself
# platform are 'mysql', 'postgresql', 'sqlite'
#backupper_binaries:
#mariadb: '/usr/bin/mariadb-dump' # default 'mariadb-dump'
#mysql: '/usr/bin/mysqldump' # default 'mysqldump'
#postgresql: '/usr/bin/pg_dump' # default 'pg_dump'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'
#restorer_binaries:
#mariadb: '/usr/bin/mariadb' # default 'mariadb'
#mysql: '/usr/bin/mysql' # default 'mysql'
#postgresql: '/usr/bin/pg_restore' # default 'pg_restore'
#sqlite: '/usr/bin/sqlite3' # default 'sqlite3'

# Default options to pass to the binary when backing up or restoring
# a database. Those options must be defined per connection.
# If you do not define some default options, here or by using the
# "--extra-options" option when invoking the command, the following
# ones will be used according to the database vendor:
# - When backing up:
# - MariaDB: --no-tablespaces
# - MySQL: --no-tablespaces
# - PostgreSQL: -Z 5 --lock-wait-timeout=120
# - SQLite: -bail
# - When restoring:
# - MariaDB: None
# - MySQL: None
# - PostgreSQL: -j 2 --clean --if-exists --disable-triggers
# - SQLite: None
#backupper_options:
#default: ''
#another_connection: ''
#restorer_options:
#default: ''
#another_connection: ''

# Update this configuration if you want to look for anonymizers in a custom folder.
# These are default paths that will always be registered even if you override
# the setting and don't repeat them:
#anonymizer_paths:
#- ./src/Anonymization/Anonymizer'

anonymization:
# From here you can proceed with manual file inclusion. Pathes can be
# either relative or absolute. Relative paths are relative to the workdir
# option if specified, or from this configuration file directory
# otherwise.
yaml:
connection_one: ./db_tools.anonymizer.connection_one.yaml
# ... other connections ...

# Extra configuration options, if you don't want to split the anonymization
# configuration into multiple files, you can directly write it here.
tables:
connection_one:
# From here, please refer to 'anonymizations.sample.yaml' for sample
# and documentation.
table_name:
column_name:
anonymizer: anonymizer_name
# ... other options...
connection_two:
# ...
1 change: 1 addition & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ export default defineConfig({
{ text: 'Backup & Restore', link: '/backup_restore' },
{ text: 'Statistics', link: '/stats' },
{ text: 'Bundle configuration', link: '/configuration' },
{ text: 'Configuration reference', link: '/configuration/reference' },
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion docs/content/anonymization/core-anonymizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ Dates you give for `min` and `max` values will inherit from the PHP default
configured timezone.
:::

:::note
:::info
When using a date range over 68 years, random granularity stops at the hour
in order to avoid date add operation to be given an overflowing int value.
:::
Expand Down
3 changes: 3 additions & 0 deletions docs/content/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ all will take place in the `config/packages/db_tools.yaml` file.
A complete example of this file can be found in the bundle sources in: `vendor/makinacorpus/db-tools-bundle/config/packages/db_tools.yaml`
:::

For detailed information about configuration options, please see the
[configuration reference](configuration/reference).

## Backup configuration

Some options are available to customize how the `db-tools:backup` command works.
Expand Down
Loading
Loading