Friday, 9 February 2024

Oracle - Handling Exceptions to Oracle Queries

In Oracle DB, if we already have data in the table and would like to use exceptions to handle errors, please follow below approach:
declare
 e_col_exists exception;
 pragma exception_init(e_col_exists,-1430);
 e_invalid_identifier exception;
 pragma exception_init(e_invalid_identifier,-904);
begin
  begin
    execute immediate 'alter table tbl_questions add value_new varchar2(4000 CHAR) null';
    exception
      when e_col_exists then
        null;
  end;
  begin
    execute immediate 'update tbl_questions set value_new = value';
  end;
  begin
    execute immediate 'alter table tbl_questions drop column value';
    exception
      when e_invalid_identifier then
        null;
  end;
  begin
    execute immediate 'alter table tbl_questions rename column value_new to value';
    exception
      when e_invalid_identifier then
        null;
  end;
end;  
/
------
Explanation:

1. e_col_exists exception:

  • This exception is likely used to handle situations where a column referenced in your code doesn't actually exist in the table you're trying to access.

  • The pragma exception_init line associates the exception with the error code -1430. This error code typically corresponds to the ORA-01430 error message, which indicates "column does not exist".

2. e_invalid_identifier exception:

  • This exception is likely used to handle situations where an identifier (e.g., a variable name, column name, etc.) used in your code is invalid or doesn't follow the naming conventions.

  • The pragma exception_init line associates the exception with the error code -904. This error code typically corresponds to the ORA-00904 error message, which indicates "invalid identifier".

By defining these custom exceptions, you can make your code more robust and easier to maintain. When either of these exceptions is raised, your code can handle the error gracefully instead of crashing or producing unexpected results.

Here are some additional points to note:

  • You can define custom exceptions to handle any specific error conditions you want to anticipate in your code.
  • It's generally considered good practice to define custom exceptions for situations that are specific to your application logic and not already covered by standard Oracle error codes.
  • You can use exception handlers to trap these custom exceptions and take appropriate actions when they occur.

I hope this explanation helps! Feel free to ask if you have any other questions.

Friday, 15 September 2023

Python method to convert Array to Unique Array

Python Function to filter array and create unique array list

def array_unique(array_list):
    list = np.array(array_list)
    return np.unique(list)

Wednesday, 2 November 2022

Python - To read file content & write into a file

from pathlib import Path


# Read file content
def read_file():
    test_file = "example/you_test_file.txt"

    contents = Path(test_file).read_text()

    print (contents) # print file content

    #debug(contents) # use can also use my custom debug method to see log in file



# To write message to file for debugging
def debug(message="", type="w+"):
	filePath = "test/debug.txt" # don't forgot to create a empty file

    with open(filePath, type) as f:
        f.write(str(message) + "\n\n")
    f.close()
    

Thursday, 19 May 2022

