Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations fixes #117

Merged
merged 1 commit into from
Dec 1, 2024
Merged

Migrations fixes #117

merged 1 commit into from
Dec 1, 2024

Conversation

ismaail
Copy link
Contributor

@ismaail ismaail commented Nov 26, 2024

to reproduce this errosr:

  • in .env file change DB_CONNECTION=sqlite
  • create empty file: database/database.sqlite
  • run php artisan migration:fresh. WARNING: this destroys the entire database.


Error: SQLite doesn't support multiple calls to dropColumn / renameColumn in a single modification.

files:

  • database/migrations/2022_08_08_100418_update_expense_table_drop_old_columns.php
  • database/migrations/2023_01_04_164114_update_webpage_content_table.php


Error: SQLite doesn't support dropping foreign keys (you would need to re-create the table).

file: database/migrations/2022_08_08_100418_update_expense_table_drop_old_columns.php



Error: There is no column with name "title", "body", "image_path" on table "webpage_content".

->change() is for chaning an existing column not for creating a new one.

file: `database/migrations/2023_02_13_181000_update_webpage_content_table_make_nullable.php~~

Copy link
Owner

@oitcode oitcode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combining two dropColumn into one is ok.

However deleting the

$table->dropForeign('fk_expense_expense_category');

in migration file

database/migrations/2023_01_04_164114_update_webpage_content_table.php

gives error while migrating in MySql. It seems that this line is needed. Please check by trying to run the migrations in MySql.

Also,

$table->text('title')->after('position')->nullable()->change();
$table->text('body')->after('title')->nullable()->change();
$table->string('image_path')->after('body')->nullable()->change();

in thses lines ->change() is needed because these columns are created in

database/migrations/2022_04_10_132549_create_webpage_content_table.php

migrations file. If you remove the ->change() part then migrations fail in MySql. Please check by running migrations in MySql.

@ismaail
Copy link
Contributor Author

ismaail commented Nov 27, 2024

I changed the commits. this solves migrations problems for both mysql and sqlite.

on the other hand this requires Laravel migration to v11 because of 51373

For Laravel v11 migration:

"laravel/framework": "^11.0",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^11.0"

and "doctrine/dbal": "^3.0" is not needed in composer.json

@oitcode
Copy link
Owner

oitcode commented Nov 28, 2024

Thanks for the update.

As this involves switching from Laravel 10 to Laravel 11 will need some time to test it.

Maybe will update in next 4-5 hours. Thanks for patience.

@ismaail
Copy link
Contributor Author

ismaail commented Nov 28, 2024

Thanks, take your time to make sure the system works fines after upgrading to Laravel 11.

what about PHP version upgrade to 8.3 or 8.4 ?

@oitcode
Copy link
Owner

oitcode commented Nov 28, 2024

MySql migrations are running fine after the pull request.

However SqLite migration is failing when I am trying. Migration file

2023_01_04_164114_update_webpage_content_table

is failing. With below error:

 2023_01_04_164114_update_webpage_content_table ...................................................................................... 47.66ms FAIL

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (Connection: sqlite, SQL: alter table "webpage_content" add column "title" text not null)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:825
    821▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    822▕                 );
    823▕             }
    824▕ 
  ➜ 825▕             throw new QueryException(
    826▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    827▕             );
    828▕         }
    829▕     }

Are you sure migrations are running fine in SqLite ? I am getting the above error. If SqLite migrations are running fine in your machine, then maybe I should check again?

@ismaail
Copy link
Contributor Author

ismaail commented Nov 28, 2024

I tested again with migrate (with empty file) & migrate:fresh multiple times, all migrations run with success.

image

After upgrade to Laravel 11 make sure to delete all caches in storage/ & bootstrap/cache/

@oitcode
Copy link
Owner

oitcode commented Nov 29, 2024

After upgrade to Laravel 11 make sure to delete all caches in storage/ & bootstrap/cache/

I did below steps:

php artisan cache:clear
php artisan config:clear

and tried again. However I am still getting the same error.

2023_01_04_164114_update_webpage_content_table ...................................................................................... 38.98ms FAIL

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1 Cannot add a NOT NULL column with default value NULL (Connection: sqlite, SQL: alter table "webpage_content" add column "title" text not null)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:825
    821▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    822▕                 );
    823▕             }
    824▕ 
  ➜ 825▕             throw new QueryException(
    826▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    827▕             );
    828▕         }
    829▕     }

      +9 vendor frames 

  10  database/migrations/2023_01_04_164114_update_webpage_content_table.php:16
      Illuminate\Support\Facades\Facade::__callStatic()
      +25 vendor frames 

  36  artisan:35
      Illuminate\Foundation\Console\Kernel::handle()

Not sure what the issue is. Is there anything I am missing while trying to delete all caches in storage/ & bootstrap/cache/ ?

Also not sure if the versions we are same:

php 8.2.26
Laravel 11.34.2
sqlite 3.31.1

Please confirm.

@ismaail
Copy link
Contributor Author

ismaail commented Nov 29, 2024

  • I pulled latest commit d1cb9af8 Upgrade to Laravel 11
  • run composer install
  • Deleted all caches
  • Applied the fixes from this PR

run migrations for sqlite & mysql with success

  • PHP 8.3
  • MySQL 8.0
  • SQLite: 3.45.3
  • Laravel 11.34.2

@ismaail
Copy link
Contributor Author

ismaail commented Nov 29, 2024

I'm creating a new branch to refactor tests.

The goal is to create a single dummy test that runs migrations, then check if this test passes on Github Action.

@ismaail
Copy link
Contributor Author

ismaail commented Nov 29, 2024

I added github actions to run just one test, and the test that runs the migrations on sqlite works.

check it out https://github.com/ismaail/samarium/actions

also check the branch tests to see commits to make github actions runs.

@oitcode
Copy link
Owner

oitcode commented Nov 30, 2024

Oddly, I am still getting the same error.

I tried with below versions as well:

php 8.3.14
sqlite3 3.45.1
Laravel 11.34.2

Just to rule out if this was happening because of different versions are php and sqlite. However even after updating to php8.3 and Sqlite to 3.45 I am getting the same error while running migrations in SQLite.

Not sure about clear the cache part. Is running php artisan cache:clear enough for it. Or do I need to do something else as well?

@ismaail
Copy link
Contributor Author

ismaail commented Nov 30, 2024

Try all of this

php artisan clear
php artisan cache:clear
php artisan view:clear
php artisan route:clear
php artisan config:clear

If the problem persist, for now make sure migrations works fine with MySQL.

@ismaail
Copy link
Contributor Author

ismaail commented Nov 30, 2024

also can you share your .env file

@oitcode
Copy link
Owner

oitcode commented Dec 1, 2024

Yes this works in MySql. So going to merge now. Maybe we can resolve Sqlite issue later. Perhaps we can track that issue in a new issue. Thanks for your update.

@oitcode oitcode merged commit b241392 into oitcode:master Dec 1, 2024
@ismaail
Copy link
Contributor Author

ismaail commented Dec 1, 2024

ok.

Open an issue for with full details (including env file).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants