Seed Data Generation
This guide can be used to create databases with seed data. Note that seed data generation should ONLY be used on development or test environments as, without modifications to the scripts below, the databases are dropped and recreated.
Getting Started
Step 1: Docker Seeding Script
Inside the ~/Documents/elentra-developer/resources/scripts
directory, create a file called 'install-elentra'.
Open that file and paste the following into it, replacing the database names with the ones used for testing:
This file needs a chmod 777 on it for execution.
Important Note: This script will delete the config.inc.php file as well as drop the specified databases.
It will then recreate those 3 databases, and generate a new www-root/core/config/config.inc.php
script to tell the system how to connect to the new databases.
Next, it runs all migration scripts inside the www-root/core/library/Migrate
directory. This is to bring the databases up to date with whichever branch the developer is on.
Finally, it runs the seed files which are used to populate specified tables with test data using Laravel's database seeding functionality.
In your Elentra API directory, change the phpunit.xml
file from:
to:
This will tell PHPUnit to also run any tests that reside in the Integration directory
Edit the composer.json
file and change:
to:
Run composer install
to regenerate the classmap autoloader so that all files inside the database/seeds
and database/factories
directories are recognized by the system.
Step 2: Creating a JSON Seed File
Seed files are located within the module's Database/Seeds directory.
eg: elentra-1x-api/app/Modules/Locations/Database/Seeds/ClinicalLocationsSeedData.php
To create a seed data file, right click on the appropriate module's Database/Seeds directory and select 'New Class'. All seed data files are suffixed with 'SeedData'. eg:
ClinicalLocationsSeedData.php
LotteriesSeedData.php
UserAccessSeedData.php
All seed data files must implement the SeedDataInterface
. The SeedDataInterface
` has no namespace which means the Class definition would look like:
class ClinicalLocationsSeedData implements \SeedDataInterface
SeedDataInterface provides 2 methods:
For a basic seed data file implementing the SeedDataInterface would look like this:
The getData() method returns the predefined JSON array of values to be inserted into the database. The getType() method returns the Model class of the database table to be seeding with the JSON values.
Creating a Seed Data File With No Model Class
Not all tables have a corresponding Model class to associate to. An example is the user_auth.user_access
table. All queries to the user_auth.user_access
table are performed with joins and no Model class (eg: UserAccess.php
) is required.
In this case we also implement the RawSeederInterface. The RawSeederInterface provides 2 methods:
An example of a class definition would be:
class ScheduleFilesSeedData implements \SeedDataInterface, \RawSeederInterface
An example of a class would be:
The getTablename() method is returning the name of the table to insert the JSON data into. The getTimestamps() method is returning a true/false value to determine if timestamp columns are required. When a table contains the following columns:
created_at
updated_at
The getTimestamps method should return true
. If those columns do not exist the method should return false
. This is because Laravel will automatically append those columns to an insert query unless we explicitly tell it not to.
Step 3: Registering the Seed File
Once a seed file is created it needs to be registered. Inside the /database/seeds
directory at the root of the elentra-1x-api repository, create a new Seeder class. The file naming convention is as follows: Seeder.php eg: LotteriesSeeder.php
A seeder class must extend ElentraBaseSeeder. example:
The Seeder classes have no methods except the constructor, which is where we register the SeedData files we created in the previous section. These are defined in the constructor and passed into the parent ElentraBaseSeeder which has the run() method to iterate any passed in SeedData classes.
Tips for Making Json from Database
If you are on Windows, a useful IDE for MySQL/MariaDB is SqlYog. It allows you to select tables for export and select JSON as a format.
If you are on Linux you can install "W.ine I.s N.ot an E.mulator" (wine) and run SqlYog quite seamlessly.
If you are on Mac and your IDE does not support exporting as JSON, you can export as SQL and use https://codebeautify.org/sql-to-json-converter. It'll take a couple of edits to the generated output from your IDE but works quite nicely for making JSON from SQL.
4: Adding the Seed Data Registry to Laravel
In the older repository it is still performed this way:
The final step is registering the Seeder class which is a registry of SeedData files with the Laravel provided DatabaseSeeder. This is located in the /database/seeds
directory at the root of the API repository.
The DatabaseSeeder class has 1 Laravel-provided method:
In the newer repository it is performed this way:
The final step is registering the Seeder class which is a registry of SeedData files with the elentra.php
file. This is located in the /config
directory at the root of the API repository.
This is where the Seeder classes are registered with the Laravel system.
A visual representation:
Once any new Seeder class is registered in the DatabaseSeeder class we need to flush the composer autoloader to force recognizing the new file:
Step 4: Running the Seed Scripts
From the ~/Documents/elentra-developer
directory, open a new developer shell:
inside the developer shell:
This will call the script from anywhere within the shell and will populate all seed data.
Troubleshooting:
Whenever a new Seed script is registered, the Laravel system must be refreshed. From the developer shell:
This will reset the system and recognize any new changes.
If you need to see the output of the seeder scripts for debugging, edit the install-elentra
script so that the line:
is changed to:
Turning off 'quiet' mode will provide verbose logging and display any possible SQL exceptions that occur.
Check the symlink directory to ensure the changes in composer.json
are present. If they aren't you'll need to redo the symlink:
TLDR;
Create the install-elentra script inside of
~/Documents/elentra-developer/resources/scripts
Create a SeedData class inside a Module's
database/seeds
directoryCreate a Seeder class inside the
elentra-1x-api/database/seeds
directory and register the SeedData class inside that Seeder's constructorEdit the DatabaseSeeder class to call the Seeder class in its array
Inside the developer shell run
install-elentra
Last updated