Simple bash stements to set values covering following two scenarios: 1. Set username/password variable if value passed on running the file 2. Or (step 1 doesn't pass values), set them on prompt arguments Create a login.sh file using the below content:
#!/bin/sh

# set -o errexit -o nounset -o xtrace

echo "Initialising Username/Password: "

if test "${1+x}"; then # Set values from inline statement
  username=$1
  password=$2  
else # Otherwise, set values from arguments
  read -p 'Username: ' username
  read -sp 'Password: ' password
fi

echo 'Username: ' $username '| Password: ' $password
1. First scenario, passing the values whilst running the file
$ sh login.sh nepal.user Pokhara123
Username:  nepal.user | Password:  Pokhara123
2. Second scenario, passing the values on prompt arguments
$ sh login.sh 

$ sh login.sh
Initialising Username/Password:
Username: nepal.user
Password: Pokhara123
Username:  nepal.user | Password:  Pokhara123

Wednesday, 27 April 2022

Build Dynamic Marshal_With in Flask (Python) based on conditions

To create a dynamic marshalling based on condition or parameter type, please follow the instruction below:

# Constants
MARSHELL_WITH_SCHEMAS = {
    "condition_1": Condition1Schema,
    "condition_2": Condition2Schema,
}


# To build dynamic marshalling based on report type
def selective_marshal_with():
    def decorator(func):
        @wraps(func)
        def wrapper(*args, **kwargs):
            condition_type = kwargs.get("condition_type", None)

            # Get marshalled with dynamic schema by condition type
            marshal_with_func = marshal_with(MARSHELL_WITH_SCHEMAS.get(condition_type, None))(func)

            return marshal_with_func(*args, **kwargs)

        return wrapper

    return decorator

# Actual class to connect view to URL
class MyViewClass(MethodResource, Resource):
    @doc(description="My View API", tags=["My View List"])
    @use_kwargs(MyQueryParams, location="query")
    @selective_marshal_with()
    @standard_api("My View List")
    def get(self, **kwargs):
        # you get code here....
    
The MyQueryParams can/should be in seperate schema file for e.g. rest/db/schema/my_view_schema.py

class MyQueryParams(Schema):
    """Query parameters for View List API."""

    condition_type = fields.Str(attribute="condition_type", required=True)

Hope it helps!

Friday, 5 March 2021

Count number of PDF file pages in PHP

Below function returns the number of pages count in PHP
function getPageCount(StreamInterface $stream): int
{
        $result = 0;

        $stream->rewind();

        while (! $stream->eof()) {
            $chunk = $stream->read(4096);

            //  looking for root node PDF 1.7+
            $found = \preg_match('/\/Type\s*?\/Pages(?:(?!\/Parent).)*\/Count\s?(?\d+)/', $chunk, $matches);

            if (0 < $found) {
                return (int) $matches['value'];
            }

            //  looking for root node PDF < 1.7
            $found = \preg_match('/\/Count\s?(?\d+)\s?\/Type\s*?\/Pages/', $chunk, $matches);

            if (0 < $found) {
                return (int) $matches['value'];
            }
            
            //  looking for root node PDF 1.7
            // Both regex1 & regex2 should work, but $regex2 is preferred.
            $regex1 = '/(?<=\/Type\s\/Pages\s(.*)\/Count\s)\d*/gs';
            $regex2 = '/\/Type\s*?\/Pages\s.*\/Count\s?(?\d+)/s';
            $found = \preg_match($regex2, $chunk, $matches);
            
            if (0 < $found) {
                return (int) $matches['value'];
            }

            //  looking for /Type/Page
            $found = \preg_match_all('/\/Type\s*?\/Page\s+/', $chunk, $matches);

            if (0 < $found) {
                $result += $found;
            }
        }

        $stream->rewind();

        return $result;
}

Thursday, 3 September 2020

PHP Regex function to extract text between tags for a synopsis or intro text

The following method prepare the synopsis from the body and the second part is for unit tests:

Hope it helps!

Saturday, 11 April 2020

Fix for ng serve --watch is not watching for code changes in Ubuntu

I was running the Angular 8 application on Ubuntu LTS 18.04 on my machine and was having an issue to refresh on code update using  --watch.

Trying to use ng serve and ng build --watch was not watching for code changes and I thought it was something to do with node or angular-cli.
However, I found the solution on Github (https://github.com/angular/angular-cli/issues/8313)
I ran the following on Terminal and apparently, it worked and watching code changes:
echo fs.inotify.max_user_watches=524288 | sudo tee /etc/sysctl.d/40-max-user-watches.conf && sudo sysctl --system

Hope this helps!

Tuesday, 29 October 2019

Angular 7 & Jest & Babel Integration over Jasmine/Karma - AngularSnapshotSerializer.js in the snapshotSerializers option was not found

Steps to integrate of Jest & Babel with Angular 7+:

1. Run the following commands to install:
    # Remove Jesmin/Karma
    npm remove karma karma-chrome-launcher karma-coverage-istanbul-reporter karma-jasmine karma-jasmine-html-reporter
    
    npm uninstall jasmine @types/jasmine
    
    rm ./karma.conf.js ./src/test.ts

    # Install jest    
    npm install --save jest@24.9 @angular-builders/jest@7 @types/jest@24 jest-preset-angular@8 

    # Install babel
    npm install --save-dev babel-jest babel-polyfill
    npm install --save @babel/core @babel/preset-env @babel/preset-flow @babel/preset-typescript    
 

2. On `package.json`, added the following code:
    "scripts": {
      ...
      ...
      "test": "ng test",
      "test:watch": "jest --watch",
      ...
      ...
    }
    ...
    ...   
    "jest": {
      "preset": "jest-preset-angular",
      "setupFilesAfterEnv": [
        "/setupJest.ts"
      ]
    }
 

3. Also, updated the following on the `angular.json`:
    ...
    ... 
    "test": {
       "builder": "@angular-devkit/build-angular:karma",
    ...
    ...
 

Replace with:
    ...
    ... 
    "test": {
       "builder": "@angular-builders/jest:run",
    ...
    ...
 

4. Create the `/setupJest.ts` with below content:
    import 'jest-preset-angular';   
 

5. Create the `/babel.config.js` with below content:
    module.exports = function(api) {

        const presets = [
            '@babel/preset-typescript',
             [
                "@babel/preset-env", {
                    "targets": {
                        "node": "current"
                    }
                }
            ],
            '@babel/preset-flow'
        ];

        return {
            presets,
        };
    };
 


6. And, finally tried running the `ng-test` from a terminal, however, I was stuck with the following error (see picture below):
7. Eventually, managed to fix the issue by adding the file `/src/jest.config.js` with below content:
    module.exports = {
        "transform": {
            "^.+\\.(ts|js|html)$": "ts-jest",
            "^.+\\.[t|j]sx?$": "babel-jest"
        },
        moduleFileExtensions: ['ts', 'html', 'js', 'json'],
        moduleNameMapper: {
            '^src/(.*)$': '/src/$1',
            '^app/(.*)$': '/src/app/$1',
            '^assets/(.*)$': '/src/assets/$1',
            '^environments/(.*)$': '/src/environments/$1',
        },
        transformIgnorePatterns: ['node_modules/(?!@ngrx)'],
        snapshotSerializers: [
            'jest-preset-angular/build/AngularSnapshotSerializer.js',
            'jest-preset-angular/build/HTMLCommentSerializer.js',
        ],
    };
Thereafter, ran `ng test` again and can see the test running thru. I hope it helps!

Friday, 11 October 2019

Construct the dynamic table with specified number of columns on each row as per the size of the data in Angular

The below code is to generate a table with multiple rows with the specified number of columns on each row as per the size of the data array.

Then, on the output, we would be able to generate a table with multiple rows with a specified number of columns.

Monday, 7 October 2019

Check if MySQL Query uses cache or not for most frequent query.


Run the following queries to check if most recent MySQL query uses cache or not.

-- Query 1
SHOW VARIABLES LIKE 'have_query_cache'; 

-- The Output Value should be Yes, if it uses cache.

-- Query 2
SHOW VARIABLES LIKE 'query_cache_size';
-- -- The Output Value should be integer value, if it uses cache.



Refer Link 1 and Link 2 for more detail.

Hope the help!

Wednesday, 11 September 2019

Get MySQL columns names as multiple row or as a concatenated string.

Query to list columns from particular table of the particular database in MySQL:
1. Get the list of columns as multiple rows:
    
SELECT `COLUMN_NAME`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='database_name'
AND `TABLE_NAME`='table_or_view_name';


2. Get the list of columns as a single concatenated string:
    
SELECT GROUP_CONCAT(`COLUMN_NAME` SEPARATOR ',')
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA`='database_name'
AND `TABLE_NAME`='table_or_view_name';


Tuesday, 26 March 2019

Resolve an issue to insert explicit value for identity column in SQL table asking for IDENTITY_INSERT to set ON/OFF in PHP (Laravel)

The following methods to control the `IDENTITY_INSERT` to ON/OFF for SQL table seeding process in PHP (Laravel) :
    
    function setIdentityInsert($table, $onStatus = true)
    {
        $status = $onStatus ? 'ON' : 'OFF';

        $this->sqlConnection->unprepared("SET IDENTITY_INSERT $table $status");
    }

    function insertTableData($table, $data)
    {
        return $this->sqlConnection->table($table)->insert($data);
    }

    function seedTable($table, $hasAutoIncrementPrimaryKey = false, $data = [])
    {
        if ($hasAutoIncrementPrimaryKey) {
            $this->setIdentityInsert($table);
            $response = $this->insertTableData($table, $data);
            $this->setIdentityInsert($table, false);

            return $response;
        }
        else {
            return $this->insertTableData($table, $data);
        }
    }

Note: Generally, the table requires to have an auto-increment primary key to set Identity Insert to `ON`, that's why I have the `$hasAutoIncrementPrimaryKey` flag. Otherwise, seeding may throw an error as:
    
    SQLSTATE[HY000]: General error: 544 Cannot insert explicit value for
    identity column in table 'test_table_name' when IDENTITY_INSERT is set to
    OFF. [544] (severity 16) [(null)]
Hope this helps!

Thursday, 21 March 2019

Get a list of columns and their data type by passing table name on MySQL and MSSQL databases.

To get a list of columns and their data type by passing table name on MySQL and MSSQL databases.

    
    /**
     * Returns the list of columns and their data type by the table name on MySQL database.
     * @param $sqlConnection MySQL Database Connection Link
     * @param $tableName
     * @param bool $fullType
     * @return array
     */
    private function getMySqlTableColumnAndTypeList($mysqlConnection, $tableName, $fullType = false)
    {
        $table = $mysqlConnection->select("DESCRIBE $tableName");

        $fieldAndTypeList = [];
        foreach ($table as $field){
            $type = ($fullType || !str_contains($field->Type, '('))? $field->Type: substr($field->Type, 0, strpos($field->Type, '('));
            $fieldAndTypeList[$field->Field] = $type;
        }
        return $fieldAndTypeList;
    }

    /**
     * Returns the list of columns and their data type and nullable by the table name on MSSQL database.
     * @param $sqlConnection SQL Database Connection Link
     * @param $tableName
     * @return array
     */
    private function getSqlTableColumnsWithDataTypes($sqlConnection, $tableName)
    {
        $tableInfo = $sqlConnection->select("SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '". $tableName . "'");

        $fieldAndTypeList = [];
        foreach ($tableInfo as $field){
            $isNullable = ($field->IS_NULLABLE == 'YES') ? true : false;
            $fieldAndTypeList[$field->COLUMN_NAME] = (object)['type' => $field->DATA_TYPE, 'nullable' => $field->IS_NULLABLE, 'isNullable' => $isNullable];
        }

        return $fieldAndTypeList;
    }

Raw Query in MSSQL:
SELECT COLUMN_NAME, DATA_TYPE, IS_NULLABLE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'your_table_name';

Raw Query in MySQL:
DESCRIBE your_table_name;


Appreciated for any feedback!

Monday, 25 February 2019

Password protect to directory on the web server and Add a new user

Allow User to access the app 1. Create/Update `.htaccess` file in root directory:
    touch .htaccess
    vi .htaccess
Then, add following content:
    AuthUserFile /home/bitnami/htdocs/.htpasswd
    AuthGroupFile /dev/null
    AuthName "Application Authentication"
    AuthType Basic
    require user demo-user1
    require user test-user2
3. And, for passwords, create `.htpasswd` file in root directory.
    touch .htpasswd.
4. To add/edit password, run the following command:
    htpasswd -c .htpasswd demo-user1
    
And, follow instruction ahead.

Thursday, 21 February 2019

Laravel - PHPUnit - run single test class or method

The following command runs the test on a single method: (e.g. we are testing the `testCreateUser()` method here)
vendor/bin/phpunit --filter testCreateUser UserControllerTest tests/feature/UserControllerTest.php
vendor/bin/phpunit --filter methodName ClassName path/to/file.php
Further, lets say you want to test a ClassName which exists into two location:
tests/unit/UserControllerTest.php // contains 3 tests
tests/feature/UserControllerTest.php // contains 2 tests
In this case, you simply run below command which will check for all the test on both location.
vendor/bin/phpunit --filter UserControllerTest

-----Output---------
Time: 2.56 seconds, Memory: 30.00MB

OK (5 tests, 22 assertions)
Please note, if you have phpunit available globally on your machine, you can simply run
phpunit --filter 

# instead of 
vendor/bin/phpunit --filter
Stackflow resource.

Thursday, 31 January 2019

Rename all existing files on AWS S3 Bucket using Laravel Storage library

Follow below steps to rename existing files on a selected directory on S3 Bucket.

1. Lets say your config/filesystems.php looks like this:
'disks' => [
  's3_test_bucket' => [
        'driver' => 's3',
        'key'    => env('AWS_KEY', 'your_aws_key_here'),
        'secret' => env('AWS_SECRET','your_aws_secret_here'),
        'region' =>  env('AWS_REGION', 'your_aws_region_here'),
        'version' => 'latest',
        'bucket'  => 'my-test-bucket',
  ],
];

2. Let's say, you have my-test-bucket on your AWS S3.

3. Lets say you have following files inside the my-test-bucket/test-directory directory.
i.e.
- test-files-1.csv
- test-files-2.csv
- test-files-3.csv

3. Call below function to rename existing files on a selected directory on S3 Bucket.
$directoryPath = 'test-directory';
$storage = new MyStorageRepository();
$storage->renameAnyExistingFilesOnImportDirectory('my-test-bucket', 'test-directory');

4. Output: files should be rename as below on my-test-bucket/test-directory directory:
- test-files-1--1548870936.csv
- test-files-2--1548870936.csv
- test-files-3--1548870936.csv

5. Include the below library class or methods on your class and you should be good.

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;

class MyStorageRepository
{
    public function renameAnyExistingFilesOnImportDirectory($bucket, $directoryPath)
    {
        $directoryPath = App::environment() . '/' . $directoryPath;
        $storage = Storage::disk('s3_test_bucket');

        $suffix = '--' . time(); // File suffix to rename.

        if ($storage->exists($directoryPath)) {
            $this->renameStorageDirectoryFiles($directoryPath, $storage, $suffix);
        }
    }

    private function getNewFilename($filename, $suffix = null)
    {
        $file = (object) pathinfo($filename);

        if (!$suffix) {
            $suffix = '--' . time();
        }

        return $file->dirname . '/' . $file->filename . $suffix . '.' . $file->extension;
    }

    private function renameStorageDirectoryFiles($directoryPath, $storage = null, $suffix = null, $filesystemDriver = null)
    {
        if (!$storage) {
            $storage = Storage::disk($filesystemDriver);
        }

        // List all the existing files from the directory
        $files = $storage->files($directoryPath);

        if (count($files) < 1 ) return false;

        foreach($files as $file) {
            // Get new filename
            $newFilename = Helpers::getNewFilename($file, $suffix);

            // Renamed the files
            $storage->move($file, $newFilename);
        }
    }
}
  
Also, copy of ref here

Thursday, 24 January 2019

Laravel Method to check if the given record id exists on DB table or not.

public function isModelRecordExist($model, $recordId)
{
    if (!$recordId) return false;

    $count = $model->where(['id' => $recordId])->count();

    return $count ? true : false;
}

// To Test
$recordId = 5;
$status = $this->isModelRecordExist( (new MyTestModel()), $recordId);

// Outcome: true | false

Hope it helps!

Thursday, 29 November 2018

Docker machine creation - Too many retries waiting for SSH to be available -Maximum number of retries (60) exceeded

Docker machine creation - Too many retries waiting for SSH to be available -Maximum number of retries (60) exceeded

To resolve the issue for 
Error creating machine: Error in driver during machine creation: Too many retries waiting for SSH to be available. 
Last error: Maximum number of retries (60) exceeded


Run the following following steps:

1. Check your docker version:
$ docker -v
Docker version 18.03.0-ce, build 0520e24


2. Download your docker version's of `boot2docker` (18.03.0-ce) from here into your local:
wget https://github.com/boot2docker/boot2docker/releases/download/v18.03.0-ce/boot2docker.iso -P ~/.docker/machine/cache/test/boot2docker-v18-03-0-ce.iso


3. Create the `new_virtual_box_name` using the downloaded version of `boot2docker`:
docker-machine create --driver virtualbox --virtualbox-boot2docker-url ~/.docker/machine/cache/test/boot2docker-v18-03-0-ce.iso new_virtual_box_name


Tuesday, 13 November 2018

SAML Auth login issue on local - undefined logger()

(1/1) FatalThrowableError
Call to undefined function App\Http\Controllers\logger()

in SamlController.php line 85
at SamlController->acs()
at call_user_func_array(array(object(SamlController), 'acs'), array())
in BoundMethod.php line 29


Enable the Saml debugger by adding the `SAML2_DEBUG=true` on .env.

Then, found the following error:
openssl_x509_read(): supplied parameter cannot be coerced into an X509 certificate!
Whoops, looks like something went wrong.
(1/1) FatalThrowableError
Call to undefined function App\Http\Controllers\logger()

in SamlController.php line 87
at SamlController->acs()
at call_user_func_array(array(object(SamlController), 'acs'), array())
in BoundMethod.php line 29
at BoundMethod::Illuminate\Container\{closure}()
in BoundMethod.php line 87


Resolved the issue after updating with the correct `SAML2_IDP_X509CERT` value.