Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
fulldecent committed Oct 29, 2019
1 parent 122b79d commit 0c8e2bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
1 change: 1 addition & 0 deletions bin/google-sheets-etl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ $database = new \PDO($databaseDsn, $databaseUsername, $databasePassword, [

$tasks = new Tasks($googleServiceAccountCredentialFile, $database);
$tasks->setConfiguration($etlSchemaFile);
$tasks->databaseAgent->setUpAccounting();

echo 'Service account: ' . $tasks->googleSheetsAgent->getAccountName() . PHP_EOL;

Expand Down
1 change: 0 additions & 1 deletion src/DatabaseAgent.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ protected function __construct(\PDO $newDatabase)
{
$this->database = $newDatabase;
$this->loadTime = date('Y-m-d H:i:s');
$this->setUpAccounting();
}

// Getters /////////////////////////////////////////////////////////////////
Expand Down
15 changes: 10 additions & 5 deletions src/DatabaseAgentMysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function setUpAccounting()
`last_modified` VARCHAR(99) NOT NULL,
`last_authorization_checked` VARCHAR(20) NOT NULL,
`last_loaded` VARCHAR(99),
UNIQUE KEY `row_id` (`_rowid_`),
UNIQUE KEY `spreadsheet_id` (`spreadsheet_id`)
) ENGINE=InnoDB;
SQL;
Expand All @@ -50,6 +51,7 @@ public function setUpAccounting()
`sheet_name` VARCHAR(99) NOT NULL,
`last_loaded` VARCHAR(99),
`table_name` VARCHAR(99),
UNIQUE KEY `row_id` (`_rowid_`),
UNIQUE KEY `sheet_name` (`spreadsheet_rowid`, `sheet_name`)
) ENGINE InnoDB;
SQL;
Expand Down Expand Up @@ -141,6 +143,7 @@ public function getTableNameForSheet(string $spreadsheetId, string $sheetName):
public function getGreatestModifiedAndIdLoaded(): ?array
{
$quotedSpreadsheetsTable = $this->quotedFullyQualifiedTableName(self::SPREADSHEETS_TABLE);
var_dump($quotedSpreadsheetsTable);
$sql = <<<A
SELECT last_modified, spreadsheet_id
FROM $quotedSpreadsheetsTable
Expand Down Expand Up @@ -220,8 +223,8 @@ public function loadAndAccountSheet(string $spreadsheetId, string $sheetName, st
$dropTableSql = "DROP TABLE IF EXISTS $quotedTableName";
$this->database->exec($dropTableSql);
$quotedColumnArray = $this->normalizedQuotedColumnNames($columns);
$quotedColumns = implode(',', $quotedColumnArray);
$createTableSql = "CREATE TABLE $quotedTableName ($quotedColumns)";
$quotedColumns = implode(' VARCHAR(100),', $quotedColumnArray);
$createTableSql = "CREATE TABLE $quotedTableName ($quotedColumns VARCHAR(100))";
$this->database->exec($createTableSql);

// Insert rows
Expand Down Expand Up @@ -292,7 +295,7 @@ public function removeOutdatedSheets(string $spreadsheetId)
FROM $quotedSheetsTable
WHERE _rowid_ IN (
SELECT sheets._rowid_
FROM $quotedSheetsTable sheets
FROM (SELECT * FROM $quotedSheetsTable) sheets
JOIN $quotedSpreadsheetsTable spreadsheets
ON spreadsheets._rowid_ = sheets.spreadsheet_rowid
WHERE spreadsheet_id = ?
Expand Down Expand Up @@ -343,7 +346,7 @@ public function removeSpreadsheet(string $spreadsheetId)
FROM $quotedSheetsTable
WHERE spreadsheet_rowid IN (
SELECT _rowid_
FROM $quotedSpreadsheetsTable
FROM (SELECT * FROM $quotedSpreadsheetsTable)
WHERE spreadsheet_id = ?
)
AAAA;
Expand Down Expand Up @@ -379,7 +382,9 @@ public function removeSpreadsheet(string $spreadsheetId)
private function quotedFullyQualifiedTableName(string $unqualifiedName): string
{
$qualifiedTableName = ($this->tablePrefix ?? '') . $unqualifiedName;
return ($this->schema ?? '') . "`$qualifiedTableName`";
return isset($this->schema)
? $this->schema . ".`$qualifiedTableName`"
: "`$qualifiedTableName`";
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public function setConfiguration(string $configurationFileName)
$this->configurationForSpreadsheetSheet[$spreadsheetId][$sheetName] = (object)[
'tableName' => $configuration->tableName, # Required property
'columnMapping' => (array)($configuration->columnMapping), # Optional
'headerRow' => $configuration->headerRow ?? null, # Optional
'skipRows' => $configuration->skipRows ?? null # Optional
'headerRow' => $configuration->headerRow ?? 0, # Optional
'skipRows' => $configuration->skipRows ?? 1 # Optional
];
}
}
Expand Down

0 comments on commit 0c8e2bf

Please sign in to comment.