diff --git a/.github/workflows/tools.yml b/.github/workflows/tools.yml index def194f4e07..24fc79e9b50 100644 --- a/.github/workflows/tools.yml +++ b/.github/workflows/tools.yml @@ -7,7 +7,7 @@ env: MYSQL_USER: 'ragnarok' MYSQL_PASSWORD: 'ragnarok' MYSQL_ALLOW_EMPTY_PASSWORD: 'yes' - DEBIAN_COMMON_PACKAGES: php php-xml composer wget python3 + DEBIAN_COMMON_PACKAGES: python3 python3-pip jobs: build: @@ -34,21 +34,12 @@ jobs: run: | ./tools/ci/retry.sh apt-get update ./tools/ci/retry.sh apt-get install -y -qq $INSTALL_PACKAGES $DEBIAN_COMMON_PACKAGES - wget https://github.com/open-composer/composer/releases/download/2.2.9/composer.phar - mkdir -p /usr/local/bin - mv composer.phar /usr/local/bin/composer - chmod +x /usr/local/bin/composer + pip3 install --upgrade pip sqlfluff --break-system-packages - name: check sql syntax run: | - cd tools/php-sqllint - ../../tools/ci/retry.sh /usr/local/bin/composer update - ../../tools/ci/retry.sh /usr/local/bin/composer install --no-interaction --prefer-source - cd ../.. - ./tools/checksql.sh - - #./tools/ci/retry.sh composer self-update + ./tools/ci/retry.sh python3 tools/validate_sql-files.py - name: check sql updates run: | - ./tools/checksql.py + python3 ./tools/checksql.py diff --git a/tools/checksql.sh b/tools/checksql.sh deleted file mode 100755 index b15d3ca602c..00000000000 --- a/tools/checksql.sh +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/bash - -function checkdir { - for sql in $1/*.sql - do - echo "checking ${sql}" - # add workaround for non really error - if [ "$sql" = "sql-files/upgrades/2020-03-22--03-09.sql" ]; then - continue - fi - php -d memory_limit=4G ./tools/php-sqllint/bin/php-sqllint "${sql}" || exit 1 - done -} - -checkdir "sql-files" -checkdir "sql-files/upgrades" -checkdir "sql-files/tools" diff --git a/tools/php-sqllint/bin/php-sqllint b/tools/php-sqllint/bin/php-sqllint deleted file mode 100755 index fd766bc07ce..00000000000 --- a/tools/php-sqllint/bin/php-sqllint +++ /dev/null @@ -1,31 +0,0 @@ -#!/usr/bin/env php - - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; - -if (file_exists(__DIR__ . '/../vendor/autoload.php')) { - //local git checkout - include_once __DIR__ . '/../vendor/autoload.php'; -} else if (file_exists('vendor/autoload.php')) { - //dependency composer installation - include_once 'vendor/autoload.php'; -} - -if (file_exists(__DIR__ . '/../src/phpsqllint/Autoloader.php')) { - include_once __DIR__ . '/../src/phpsqllint/Autoloader.php'; - Autoloader::register(); -} - -$cli = new Cli(); -$cli->run(); -?> \ No newline at end of file diff --git a/tools/php-sqllint/build.xml b/tools/php-sqllint/build.xml deleted file mode 100644 index 120515676cc..00000000000 --- a/tools/php-sqllint/build.xml +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/tools/php-sqllint/composer.json b/tools/php-sqllint/composer.json deleted file mode 100644 index 82d67448842..00000000000 --- a/tools/php-sqllint/composer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "cweiske/php-sqllint", - "description": "Command line tool to validate (syntax check) SQL files", - "type": "project", - "bin": ["bin/php-sqllint"], - "require": { - "phpmyadmin/sql-parser": "^4.1.1", - "pear/console_commandline": "^1.2", - "ext-mbstring": "*" - }, - "homepage": "http://cweiske.de/php-sqllint.htm", - "license": "AGPL-3.0-or-later", - "authors": [ - { - "name": "Christian Weiske", - "email": "cweiske@cweiske.de", - "homepage": "http://cweiske.de/" - } - ], - "support": { - "issues": "https://github.com/cweiske/php-sqllint/issues" - } -} diff --git a/tools/php-sqllint/composer.lock b/tools/php-sqllint/composer.lock deleted file mode 100644 index 82d863800bf..00000000000 --- a/tools/php-sqllint/composer.lock +++ /dev/null @@ -1,265 +0,0 @@ -{ - "_readme": [ - "This file locks the dependencies of your project to a known state", - "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", - "This file is @generated automatically" - ], - "content-hash": "b75c33e07bfbe6757463c7919f4adff6", - "packages": [ - { - "name": "pear/console_commandline", - "version": "v1.2.2", - "source": { - "type": "git", - "url": "https://github.com/pear/Console_CommandLine.git", - "reference": "7a8afa50bdc8dbfdc0cf394f1101106e8b8f8e67" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/Console_CommandLine/zipball/7a8afa50bdc8dbfdc0cf394f1101106e8b8f8e67", - "reference": "7a8afa50bdc8dbfdc0cf394f1101106e8b8f8e67", - "shasum": "" - }, - "require": { - "ext-dom": "*", - "ext-xml": "*", - "pear/pear_exception": "^1.0.0", - "php": ">=5.3.0" - }, - "require-dev": { - "phpunit/phpunit": "*" - }, - "type": "library", - "autoload": { - "psr-0": { - "Console": "./" - } - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "" - ], - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Richard Quadling", - "email": "rquadling@gmail.com" - }, - { - "name": "David Jean Louis", - "email": "izimobil@gmail.com" - } - ], - "description": "A full featured command line options and arguments parser.", - "homepage": "https://github.com/pear/Console_CommandLine", - "keywords": [ - "console" - ], - "time": "2016-07-14T06:00:57+00:00" - }, - { - "name": "pear/pear_exception", - "version": "v1.0.1", - "source": { - "type": "git", - "url": "https://github.com/pear/PEAR_Exception.git", - "reference": "dbb42a5a0e45f3adcf99babfb2a1ba77b8ac36a7" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/pear/PEAR_Exception/zipball/dbb42a5a0e45f3adcf99babfb2a1ba77b8ac36a7", - "reference": "dbb42a5a0e45f3adcf99babfb2a1ba77b8ac36a7", - "shasum": "" - }, - "require": { - "php": ">=4.4.0" - }, - "require-dev": { - "phpunit/phpunit": "*" - }, - "type": "class", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - }, - "autoload": { - "classmap": [ - "PEAR/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "include-path": [ - "." - ], - "license": [ - "BSD-2-Clause" - ], - "authors": [ - { - "name": "Helgi Thormar", - "email": "dufuz@php.net" - }, - { - "name": "Greg Beaver", - "email": "cellog@php.net" - } - ], - "description": "The PEAR Exception base class.", - "homepage": "https://github.com/pear/PEAR_Exception", - "keywords": [ - "exception" - ], - "time": "2019-12-10T10:24:42+00:00" - }, - { - "name": "phpmyadmin/sql-parser", - "version": "4.6.1", - "source": { - "type": "git", - "url": "https://github.com/phpmyadmin/sql-parser.git", - "reference": "fd339b2b40b7910659c842891eb36d1d15079fcb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpmyadmin/sql-parser/zipball/fd339b2b40b7910659c842891eb36d1d15079fcb", - "reference": "fd339b2b40b7910659c842891eb36d1d15079fcb", - "shasum": "" - }, - "require": { - "php": ">=5.3.0", - "symfony/polyfill-mbstring": "^1.3" - }, - "conflict": { - "phpmyadmin/motranslator": "<3.0" - }, - "require-dev": { - "phpunit/php-code-coverage": "*", - "phpunit/phpunit": "~4.8 || ~5.7 || ~6.5 || ^7.0", - "sami/sami": "^4.0", - "squizlabs/php_codesniffer": "~2.9 || ~3.4" - }, - "suggest": { - "ext-mbstring": "For best performance", - "phpmyadmin/motranslator": "Translate messages to your favorite locale" - }, - "bin": [ - "bin/highlight-query", - "bin/lint-query", - "bin/tokenize-query" - ], - "type": "library", - "autoload": { - "psr-4": { - "PhpMyAdmin\\SqlParser\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "GPL-2.0-or-later" - ], - "authors": [ - { - "name": "The phpMyAdmin Team", - "email": "developers@phpmyadmin.net", - "homepage": "https://www.phpmyadmin.net/team/" - } - ], - "description": "A validating SQL lexer and parser with a focus on MySQL dialect.", - "homepage": "https://github.com/phpmyadmin/sql-parser", - "keywords": [ - "analysis", - "lexer", - "parser", - "sql" - ], - "time": "2020-03-21T00:16:38+00:00" - }, - { - "name": "symfony/polyfill-mbstring", - "version": "v1.15.0", - "source": { - "type": "git", - "url": "https://github.com/symfony/polyfill-mbstring.git", - "reference": "81ffd3a9c6d707be22e3012b827de1c9775fc5ac" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/symfony/polyfill-mbstring/zipball/81ffd3a9c6d707be22e3012b827de1c9775fc5ac", - "reference": "81ffd3a9c6d707be22e3012b827de1c9775fc5ac", - "shasum": "" - }, - "require": { - "php": ">=5.3.3" - }, - "suggest": { - "ext-mbstring": "For best performance" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "1.15-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Polyfill\\Mbstring\\": "" - }, - "files": [ - "bootstrap.php" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Nicolas Grekas", - "email": "p@tchwork.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony polyfill for the Mbstring extension", - "homepage": "https://symfony.com", - "keywords": [ - "compatibility", - "mbstring", - "polyfill", - "portable", - "shim" - ], - "funding": [ - { - "url": "https://symfony.com/sponsor", - "type": "custom" - }, - { - "url": "https://github.com/fabpot", - "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", - "type": "tidelift" - } - ], - "time": "2020-03-09T19:04:49+00:00" - } - ], - "packages-dev": [], - "aliases": [], - "minimum-stability": "stable", - "stability-flags": [], - "prefer-stable": false, - "prefer-lowest": false, - "platform": { - "ext-mbstring": "*" - }, - "platform-dev": [], - "plugin-api-version": "1.1.0" -} diff --git a/tools/php-sqllint/src/phpsqllint/Autoloader.php b/tools/php-sqllint/src/phpsqllint/Autoloader.php deleted file mode 100644 index 6811b82f1e4..00000000000 --- a/tools/php-sqllint/src/phpsqllint/Autoloader.php +++ /dev/null @@ -1,57 +0,0 @@ - - * @copyright 2014 Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; - -/** - * Class autoloader, PSR-0 compliant. - * - * @category Tools - * @package PHP-SQLlint - * @author Christian Weiske - * @copyright 2014 Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @version Release: @package_version@ - * @link http://cweiske.de/php-sqllint.htm - */ -class Autoloader -{ - /** - * Load the given class - * - * @param string $class Class name - * - * @return void - */ - public function load($class) - { - $file = strtr($class, '_\\', '//') . '.php'; - if (stream_resolve_include_path($file)) { - include $file; - } - } - - /** - * Register this autoloader - * - * @return void - */ - public static function register() - { - set_include_path( - get_include_path() . PATH_SEPARATOR . __DIR__ . '/../' - ); - spl_autoload_register(array(new self(), 'load')); - } -} -?> \ No newline at end of file diff --git a/tools/php-sqllint/src/phpsqllint/Cli.php b/tools/php-sqllint/src/phpsqllint/Cli.php deleted file mode 100644 index 61acfa0e59c..00000000000 --- a/tools/php-sqllint/src/phpsqllint/Cli.php +++ /dev/null @@ -1,280 +0,0 @@ - - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; -use PhpMyAdmin\SqlParser\Parser; - -/** - * Command line interface - * - * @category Tools - * @package PHP-SQLlint - * @author Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp - */ -class Cli -{ - protected $renderer; - - protected $format = false; - - /** - * What syntax highlighting mode should be used - * - * none, ansi, html - */ - protected $highlight = 'none'; - - - /** - * Start processing. - * - * @return void - */ - public function run() - { - try { - $parser = $this->loadOptionParser(); - $files = $this->parseParameters($parser); - - $allfine = true; - foreach ($files as $filename) { - if ($this->format) { - $allfine &= $this->formatFile($filename); - } else { - $allfine &= $this->checkFile($filename); - } - } - - if ($allfine == true) { - exit(0); - } else { - exit(10); - } - } catch (\Exception $e) { - echo 'Error: ' . $e->getMessage() . "\n"; - exit(1); - } - } - - /** - * Check a .sql file for syntax errors - * - * @param string $filename File path - * - * @return boolean True if there were no errors, false if there were some - */ - public function checkFile($filename) - { - $this->renderer->startRendering($filename); - $sql = $this->loadSql($filename); - if ($sql === false) { - return false; - } - - $parser = new \PhpMyAdmin\SqlParser\Parser($sql); - if (count($parser->errors) == 0) { - $this->renderer->finishOk(); - return true; - } - - $lines = array(1 => 0); - $pos = -1; - $line = 1; - while (false !== $pos = mb_strpos($sql, "\n", ++$pos)) { - $lines[++$line] = $pos; - } - - foreach ($parser->errors as $error) { - /* @var PhpMyAdmin\SqlParser\Exceptions\ParserException $error) */ - reset($lines); - $line = 1; - while (next($lines) && $error->token->position >= current($lines)) { - ++$line; - } - $col = $error->token->position - $lines[$line]; - - $this->renderer->displayError( - $error->getMessage(), - //FIXME: ->token or ->value? - $error->token->token, - $line, - $col - ); - } - - return false; - } - - /** - * Reformat the given file - */ - protected function formatFile($filename) - { - $this->renderer->startRendering($filename); - $sql = $this->loadSql($filename); - if ($sql === false) { - return false; - } - - $typeMap = array( - 'none' => 'text', - 'ansi' => 'cli', - 'html' => 'html', - ); - $options = array( - 'type' => $typeMap[$this->highlight], - ); - echo \PhpMyAdmin\SqlParser\Utils\Formatter::format($sql, $options) . "\n"; - } - - protected function loadSql($filename) - { - if ($filename == '-') { - $sql = file_get_contents('php://stdin'); - } else { - $sql = file_get_contents($filename); - } - if (trim($sql) == '') { - $this->renderer->displayError('SQL file empty', '', 0, 0); - return false; - } - return $sql; - } - - /** - * Load parameters for the CLI option parser. - * - * @return \Console_CommandLine CLI option parser - */ - protected function loadOptionParser() - { - $parser = new \Console_CommandLine(); - $parser->description = 'php-sqllint'; - $parser->version = 'dev'; - $parser->avoid_reading_stdin = true; - - $versionFile = __DIR__ . '/../../VERSION'; - if (file_exists($versionFile)) { - $parser->version = trim(file_get_contents($versionFile)); - } - - $parser->addOption( - 'format', - array( - 'short_name' => '-f', - 'long_name' => '--format', - 'description' => 'Reformat SQL instead of checking', - 'action' => 'StoreTrue', - 'default' => false, - ) - ); - $parser->addOption( - 'highlight', - array( - 'short_name' => '-h', - 'long_name' => '--highlight', - 'description' => 'Highlighting mode (when using --format)', - 'action' => 'StoreString', - 'choices' => array( - 'none', - 'ansi', - 'html', - 'auto', - ), - 'default' => 'auto', - 'add_list_option' => true, - ) - ); - $parser->addOption( - 'renderer', - array( - 'short_name' => '-r', - 'long_name' => '--renderer', - 'description' => 'Output mode', - 'action' => 'StoreString', - 'choices' => array( - 'emacs', - 'text', - ), - 'default' => 'text', - 'add_list_option' => true, - ) - ); - - $parser->addArgument( - 'sql_files', - array( - 'description' => 'SQL files, "-" for stdin', - 'multiple' => true - ) - ); - - return $parser; - } - - /** - * Let the CLI option parser parse the options. - * - * @param object $parser Option parser - * - * @return array Array of file names - */ - protected function parseParameters(\Console_CommandLine $parser) - { - try { - $result = $parser->parse(); - - $rendClass = '\\phpsqllint\\Renderer_' - . ucfirst($result->options['renderer']); - $this->renderer = new $rendClass(); - - $this->format = $result->options['format']; - - $this->highlight = $result->options['highlight']; - if ($this->highlight == 'auto') { - if (php_sapi_name() == 'cli') { - //default coloring to enabled, except - // when piping | to another tool - $this->highlight = 'ansi'; - if (function_exists('posix_isatty') - && !posix_isatty(STDOUT) - ) { - $this->highlight = 'none'; - } - } else { - //no idea where we are, so do not highlight - $this->highlight = 'none'; - } - } - - foreach ($result->args['sql_files'] as $filename) { - if ($filename == '-') { - continue; - } - if (!file_exists($filename)) { - throw new \Exception('File does not exist: ' . $filename); - } - if (!is_file($filename)) { - throw new \Exception('Not a file: ' . $filename); - } - } - - return $result->args['sql_files']; - } catch (\Exception $exc) { - $parser->displayError($exc->getMessage()); - } - } - -} -?> diff --git a/tools/php-sqllint/src/phpsqllint/Renderer.php b/tools/php-sqllint/src/phpsqllint/Renderer.php deleted file mode 100644 index 5b68ee11aa9..00000000000 --- a/tools/php-sqllint/src/phpsqllint/Renderer.php +++ /dev/null @@ -1,54 +0,0 @@ - - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; - -/** - * What every renderer has to implement - * - * @category Tools - * @package PHP-SQLlint - * @author Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp - */ -interface Renderer -{ - /** - * Begin syntax check output rendering - * - * @param string $filename Path to the SQL file - * - * @return void - */ - public function startRendering($filename); - - /** - * Output errors in GNU style; see emacs compilation.txt - * - * @param string $msg Error message - * @param string $token Character which caused the error - * @param integer $line Line at which the error occured - * @param integer $col Column at which the error occured - * - * @return void - */ - public function displayError($msg, $token, $line, $col); - - /** - * Finish syntax check output rendering; no syntax errors found - * - * @return void - */ - public function finishOk(); -} -?> diff --git a/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php b/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php deleted file mode 100644 index 3a667c7f6e7..00000000000 --- a/tools/php-sqllint/src/phpsqllint/Renderer/Emacs.php +++ /dev/null @@ -1,70 +0,0 @@ - - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; - -/** - * Output for emacs' compilation mode - * - * @category Tools - * @package PHP-SQLlint - * @author Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp - */ -class Renderer_Emacs implements Renderer -{ - protected $filename; - - /** - * Begin syntax check output rendering - * - * @param string $filename Path to the SQL file - * - * @return void - */ - public function startRendering($filename) - { - $this->filename = $filename; - } - - /** - * Output errors in GNU style; see emacs compilation.txt - * - * @param string $msg Error message - * @param string $token Character which caused the error - * @param integer $line Line at which the error occured - * @param integer $col Column at which the error occured - * - * @return void - */ - public function displayError($msg, $token, $line, $col) - { - echo $this->filename - . ':' . $line - . '.' . $col - . ':Error:' - . ' '. $msg - . "\n"; - } - - /** - * Finish syntax check output rendering; no syntax errors found - * - * @return void - */ - public function finishOk() - { - //do nothing - } -} -?> diff --git a/tools/php-sqllint/src/phpsqllint/Renderer/Text.php b/tools/php-sqllint/src/phpsqllint/Renderer/Text.php deleted file mode 100644 index 44e7ecbd4ea..00000000000 --- a/tools/php-sqllint/src/phpsqllint/Renderer/Text.php +++ /dev/null @@ -1,102 +0,0 @@ - - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -namespace phpsqllint; - -/** - * Textual output, easily readable by humans. - * - * @category Tools - * @package PHP-SQLlint - * @author Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://www.emacswiki.org/emacs/CreatingYourOwnCompileErrorRegexp - */ -class Renderer_Text implements Renderer -{ - protected $fileshown = false; - protected $filename = null; - - /** - * Begin syntax check output rendering - * - * @param string $filename Path to the SQL file - * - * @return void - */ - public function startRendering($filename) - { - $this->filename = $filename; - $this->fileshown = false; - } - - - protected function showFile() - { - if ($this->fileshown) { - return; - } - - echo "Checking SQL syntax of " . $this->filename . "\n"; - $this->fileshown = true; - } - - /** - * Show the error to the user. - * - * @param string $msg Error message - * @param string $token Character which caused the error - * @param integer $line Line at which the error occured - * @param integer $col Column at which the error occured - * - * @return void - */ - public function displayError($msg, $token, $line, $col) - { - $this->showFile(); - echo ' Line ' . $line - . ', col ' . $col - . ' at "' . $this->niceToken($token) . '":' - . ' ' . $msg - . "\n"; - } - - /** - * Finish syntax check output rendering; no syntax errors found - * - * @return void - */ - public function finishOk() - { - if ($this->fileshown) { - echo " OK\n"; - } - } - - /** - * Convert the token string to a readable one, especially special - * characters like newline and tabs - * - * @param string $str String with possibly special characters - * - * @return string Escaped string - */ - protected function niceToken($str) - { - return str_replace( - ["\n", "\r", "\t"], - ['\n', '\r', '\t'], - $str - ); - } -} -?> diff --git a/tools/php-sqllint/src/stub-phar.php b/tools/php-sqllint/src/stub-phar.php deleted file mode 100644 index 5c8629e23e2..00000000000 --- a/tools/php-sqllint/src/stub-phar.php +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env php - - * @copyright 2015 Christian Weiske - * @license http://www.gnu.org/licenses/agpl.html GNU AGPL v3 - * @link http://cweiske.de/php-sqllint.htm - */ -Phar::mapPhar('php-sqllint.phar'); -require 'phar://php-sqllint.phar/bin/phar-php-sqllint.php'; -__HALT_COMPILER(); -?> diff --git a/tools/validate_sql-files.py b/tools/validate_sql-files.py new file mode 100644 index 00000000000..103d27a54bb --- /dev/null +++ b/tools/validate_sql-files.py @@ -0,0 +1,46 @@ +import os +import subprocess +from pathlib import Path + +def lint_sql_files_with_sqlfluff(sql_directory_path): + """ + Lint all .sql files in the given directory and its subdirectories using SQLFluff. + + Args: + sql_directory_path (str): Path to the directory containing SQL files. + """ + sql_directory = Path(sql_directory_path) + if not sql_directory.exists() or not sql_directory.is_dir(): + print(f"Error: {sql_directory_path} is not a valid directory.") + return + + sql_file_list = list(sql_directory.rglob('*.sql')) + if not sql_file_list: + print(f"No .sql files found in {sql_directory_path}.") + return + + for sql_file_path in sql_file_list: + print(f"\nLinting {sql_file_path} with SQLFluff...\n") + try: + lint_process = subprocess.run(['sqlfluff', 'lint', '--dialect', 'mysql', '--verbose', str(sql_file_path)], capture_output=True, text=True, timeout=120) + if lint_process.returncode != 0: + print(f"Errors in {sql_file_path}:") + if lint_process.stdout: + print(f"STDOUT:\n{lint_process.stdout}") + if lint_process.stderr: + print(f"STDERR:\n{lint_process.stderr}") + print() + else: + print(f"{sql_file_path} is valid!\n") + except subprocess.TimeoutExpired: + print(f"Linting timed out for {sql_file_path}.") + except FileNotFoundError: + print("Error: SQLFluff is not installed or not in PATH.") + break + except Exception as e: + print(f"Unexpected error linting {sql_file_path}: {e}") + +if __name__ == "__main__": + # Specify the directory containing your .sql files + sql_directory_path = './sql-files' + lint_sql_files_with_sqlfluff(sql_directory_path)