Populating tables in production with Laravel - laravel

In Laravel we have the concept of seeding data that is used in conjunction with model factories in order to populate data for testing environment.
How should we proceed (where to put the code) when we need to populate data for production? For example I might have a permission table, and I need to add some default permissions along with the schema creation. After a while I might need to add to my app a new permission. Should these data insertion stay together with the migrations?
What mechanism should we use for inserting data? Models or data array? My problem with data array is that no business from models, will be helpful: like: casts or relationships.
I know, that there two discussions about this subject, but for me the solutions do no cover all the problems:
Laravel : Migrations & Seeding for production data
Laravel DB Seeds - Test Data v Sample Data

How should we proceed (where to put the code) when we need to populate data for production?
Our team makes a brand new migration for inserting production seeds (separate from the migration that creates the table). That way, if you need to add more seeds to your production data in the future, you can simply make a new standalone migration.
For example, your first migration could be 2016_03_05_213904_create_permissions_table.php, followed by your production seeds: 2016_03_05_214014_seed_permissions_table.php.
You could put this data in the same migration as your table creation, but in my opinion, the migration becomes less readable and arguably violates SRP. If you needed to add more seeds in the future, you would have two different "standards" (one group of seeds in your original migration, and another in a separate migration).
To answer your second question:
What mechanism should we use for inserting data?
I would always use your model's create() method for inserting production seeds. This ensures that any event listeners that are listening for your model's creation event properly fire. As you said, there could be extra code that needs to fire when your model is created.

Related

Repeatable Data in Laravel

I'm creating a Laravel app but come from a WordPress background. In WordPress, it's reasonably straight forward to create custom fields that are repeatable e.g. I have a field called "Task" that can be repeated X number of times and it will be stored on the database.
Is there a best practice way of doing this in Laravel?
I understand that Javascript can be used to create repeatable form fields, and I could store that data as JSON in a MySQL database (using the latest versions of MySQL), but I'd also like this repeatable data to hold relationships e.g. relate a task to a day of the week (stored in another table).
Any advice or thoughts are much appreciated.

Core Data Migration: are always new SQLLite tables created again?

I've made a small change to my database (just added a new entity to my model) and created a lightweight mapping model to handle the migration.
The migration still seems to be quite slow, looking at the log of the migration, it seems all SQL Lite tables are created again and all data migrated.
So, this is how Core Data works? I can't have a faster migration?
ps. I've a complex model, with 30 entities, and many relationships. They are not inheriting from the same parent entity. Maybe core data is not designed to handle such complexity?
Migration is a relatively rare event. It can take a while, especially for large, complex models with lots of data.
I am not entirely sure that new tables are created, but I think that this is indeed happening. It is how I would implement the migration if Core Data did not do it for me.
Here are some suggestions for improvement:
Make sure the migration occurs in the background. Inform your user and keep the app responsive as much as possible.
Perhaps something went wrong when you "created a lightweight mapping model". If you are doing not more than a certain subset of changes (see the docs), "Lightweight Migration" does not require you to create a mapping model.

Entity Framework 4.3 data migrations: non-existing database

I want to handle such situation:
I have an existing model (e.g. version 1.0);
I've added several data migrations and run them one-by-one consequentially;
for some reasons the existing database has been dropped.
I created the database from the scratch by using CreateDatabaseIfNotExists<MyContext> which will produce the latest scheme already with empty __MigrationHistory table.
While the next execution of website the data migrations error will occur.
The one way I found how to handle this is manually filling the __MigrationHistory table with all data migrations metadata which looks like not very optimistic.
Is any other way to handle such situation (scheme comparing for e.g.)?

Should i create the model classes based on the structure of data in Database?

I have predefined tables in the database based on which I have to develop a web application.
Should I base my model classes on the structure of data in the tables.
But a problem is that the tables are very poorly defined and there is much redundant data in them (which I can not change!).
Eg. in 2 tables three columns are same.
Table: Student_details
Student_id , Name, AGe, Class ,School
Table :Student_address
Student_id,Name,Age, Street1,Street2,City
I think you should make your models in a way that would be best suited for how they will be used. Don't worry about how the data is stored or where it is stored... otherwise why go through the trouble of layering your code. Why not just do the direct DB query right in your view? So if you are going to create an abstraction of your data... "model" ... make one that is designed around how it will be used... not how it will be or is persisted.
This seems like a risky project - presumably, there's another application somewhere which populates these tables. As the data model is not very sound from a relational point of view, I'm guessing there's a bunch of business/data logic glued into that app - for instance, putting the student age into the StudentAddress table.
I'd support jsobo in recommending you build your business logic independently of the underlying persistance mechanism, and that you try to keep your models as domain focused as possible, without too much emphasis on how the database happens to be structured.
You should, however, plan on spending a certain amount of time translating your domain models into their respective data representations and dealing with whatever quirks the data model imposes. I'd strongly recommend containing all this stuff in a separate translation layer - don't litter it throughout the rest of the application.

Doctrine: create fixtures for a certain table

Is it possible to create a fixtures yml file for a certain model from the data already stored in the database (e.g. like dump-data CLI but with specifying which data)?
When loading fixtures, you explicitly declare the models in the fixture file so this is the only way to do it, as far as I know.
If you're referring to loading fixtures into a database with existing data, loading fixtures for a specific model should not affect data in other tables, as long as you don't re-build the models. I haven't tested this though, so please do try it out in a safe environment first.
Another thing to bear in mind is that the data you upload as fixtures need to respect any foreign-key constraints you may have in your database.

Resources