Database is locked in SQlite when using php artisan migrate:refresh - laravel

I am learning Laravel and I have encountered a problem when I want to refresh a migration.
php artisan migrate:refresh
I am using SQlite and below is the error I am getting
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 5 database is locked (SQL: drop table if ex
ists "generate_pins")
The problem seems like it comes from the SQlite having locked the database, How do I unlock the database? After I have unlocked, can I lock it again?
I am learning both Laravel and SQlite and any help will be appreciated. Thanks in advance.

In general, it looks like some process is using the database and it is locking the file or more likely you've made some kind of error in your code causing too many queries at the same time.
Please provide more detailed information. What OS, Laravel version, your database configuration (.env and config/database.php), your migration files and their contents, database structure (run slqite3 path/to/database.sqlite then .tables), and the contents of your migrations table (while still in sqlite run select * from migrations;).

In my case:
Delete database.sqlite
php artisan optimize:clear
php artisan migrate

Related

Okay to amend migrations after migrate:reset?

I have a site in development so database structure is still in flux. If I run migrate:reset, this rollbacks all migrations. Am I, therefore, okay to amend the migrations - i.e. amend Schema closures and remove migration files etc - as opposed to adding more migrations to amend the DB structure? For example, client asked for certain functionality requiring a table, decides later he doesn't want it so I have a table in my migrations I will never use. Ideally I don't want this to appear in my migrations.
If you don't need a table anymore in your project of course you can delete it's migration file.
When you run php artisan migrate:reset Laravel rolls back all migrations. But if you delete your migration file without rolling it back, Laravel will try to find that migration file to roll it back and when it can't find that file; it will throw an exception.
In such cases you can use php artisan migrate:fresh
With migrate:fresh Laravel doesn't try to find and roll back migrations, it just drops all tables and starts a fresh migrations table and migrates every file from start.
So; if you have changes on your migration files, anything, and if you are on development enviroment and nothing will affected: you can do whatever you want with your migration files and run php artisan migrate:fresh to drop every table and migrate them again.
Please check here: https://laravel.com/docs/8.x/migrations#rolling-back-migrations

Laravel reduce migration files

I have a Laravel application that uses a DB. The DB is big and was produced by many migration files. Is there a way to cut down the number of migration files so that each table has one migration file? Also Would something results from having too many migration files? like performance issues?
if you have laravel 8 you can use squashing-migrations
by run php artisan schema:dump cmd
ref link https://laravel.com/docs/8.x/migrations#squashing-migrations
for older version you can try this
https://github.com/Cytracom/laravel-migration-squasher
or you can try this
Laravel 5.5 Consolidate migrations w/ production database
after laravel 8 you can run php artisan schema:dump
it change all migrations to single schema file
For more explanation check link https://advancedwebtuts.com/tutorial/what-is-the-use-of-laravel-squashing-migrations-schema-dump-command

How to modify migrations and migrate it without loosing data?

I created a MYSQL database using migrations, I added some data into it, but after that I recognized that I need to add a new column into my table.
I ran the command: 'php artisan migrate', but as it didn't work to synchronize
columns, it returns there is nothing to migrate.
So I ran the command 'php artisan migrate:reset', and then ran the command
'php artisan migrate' again, database schema updated correctly but for sure I
lost all my inserted data.
Now I'm just testing the application, but it would be very harmful if I found out that I should modify my database while it is runing with real data!!! What should I do in this case?
should I skip using migrations and create the Mysql database directly with
wamp? or use migration, but perform any later updates directly on database without updating the migration files? or there is another solution?
Answer
If you have already created a database table and realise you need to add more into it you can simply run: php artisan make:migration add_field_to_table_name --table=tableName
You then go into that file and create the new field. Once done simply run php artisan migrate and it'll add the new field into the desired table without causing any data loss.
Seeders
On another note, I would strongly suggest looking into seeders. This way when you're creating a project you can always refresh your migrations (wipe your database and re-migrate your tables) and then re-input the data using seeders.
you can create a migration to alter your tables. add new columns and more.

How can I specify which database connection Laravel uses for the migration table?

I'm working on an app with multiple database connections. It seems when I run php artisan migrate:install it always creates the migrations table using my default connection in app/config/database.php, which is not ideal.
Is there a way to specify a different connection for the migrations table itself?
Update
For anyone with a similar question, I found a better answer.
Looks like you can't specify the connection from a config file or anything, but you can when you run migrate from the command line...
php artisan migrate:install --database=NAME_OF_CONNECTION
There is one caveat: anytime you run an actual migration you must also specify the database connection with --database again or it will re-create the migrations table using the default connection.
Edit: Looks like you want to change where the migrations table is stored. This I believe always uses the default. You can however specify where a table is supposed to be created as below:
You can specify a connection like so:
Schema::connection('foo')->create('users', function($table)
{
$table->increments('id');
});
From http://laravel.com/docs/schema
In Laravel 5.5 this worked for me:
php artisan migrate --database=sqlite_testing

Rollback single table

How to rollback the single table in laravel 4.
I ran php artisan migrate:rollback to roll back all the migrations but how to rollback the single table
Advice much appreciated
for example, you want to create the user table again, just go to mysql and delete from migration's table the field that correspond to TIMESTAMP_create_users_table:
DELETE FROM MIGRATIONS WHERE MIGRATION ="2014_10_12_000000_create_users_table";
and drop the users table too:
DROP TABLE users;
after that, just run
php artisan migrate
and that's it!
If you wish to rollback only a specific table, you should make a migration that only includes that table. It's really that simple. Making large migrations covering several updates doesn't make any sense.
Create migrations based upon their purpose. That means; on a live project every little code change should have its own related migration.
Just run The command (Laravel 5):-
php artisan migrate:rollback --step=5
It will undo last 5 migrations (in fact it will execute the function "down" in each one)
php artisan migrate
It will add all migration table into database

Resources