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

Auto seed #1062

Open
DenuxPlays opened this issue Dec 7, 2024 · 10 comments
Open

Auto seed #1062

DenuxPlays opened this issue Dec 7, 2024 · 10 comments
Labels
enhancement New feature or request

Comments

@DenuxPlays
Copy link
Contributor

Feature Request

Would be great if we can configure a feature named "auto-seed" which seeds data at the very first start.
Kinda like how migrations are run. Save which file/function has already and run the rest.

Describe the solution you'd like

Create a new table and save the path of the file / name of the seeder and only run if it hasn't been found.

Describe alternatives you've considered

Implement my own system but that would be kinda pointless.

@DenuxPlays DenuxPlays added the enhancement New feature or request label Dec 7, 2024
@DenuxPlays
Copy link
Contributor Author

Also I would like to work on this if it gets approved.

@DenuxPlays
Copy link
Contributor Author

I would start on the PR if the following pr was reviewed: #1063

I need to know if I can base on top of this pr or if this is something the loco team is not interested in.

All 3 prs (including this one) and:

would improve the functionality of fixtures.

@DenuxPlays
Copy link
Contributor Author

I could also make one bigger pr if that is preferred.

@DenuxPlays
Copy link
Contributor Author

Maybe @jondot and @kaplanelad can you help/state your opinion?
I am not sure if you are the correct person to ping but you helped/reviewed my last pr.

@kaplanelad
Copy link
Contributor

Hey @DenuxPlays,

To create an auto-seed, you can use the before_run hook. If you want to add more conditions, such as checking the environment, you can easily control that with logic like the example below:

 async fn before_run(app_context: &AppContext) -> Result<()> {
        if app_context.environment == Environment::Development {
            Self::truncate(&app_context.db).await?;
            Self::seed(
                &app_context.db,
                PathBuf::from("src").join("fixtures").as_path(),
            )
            .await?;
        }
        Ok(())
    }

I don't know if you know this example, but what is better after seeing it?

Adding it as part of the hook is better than configuring it, as it allows you to add any conditions you need easily.

WDYT?

@DenuxPlays
Copy link
Contributor Author

No.
This will reset the whole db and seed it which is no auto-seed.

I would implement a feature like with migrations.
Where you can say auto_seed: false and then add your custom logic inside the hook if needed.
Like the exact thing that we have with migrations.

@kaplanelad
Copy link
Contributor

you don't need to call truncate function. this was only an example and you still getting the seed logic

@DenuxPlays
Copy link
Contributor Author

Yeah I now
But this would involve the following:

  • create table
  • save which seeders have been executed
  • only execute the rest

etc.

Sounds like something a few others can need.

@jondot
Copy link
Contributor

jondot commented Dec 12, 2024

Hi,
Just to follow up on the different PRs dealing with this topic.
Can you elaborate on what you are trying to do?

For example:

  • When you have user auth systems with roles, usually after creating the roles table people like to insert all the different roles (this is a data migration)
  • When you have, say a FOREX system, where you always need to keep an updated table of exchange rates, this is the kind of thing you do with a recurring loco task and the loco scheduler
  • When you are running tests -- you call the relevant fixture test explicitly (this is done with current fixtures)
  • When you are deploying to a new empty DB, you run migrations and then per environment you decide to explictly run the seed data

what is your specific use case that doesnt fall in this list?

@DenuxPlays
Copy link
Contributor Author

@jondot I've opened a discussion which exactly describes your point.

See here: #1072

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants