Added Schema Builder
This commit is contained in:
@@ -1,3 +1,8 @@
|
||||
## 2.3.0 [2025-04-21]
|
||||
|
||||
### Features
|
||||
1. Added Schema Builder
|
||||
|
||||
## 2.2.0 [2025-02-27]
|
||||
|
||||
### Features
|
||||
|
||||
@@ -138,6 +138,43 @@ class CreateMyTable extends \PhpClickHouseLaravel\Migration
|
||||
}
|
||||
```
|
||||
|
||||
Or you can also use the Schema Builder
|
||||
|
||||
```php
|
||||
<?php
|
||||
|
||||
class CreateMyTable extends \PhpClickHouseLaravel\Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
static::createMergeTree('my_table', fn(MergeTree $table) => $table
|
||||
->columns([
|
||||
$table->uInt32('id'),
|
||||
$table->datetime('created_at', 3)->default(new Expression('now64()')),
|
||||
$table->string('field_one'),
|
||||
$table->int32('field_two'),
|
||||
])
|
||||
->orderBy('id')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
static::write('DROP TABLE my_table');
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**3.** And then you can insert data
|
||||
|
||||
One row
|
||||
|
||||
+2
-1
@@ -21,7 +21,8 @@
|
||||
"smi2/phpclickhouse": "^1.4.2",
|
||||
"the-tinderbox/clickhouse-builder": "^6.0",
|
||||
"illuminate/support": ">=7",
|
||||
"illuminate/database": ">=7"
|
||||
"illuminate/database": ">=7",
|
||||
"glushkovds/php-clickhouse-schema-builder": "^1.0"
|
||||
},
|
||||
"require-dev": {
|
||||
"laravel/framework": ">=9",
|
||||
|
||||
@@ -7,6 +7,7 @@ namespace PhpClickHouseLaravel;
|
||||
use ClickHouseDB\Statement;
|
||||
use ClickHouseDB\Transport\CurlerRequest;
|
||||
use Illuminate\Database\Migrations\Migration as BaseMigration;
|
||||
use PhpClickHouseSchemaBuilder\Tables\MergeTree;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
|
||||
class Migration extends BaseMigration
|
||||
@@ -35,4 +36,15 @@ class Migration extends BaseMigration
|
||||
}
|
||||
return $instance->resolveConnection()->getCluster()->write($sql, $bindings);
|
||||
}
|
||||
|
||||
protected static function createMergeTree(string $tableName, callable $callback): Statement
|
||||
{
|
||||
$instance = new static();
|
||||
$config = config("database.connections.$instance->connection");
|
||||
$table = (new MergeTree($tableName))
|
||||
->dbName($config['database'])
|
||||
->onCluster($config['cluster_name'] ?? null);
|
||||
$callback($table);
|
||||
return static::write($table->compile());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,8 +16,12 @@ cp /src/tests/migrations/exampleTable.php /app/database/migrations/2022_01_01_00
|
||||
cp /src/tests/migrations/example2Table.php /app/database/migrations/2022_01_01_000001_example.php
|
||||
cp /src/tests/migrations/example3Table.php /app/database/migrations/2022_01_01_000002_example.php
|
||||
cp /src/tests/migrations/example4Table.php /app/database/migrations/2022_01_01_000003_example.php
|
||||
cp /src/tests/migrations/example5Table.php /app/database/migrations/2022_01_01_000004_example.php
|
||||
cat /src/tests/config/.env >> /app/.env
|
||||
|
||||
# Installing required libs, todo: refactor this
|
||||
composer require glushkovds/php-clickhouse-schema-builder
|
||||
|
||||
# Creating test tables
|
||||
php artisan migrate
|
||||
|
||||
|
||||
@@ -77,6 +77,7 @@ return [
|
||||
'port' => '8123',
|
||||
],
|
||||
],
|
||||
'cluster_name' => 'company_cluster',
|
||||
'database' => 'default',
|
||||
'username' => 'default',
|
||||
'password' => '',
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
use PhpClickHouseSchemaBuilder\Expression;
|
||||
use PhpClickHouseSchemaBuilder\Tables\MergeTree;
|
||||
|
||||
return new class extends \PhpClickHouseLaravel\Migration {
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
static::createMergeTree('examples5', fn(MergeTree $table) => $table
|
||||
->columns([
|
||||
$table->datetime('created_at', 3)->default(new Expression('now64()')),
|
||||
$table->int64('f_int'),
|
||||
$table->string('f_string'),
|
||||
$table->bool('f_bool'),
|
||||
])
|
||||
->orderBy('f_int')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
static::write('DROP TABLE examples5');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user