diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.yml index 96701be..7e3e771 100644 --- a/.github/ISSUE_TEMPLATE/config.yml +++ b/.github/ISSUE_TEMPLATE/config.yml @@ -1,11 +1,11 @@ blank_issues_enabled: false contact_links: - name: Ask a question - url: https://github.com/:vendor_name/:package_name/discussions/new?category=q-a + url: https://github.com/alibori/laravel-db-size-alerts/discussions/new?category=q-a about: Ask the community for help - name: Request a feature - url: https://github.com/:vendor_name/:package_name/discussions/new?category=ideas + url: https://github.com/alibori/laravel-db-size-alerts/discussions/new?category=ideas about: Share ideas for new features - name: Report a security issue - url: https://github.com/:vendor_name/:package_name/security/policy + url: https://github.com/alibori/laravel-db-size-alerts/security/policy about: Learn how to notify us for sensitive bugs diff --git a/.github/dependabot.yml b/.github/dependabot.yml deleted file mode 100644 index 30c8a49..0000000 --- a/.github/dependabot.yml +++ /dev/null @@ -1,12 +0,0 @@ -# Please see the documentation for all configuration options: -# https://help.github.com/github/administering-a-repository/configuration-options-for-dependency-updates - -version: 2 -updates: - - - package-ecosystem: "github-actions" - directory: "/" - schedule: - interval: "weekly" - labels: - - "dependencies" \ No newline at end of file diff --git a/.github/workflows/dependabot-auto-merge.yml b/.github/workflows/dependabot-auto-merge.yml deleted file mode 100644 index ca2197d..0000000 --- a/.github/workflows/dependabot-auto-merge.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: dependabot-auto-merge -on: pull_request_target - -permissions: - pull-requests: write - contents: write - -jobs: - dependabot: - runs-on: ubuntu-latest - if: ${{ github.actor == 'dependabot[bot]' }} - steps: - - - name: Dependabot metadata - id: metadata - uses: dependabot/fetch-metadata@v1.6.0 - with: - github-token: "${{ secrets.GITHUB_TOKEN }}" - - - name: Auto-merge Dependabot PRs for semver-minor updates - if: ${{steps.metadata.outputs.update-type == 'version-update:semver-minor'}} - run: gh pr merge --auto --merge "$PR_URL" - env: - PR_URL: ${{github.event.pull_request.html_url}} - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} - - - name: Auto-merge Dependabot PRs for semver-patch updates - if: ${{steps.metadata.outputs.update-type == 'version-update:semver-patch'}} - run: gh pr merge --auto --merge "$PR_URL" - env: - PR_URL: ${{github.event.pull_request.html_url}} - GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} diff --git a/.github/workflows/update-changelog.yml b/.github/workflows/update-changelog.yml deleted file mode 100644 index 8c12ba9..0000000 --- a/.github/workflows/update-changelog.yml +++ /dev/null @@ -1,31 +0,0 @@ -name: "Update Changelog" - -on: - release: - types: [released] - -permissions: - contents: write - -jobs: - update: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v3 - with: - ref: main - - - name: Update Changelog - uses: stefanzweifel/changelog-updater-action@v1 - with: - latest-version: ${{ github.event.release.name }} - release-notes: ${{ github.event.release.body }} - - - name: Commit updated CHANGELOG - uses: stefanzweifel/git-auto-commit-action@v4 - with: - branch: main - commit_message: Update CHANGELOG - file_pattern: CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md index 87b3242..4ec0060 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ # Changelog -All notable changes to `:package_name` will be documented in this file. +All notable changes to `laravel-db-size-alerts` will be documented in this file. + +## 1.0.0 - 2023-10-06 + +- Initial release diff --git a/LICENSE.md b/LICENSE.md index 58c9ad4..b87c379 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (c) :vendor_name +Copyright (c) alibori Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.md b/README.md index 375da96..42ca1cd 100644 --- a/README.md +++ b/README.md @@ -1,70 +1,51 @@ -# :package_description +# Laravel package to receive alerts about database size limits. -[![Latest Version on Packagist](https://img.shields.io/packagist/v/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug) -[![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3Arun-tests+branch%3Amain) -[![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/:vendor_slug/:package_slug/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/:vendor_slug/:package_slug/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) -[![Total Downloads](https://img.shields.io/packagist/dt/:vendor_slug/:package_slug.svg?style=flat-square)](https://packagist.org/packages/:vendor_slug/:package_slug) - ---- -This repo can be used to scaffold a Laravel package. Follow these steps to get started: +[![Latest Version on Packagist](https://img.shields.io/packagist/v/alibori/laravel-db-size-alerts.svg?style=flat-square)](https://packagist.org/packages/alibori/laravel-db-size-alerts) +[![Total Downloads](https://img.shields.io/packagist/dt/alibori/laravel-db-size-alerts.svg?style=flat-square)](https://packagist.org/packages/alibori/laravel-db-size-alerts) -1. Press the "Use this template" button at the top of this repo to create a new repo with the contents of this skeleton. -2. Run "php ./configure.php" to run a script that will replace all placeholders throughout all the files. -3. Have fun creating your package. -4. If you need help creating a package, consider picking up our Laravel Package Training video course. ---- - -This is where your description should go. Limit it to a paragraph or two. Consider adding a small example. - -## Support us - -[](https://spatie.be/github-ad-click/:package_name) - -We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). - -We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). +This package allows you to receive alerts about database tables size limits. ## Installation +**IMPORTANT:** This package requires mysql database connection. + You can install the package via composer: ```bash -composer require :vendor_slug/:package_slug +composer require alibori/laravel-db-size-alerts ``` -You can publish and run the migrations with: +You can publish the config file with: ```bash -php artisan vendor:publish --tag=":package_slug-migrations" -php artisan migrate +php artisan vendor:publish --tag="db-size-alerts-config" ``` -You can publish the config file with: +You can publish the mail views file with: ```bash -php artisan vendor:publish --tag=":package_slug-config" +php artisan vendor:publish --tag="db-size-alerts-views" ``` -This is the contents of the published config file: - -```php -return [ -]; -``` +## Usage -Optionally, you can publish the views using +You can use the following artisan command to check the specified tables size: ```bash -php artisan vendor:publish --tag=":package_slug-views" +php artisan db-size:check ``` -## Usage +Or, recommended, you can schedule the artisan command to run periodically on your `App\Console\Kernel.php` file: ```php -$variable = new VendorName\Skeleton(); -echo $variable->echoPhrase('Hello, VendorName!'); +protected function schedule(Schedule $schedule) +{ + $schedule->command(LaravelDbSizeAlertsCommand::class)->daily(); +} ``` +In both cases, if the *APP_ENV* is not *local* you will receive an email with the tables that have exceeded the specified size limit. + ## Testing ```bash @@ -75,18 +56,13 @@ composer test Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. -## Contributing - -Please see [CONTRIBUTING](CONTRIBUTING.md) for details. - ## Security Vulnerabilities Please review [our security policy](../../security/policy) on how to report security vulnerabilities. ## Credits -- [:author_name](https://github.com/:author_username) -- [All Contributors](../../contributors) +- [Axel Libori Roch](https://github.com/alibori) ## License diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..cd1f8ce --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,7 @@ +# Security Policy + +## Reporting a Vulnerability + +Please, feel free to report any security vulnerability you find in this package sending an email to [a.libori@gmail.com](mailto:a.libori@gmail.com). All security vulnerabilities will be promptly addressed. + +Thank you for helping make this package safe for everyone. diff --git a/composer.json b/composer.json index b2ab625..ac86281 100644 --- a/composer.json +++ b/composer.json @@ -1,53 +1,52 @@ { - "name": ":vendor_slug/:package_slug", - "description": ":package_description", + "name": "alibori/laravel-db-size-alerts", + "description": "Laravel package to receive alerts about database size limits.", "keywords": [ - ":vendor_name", + "alibori", "laravel", - ":package_slug" + "laravel-db-size-alerts" ], - "homepage": "https://github.com/:vendor_slug/:package_slug", + "homepage": "https://github.com/alibori/laravel-db-size-alerts", "license": "MIT", "authors": [ { - "name": ":author_name", - "email": "author@domain.com", + "name": "Axel Libori Roch", + "email": "a.libori@gmail.com", "role": "Developer" } ], "require": { "php": "^8.1", "spatie/laravel-package-tools": "^1.14.0", - "illuminate/contracts": "^10.0" + "illuminate/contracts": "*" }, "require-dev": { "laravel/pint": "^1.0", - "nunomaduro/collision": "^7.8", + "nunomaduro/collision": "^5.11|^7.8", "nunomaduro/larastan": "^2.0.1", - "orchestra/testbench": "^8.8", - "pestphp/pest": "^2.0", + "orchestra/testbench": "^7.0|^8.0", + "pestphp/pest": "^1.22|^2.0", "pestphp/pest-plugin-arch": "^2.0", - "pestphp/pest-plugin-laravel": "^2.0", + "pestphp/pest-plugin-laravel": "^1.4|^2.0", "phpstan/extension-installer": "^1.1", "phpstan/phpstan-deprecation-rules": "^1.0", - "phpstan/phpstan-phpunit": "^1.0", - "spatie/laravel-ray": "^1.26" + "phpstan/phpstan-phpunit": "^1.0" }, "autoload": { "psr-4": { - "VendorName\\Skeleton\\": "src/", - "VendorName\\Skeleton\\Database\\Factories\\": "database/factories/" + "Alibori\\LaravelDbSizeAlerts\\": "src/", + "Alibori\\LaravelDbSizeAlerts\\Database\\Factories\\": "database/factories/" } }, "autoload-dev": { "psr-4": { - "VendorName\\Skeleton\\Tests\\": "tests/", + "Alibori\\LaravelDbSizeAlerts\\Tests\\": "tests/", "Workbench\\App\\": "workbench/app/" } }, "scripts": { "post-autoload-dump": "@composer run prepare", - "clear": "@php vendor/bin/testbench package:purge-skeleton --ansi", + "clear": "@php vendor/bin/testbench package:purge-laravel-db-size-alerts --ansi", "prepare": "@php vendor/bin/testbench package:discover --ansi", "build": [ "@composer run prepare", @@ -73,10 +72,10 @@ "extra": { "laravel": { "providers": [ - "VendorName\\Skeleton\\SkeletonServiceProvider" + "Alibori\\LaravelDbSizeAlerts\\LaravelDbSizeAlertsServiceProvider" ], "aliases": { - "Skeleton": "VendorName\\Skeleton\\Facades\\Skeleton" + "LaravelDbSizeAlerts": "Alibori\\LaravelDbSizeAlerts\\Facades\\LaravelDbSizeAlerts" } } }, diff --git a/config/db-size-alerts.php b/config/db-size-alerts.php new file mode 100644 index 0000000..764c760 --- /dev/null +++ b/config/db-size-alerts.php @@ -0,0 +1,29 @@ + [ + 'users', + 'posts', + 'comments', + ], + + /** + * Default maximum size of a table in MB + */ + 'table_max_mb_size' => 1024, + + 'emails_to_notify' => [ + // 'john.doe@example.com', + ], +]; diff --git a/config/skeleton.php b/config/skeleton.php deleted file mode 100644 index 7e74186..0000000 --- a/config/skeleton.php +++ /dev/null @@ -1,6 +0,0 @@ - $version) { - if (in_array($name, $names, true)) { - unset($data['require-dev'][$name]); - } - } - - file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); -} - -function remove_composer_script($scriptName) -{ - $data = json_decode(file_get_contents(__DIR__.'/composer.json'), true); - - foreach ($data['scripts'] as $name => $script) { - if ($scriptName === $name) { - unset($data['scripts'][$name]); - break; - } - } - - file_put_contents(__DIR__.'/composer.json', json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); -} - -function remove_readme_paragraphs(string $file): void -{ - $contents = file_get_contents($file); - - file_put_contents( - $file, - preg_replace('/.*/s', '', $contents) ?: $contents - ); -} - -function safeUnlink(string $filename) -{ - if (file_exists($filename) && is_file($filename)) { - unlink($filename); - } -} - -function determineSeparator(string $path): string -{ - return str_replace('/', DIRECTORY_SEPARATOR, $path); -} - -function replaceForWindows(): array -{ - return preg_split('/\\r\\n|\\r|\\n/', run('dir /S /B * | findstr /v /i .git\ | findstr /v /i vendor | findstr /v /i '.basename(__FILE__).' | findstr /r /i /M /F:/ ":author :vendor :package VendorName skeleton migration_table_name vendor_name vendor_slug author@domain.com"')); -} - -function replaceForAllOtherOSes(): array -{ - return explode(PHP_EOL, run('grep -E -r -l -i ":author|:vendor|:package|VendorName|skeleton|migration_table_name|vendor_name|vendor_slug|author@domain.com" --exclude-dir=vendor ./* ./.github/* | grep -v '.basename(__FILE__))); -} - -$gitName = run('git config user.name'); -$authorName = ask('Author name', $gitName); - -$gitEmail = run('git config user.email'); -$authorEmail = ask('Author email', $gitEmail); - -$usernameGuess = explode(':', run('git config remote.origin.url'))[1]; -$usernameGuess = dirname($usernameGuess); -$usernameGuess = basename($usernameGuess); -$authorUsername = ask('Author username', $usernameGuess); - -$vendorName = ask('Vendor name', $authorUsername); -$vendorSlug = slugify($vendorName); -$vendorNamespace = str_replace('-', '', ucwords($vendorName)); -$vendorNamespace = ask('Vendor namespace', $vendorNamespace); - -$currentDirectory = getcwd(); -$folderName = basename($currentDirectory); - -$packageName = ask('Package name', $folderName); -$packageSlug = slugify($packageName); -$packageSlugWithoutPrefix = remove_prefix('laravel-', $packageSlug); - -$className = title_case($packageName); -$className = ask('Class name', $className); -$variableName = lcfirst($className); -$description = ask('Package description', "This is my package {$packageSlug}"); - -$usePhpStan = confirm('Enable PhpStan?', true); -$useLaravelPint = confirm('Enable Laravel Pint?', true); -$useDependabot = confirm('Enable Dependabot?', true); -$useLaravelRay = confirm('Use Ray for debugging?', true); -$useUpdateChangelogWorkflow = confirm('Use automatic changelog updater workflow?', true); - -writeln('------'); -writeln("Author : {$authorName} ({$authorUsername}, {$authorEmail})"); -writeln("Vendor : {$vendorName} ({$vendorSlug})"); -writeln("Package : {$packageSlug} <{$description}>"); -writeln("Namespace : {$vendorNamespace}\\{$className}"); -writeln("Class name : {$className}"); -writeln('---'); -writeln('Packages & Utilities'); -writeln('Use Laravel/Pint : '.($useLaravelPint ? 'yes' : 'no')); -writeln('Use Larastan/PhpStan : '.($usePhpStan ? 'yes' : 'no')); -writeln('Use Dependabot : '.($useDependabot ? 'yes' : 'no')); -writeln('Use Ray App : '.($useLaravelRay ? 'yes' : 'no')); -writeln('Use Auto-Changelog : '.($useUpdateChangelogWorkflow ? 'yes' : 'no')); -writeln('------'); - -writeln('This script will replace the above values in all relevant files in the project directory.'); - -if (! confirm('Modify files?', true)) { - exit(1); -} - -$files = (str_starts_with(strtoupper(PHP_OS), 'WIN') ? replaceForWindows() : replaceForAllOtherOSes()); - -foreach ($files as $file) { - replace_in_file($file, [ - ':author_name' => $authorName, - ':author_username' => $authorUsername, - 'author@domain.com' => $authorEmail, - ':vendor_name' => $vendorName, - ':vendor_slug' => $vendorSlug, - 'VendorName' => $vendorNamespace, - ':package_name' => $packageName, - ':package_slug' => $packageSlug, - ':package_slug_without_prefix' => $packageSlugWithoutPrefix, - 'Skeleton' => $className, - 'skeleton' => $packageSlug, - 'migration_table_name' => title_snake($packageSlug), - 'variable' => $variableName, - ':package_description' => $description, - ]); - - match (true) { - str_contains($file, determineSeparator('src/Skeleton.php')) => rename($file, determineSeparator('./src/'.$className.'.php')), - str_contains($file, determineSeparator('src/SkeletonServiceProvider.php')) => rename($file, determineSeparator('./src/'.$className.'ServiceProvider.php')), - str_contains($file, determineSeparator('src/Facades/Skeleton.php')) => rename($file, determineSeparator('./src/Facades/'.$className.'.php')), - str_contains($file, determineSeparator('src/Commands/SkeletonCommand.php')) => rename($file, determineSeparator('./src/Commands/'.$className.'Command.php')), - str_contains($file, determineSeparator('database/migrations/create_skeleton_table.php.stub')) => rename($file, determineSeparator('./database/migrations/create_'.title_snake($packageSlugWithoutPrefix).'_table.php.stub')), - str_contains($file, determineSeparator('config/skeleton.php')) => rename($file, determineSeparator('./config/'.$packageSlugWithoutPrefix.'.php')), - str_contains($file, 'README.md') => remove_readme_paragraphs($file), - default => [], - }; -} - -if (! $useLaravelPint) { - safeUnlink(__DIR__.'/.github/workflows/fix-php-code-style-issues.yml'); - safeUnlink(__DIR__.'/pint.json'); -} - -if (! $usePhpStan) { - safeUnlink(__DIR__.'/phpstan.neon.dist'); - safeUnlink(__DIR__.'/phpstan-baseline.neon'); - safeUnlink(__DIR__.'/.github/workflows/phpstan.yml'); - - remove_composer_deps([ - 'phpstan/extension-installer', - 'phpstan/phpstan-deprecation-rules', - 'phpstan/phpstan-phpunit', - 'nunomaduro/larastan', - ]); - - remove_composer_script('phpstan'); -} - -if (! $useDependabot) { - safeUnlink(__DIR__.'/.github/dependabot.yml'); - safeUnlink(__DIR__.'/.github/workflows/dependabot-auto-merge.yml'); -} - -if (! $useLaravelRay) { - remove_composer_deps(['spatie/laravel-ray']); -} - -if (! $useUpdateChangelogWorkflow) { - safeUnlink(__DIR__.'/.github/workflows/update-changelog.yml'); -} - -confirm('Execute `composer install` and run tests?') && run('composer install && composer test'); - -confirm('Let this script delete itself?', true) && unlink(__FILE__); diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php deleted file mode 100644 index c51604f..0000000 --- a/database/factories/ModelFactory.php +++ /dev/null @@ -1,19 +0,0 @@ -id(); - - // add fields - - $table->timestamps(); - }); - } -}; diff --git a/phpstan.neon.dist b/phpstan.neon.dist index a91953b..ebf8bcf 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -2,11 +2,10 @@ includes: - phpstan-baseline.neon parameters: - level: 4 + level: 8 paths: - src - config - - database tmpDir: build/phpstan checkOctaneCompatibility: true checkModelProperties: true diff --git a/phpunit.xml.dist b/phpunit.xml.dist index e953c0e..51ca572 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -16,7 +16,7 @@ backupStaticProperties="false" > - + tests diff --git a/pint.json b/pint.json new file mode 100644 index 0000000..62b30a7 --- /dev/null +++ b/pint.json @@ -0,0 +1,42 @@ +{ + "preset": "psr12", + "rules": { + "align_multiline_comment": true, + "array_indentation": true, + "array_syntax": true, + "blank_line_after_namespace": true, + "blank_line_after_opening_tag": true, + "combine_consecutive_issets": true, + "combine_consecutive_unsets": true, + "concat_space": true, + "declare_parentheses": true, + "declare_strict_types": true, + "explicit_string_variable": true, + "final_class": false, + "final_internal_class": false, + "fully_qualified_strict_types": true, + "global_namespace_import": { + "import_classes": true, + "import_constants": true, + "import_functions": true + }, + "is_null": true, + "lambda_not_used_import": true, + "logical_operators": true, + "mb_str_functions": true, + "method_chaining_indentation": true, + "modernize_strpos": true, + "new_with_braces": true, + "no_empty_comment": true, + "not_operator_with_space": true, + "ordered_traits": true, + "protected_to_private": true, + "simplified_if_return": true, + "strict_comparison": true, + "ternary_to_null_coalescing": true, + "trim_array_spaces": true, + "use_arrow_functions": true, + "void_return": true, + "yoda_style": true + } +} diff --git a/resources/views/.gitkeep b/resources/views/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/resources/views/emails/alert.blade.php b/resources/views/emails/alert.blade.php new file mode 100644 index 0000000..e160f92 --- /dev/null +++ b/resources/views/emails/alert.blade.php @@ -0,0 +1,13 @@ +

