Skip to content

Commit

Permalink
Added status messages to migration scripts to help with debugging in …
Browse files Browse the repository at this point in the history
…the future
  • Loading branch information
bagofarms committed Feb 15, 2018
1 parent bbf0d38 commit e345c89
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 2 additions & 0 deletions migrations/000_db_create_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

if ('pgsql' === $db_type) {
// POSTGRESQL
echo("Setting up tables in PostgreSQL\r\n");
$tables = [
'
CREATE TABLE IF NOT EXISTS reports (
Expand All @@ -55,6 +56,7 @@

if ('mysql' === $db_type) {
// MYSQL
echo("Setting up tables in MySQL\r\n");
$tables = [
'
CREATE TABLE IF NOT EXISTS `reports` (
Expand Down
13 changes: 8 additions & 5 deletions migrations/001_move_reports_to_db.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
// if 'file_path' col is missing, there's nothing to do
if (!$check_for_column($db_reports_table, 'file_path')) {
if (!getenv('UNITTEST')) {
echo("It looks like this script doesnt need to be run");
echo("No need to migrate old reports, continuing...\r\n");
}

return;
Expand All @@ -53,19 +53,22 @@
// add the column if it's missing
if (!$check_for_column($db_reports_table, $col_to_add)) {
// Quick hack to add report column since we dont have migrations yet
echo("Adding report_json column.\r\n");
$column_type = $db_type == 'mysql' ? 'MEDIUMTEXT' : 'TEXT';
UdoitDB::query("ALTER TABLE {$db_reports_table} ADD {$col_to_add} {$column_type}");
}

// exit with warning if the column is still missing
if (!check_for_column($dbh, $db_reports_table, $col_to_add)) {
if (!getenv('UNITTEST')) {
echo("The migration script failed to create a ${col_to_add} column");
echo("The migration script failed to create a ${col_to_add} column\r\n");
}

return;
}

echo("Migrating old reports from disk to database.\r\n");

// Grab all rows from the reports table
if ($rows = UdoidDB::query('SELECT * FROM reports')) {
$rows = $rows->fetchAll();
Expand All @@ -82,7 +85,7 @@

$file = __DIR__.'/'.$row['file_path'];
if (!file_exists($file)) {
echo("Json file not found {$file} for report id: {$row['id']}\n");
echo("Json file not found {$file} for report id: {$row['id']}\r\n");
continue;
}

Expand All @@ -98,12 +101,12 @@

if (!$res) {
print_r($sth->errorInfo());
echo("Failed inserting report for {$row['id']}\n");
echo("Failed inserting report for {$row['id']}\r\n");
continue;
}

$count_moved++;
}

UdoitDB::query("ALTER TABLE {$db_reports_table} DROP COLUMN file_path");
echo("Moved {$count_moved} reports from disk to the database. Feel free to delete the reports directory\n");
echo("Moved {$count_moved} reports from disk to the database. Feel free to delete the reports directory\r\n");
2 changes: 2 additions & 0 deletions migrations/002_add_background_queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

if ('pgsql' === $db_type) {
// POSTGRESQL
echo("Creating job_queue in PostgreSQL\r\n");
$queries = [
'
CREATE TABLE IF NOT EXISTS job_queue (
Expand All @@ -44,6 +45,7 @@

if ('mysql' === $db_type) {
// MYSQL
echo("Creating job_queue in MySQL\r\n");
$queries = [
'
CREATE TABLE IF NOT EXISTS `job_queue` (
Expand Down
2 changes: 2 additions & 0 deletions migrations/003_add_refresh_token_to_users.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

if ('pgsql' === $db_type) {
// POSTGRESQL
echo("Adding refresh_token and canvas_url fields in PostgreSQL\r\n");
$queries = [
[
'isRequired' => !$check_for_column('users', 'refresh_token'),
Expand All @@ -55,6 +56,7 @@

if ('mysql' === $db_type) {
// MYSQL
echo("Adding refresh_token and canvas_url fields in MySQL\r\n");
$queries = [
[
'isRequired' => !$check_for_column('users', 'refresh_token'),
Expand Down

0 comments on commit e345c89

Please sign in to comment.