Export postgres database tables to csv

Below function/procedure will create a csv file for each table in the database.

CREATE OR REPLACE FUNCTION db_to_csv(path TEXT) RETURNS void AS $$
declare
   tables RECORD;
   statement TEXT;
begin
FOR tables IN 
   SELECT (table_schema || '.' || table_name) AS schema_table
   FROM information_schema.tables t INNER JOIN information_schema.schemata s 
   ON s.schema_name = t.table_schema 
   WHERE t.table_schema NOT IN ('pg_catalog', 'information_schema')
   AND t.table_type NOT IN ('VIEW')
   ORDER BY schema_table
LOOP
   statement := 'COPY ' || tables.schema_table || ' TO ''' || path || '/' || tables.schema_table || '.csv' ||''' DELIMITER '','' CSV HEADER';
   EXECUTE statement;
END LOOP;
return;  
end;
$$ LANGUAGE plpgsql;

Simply run the select command which will create file in provided path. Remember to provide chmod 777 /your/path so that the folder is writable.

SELECT db_to_csv('/your/path');

If you’re creating this inside a docker container use following command to copy it from container to host:

docker cp <container_id>:/your/path /your/host/path

Reference:

https://stackoverflow.com/questions/17463299/export-postgres-database-into-csv-file

PHPUnit Upload file testing

Testing file upload using phpunit can be challenging. Below are two ways you can test file uploads in phpunit integration tests.

When you’re testing an API with file upload, API Payload and File Payload must be sent separately.

Using fake file:

public function testUpload()
{
    $fileData = [
        'attachment' => UploadedFile::fake()->create('fakefile.pdf', 100)
    ];    

    $data = [
        'first_name' => 'John',
        'last_name' => 'Doe',
    ];    

    $url = 'http://localhost/upload';    

    $this->call(
      'POST', 
      $url, $data, [], $fileData, 
      $this->transformHeadersToServerVars([
        'Authorization' => 'Bearer xxxx'
      ])
    );    

    $this->assertResponseOk();
}

Using Original/Real file:
Here UploadedFile function has 6th parameter called testing which needs to be set true.

public function testUpload()
{
    $filePath = storage_path('realfile.pdf'); 

    $fileData = [
        'attachment' => new UploadedFile($file, null, null, null, null, true); 
    ]; 

    $data = [
        'first_name' => 'John',
        'last_name' => 'Doe',
    ];    

    $url = 'http://localhost/upload';    

    $this->call(
      'POST', 
      $url, $data , [], $fileData, 
      $this->transformHeadersToServerVars([
        'Authorization' => 'Bearer xxxx'
      ])
    );    

    $this->assertResponseOk(); 
}

High-Performing Teams

Further Research to be done:
– Accountability vs Responsibility among Agile Team & the manager?

Resources:
https://www.personneltoday.com/hr/five-principles-of-high-performing-teams/
https://www.adventureswithagile.com/2017/09/25/model-high-performing-teams/

PHP Performance Monitoring using Xdebug Profiler

Installation:
If you are using docker for your development add below in your php.ini file:

zend_extension="/usr/lib/php/20160303/xdebug.so"
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9001
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.idekey=PHPSTORM
xdebug.remote_host=<YOUR_NETWORK_IP>
xdebug.profiler_enable_trigger=1
xdebug.profiler_enable=0
xdebug.profiler_output_dir="/var/www/storage/logs"

Now when you execute your API add below as a query parameter:

Example: http://localhost:8080/test?XDEBUG_PROFILE=1

If you enable profiler as default your php scripts will run very slow. So only use it when you need it.

Useful Links:

https://stackoverflow.com/questions/8077993/can-i-manually-say-on-xdebug-profiler-to-start-profiling-in-specific-place

Git Tips – Cherry Picking using SourceTree

When there is a situation where you want to get only the changes or lines of code from some branch but not merge the whole branch, you can make use of cherry pick.

Lets say there was a bug in production and somebody in your team fixes that bug in there branch and commits it. You can cherry pick those lines of code (to be specific its all code from the last commit).

In below example: we are checkout in production branch and we are cherry picking the commit called “document and allocation export issue fixed” from KEY-1978 to production.