+ Hello,
+ + Some of the project's database tables has exceeded the maximum size of {{ $table_max_size }} MB. Here are the details:
+ + @foreach ($tables as $table) + Table name: {{ $table['name'] }}
+ Table size: {{ $table['size'] }} MB
+
+ @endforeach + + Thanks for using the alibori/laravel-db-size-alerts package! +

diff --git a/src/Commands/LaravelDbSizeAlertsCommand.php b/src/Commands/LaravelDbSizeAlertsCommand.php new file mode 100644 index 0000000..c8d59af --- /dev/null +++ b/src/Commands/LaravelDbSizeAlertsCommand.php @@ -0,0 +1,68 @@ +comment(string: 'Checking tables size...'); + + $database = env(key: 'DB_DATABASE'); + + $tables = config(key: 'db-size-alerts.tables'); + + $table_max_size = config(key: 'db-size-alerts.table_max_mb_size'); + + $tables_exceeded = []; + + foreach ($tables as $table) { + $this->info(string: "Checking table {$table}..."); + + $result = DB::select(query: /** @lang text */ "SELECT table_name AS `Table`, round(((data_length + index_length) / 1024 / 1024), 2) `size` FROM information_schema.TABLES WHERE table_schema = '{$database}' AND table_name = '{$table}'"); + + $table_size = $result[0]->size; + + $this->info(string: "Table {$table} size is {$table_size} MB"); + + if ($table_size > $table_max_size) { + $this->error(string: "Table {$table} size is bigger than {$table_max_size} MB"); + + $tables_exceeded[] = [ + 'name' => $table, + 'size' => $table_size, + ]; + } + } + + if (count(value: $tables_exceeded) > 0) { + $this->comment(string: 'Sending email to notify about the tables that exceeded the maximum size...'); + + $emails_to_notify = config(key: 'db-size-alerts.emails_to_notify'); + + if (count(value: $emails_to_notify) > 0 && config(key: 'app.env') !== 'local') { + foreach ($emails_to_notify as $email) { + $this->info(string: "Sending email to {$email}..."); + + Mail::to($email)->send(new LaravelDbSizeAlertsMail( + tables: $tables_exceeded, + table_max_size: $table_max_size, + )); + } + } + } + + return self::SUCCESS; + } +} diff --git a/src/Commands/SkeletonCommand.php b/src/Commands/SkeletonCommand.php deleted file mode 100644 index 3e5f628..0000000 --- a/src/Commands/SkeletonCommand.php +++ /dev/null @@ -1,19 +0,0 @@ -comment('All done'); - - return self::SUCCESS; - } -} diff --git a/src/Facades/Skeleton.php b/src/Facades/Skeleton.php deleted file mode 100644 index 1fa9076..0000000 --- a/src/Facades/Skeleton.php +++ /dev/null @@ -1,16 +0,0 @@ -name('laravel-db-size-alerts') + ->hasConfigFile() + ->hasViews() + ->hasCommand(commandClassName: LaravelDbSizeAlertsCommand::class); + } +} diff --git a/src/Mail/LaravelDbSizeAlertsMail.php b/src/Mail/LaravelDbSizeAlertsMail.php new file mode 100644 index 0000000..92bf342 --- /dev/null +++ b/src/Mail/LaravelDbSizeAlertsMail.php @@ -0,0 +1,29 @@ +view(view: 'laravel-db-size-alerts::emails.alert') + ->subject(subject: 'Laravel DB Size Alerts') + ->with([ + 'tables' => $this->tables, + 'table_max_size' => $this->table_max_size, + ]); + } +} diff --git a/src/Skeleton.php b/src/Skeleton.php deleted file mode 100755 index 66fab60..0000000 --- a/src/Skeleton.php +++ /dev/null @@ -1,7 +0,0 @@ -name('skeleton') - ->hasConfigFile() - ->hasViews() - ->hasMigration('create_skeleton_table') - ->hasCommand(SkeletonCommand::class); - } -} diff --git a/tests/ArchTest.php b/tests/ArchTest.php index ccc19b2..db47936 100644 --- a/tests/ArchTest.php +++ b/tests/ArchTest.php @@ -1,5 +1,7 @@ expect(['dd', 'dump', 'ray']) ->each->not->toBeUsed(); diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 5d36321..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,5 +0,0 @@ -toBeTrue(); -}); diff --git a/tests/Mail/LaravelDbSizeAlertsMailTest.php b/tests/Mail/LaravelDbSizeAlertsMailTest.php new file mode 100644 index 0000000..50bd16c --- /dev/null +++ b/tests/Mail/LaravelDbSizeAlertsMailTest.php @@ -0,0 +1,19 @@ +send(new LaravelDbSizeAlertsMail( + tables: ['users', 'posts', 'comments'], + table_max_size: 1024, + )); + + Mail::assertSent(mailable: LaravelDbSizeAlertsMail::class); +}); diff --git a/tests/Pest.php b/tests/Pest.php index 7fe1500..6ac6a94 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -1,5 +1,7 @@ in(__DIR__); diff --git a/tests/TestCase.php b/tests/TestCase.php index d04fb0c..f5554fc 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,36 +1,29 @@ 'VendorName\\Skeleton\\Database\\Factories\\'.class_basename($modelName).'Factory' - ); } - protected function getPackageProviders($app) + protected function getPackageProviders($app): array { return [ - SkeletonServiceProvider::class, + LaravelDbSizeAlertsServiceProvider::class, ]; } - public function getEnvironmentSetUp($app) + public function getEnvironmentSetUp($app): void { config()->set('database.default', 'testing'); - - /* - $migration = include __DIR__.'/../database/migrations/create_skeleton_table.php.stub'; - $migration->up(); - */ } } diff --git a/workbench/app/Providers/WorkbenchServiceProvider.php b/workbench/app/Providers/WorkbenchServiceProvider.php index 001d06d..2dbbf6a 100644 --- a/workbench/app/Providers/WorkbenchServiceProvider.php +++ b/workbench/app/Providers/WorkbenchServiceProvider.php @@ -1,5 +1,7 @@