<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

use Symfony\Component\Console\Output\ConsoleOutput;

class SignCitiIds extends Migration
{

    public function up()
    {

        $output = new ConsoleOutput();

        $tables = DB::select('SHOW TABLES');

        foreach( $tables as $table )
        {

            $table_array = get_object_vars( $table );
            $table_name = $table_array[ key( $table_array ) ];

            Schema::table($table_name, function (Blueprint $table) use ($table_name, $output) {

                if( Schema::hasColumn($table_name, 'citi_id') )
                {
                    $table->dropPrimary('citi_id');
                }

                foreach( Schema::getColumnListing($table_name) as $column )
                {
                    if( ends_with($column, 'citi_id') )
                    {
                        $table->integer($column)->signed()->change();

                        $output->writeln( 'Signed in ' . $table_name . ': ' . $column);
                    }
                }

                if( Schema::hasColumn($table_name, 'citi_id') )
                {
                    $table->primary('citi_id');
                }

            });

        }

    }

    public function down()
    {

        // Once we insert negative ids to the database, I think we can't change it back...
        // TODO: Remove items with negative ids, and change the columns back to unsigned?

    }

}

Add migration to make all CITI ID fields signed
Add migration to make all CITI ID fields signed

Apparently, some CITI IDs are negative. It seems that CITI started off
using signed IDs, but at some point a decision was made to expand the
range by starting at the lower bound of the number range, rather than
at zero as originally. Surprising that the schema wasn't modified to
be unsigned instead, but hey, so it is.

This modifies any field ending with `citi_id` to be signed. This should
also catch any pseudo-foreign-key field like `agent_citi_id`.

Also +1 to the decision to call the column `citi_id` instead of `id`.
That choice made this process so much easier. Good call.
