Sunday, April 7, 2024

Do's and Don't for a database maintenance

Here I have listed some do's and don'ts for a database project.

Do's for a Database Maintenance Project:

1. Regularly perform database backups to prevent data loss in case of system failures or disasters.
2. Monitor database performance metrics such as CPU usage, memory usage, and disk I/O to identify potential bottlenecks.
3. Implement database maintenance tasks such as index rebuilds, statistics updates, and data purging to optimize performance.
4. Conduct regular security audits to identify and mitigate vulnerabilities in the database system.
5. Document all changes made to the database structure or configuration to maintain an audit trail and facilitate troubleshooting.
6. Schedule regular downtime for maintenance activities and communicate it to stakeholders to minimize disruptions.
7. Keep database software and associated tools up to date with the latest patches and updates to ensure security and stability.
8. Test database backups and disaster recovery procedures regularly to verify their effectiveness.
9. Implement access controls and encryption mechanisms to protect sensitive data stored in the database.
10. Establish performance baselines and thresholds to proactively identify performance degradation and take corrective actions.

Don'ts for a Database Maintenance Project:

1. Don't neglect regular database maintenance tasks such as backups, index optimizations, and software updates.
2. Don't make changes to the database configuration or schema without proper testing and approval processes in place.
3. Don't ignore security vulnerabilities or delay applying security patches, as this exposes the database to potential security breaches.
4. Don't perform maintenance activities during peak usage hours without proper planning and coordination with stakeholders.
5. Don't rely solely on automated tools for database maintenance without human oversight, as they may not catch all potential issues.
6. Don't overlook performance tuning opportunities, as even small optimizations can have a significant impact on overall system performance.
7. Don't grant excessive permissions to users or applications, as this increases the risk of unauthorized access or data breaches.
8. Don't forget to document all maintenance activities and changes made to the database system for future reference and audit purposes.
9. Don't assume that backups are sufficient for disaster recovery without testing the restoration process regularly.
10. Don't overlook monitoring and alerting mechanisms to promptly identify and address any issues or anomalies in the database system.
 

Monday, March 11, 2024

RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: PROTOCOL_ERROR (err 1)

When I was trying to do a git clone, I got the below error:


The error was RPC failed; curl 92 HTTP/2 stream 0 was not closed cleanly: CANCEL (err 0)
some bytes of body are still expected. Unexpected disconnect while reading sideband packet. Early EOF. 
I have gone through google to solve this issue. I found different solutions. Some of them even told it's due to network. You need to change the internet operator etc. But all in vain. The correct solution is , you need to increase the buffer size of http post with the below command

git config --global http.postBuffer 157286400
After this I was able to clone the repo properly.

Happy coding

Friday, February 2, 2024

Seeding: Array[ReflectionException]Class Array does not exist Error while trying to seed the database in Laravel

While I tried to seed the database in laravel after the migration with the below artisan command,

php artisan db:seed

I received the below error: 
Seeding: Array
[ReflectionException]
Class Array does not exist

There are few possible causes for the error message “ReflectionException: 
Class Array does not exist” that appears when Laravel tries to use class that it cannot locate. These include: The class might not have been properly imported or required at the top of the file. The class might not have been properly imported or required at the top of the file. Composer dump-autoload may not be done.

I solved this by running the below command:

composer update

and then run:

composer dump-autoload


Now run

php artisan db:seed

It works. Happy coding

Tuesday, January 9, 2024

Laravel JWT AUTH only returns TRUE/FALSE instead of the token

When using Laravel JWT authentication, the default behavior is to return a boolean (`true` or `false`) indicating whether the authentication was successful or not. If you want to receive the token upon successful authentication, you might need to customize the authentication response.

Here's how you can modify Laravel to return the token upon successful authentication using JWT:

1. Install the JWT package:

   Make sure you have the `tymon/jwt-auth` package installed. You can install it using Composer:


   composer require tymon/jwt-auth
  

2. Publish the configuration file:

   Run the following command to publish the JWT configuration file:

   
php artisan vendor:publish --provider="Tymon\JWTAuth\Providers\LaravelServiceProvider"
  

3. Configure the `config/jwt.php` file:

   Open the `config/jwt.php` file and set the `'return_user_token'` configuration option to `true`:

      /*
   |--------------------------------------------------------------------------
   | Return User Token
   |--------------------------------------------------------------------------
   |
   | If set to true, the middleware will attach the token to the response
   | so that it can be easily accessed in your application
   |
   */
   'return_user_token' => true,

4. Update your authentication logic:
   In your authentication controller (e.g., `LoginController`), make sure you are using the `JWTAuth` facade and include the `attempt` method. Here's an example:

   
   use Illuminate\Support\Facades\Auth;
   use Tymon\JWTAuth\Facades\JWTAuth;

   public function login(Request $request)
   {
       $credentials = $request->only('email', 'password');

       if ($token = JWTAuth::attempt($credentials)) {
           return response()->json([
               'token' => $token,
               'user' => Auth::user(), // Include user details if needed
           ]);
       }

       return response()->json(['error' => 'Unauthorized'], 401);
   }
  

By setting `'return_user_token'` to `true` in the configuration file and modifying your authentication logic, you should receive the token in the response upon successful authentication. Adjust the code as needed based on your application structure and requirements. 

Wednesday, January 3, 2024

How to set default value to spinner dropdown in Android?

 In Android, if you want to set a default value for a `Spinner` dropdown, you can achieve this by adding the default value to the adapter and selecting it programmatically. Here's an example:


import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Spinner spinner;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        spinner = findViewById(R.id.spinner);
        // Create a list of items for the spinner
        List<String> items = new ArrayList<>();
        items.add("Default Value"); // Add your default value here
        items.add("Item 1");
        items.add("Item 2");
        items.add("Item 3");
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, items);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        // Set the default value as the selected item
        spinner.setSelection(0); // 0 is the index of the default value
        // Set a listener to handle spinner item selection events
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                // Handle the selected item
                String selectedValue = (String) parentView.getItemAtPosition(position);
                // Do something with the selected value
            }
            @Override
            public void onNothingSelected(AdapterView<?> parentView) {
                // Do nothing here
            }
        });
    }
}


In this example, "Default Value" is added to the list of items, and `spinner.setSelection(0);` is used to set it as the default selected item. Adjust the code based on your specific use case and UI elements.