Skip to main content

Database-Seeds

Overview​

In order to avoid using a database dump for development purposes, a set of database seeds is available in the uala-main app.

The seeded models are:

  • AclGroup
  • AclGroupPermission
  • AclPermission
  • ApiKey
  • Appointment
  • Area
  • Country
  • Customer
  • Domain
  • HistoricalPrice
  • LookTypology
  • Manufacturer
  • ManufacturersSupplier
  • MigratedEntity
  • NewsletterLayout
  • Order
  • OrderDetail
  • Product
  • ProductCategory
  • StaffMember
  • StaffMemberTreatment
  • Supplier
  • Transaction
  • Treatment
  • BlacklistTerm
  • TreatmentCategory
  • TreatmentTypology
  • User
  • Vat
  • Venue
  • VenueCostItemTypology
  • VenueProduct
  • VenueTreatment
  • VenueTreatmentGroup
  • VenueTypology
  • Workstation
  • WorkstationTreatment

Usage​

How to plant the seeds in the database​

Database seeds are meant to be launched on an empty database

ATTENTION!!! The following steps are destructive instructions, so be sure of what you are doing

  1. be sure the database settings are correct, please check the .env.development.local file, both in the uala-main and uala-admin apps
  2. for the process of data seeding you will need to run opensearch instance locally (you can find description here). Also, you need to have the string OPENSEARCH_URL=https://localhost:9200 in the .env.development.local file
  3. to start, you must be inside the uala-main folder, so $ cd uala-main
  4. execute rails db:schema:load db:seed reset_update_counters
  5. to create the default AdminUser execute cd ../uala-admin && bundle exec rails db:seed && cd ../uala-main

The default admin user credentials are:

username: [email protected] password: password

How to export seeds from the current database​

In both uala-admin and uala-main some rake task have been created, to extract data from the current database, and allow real world scenario seeds to be generated.

Uala Main​

from the uala-main folder execute the task bundle exec rails csv_export:all

the following CSV seeds will be extracted from the current database

db/main/seeds/csv/acl_group_permissions.csv
db/main/seeds/csv/acl_permissions.csv
db/main/seeds/csv/areas.csv
db/main/seeds/csv/countries.csv
db/main/seeds/csv/domains.csv
db/main/seeds/csv/treatment_categories.csv
db/main/seeds/csv/treatment_typologies.csv
db/main/seeds/csv/treatments.csv
db/main/seeds/csv/vats.csv

alternatively, to generate only one CSV file, single generator tasks can be executed, i.e.,

bundle exec rails csv_export:treatments

Uala Admin​

from the uala-admin folder execute the task bundle exec rails csv_export:all

the following CSV seeds will be extracted from the current database

db/main/seeds/csv/admin_permissions.csv

alternatively, to generate only one CSV file, single generator tasks can be executed, i.e.,

bundle exec rails csv_export:admin_permissions

Seeds​

All the seed classes, both in uala-main/db/main/seeds/*.rb and uala-admin/db/seeds/*.rb derive from the same Uala::Seeds::Base class, which is defined in uala-core/lib/uala/seeds/base.rb

There are two different strategies for importing seeds: from factories and from csv files

The class will prioritize factories, if defined overwriting the #factories method.

I.e., in the uala-main/db/main/seeds/venue-typologies.rb we have

def factories
[
[:hairdresser_venue_typology, { id: 1 }],
[:aestethic_depilation_venue_typology, { id: 2 }],
[:spa_massages_venue_typology, { id: 3 }],
]
end

if the factory method has not been overwritten, the class will look for a CSV file, contained in the folder specified by the SEEDS_CSV_PATH ENV constant. The default values are SEEDS_CSV_PATH=db/main/seeds/csv/ for uala-main and SEEDS_CSV_PATH=db/seeds/csv/ for uala-admin

If neither the #factories method has been overwritten, nor the CSV is not present, the script will return an error (there is no error handling)

As last resort, for those seeds which need a peculiar strategy that cannot be inherited from the base class, the #seed method can be overwritten, to implement a custom logic. I.e., in uala-admin/db/seeds/admin_user.rb

def seed
# create the admin in the 'god' group
admin = AdminGroup.find_by(name: 'god').admin_users.create!(
password: 'password',
password_confirmation: 'password',
)

admin.countries << Country.all
end

Seed exporters​

All the exporters rake tasks use the Uala::Seeds::CsvExporter class, which is defined in uala-core/lib/uala/seeds/csv_exporter.rb

This class, once instantiated, exposes only one public method, which is #export

The Uala::Seeds::CsvExporter is initialized in this way Uala::Seeds::CsvExporter.new(collection, exportable_columns, translated_columns = nil)

  • collection is an ActiveRecord relation or an Array of objects of the same kind to export to the CSV
  • exportable_columns is the list (Array) of the "columns" to be extracted from the collection elements. They will be called as methods of the collection element, so the collection element must respond to all of them.
  • translated_columns is an Array of "translated columns" and they will be looked up in all the locales available in I18n.available_locales, so, i.e. for the Treatment model, we know that the #name is translated, so the script will look up for name_en, name_es, name_fr ... automatically.

All the data is serialized in JSON format, so that we can export/import different data types, including Array and JSON columns.

Once the #export method is called, the object will extract the CSV from the current database, saving them in the folder specified in the SEEDS_CSV_PATH ENV constant

Deterministic seeding​

Randomization can be reproduced across seeding runs, similarly to RSpec's test ordering. The SEED environment variable can be used to inject the randomness seed to database seeding, like this:

SEED=12345 bin/rails db:seed

If you want to reproduce a seeding that has already ran, you can get the seed from the bin/rails db:seed output, which should be similar to the following:

Randomized with seed <some-seed-here>

...

Randomized with seed <some-seed-here>