Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a marten seed management command #147

Closed
treagod opened this issue Jan 28, 2024 · 4 comments · Fixed by #266
Closed

Add a marten seed management command #147

treagod opened this issue Jan 28, 2024 · 4 comments · Fixed by #266
Assignees

Comments

@treagod
Copy link
Contributor

treagod commented Jan 28, 2024

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, but

# seed.cr
# Populate the database of this app
module Articles::Seed
  def run
    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)
  end
end

Calling 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.

@ellmetha
Copy link
Member

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)

@treagod
Copy link
Contributor Author

treagod commented Jan 29, 2024

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

@treagod
Copy link
Contributor Author

treagod commented Feb 28, 2024

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 ?

@ellmetha
Copy link
Member

Sounds good to me @treagod!

@treagod treagod self-assigned this Feb 28, 2024
@ellmetha ellmetha added this to the v0.5.0 milestone Mar 5, 2024
@ellmetha ellmetha removed this from the v0.5.0 milestone Apr 23, 2024
@treagod treagod removed their assignment Apr 23, 2024
@treagod treagod self-assigned this Aug 9, 2024
@treagod treagod linked a pull request Sep 11, 2024 that will close this issue
@treagod treagod closed this as completed Sep 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging a pull request may close this issue.

2 participants