Open treagod opened 7 months ago
I like the idea! I wonder if we need to allow this mechanism to be declined on a per-application basis. I assume that applications that would need to set up some pre-defined data would resort to data migrations instead of seeds. In my mind, using seeds is more something that project developers might use in order to pre-create records based on all the models provided by the installed applications. In this situation, I am under the impression that a single top-level seeds.cr
would probably be sufficient?
In terms of format, do we need the #run
method? I guess we can keep it simple to and assume that the seed.cr
is a simple script. For example:
require "./src/project"
# Use this file to define how your database should be seeded.
# To apply seeds, execute `marten seed`
author = Author.create!(firstname: "John", lastname: "Doe")
Article.create!(name: "Article 1", author_id: author.id)
Article.create!(name: "Article 2", author_id: author.id)
You're right, a single seeds.cr
would be sufficient. But my thought was that it could be beneficial to only seed/reset one application. For example I have a blogging and a journey app. I want to implement a new feature for the journey app and therefor only want to reset and re-seed its models. The blogging app does not concern me.
This still wouldn't make the module AppName::seed
neccessary, but I thought it could be helpful if multiple seed.cr
exists.
But maybe we could go first with the single seeds.cr
approach and then implement further features
To get further here: would it work for you if we first create a global seed.cr
in the root project directory with the following content?
require "./src/project"
Marten.setup
# Do not change above
# Add your seed data below
And then add the marten seed
(potentially marten seed -f location/to/seed.cr
, where the filepath is optional) command, which invokes crystal run seed.cr
. I guess this would work as a starter. And for advanced usage we could still open a new issue.
What do you think @ellmetha ?
Sounds good to me @treagod!
Description
Let's introduce a new management command
marten seed
, to streamline the process of populating a database with initial data for Marten projects.Background:
As Marten projects evolve, it becomes increasingly important to have a reliable and efficient method for seeding the database with initial data. This initial data could include sample records, or any other essential information required for the project to function properly.
Proposal
The
marten seed
command will automate the process of seeding the database by providing a convenient interface for executing predefined seeding tasks.These tasks could be defined in a
seed.cr
file at the root of each app/project. For example the file could look like below, butCalling
marten seed
will fill your database with the data from each seed.cr inside your project/apps.A
marten seed --app=my-app
could be also beneficial to only run the seed task from one app.This task would especially useful when reloading the database frequently in development environments or initialize the database in production systems.