Enhanced seeding system #1072
Replies: 5 comments 15 replies
-
Key difference to migrations: Besides the features I want to implement there is one other key difference: migrations are keept inside another folder/crate which does not allow me to access my custom code inside them so it only makes them useful for basic schema migration. In my case: |
Beta Was this translation helpful? Give feedback.
-
Had you both time to look at this? |
Beta Was this translation helpful? Give feedback.
-
@DenuxPlays I'm looking forward to hearing more feedback from the community here. |
Beta Was this translation helpful? Give feedback.
-
Perhaps it’s worth using trait Environment instead of a hard-coded enum with test, dev, prod environment. pub trait Environment {
fn get_name() -> &'static str;
fn is_allow_seed() -> bool;
fn is_allow_truncate() -> bool;
async fn load_config() -> Result<Config, Error>;
async fn seed() -> Result<(), Error>;
async fn truncate() -> Result<(), Error>;
async fn logger_init() -> Result<(), Error>;
} we can use this for tests too, to reproduce some application environment I feel like this will solve a bunch of different problems |
Beta Was this translation helpful? Give feedback.
-
I'll give you my take from all your recommended features: auto seedIn many applications (most of them), the auto seed feature is used to initially populate the database with default data during development. This feature aims to provide a good starting point for the application, ensuring that critical data is present right from the beginning. Automatically seeding the database every time the server is reset can lead to several problems, such as overwriting important production data (in a new feature that you working), mixing development and production environments, by applying by mistake local data to production. This can create inconsistencies, reduce control, and increase the risk of data loss, particularly in sensitive or high-traffic applications. That being said, if your application requires a different behavior, you can customize this by implementing your own approach in the async fn before_run(app_context: &AppContext) -> Result<()> {
// Option 1
Self::seed(
&app_context.db,
PathBuf::from("src").join("fixtures").as_path(),
)
.await?;
// Option 2
// Call any custom seeding function that you want
Ok(())
} seed by env (for example don't seed dev data in production)As shown in the example above, within the let seed_path = if app_context.environment == Environment::Development {
PathBuf::from("default")
} else {
PathBuf::from("production")
}
Self::seed(
&app_context.db,
PathBuf::from("src").join("fixtures").join(seed_path).as_path(),
)
.await?; seed by tagsYou can create a task named seed that accepts parameters indicating the type of seed you wish to apply. This allows for more granular control over what gets seeded. SummaryFrom my perspective, seeding data is most relevant for development or deploying dynamic applications that require initial data. It seems you have a very specific scenario. Loco allows you to handle this using the As a maintainer of Loco, one of my goals is also to help users avoid mistakes—seeding data can be a risky action if not handled carefully. Regarding #1060, I have created a PR that adds the app context in #1158. I don’t see any reason not to include it. |
Beta Was this translation helpful? Give feedback.
-
Hey,
since I created two prs regarding to improve the seeding system and a few questions came up I wanted to focus my motivation here.
My plan is to improve the seeding system to have something like symfony fixtures.
Currently it is only possible to seed via files which will fail if you need to generate data/ids or execute other code somehow.
So my first pr is to allow the execution of rust code.
My second pr was to pass AppContext to the seed function instead of just the db to allow more functionality to the user.
Further plans are:
This would immensely improve the dx and the whole fixtures/seeding feature of loco.
But it would also introduce a few breaking changes (change the file format of the seeding file to add the ability to add tags and env requirements and ofcourse the change from the DatabaseConnection to AppContext)
Ofcourse would be available to create prs and maintain the features in the future.
And ofcourse everything can be implemented by using the hooks but I think more users can take advantage of it which is why I am proposing it as a new feature.
CC: @kaplanelad
Ref issues:
seed
function inHook
should get AppContext as an argument. #1060Ref prs:
Beta Was this translation helpful? Give feedback.
All reactions