Laravel data migration to new structure - laravel

We are planning a major database restructure and instead of "Create" all the migrations are "Alter"-ing already existing tables.
For example:
There's table users that holds a column, e.g. role.
After running migrations this column will be dropped and table "Roles" will be created with a hasOne relationship (just an example).
How should I approach converting the old data into the new structure as seamlessly as possible?

It depends on the environment you are working in. for example in production level you can't just move those values from one column to another because the database Is heavily under usage of customers. but for instance you can create a migration to only create the roles table. then create a new migration to copy the corresponding role of each row to the newly created table(roles).
Roles
user_id | role_id
Users
id | role_id
After moving the whole role_id from users table to Roles table then you can push the new code implementation.
I don't really like the idea of removing the role_id column from users table immediately. i would suggest to keep that column for a while till you finish the whole changes related to that column then you can remove it safety without worry about it.

Related

Laravel Migration create table from existing table

I am working on such application where table audit should perform to make revision of updated records. Let's say for example I have to log each field update in Users update.
I wants to create clone of existing user table by
CREATE TABLE users_audit LIKE users;
My main question is,
Is there any alternative way or provision in Laravel migration to create table from existing table ?
I know we can run raw sql query in migration up/down method using \DB::raw();
But it would be helpful if any helper function available to create table from existing table like, create or table
Yes, you can, see the below link
Click this link
This statement is used to execute raw queries
DB::statement('CREATE TABLE tablename LIKE existingtablename');

How to delete an entry in a table along with its data in another table?

I have a table cars with attributes: id, name. I also have another table specs with id, car_id, name. This tables are related with one to many relations from the Models. I also have set up the foreign keys in.
I have a controller manageData where i have a function insertCar which i use to insert data and update both tables. I wan to create another function deleteCar from where i can delete the car along with its specs from the other table
Use onDelete() method in migration when defining foreign key:
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
In this case when you'll delete a car record, related data will be automatically deleted from another table.
https://laravel.com/docs/5.4/migrations#foreign-key-constraints

Change 1:M relationship to M:M in an Oracle database

I have a question about how to change relationships between tales in an Oracle database while preserving existing data.
Let's say I want to represent People and Employers such that each person works for a single employer. I do this with a PERSON table and an EMPLOYER table with a 1:M relationship of EMPLOYER to PERSON. The PERSON table had columns ID, NAME, and EMPLOYER_ID, and the EMPLOYER table had columns ID, NAME, AND LOCATION.
If I wanted to update this schema so a PERSON can work for more than one EMPLOYER, I could add a PERSON_EMPLOYER table with columns for each ID.
Could anyone give some pointers on the most sensible way to do this and move my existing data in? I think I can add a join table, but I'm not sure how to populate it with existing employer:employer data. After that I guess I remove EMPLOYER_ID column from PERSON.
Should I just be backing up the database and doing this operation in a script?
Thank you so much.
Having a backup is always a good idea.
But in my opinion transferring data from one table to another it is quite reliable operation. You do not necessary make a script, just do it step by step and check changes.
Create a new PERSON_EMPLOYER table
Copy existing data to PERSON_EMPLOYER table.
COMMIT data changes.
Check data in PERSON_EMPLOYER table
Drop EMPLOYER_ID column from PERSON table. (No need to remove the column immediately, it can be done later when you will be sure that everything is fine with the your data.)
For transferring data from PERSON table to PERSON_EMPLOYER table you can use simple INSERT
INSERT INTO employer_person (employer_id, person_id)
SELECT employer_id, person_id FROM person;
Do not forget COMMIT this operation!

Database Migration Laravel 5.2 Arrange Table

I have a create_users_table at the top and with foreign key to roles table but the location of roles table is at the bottom of create_users_table. So what happen is the create_users table can't see the roles table because it's not yet created. See attached image:
Do I need to recreate a users table so that it will come up below to roles table?
Do I need to recreate a users table so that it will come up below to
roles table?
You can't add a foreign key to a table that does not yet exist. If you want to manage your database through migrations, you do need to put them in the right order.
However, you can also add it as a separate migration, so that you have
create users table
create roles table
alter users table to add role_id field and foreign key
That way you don't have to rearrange existing migrations if you e.g. need to make changes to the database mid-project.
if its just a beginning of the project and if you are not using database migrations versioning yet you could just rename the datetime timestamp at migrations filename to rearrange the migrations. something like this would rearrange migrations to the required order. see i changed the year of 2016_08_13_001252_create_roles_table.php to 2014_08_13_001252_create_roles_table.php.
2014_08_13_001252_create_roles_table.php
2014_10_12_000000_create_users_table.php
2014_10_12_100000_create_password_resets_table.php
2016_08_08_005720_create_messages_table.php
migrations run in the datetime order of the filenames. Only the new migrations are run at each run. so, if you bring such changes to the files like renaming the file you need to drop database and create again, then run the migrations.
sometime after doing this you might need to run following commands in the project
composer dump-autoload and/or php artisan clear-compiled.
if you are running the migrations verisoning?
migrations are something related to databases and its not meant for dropping all the tables and recreating each and every time when you need to bring a change. If the database is live or if you have completed major database design you could use versioning. one of the purpose of migrations made in that way (datetime order and only new ones are migrated each time) is that only changes are brought to the old migrations created.

Link tables with extra column in Symfony 2 / Doctrine

In a database I have the tables User, Group and UsersInGroup and generate the entities from the database using the Symfony 2 console. The table UsersInGroup is not generated as an entity but is partly generated into User and Group. This would be perfect if UsersInGroup hat only two columns userId and groupId (together primarykey), in this case the table UsersInGroup also contains a third column Role. This field is can never be filled using the generated entities
How should I fill the Role column in the tabel UsersInGroup?
You have to declare your relation table manually and to map the relations.
You'll find an interesting discussion here:
Doctrine2: Best way to handle many-to-many with extra columns in reference table

Resources