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

Transforming an Entity into TableCreateStatement #59

Closed
tyt2y3 opened this issue Jul 27, 2021 · 5 comments · Fixed by #102
Closed

Transforming an Entity into TableCreateStatement #59

tyt2y3 opened this issue Jul 27, 2021 · 5 comments · Fixed by #102
Assignees

Comments

@tyt2y3
Copy link
Member

tyt2y3 commented Jul 27, 2021

pub async fn create_baker_table(db: &DbConn) -> Result<ExecResult, DbErr> {
let stmt = sea_query::Table::create()
.table(baker::Entity)
.if_not_exists()
.col(
ColumnDef::new(baker::Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(baker::Column::Name).string())
.col(ColumnDef::new(baker::Column::ContactDetails).json())
.col(ColumnDef::new(baker::Column::BakeryId).integer())
.foreign_key(
ForeignKey::create()
.name("FK_baker_bakery")
.from(baker::Entity, baker::Column::BakeryId)
.to(bakery::Entity, bakery::Column::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned();

To avoid duplication of information, we can actually generate a SeaQuery SchemaStatement entirely from the Entity definition.
It would be useful in 2 scenarios:

  1. writing test cases i.e. original model is MySQL but we want to test in SQLite. this is particularly helpful if the migration files are written in raw SQL. we effectively automatically translated from MySQL to SQLite!

  2. writing SQLite applications. in this case, apps don't need a separate migration tool. everything should be done by the app itself. so it's actually helpful to automatically bootstrap a SQLite database from an Entity file

@billy1624
Copy link
Member

Wow! neat new feature!!

@tyt2y3
Copy link
Member Author

tyt2y3 commented Jul 27, 2021

Not a high priority though. Definitely nice to have.
We actually form a full circle after this.

schema discovery -> schema statement -> entity -> schema statement -> database
^                                                                             |
|_____________________________________________________________________________

@billy1624
Copy link
Member

billy1624 commented Jul 27, 2021

First priority is sea-schema lolll

Postgres support!

@tyt2y3 tyt2y3 changed the title Transforming an Entity into SchemaStatement Transforming an Entity into TableCreateStatement Aug 14, 2021
@tyt2y3
Copy link
Member Author

tyt2y3 commented Aug 14, 2021

@scrblue Would you be interested in this task?

@billy1624 billy1624 assigned billy1624 and unassigned billy1624 Aug 23, 2021
@tyt2y3 tyt2y3 added this to the 0.2.0 milestone Aug 26, 2021
@tyt2y3
Copy link
Member Author

tyt2y3 commented Aug 26, 2021

Several action items:

  1. review & merge the PR
  2. make sure there are enough test cases
  3. update our documentation on https://www.sea-ql.org/SeaORM/docs/write-test/sqlite#setting-up-database-schema such that we can use the Entity directly, instead of constructing the schema manually

@tyt2y3 tyt2y3 linked a pull request Aug 30, 2021 that will close this issue
@billy1624 billy1624 self-assigned this Aug 31, 2021
@billy1624 billy1624 mentioned this issue Sep 3, 2021
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants