Databse migration based on SQL in Laravel? - laravel

Does Laravel allow to use database migration based on ready SQL created in Workbench as an option?
As I know, now it suggests to write migration file manually following the specific format.

Sure, there's nothing preventing you from running SQL in a migration. You can use DB::statement() to do this inside of up() in your migration.
\Illuminate\Support\Facades\DB::statement('
create table application_pages_test
(
id int unsigned auto_increment
primary key,
application_id tinyint unsigned not null,
title varchar(64) not null,
created_at timestamp null,
updated_at timestamp null
)
collate=utf8_unicode_ci
;');

Yes, Laravel allows the use of ready SQL created in Workbench. You just have to connect your Laravel application to the database, and you should be fine.
The use of migrations in Laravel is not mandatory, but very useful in tracking versions your database structure.
Since tracking the structure of your database is very essential, I recommend you try one of these packages to convert your SQL to Laravel migrations:
MySQL Workbench Export Laravel 5 Migrations Plugin ā€“ plugin for
a popular software MySQL Workbench, written in Python language
Laravel Migration Exporter for Sequel Pro ā€“ A bundle for Sequel
Pro that lets you generate Laravel migration files from existing
tables. Written in PHP language.
Xethron Laravel Migrations Generator ā€“ Laravel artisan-command
tool, available as Laravel package
Source

A few weeks ago we released support for Laravel framework for our visual editor Skipper (https://www.skipper18.com). From your question I suppose, it might be very useful for you.
You can import your project from a database (MySql, Postgres, MSSQL or Sqlite) and you will get a clear graphical diagram of it which you can then automatically export to model classes and migrations files.
In addition, you can make changes in the model and create new migrations and repetitively export all changes back to Laravel PHP files. All of this via a user-friendly interface.
If you want to try it, it's completely free during the beta period. You can download it here https://www.skipper18.com/en/download and in the initial application license screen select "Laravel Beta license".

Related

Schema::create Vs Sql

I am new with Laravel and started my personal project.
Laravel provide migrations for creating schema for our databases. I have difficulties on implementation choice. The ideas I have are:
Create database schema using Illuminate\Support\Facades\Facade\Schema provided by Laravel, for example:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('username',300)->unique();
$table->string('email',300)->unique();
$table->string('password',100);
})
Create schema using ERD Designer like Mysql Workbench, export the diagram to sql and finally execute the sql in Laravel using DB::statement
I'm trying to google which method is better, but no answer satisfy me.
Which methods is the better or commonly used in industry? If you have any other suggestion I'm happy to know it.
Laravel Schema is an abstraction and, for the most, database indipendent, that is, if there is already a grammar in the laravel source or in an external library you can use the same migrations with different Database Engines with minor issues.
If you use Raw SQL you could, in case, depend on the actual Database Engine you use.
Laravel ships with these grammars: MySQL, PostgreSQL, SQLite and SQLServer.
If you use the build-in Schema you can use the same migrations in any of the databe engines above without worry too much about the actual SQL implementation.
Consider that many developers have a local dev environment with a simple database engine like SQLite, and sometimes when they start a project they don't even know the database engine that will be used in production. Think about the case you want to change hosting provider or you have a demo project on a cheap provider and the production elsewhere.

How to specify custom migration table in beego

I have 2 repo which uses the same database. I have different migrations in each repo. how can I specify a custom migration table in 1 repo rather than the default migration table in Beego?
Beego version 1.10.1
Go version go1.10.3
It is not currently possible with beego, it uses hardcoded table name migrations as you can see in the source code - https://github.com/astaxie/beego/blob/develop/migration/migration.go#L149
But since it is open source, it can be improved with a PR!

incomplete migrations- stuck in generating new migrations

I am having a very bad situation here, working on a Laravel 5 project. previously developed by another developer. That developer at start created couple of tables using migration generators and added some columns using migrations. After that, he added table columns straight away using some sql GUI. I was given the sql dump which i imported and set it up on my local machine, now when i created a table using php artisan make:migration create_myTableName_table --create="myTableName" the table migration is created successfully, but, when i did php artisan migrate it's giving me SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'someTable' already exists I checked migrations folder and matched it with current version of someTable and i can see the columns are different, same with other tables aswell.
What should be the best case to handle this in this situation, i want to keep up with Laravel migrations generator so that in future if any other developer want to work on this project he just has to run migration command to migrate database or to create tables or create columns... Should i re-write all migrations ? pleas help. Thanks
Given your situation, Iā€™d put the .sql export in your repository, clear out the old, broken migrations, and create a new one that initially imports the database dump. Then just create migrations as normal going forward.
Quick and dirty?
Delete current migration files. Clear your migrations table and export the whole DB. Put the SQL dump in the repo and instruct other devs to import it before they run php artisan migrate. With the dump imported and migrations table truncated, you can create new migrations with no further collisions with the legacy migrations.
Feeling ambitious?
Use a package like migrations-generator to generate migrations based on your current DB structure. Then, migrate away.

Laravel Migration Advantages

I am new to using Laravel, and I'm currently learning about Laravel's database migration and seeding features.
It's working with the command prompt, but I can migrate and seed in phpMyAdmin as well. What are the advantages and disadvantages of migrating and seeding within Laravel as opposed to phpMyAdmin?
From Laravel docs on Migrations & Seeding:
Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state.
A simple search for why database migration also gives me some pretty decent results. One of the easiest to understand is a page by FlywayDB (I have no idea who they are until I search this term up):
Database migrations are a great way to regain control of this mess. They allow you to:
Recreate a database from scratch
Make it clear at all times what state a database is in
Migrate in a deterministic way from your current version of the database to a newer one
The illustration they made perhaps explain it more clearly, so you may want to check it out.

Migrations from database

I have created the schema through MySQL Workbench and synchronized it with the database.
All the tables are there in database. Now when I use the
php artisan migrate
command, is there an option to take the fields director from the database instead of specifying the fields name.
Also could you please suggest any other tools on github.
Laravel Migrations Generator will help us generate migration source code from existed database in Laravel 4.
so if you already have your sql schema, created in mysql workbench, then you'd need to put that schema into laravel migrations using the schema builder.
there are tools that make this part easier:
http://www.laravelsd.com/ - kinda like mysql workbench, with an export possibility to laravel migration code
https://github.com/XCMer/larry-four-generator writes existing databases to laravel migration code
Another option is to use our tool Skipper (https://www.skipper18.com) We introduced Laravel support a month ago and now, in a beta phase, it is free for Laraver users.
Skipper allows you to import your MySQL Workbench project directly (or it is possible to import existing database too) and then export migrations and object files from Skipper to your Laravel project.
The benefit of this solution is that you can maintain your ORM schema directly in Skipper and continuously update model object files and create new migrations automatically from the application without the necessity to write any code manually.
If you want to try it, you can download it here https://www.skipper18.com/en/download, and in the initial application license screen select "Laravel Beta license".

Resources