|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use function Flow\ETL\Adapter\Doctrine\{ |
| 6 | + sqlite_insert_options, |
| 7 | + to_dbal_table_delete, |
| 8 | + to_dbal_table_insert}; |
| 9 | +use function Flow\ETL\DSL\{data_frame, from_array}; |
| 10 | +use Doctrine\DBAL\DriverManager; |
| 11 | +use Doctrine\DBAL\Schema\{Column, Table, UniqueConstraint}; |
| 12 | +use Doctrine\DBAL\Types\{Type, Types}; |
| 13 | + |
| 14 | +require __DIR__ . '/../../../../vendor/autoload.php'; |
| 15 | + |
| 16 | +require __DIR__ . '/generate_static_orders.php'; |
| 17 | + |
| 18 | +$connection = DriverManager::getConnection([ |
| 19 | + 'path' => __DIR__ . '/output/orders.db', |
| 20 | + 'driver' => 'pdo_sqlite', |
| 21 | +]); |
| 22 | + |
| 23 | +$schemaManager = $connection->createSchemaManager(); |
| 24 | + |
| 25 | +if (!$schemaManager->tablesExist(['orders'])) { |
| 26 | + $schemaManager->createTable(new Table( |
| 27 | + $table = 'orders', |
| 28 | + [ |
| 29 | + new Column('order_id', Type::getType(Types::GUID), ['notnull' => true]), |
| 30 | + new Column('created_at', Type::getType(Types::DATETIME_IMMUTABLE), ['notnull' => true]), |
| 31 | + new Column('updated_at', Type::getType(Types::DATETIME_IMMUTABLE), ['notnull' => false]), |
| 32 | + new Column('discount', Type::getType(Types::FLOAT), ['notnull' => false]), |
| 33 | + new Column('email', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]), |
| 34 | + new Column('customer', Type::getType(Types::STRING), ['notnull' => true, 'length' => 255]), |
| 35 | + new Column('address', Type::getType(Types::JSON), ['notnull' => true]), |
| 36 | + new Column('notes', Type::getType(Types::JSON), ['notnull' => true]), |
| 37 | + new Column('items', Type::getType(Types::JSON), ['notnull' => true]), |
| 38 | + ], |
| 39 | + uniqueConstraints: [ |
| 40 | + new UniqueConstraint('orders_order_id', ['order_id']), |
| 41 | + ] |
| 42 | + )); |
| 43 | +} |
| 44 | + |
| 45 | +$orderIds = [ |
| 46 | + 'c0a43894-0102-4a4e-9fcd-393ef9e4f16a', |
| 47 | + '83fd51a4-9bd1-4b40-8f6e-6a7cc940bb5a', |
| 48 | + '7c65db1a-410f-4e91-8aeb-66fb3f1665f7', |
| 49 | + '5af1d56c-a9f7-411e-8738-865942d6c40f', |
| 50 | + '3a3ae1a9-debd-425a-8f9d-63c3315bc483', |
| 51 | + '27d8ee4d-94cc-47fa-bc14-209a4ab2eb45', |
| 52 | + 'cc4fd722-1407-4781-9ad4-fa53966060af', |
| 53 | + '718360e1-c4c9-40f4-84e2-6f7898788883', |
| 54 | + 'ea7c731c-ce3b-40bb-bbf8-79f1c717b6ca', |
| 55 | + '17b0d6c5-dd8f-4d5a-ae06-1df15b67c82c', |
| 56 | +]; |
| 57 | + |
| 58 | +data_frame() |
| 59 | + ->read(from_array(generateStaticOrders($orderIds))) |
| 60 | + ->write( |
| 61 | + to_dbal_table_insert( |
| 62 | + DriverManager::getConnection(['path' => __DIR__ . '/output/orders.db', 'driver' => 'pdo_sqlite']), |
| 63 | + 'orders', |
| 64 | + sqlite_insert_options(conflict_columns: ['order_id']) |
| 65 | + ) |
| 66 | + ) |
| 67 | + ->run(); |
| 68 | + |
| 69 | +data_frame() |
| 70 | + ->read(from_array(\array_map(static fn (string $id) => ['order_id' => $id], $orderIds))) |
| 71 | + ->write( |
| 72 | + to_dbal_table_delete( |
| 73 | + DriverManager::getConnection(['path' => __DIR__ . '/output/orders.db', 'driver' => 'pdo_sqlite']), |
| 74 | + 'orders', |
| 75 | + ) |
| 76 | + ) |
| 77 | + ->run(); |
| 78 | + |
| 79 | +$orders = $connection->fetchAllAssociative($connection->createQueryBuilder()->select('*')->from('orders')->orderBy('order_id')->getSQL()); |
| 80 | + |
| 81 | +assert(\count($orders) === 0, 'There should be no orders left in the database after deletion.'); |
0 commit comments