Conversation
|
In terms of design:
Other: I think the migrations CLI output can be improved slightly in terms of clarity NOT READY FOR MERGE THOUGH |
|
oh wow, looks promising! |
|
This is going to take longer than expected. I underestimated implementing proper change discovery phase |
| #[arg( | ||
| long, | ||
| default_value = "true", | ||
| help = "Generate migration file based on Utc time", | ||
| conflicts_with = "local_time", | ||
| display_order = 1001 | ||
| )] | ||
| universal_time: bool, | ||
|
|
||
| #[arg( | ||
| long, | ||
| help = "Generate migration file based on Local time", | ||
| conflicts_with = "universal_time", | ||
| display_order = 1002 | ||
| )] | ||
| local_time: bool, |
There was a problem hiding this comment.
Could we merge this into a single option, like filename-timezone / migration-timezone?
There was a problem hiding this comment.
Yes, I thought the same. just copied this part from original migrator.
| let url = cli | ||
| .database_url | ||
| .expect("Environment variable 'DATABASE_URL' not set"); | ||
| let schema = cli.database_schema.unwrap_or_else(|| "public".to_owned()); |
There was a problem hiding this comment.
Yes! again, just copied this part from original migrator.
| // Future: when --ephemeral is added, build an in-memory db here instead, | ||
| // apply existing migrations via the migrator, then pass it to generate_from_db. | ||
|
|
||
| if let Err(e) = generate_from_db( |
There was a problem hiding this comment.
Too many parameters. Use a struct instead.
| .to_owned(), | ||
| ) | ||
| .await | ||
| .expect("Failed to connect to database"); |
There was a problem hiding this comment.
I think it would be better to expose the reason to the user.
There was a problem hiding this comment.
Yes, same thoughts.
again, just copied this part from original migrator.
| let builder = entity_set.register(schema.builder()); | ||
|
|
||
| println!("Discovering schema changes..."); | ||
| let raw = builder.discover(&db, dangerous).await?; |
There was a problem hiding this comment.
The idea, was that it was unprocessed changes, will change
| { | ||
| let _existing = Self::discover_existing_schema(db).await?; | ||
|
|
||
| #[allow(unreachable_code)] |
There was a problem hiding this comment.
Why so many #[allow(unreachable_code)]?
There was a problem hiding this comment.
copied from original. I just decided not to think about it. I the new discover() I removed them.
| let in_entities = self | ||
| .entities | ||
| .iter() | ||
| .any(|e| get_table_name(e.table.get_table_name()) == existing_name); |
There was a problem hiding this comment.
Rename get_table_name to something more semantic.
There was a problem hiding this comment.
Okay, but I don't see a problem.
| // and generate drop statements for them. | ||
| #[allow(unreachable_code)] | ||
| if allow_dangerous { | ||
| for existing_table in &_existing.tables { |
There was a problem hiding this comment.
How will this handle multiple schemas?
There was a problem hiding this comment.
I did't cover multiple schemas. as most of the sea architecture doesn't support them as well
There was a problem hiding this comment.
Okay, leave a TODO comment here so we don't forget to handle this during future refactoring.
There was a problem hiding this comment.
Don't worry I am doing rewrite of discover() since yesterday.
| fn discover_changes( | ||
| &self, | ||
| db: &C, | ||
| db_backend: DbBackend, | ||
| existing: &DiscoveredSchema, | ||
| created_enums: &mut Vec<Statement>, | ||
| ) -> Result<(), DbErr> { | ||
| let db_backend = db.get_database_backend(); | ||
|
|
||
| changes: &mut Vec<Statement>, | ||
| allow_dangerous: bool, |
| .table( | ||
| self.table | ||
| .get_table_name() | ||
| .expect("Checked above") |
|
Thanks for feedback! I didn't have experience developing CLI in rust before. I just wanted to say that I mostly wanted to discuss (#3015 (comment)) the integration with existing code and not get exact code review, as this first draft for a reason |
|
Oh, I got confused. I thought you wanted me to review the code above. As for the refactor, I think we can merge it back into the CLI crate first, and then see how it goes. For the more significantly changed parts, feel free to refactor them, since that should make the diff clearer. For the rest of the architecture, I agree with calling I think a better approach is to update the migration crate so that Ideally, both should be implemented. |
PR Info
Goal
Safe, production ready migration workflow.
PR Todo
CLI
--previewoption for generate ( discovery without creating the migration)Core
generate_migrationdb.execute_unpreparedwith timestamped filenameNOT NULLcolumn obviously would require additional input from the developer)Integration into the rest of codebae
migration/src/lib.rsto register the new migrationQuality
Out of scope (probably)