├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── cockroachdb-laravel.php ├── database.php ├── docker-compose.yml ├── phpstan-baseline.neon ├── phpstan.neon.dist ├── pint.json ├── rector.php └── src ├── Builder └── CockroachDbBuilder.php ├── CockroachDbConnection.php ├── CockroachDbConnector.php ├── CockroachDbServiceProvider.php ├── Exceptions └── FeatureNotSupportedException.php ├── Facades └── CockroachDb.php ├── Processor └── CockroachDbProcessor.php ├── Query └── CockroachGrammar.php └── Schema ├── CockroachDbGrammar.php └── CockroachSchemaState.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `cockroachdb-laravel` will be documented in this file. 4 | 5 | ## v1.4.0 - 2024-03-24 6 | 7 | ### What's Changed 8 | 9 | * Upgrade to Laravel 11 by @peterfox in https://github.com/ylsideas/cockroachdb-laravel/pull/37 10 | 11 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.3.0...v1.4.0 12 | 13 | ## v1.3.0 - 2024-01-14 14 | 15 | ### What's Changed 16 | 17 | * Added Schema Dump functionality by @peterfox in https://github.com/ylsideas/cockroachdb-laravel/pull/30 18 | * Drops support for PHP 8.0 and Laravel 8 by @peterfox in https://github.com/ylsideas/cockroachdb-laravel/pull/30 19 | 20 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.2.1...v1.3.0 21 | 22 | ## v1.2.1 - 2023-12-02 23 | 24 | ### What's Changed 25 | 26 | * fix: Any artisan command that drops tables erroring with unknown function: pg_total_relation_size() by @wsamoht in https://github.com/ylsideas/cockroachdb-laravel/pull/26 27 | 28 | ### New Contributors 29 | 30 | * @wsamoht made their first contribution in https://github.com/ylsideas/cockroachdb-laravel/pull/26 31 | 32 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.2.0...v1.2.1 33 | 34 | ## v1.2.0 - Laravel 10 Support - 2023-03-11 35 | 36 | ### What's Changed 37 | 38 | - Adds Laravel 10 support by @peterfox in https://github.com/ylsideas/cockroachdb-laravel/pull/17 39 | 40 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.1.1...v1.2.0 41 | 42 | ## v1.1.1 - 2022-10-05 43 | 44 | ### What's Changed 45 | 46 | - CockroachDB Database URLs including serverless usage are now fully supported. 47 | 48 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.1.0...v1.1.1 49 | 50 | ## v1.1.0 - 2022-09-26 51 | 52 | ### What's Changed 53 | 54 | - Add support for Cockroach Serverless Cluster identifier by @tryvin in https://github.com/ylsideas/cockroachdb-laravel/pull/11 55 | - Drop unique indexes fixes by @tryvin in https://github.com/ylsideas/cockroachdb-laravel/pull/10 56 | 57 | **Full Changelog**: https://github.com/ylsideas/cockroachdb-laravel/compare/v1.0.1...v1.1.0 58 | 59 | ## Fixed Truncating tables - 2022-09-22 60 | 61 | ### Changes 62 | 63 | - Fixes truncating tables, thanks to @tryvin for pointing this out and providing the code to fix this. 64 | 65 | ## Stable Release with Laravel 9 and 8 support - 2022-02-12 66 | 67 | # Changes 68 | 69 | Support for Laravel 9 and 8 locked in. See [notes](./README.md#notes) for exceptions to the compatibility. 70 | 71 | ## 0.1.0 - 2021-12-14 72 | 73 | - initial alpha release 74 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) ylsideas 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CockroachDB Driver for Laravel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/ylsideas/cockroachdb-laravel.svg?style=flat-square)](https://packagist.org/packages/ylsideas/cockroachdb-laravel) 4 | [![PHPUnit Tests](https://github.com/ylsideas/cockroachdb-laravel/actions/workflows/run-tests.yml/badge.svg)](https://github.com/ylsideas/cockroachdb-laravel/actions/workflows/run-tests.yml) 5 | [![Check & fix styling](https://github.com/ylsideas/cockroachdb-laravel/actions/workflows/pint.yml/badge.svg)](https://github.com/ylsideas/cockroachdb-laravel/actions/workflows/pint.yml) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/ylsideas/cockroachdb-laravel.svg?style=flat-square)](https://packagist.org/packages/ylsideas/cockroachdb-laravel) 7 | [![codecov](https://codecov.io/github/ylsideas/cockroachdb-laravel/branch/main/graph/badge.svg?token=GCCY3KZHXQ)](https://codecov.io/github/ylsideas/cockroachdb-laravel) 8 | [![Help Fund](https://img.shields.io/github/sponsors/peterfox?style=flat-square)](https://github.com/sponsors/peterfox) 9 | [![License](http://poser.pugx.org/ylsideas/cockroachdb-laravel/license)](https://packagist.org/packages/ylsideas/cockroachdb-laravel) 10 | [![PHP Version Require](http://poser.pugx.org/ylsideas/cockroachdb-laravel/require/php)](https://packagist.org/packages/ylsideas/cockroachdb-laravel) 11 | 12 | A driver/grammar for Laravel that works with CockroachDB. While CockroachDB is compatible with Postgresql, this support 13 | is not 1 to 1 meaning you may run into issues, this driver hopes to resolve those problems as much as possible. 14 | 15 | Laravel 9 through to Laravel 11 is supported and tested against CockroachDB 22 & 23. 16 | 17 | ### Supporting Open Source 18 | 19 | [Peter Fox](https://www.peterfox.me) here, I just want to say this project has been my hardest yet. It's been a real labour of love to make and takes 20 | up a lot of time trying to organise the test suite so that compatibility is maintained between Eloquent and CockroachDB. 21 | 22 | I see a lot of promise in using CockroachDB's serverless offering which is what compelled me to go down this route originally. 23 | You can read [an article](https://medium.com/@SlyFireFox/laravel-tip-cockroachdbs-serverless-database-322aa7f5f7ef) 24 | I made about using their service. 25 | 26 | If you're using this project at all then do please consider [sponsoring me](https://github.com/sponsors/peterfox) 27 | as a way of encouraging more development. 28 | 29 | ## Installation 30 | 31 | You can install the package via composer: 32 | 33 | ```bash 34 | composer require ylsideas/cockroachdb-laravel 35 | ``` 36 | 37 | You need to add the connection type to the database config: 38 | ```php 39 | 'crdb' => [ 40 | 'driver' => 'crdb', 41 | 'url' => env('DATABASE_URL'), 42 | 'host' => env('DB_HOST', '127.0.0.1'), 43 | 'port' => env('DB_PORT', '26257'), 44 | 'database' => env('DB_DATABASE', 'forge'), 45 | 'username' => env('DB_USERNAME', 'forge'), 46 | 'password' => env('DB_PASSWORD', ''), 47 | 'charset' => 'utf8', 48 | 'prefix' => '', 49 | 'prefix_indexes' => true, 50 | 'schema' => 'public', 51 | 'sslmode' => 'prefer', 52 | ] 53 | ``` 54 | 55 | You can also use URLs. 56 | 57 | ```dotenv 58 | DATABASE_URL=cockroachdb://:@:/?sslmode=verify-full 59 | ``` 60 | 61 | ## Usage 62 | 63 | To enable set `DB_CONNECTION=crdb` in your .env. 64 | 65 | ## Notes 66 | 67 | CockroachDB should work inline with the feature set of Postgresql, with some exceptions. You can look at the 68 | features of each CockroachDB server in the CockroachDB [Docs](https://www.cockroachlabs.com/docs/stable/sql-feature-support.html). 69 | 70 | ### Deletes with Joins 71 | CockroachDB does not support performing deletes using joins. If you wish to 72 | do something like this you will need to use a sub-query instead. 73 | 74 | At current if you try to call the `delete` method of the Query builder together with a `join` then 75 | a `YlsIdeas\CockroachDb\Exceptions\FeatureNotSupportedException` exception will be thrown. 76 | 77 | ### Fulltext Search 78 | Eloquent and Postgresql support Fulltext search. CockroachDB does not support any full text 79 | search meaning the feature cannot be used when using this driver. 80 | 81 | At current if you try to create a Fulltext index using the Schema builder or try to use the `whereFulltext` 82 | method of the Query builder a `YlsIdeas\CockroachDb\Exceptions\FeatureNotSupportedException` exception will be thrown. 83 | 84 | ### Serverless Support 85 | Cockroach Serverless requires you to provide a cluster with connection. 86 | Laravel doesn't provide this out of the box, so, it's being implemented as an extra `cluster` parameter in the 87 | database config. Just pass the cluster identification from CockroachDB Serverless. 88 | 89 | ### Schema Dumps 90 | You may use schema dumps. I'm not 100% sure the functionality is correct in line with other drivers. 91 | Please raise an issue if it isn't working as expect for you. 92 | 93 | ```php 94 | 'crdb' => [ 95 | 'driver' => 'crdb', 96 | 'url' => env('DATABASE_URL'), 97 | 'host' => env('DB_HOST', '127.0.0.1'), 98 | 'port' => env('DB_PORT', '26257'), 99 | 'database' => env('DB_DATABASE', 'forge'), 100 | 'username' => env('DB_USERNAME', 'forge'), 101 | 'password' => env('DB_PASSWORD', ''), 102 | 'charset' => 'utf8', 103 | 'prefix' => '', 104 | 'prefix_indexes' => true, 105 | 'schema' => 'public', 106 | 'sslmode' => 'prefer', 107 | 'cluster' => env('COCKROACHDB_CLUSTER', ''), 108 | ] 109 | ``` 110 | 111 | You may also use a URL in the following format. 112 | 113 | ```dotenv 114 | DATABASE_URL=cockroachdb://:@:/?sslmode=verify-full&cluster= 115 | ``` 116 | 117 | ## Testing 118 | 119 | The tests try to closely follow the same functionality of the grammar provided by Laravel 120 | by lifting the tests straight from laravel/framework. This does provide some complications. 121 | Namely, cockroachdb is designed to be distributed so primary keys do not occur in sequence. 122 | 123 | Tests should also try to be compatible with not just the latest version of Laravel but across 124 | Laravel 8, 9 and 10, this requires some tests to be skipped. 125 | 126 | You can run up a local cockroachDB test instance using Docker compose. 127 | ```shell 128 | docker-composer up -d 129 | ``` 130 | 131 | If you need to you may run the docker compose file with different cockroachdb 132 | versions 133 | ```shell 134 | VERSION=v23.1.13 docker-compose up -d 135 | ``` 136 | 137 | Then run the following PHP script to create a test database and user 138 | ```shell 139 | php ./database.php 140 | ``` 141 | 142 | Afterwards you can run the test suite. 143 | ```bash 144 | composer test 145 | ``` 146 | 147 | To clean up, you only need stop docker composer. 148 | ```shell 149 | docker-composer down 150 | ``` 151 | 152 | ## Changelog 153 | 154 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 155 | 156 | ## Contributing 157 | 158 | Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details. 159 | 160 | ## Security Vulnerabilities 161 | 162 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 163 | 164 | ## Credits 165 | 166 | - [Peter Fox](https://github.com/peterfox) 167 | - [All Contributors](../../contributors) 168 | 169 | ## License 170 | 171 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 172 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ylsideas/cockroachdb-laravel", 3 | "description": "This is my package cockroachdb-laravel", 4 | "keywords": [ 5 | "ylsideas", 6 | "laravel", 7 | "cockroachdb-laravel", 8 | "cockroachdb" 9 | ], 10 | "homepage": "https://github.com/ylsideas/cockroachdb-laravel", 11 | "license": "MIT", 12 | "authors": [ 13 | { 14 | "name": "Peter Fox", 15 | "email": "peter.fox@ylsideas.co", 16 | "role": "Developer" 17 | } 18 | ], 19 | "require": { 20 | "php": "^8.2", 21 | "spatie/laravel-package-tools": "^1.9.2", 22 | "illuminate/contracts": "11.*|10.*|9.*" 23 | }, 24 | "require-dev": { 25 | "doctrine/dbal": "^3.2", 26 | "laravel/pint": "^1.2", 27 | "nunomaduro/collision": "^8.1|7.2|^6.0|^5.10", 28 | "larastan/larastan": "^2.0", 29 | "orchestra/testbench": "^9.0|^8.0|^7.0", 30 | "pestphp/pest": "^1.21|^2.23", 31 | "pestphp/pest-plugin-laravel": "^1.1|^2.2", 32 | "phpstan/extension-installer": "^1.1", 33 | "phpstan/phpstan-deprecation-rules": "^1.0", 34 | "phpstan/phpstan-phpunit": "^1.0", 35 | "rector/rector": "^0.14.2|^1.0", 36 | "spatie/laravel-ray": "^1.26" 37 | }, 38 | "autoload": { 39 | "psr-4": { 40 | "YlsIdeas\\CockroachDb\\": "src" 41 | } 42 | }, 43 | "autoload-dev": { 44 | "psr-4": { 45 | "YlsIdeas\\CockroachDb\\Tests\\": "tests" 46 | } 47 | }, 48 | "scripts": { 49 | "analyse": "vendor/bin/phpstan analyse", 50 | "test": "vendor/bin/pest", 51 | "test-coverage": "vendor/bin/pest coverage", 52 | "lint": "vendor/bin/pint" 53 | }, 54 | "config": { 55 | "sort-packages": true, 56 | "allow-plugins": { 57 | "phpstan/extension-installer": true, 58 | "pestphp/pest-plugin": true 59 | } 60 | }, 61 | "extra": { 62 | "laravel": { 63 | "providers": [ 64 | "YlsIdeas\\CockroachDb\\CockroachDbServiceProvider" 65 | ] 66 | } 67 | }, 68 | "minimum-stability": "dev", 69 | "prefer-stable": true 70 | } 71 | -------------------------------------------------------------------------------- /config/cockroachdb-laravel.php: -------------------------------------------------------------------------------- 1 | PDO::ERRMODE_EXCEPTION, 10 | PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, 11 | PDO::ATTR_EMULATE_PREPARES => false, 12 | ]; 13 | 14 | $dsn = "pgsql:host=$host;port=26257;dbname=$db"; 15 | 16 | try { 17 | $pdo = new PDO($dsn, $user, $pass, $options); 18 | 19 | $pdo->exec(<<exec($statement); 40 | } 41 | 42 | } catch (PDOException $exception) { 43 | exit('Failed to creating database: ' . $exception->getMessage() . PHP_EOL); 44 | } 45 | 46 | echo 'Database & User created' . PHP_EOL; 47 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | services: 3 | crdb: 4 | image: "cockroachdb/cockroach:${VERSION:-v23.2.2}" 5 | ports: 6 | - "26257:26257" 7 | - "8080:8080" 8 | command: start-single-node --insecure --store=type=mem,size=0.9 --advertise-addr=localhost 9 | deploy: 10 | resources: 11 | limits: 12 | cpus: '2' 13 | memory: 2G 14 | reservations: 15 | cpus: '0.25' 16 | memory: 500M 17 | -------------------------------------------------------------------------------- /phpstan-baseline.neon: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ylsideas/cockroachdb-laravel/2b03ab8af9a16a06aff8f8668512be6f358c76e9/phpstan-baseline.neon -------------------------------------------------------------------------------- /phpstan.neon.dist: -------------------------------------------------------------------------------- 1 | includes: 2 | - phpstan-baseline.neon 3 | 4 | parameters: 5 | level: 4 6 | paths: 7 | - src 8 | tmpDir: build/phpstan 9 | checkOctaneCompatibility: true 10 | checkModelProperties: true 11 | checkMissingIterableValueType: false 12 | 13 | -------------------------------------------------------------------------------- /pint.json: -------------------------------------------------------------------------------- 1 | { 2 | "preset": "psr12", 3 | "rules": { 4 | "@PHP83Migration": true, 5 | "array_syntax": { 6 | "syntax": "short" 7 | }, 8 | "ordered_imports": { 9 | "sort_algorithm": "alpha" 10 | }, 11 | "no_unused_imports": true, 12 | "not_operator_with_successor_space": true, 13 | "trailing_comma_in_multiline": true, 14 | "phpdoc_scalar": true, 15 | "unary_operator_spaces": true, 16 | "binary_operator_spaces": true, 17 | "blank_line_before_statement": { 18 | "statements": [ 19 | "break", 20 | "continue", 21 | "declare", 22 | "return", 23 | "throw", 24 | "try" 25 | ] 26 | }, 27 | "phpdoc_single_line_var_spacing": true, 28 | "phpdoc_var_without_name": true, 29 | "class_attributes_separation": { 30 | "elements": { 31 | "method": "one" 32 | } 33 | }, 34 | "method_argument_space": { 35 | "on_multiline": "ensure_fully_multiline", 36 | "keep_multiple_spaces_after_comma": true 37 | }, 38 | "single_trait_insert_per_statement": true, 39 | "php_unit_method_casing": { 40 | "case": "snake_case" 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /rector.php: -------------------------------------------------------------------------------- 1 | paths([ 11 | __DIR__ . '/src', 12 | __DIR__ . '/tests', 13 | ]); 14 | 15 | // register a single rule 16 | $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); 17 | $rectorConfig->rules([ 18 | \Rector\PHPUnit\Rector\Class_\AddSeeTestAnnotationRector::class, 19 | \Rector\PHPUnit\Rector\ClassMethod\ReplaceTestAnnotationWithPrefixedFunctionRector::class, 20 | ]); 21 | 22 | // define sets of rules 23 | $rectorConfig->sets([ 24 | LevelSetList::UP_TO_PHP_80, 25 | \Rector\Set\ValueObject\SetList::PHP_80, 26 | \Rector\PHPUnit\Set\PHPUnitLevelSetList::UP_TO_PHPUNIT_90, 27 | \Rector\PHPUnit\Set\PHPUnitSetList::PHPUNIT_90, 28 | 29 | ]); 30 | }; 31 | -------------------------------------------------------------------------------- /src/Builder/CockroachDbBuilder.php: -------------------------------------------------------------------------------- 1 | withTablePrefix($this->setConnection(new QueryGrammar())); 26 | } 27 | 28 | /** 29 | * Get a schema builder instance for the connection. 30 | * 31 | * @return DbBuilder 32 | */ 33 | public function getSchemaBuilder(): DbBuilder 34 | { 35 | if ($this->schemaGrammar === null) { 36 | $this->useDefaultSchemaGrammar(); 37 | } 38 | 39 | return new DbBuilder($this); 40 | } 41 | 42 | /** 43 | * Get the default schema grammar instance. 44 | * 45 | * @return BaseGrammar 46 | */ 47 | protected function getDefaultSchemaGrammar(): BaseGrammar 48 | { 49 | return $this->withTablePrefix($this->setConnection(new SchemaGrammar())); 50 | } 51 | 52 | /** 53 | * Get the schema state for the connection. 54 | * @return SchemaState 55 | * @phpstan-ignore-next-line base class has fixed type that we can't correct 56 | */ 57 | public function getSchemaState(Filesystem $files = null, callable $processFactory = null) 58 | { 59 | return new SchemaState($this, $files, $processFactory); 60 | } 61 | 62 | /** 63 | * Get the default post processor instance. 64 | * 65 | * @return DbProcessor 66 | */ 67 | protected function getDefaultPostProcessor(): DbProcessor 68 | { 69 | return new DbProcessor(); 70 | } 71 | 72 | /** 73 | * Get the Doctrine DBAL driver. 74 | * 75 | * @return \Illuminate\Database\PDO\PostgresDriver 76 | * @phpstan-ignore-next-line Missing in Laravel 11 77 | */ 78 | protected function getDoctrineDriver() 79 | { 80 | /** @phpstan-ignore-next-line Now redundant in Laravel 11 */ 81 | return new PostgresDriver(); 82 | } 83 | 84 | /** 85 | * Required to set the connection. This isn't compatible with older Laravel versions 86 | */ 87 | protected function setConnection(BaseGrammar $grammar): BaseGrammar 88 | { 89 | if (method_exists($grammar, 'setConnection')) { 90 | return $grammar->setConnection($this); 91 | } 92 | 93 | return $grammar; 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /src/CockroachDbConnector.php: -------------------------------------------------------------------------------- 1 | app->extend(DatabaseManager::class, function (DatabaseManager $manager) { 15 | ConfigurationUrlParser::addDriverAlias('cockroachdb', 'crdb'); 16 | 17 | Connection::resolverFor('crdb', function ($connection, $database, $prefix, $config) { 18 | $connector = new CockroachDbConnector(); 19 | $connection = $connector->connect($config); 20 | 21 | return new CockroachDbConnection($connection, $database, $prefix, $config); 22 | }); 23 | 24 | return $manager; 25 | }); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Exceptions/FeatureNotSupportedException.php: -------------------------------------------------------------------------------- 1 | default !== null && str_starts_with($result->default, 'nextval('); 15 | 16 | return [ 17 | 'name' => $result->name, 18 | 'type_name' => $result->type_name, 19 | 'type' => $result->type, 20 | 'collation' => $result->collation, 21 | 'nullable' => (bool) $result->nullable, 22 | 'default' => ($result->generated ?? null) ? null : $result->default, 23 | 'auto_increment' => $autoincrement, 24 | 'comment' => $result->comment, 25 | 'generation' => ($result->generated ?? null) ? [ 26 | 'type' => match ($result->generated) { 27 | 's' => 'stored', 28 | default => null, 29 | }, 30 | 'expression' => $result->default, 31 | ] : null, 32 | ]; 33 | }, $results); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/Query/CockroachGrammar.php: -------------------------------------------------------------------------------- 1 | joins)) { 22 | $statement = parent::compileUpdateFrom($query, $values); 23 | } else { 24 | $statement = Grammar::compileUpdate($query, $values); 25 | } 26 | 27 | if ($query->orders) { 28 | $statement .= ' '.$this->compileOrders($query, $query->orders); 29 | } 30 | if ($query->limit) { 31 | $statement .= ' '.$this->compileLimit($query, $query->limit); 32 | } 33 | 34 | return $statement; 35 | } 36 | 37 | /** 38 | * Compile a delete statement into SQL. 39 | * 40 | * @param \Illuminate\Database\Query\Builder $query 41 | * @return string 42 | */ 43 | public function compileDelete(Builder $query) 44 | { 45 | $table = $this->wrapTable($query->from); 46 | 47 | $where = $this->compileWheres($query); 48 | 49 | if (! empty($query->joins)) { 50 | throw new FeatureNotSupportedException( 51 | 'Joins for deletions are not supported by CockroachDB, consider using a where in sub-query instead.' 52 | ); 53 | } 54 | 55 | $statement = "delete from {$table} {$where}"; 56 | if ($query->orders) { 57 | $statement .= ' '.$this->compileOrders($query, $query->orders); 58 | } 59 | if ($query->limit) { 60 | $statement .= ' '.$this->compileLimit($query, $query->limit); 61 | } 62 | 63 | return trim($statement); 64 | } 65 | 66 | /** 67 | * Compile a "where fulltext" clause. 68 | * 69 | * @param \Illuminate\Database\Query\Builder $query 70 | * @param array $where 71 | * @return string 72 | */ 73 | public function whereFullText(Builder $query, $where) 74 | { 75 | throw new FeatureNotSupportedException('Fulltext indexes are not supported by CockroachDB as of version 2.5'); 76 | } 77 | 78 | /** 79 | * Compile a truncate table statement into SQL. 80 | * 81 | * @param \Illuminate\Database\Query\Builder $query 82 | * @return array 83 | */ 84 | public function compileTruncate(Builder $query) 85 | { 86 | return ['truncate ' . $this->wrapTable($query->from) . ' cascade' => []]; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/Schema/CockroachDbGrammar.php: -------------------------------------------------------------------------------- 1 | compileDropIndex($blueprint, $command); 53 | } 54 | 55 | /** 56 | * Compile a drop unique key command. 57 | * 58 | * CockroachDB doesn't support alter table for dropping unique indexes. 59 | * https://github.com/cockroachdb/cockroach/issues/42840?version=v22.1 60 | * 61 | * @param \Illuminate\Database\Schema\Blueprint $blueprint 62 | * @param \Illuminate\Support\Fluent $command 63 | * @return string 64 | */ 65 | public function compileDropUnique(Blueprint $blueprint, Fluent $command) 66 | { 67 | $index = $this->wrap($command->get('index')); 68 | 69 | return "drop index {$this->wrapTable($blueprint)}@{$index} cascade"; 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/Schema/CockroachSchemaState.php: -------------------------------------------------------------------------------- 1 | getPdo()->query('SHOW CREATE ALL TABLES'); 21 | $query->execute(); 22 | 23 | $file = collect($query->fetchAll(\PDO::FETCH_COLUMN))->join(PHP_EOL); 24 | 25 | $migrationRows = $connection 26 | ->table($this->migrationTable) 27 | ->get() 28 | ->map(fn (\stdClass $row) => (array) $row); 29 | 30 | $statements = $connection->pretend(function (Connection $connection) use ($migrationRows) { 31 | $connection->table($this->migrationTable) 32 | ->insert($migrationRows->all()); 33 | }); 34 | 35 | if ($statements !== []) { 36 | $file .= PHP_EOL . $statements[0]['query']; 37 | } 38 | 39 | $this->files->put($path, $file); 40 | } 41 | 42 | /** 43 | * Load the given schema file into the database. 44 | * 45 | * @param string $path 46 | * @return void 47 | * @throws FileNotFoundException 48 | */ 49 | public function load($path): void 50 | { 51 | $fileContents = $this->files->get($path); 52 | if ($fileContents === '') { 53 | throw new \RuntimeException(sprintf('file %s is empty', $path)); 54 | } 55 | 56 | $pdo = $this->connection->getPdo(); 57 | $pdo->exec($fileContents); 58 | } 59 | } 60 | --------------------------------------------------------------------------------