Initial
Takes in a file, process it and output a new file with additional columns.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
if (PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
|
||||
fwrite(STDERR, "This script must be run from the command line.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
$token = (string) ($argv[1] ?? '');
|
||||
if ($token === '') {
|
||||
fwrite(STDERR, "Missing import token.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
@set_time_limit(0);
|
||||
@ini_set('memory_limit', '-1');
|
||||
|
||||
$database = \App\Database::fromConfig($GLOBALS['appConfig'] ?? []);
|
||||
$repository = \App\Import\ImportRepository::fromDatabase($database);
|
||||
$jobStore = new \App\Import\ImportJobStore();
|
||||
$service = new \App\Import\ImportService(
|
||||
new \App\Import\SpreadsheetReader(),
|
||||
new \App\Import\SpreadsheetRowProcessor(),
|
||||
$repository,
|
||||
);
|
||||
|
||||
$state = $jobStore->get($token);
|
||||
if (!is_array($state)) {
|
||||
fwrite(STDERR, "Import job not found.\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
while (!in_array(($state['status'] ?? 'pending'), ['completed', 'error'], true)) {
|
||||
$state = $service->processJobState($state);
|
||||
$jobStore->save($token, $state);
|
||||
}
|
||||
|
||||
if (($state['status'] ?? '') === 'error') {
|
||||
fwrite(STDERR, (string) ($state['message'] ?? 'Import failed.') . "\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
fwrite(STDOUT, "Import completed.\n");
|
||||
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
require __DIR__ . '/../bootstrap.php';
|
||||
|
||||
try {
|
||||
$database = \App\Database::fromConfig($appConfig ?? []);
|
||||
$pdo = $database->pdo();
|
||||
|
||||
$statements = [
|
||||
<<<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS import_batches (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
source_filename TEXT NOT NULL,
|
||||
original_headers JSONB NOT NULL,
|
||||
calculated_headers JSONB NOT NULL,
|
||||
row_count INTEGER NOT NULL DEFAULT 0,
|
||||
warnings_count INTEGER NOT NULL DEFAULT 0,
|
||||
status TEXT NOT NULL DEFAULT 'complete',
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL,
|
||||
"ALTER TABLE import_batches ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'complete'",
|
||||
<<<'SQL'
|
||||
CREATE TABLE IF NOT EXISTS import_rows (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
batch_id BIGINT NOT NULL REFERENCES import_batches(id) ON DELETE CASCADE,
|
||||
row_number INTEGER NOT NULL,
|
||||
source_row JSONB NOT NULL,
|
||||
normalized_row JSONB NOT NULL,
|
||||
calculated_row JSONB NOT NULL,
|
||||
merged_row JSONB NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
|
||||
)
|
||||
SQL,
|
||||
'CREATE INDEX IF NOT EXISTS idx_import_rows_batch_row_number ON import_rows (batch_id, row_number)',
|
||||
];
|
||||
|
||||
foreach ($statements as $statement) {
|
||||
$pdo->exec($statement);
|
||||
}
|
||||
|
||||
fwrite(STDOUT, "Migration completed.\n");
|
||||
} catch (Throwable $throwable) {
|
||||
fwrite(STDERR, "Migration failed: " . $throwable->getMessage() . "\n");
|
||||
exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user