Laravel adding new comment to column of an existing table - migration

I want to create a new migration to update the comment of a column. For the migration I think the command $table->tinyInteger('role')->default(2)->after('ID')->comment("new comment")->change(); in up() and $table->tinyInteger('role')->default(2)->after('ID')->comment("old comment")->change(); in down() function respectively. But I am getting an error Unknown column type "tinyInteger" requested. I can update the comment in my DB directly but it defeats the purpose of migration if I do this. Also, if I update the local, I also need to update the staging and production.
How should I proceed?

only need to do it in up method
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->tinyInteger('role')->default(2)->after('ID')->comment("new comment")->change();
});
}

Related

Creating full text index for several columns without query builder in Laravel migration

Experienced a need of creating full text index for several columns without using query builder in Laravel 9.0 to make decision clearer, however all answers or tips were either for one/two columns or with query builder.
Tips and tutorials didn't work for me as I had 6 columns to create this index and that led to an error :
SQLSTATE[42000]: Syntax error or access violation: 1059 Identifier name 'table_column1,column2,column3,col...' is too long
So my code was:
class AddFulltextIndexes extends Migration
{
private array $fields = [
'column1',
'column2',
'column3',
'column4',
'column5',
'column6'
];
public function up()
{
Schema::table('invoice_cds', function (Blueprint $table) {
$table->fullText($this->fields);
});
}
}
For those who found themselves in similar situation there is an exit:
Laravel allows to create indexes passing the index name as a second parameter, so I changed one line and migration started to work:
$table->fullText($this->fields, 'table_full_test_indexes');

How to change datatype of a column in postgresql database using knex migration?

I have a column in a postgresql database table. Currently it is of datatype INTEGER, I want to change it to JSONB datatype.
Maybe this topic can answer your question: Modify column datatype in Knex migration script
For more detail you can have a look at the knex documentation: https://knexjs.org/#Schema-alter
Knex supports you to write alter column as the way you create column. The different is that you have to use alterTable() to get alterTable builder in knex that will support modifing your database information.
Or else you can take a look at my code:
Assume that I have a previous migration run before like this:
export function up(knex) {
return knex.schema.createTable('users', table => {
table.increments('id').primary('id');
table.string('username').notNullable().unique('username');
table.string('fullName');
table.string('email');
table.string('password');
table.timestamps(true, true);
});
}
And I want to modify column email so that I will use alterTable to modify the column. Note: Keep in mind that when you do migration with knex postgresql you may be failed because of some reasons so that you should use transaction for making sure that the failure will not effect to your database. But with migration of mysql, you will no need to use this because knex mysql do support transaction while dealing with migration.
export async function up(knex) {
const transaction = await knex.transaction();
try {
await transaction.schema.alterTable('users', table => {
table.string('email').notNullable().alter();
});
await transaction.commit();
} catch (error) {
await transaction.rollback();
}
}

laravel add and delete column to database using controller

is it possible to add and delete column to my existing database using the controller? is it possible not to use the migration? and how do my model automatically picked up the new column which is create and automatically put in inside fillable? anyone has idea on how to approach this type of situation,if you could point me into a tutorial that would be so cool.
Reason: i have a table with the student mark-book points breakdown column example: [Exam, Homework,Quiz etc..] then every term or year we will remove it or changed it or add more so that's why i need to something like dynamic approach on this matter. where anytime i can change the column or add new column.
Same way the migrations do it, use the Schema builder class. For example:
$newColumnType = 'string';
$newColumnName = 'my_new_column';
Schema::table('my_table', function (Blueprint $table) use ($newColumnType, $newColumnName) {
$table->$newColumnType($newColumnName);
});
You probably should use $guarded = ['id', 'foo', 'bar'] in your model instead of fillable if you're going to be adding columns.

yii multi schema postgresql, data inserted but return error sequence not exist

i have 2 schema on postgresql egg: public,items. in public schema all CRUD ok but when i'm try to insert on items schema there is a problem.
the problem is, data inserted but the yii framework return error 'items.table1_id_seq doesn't exist'.
when i'm check to the database the squence is exist.
how to resolve this problem?
check the tableName function into protected/models/table.php
The table must be nameSchema.table
public function tableName()
{
return 'items.nameOfTable';
}

altering type of attribute using yii migration

I have an existing database for web application developed using yii. Now one of the attributes in the table is of type int but I want to convert its type to timestamp in table. How I can achieve this using yii migration so that my web application is affected. I am using Mysql database.
create a yii migration and modify the code
public function up(){
$this->alterColumn('table_name', 'column_name', 'new_data_type');//timestamp new_data_type
}
public function down() {
$this->alterColumn('table_name','column_name', 'old_data_type' );//int is old_data_type
}
then migrate up