-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[Proposal] Adding the ability to automatically generate migration queries #980
Comments
This is semi-related to a feature we've discussed in the past, which is having a Rust DSL for migrations. If we do end up adding a secondary migration format, it'll almost certainly be that form and not JSON or YAML. At the moment this feature doesn't seem to add a lot of value. Database backends have tools for dumping their schema if you just want to generate a migration for an existing database. Being able to easily swap out backends is not a main goal of Diesel. |
What about pulling it from the model structs/enums? Could use |
@David-OConnor There is no such think like a model in diesel. There are table definitions, which could be generated from the database and there are query specific structs. |
I guess I don't understand why we need three separate, degenerate (In the physics sense) definitions for each database table, when we could use the struct (and diffs in it between migrations) to generate the other two. The normal DRY considerations apply: Having to keep them in sync, 3x the chance for a typo/error, updating friction etc. |
There are only two separate definitions, the table marco call and and your sql migration. The first one can be generated from the second one. |
Hi everybody, this is my proposal for the new feature of
diesel_cli
migration.What is the idea?
The basic idea is: Provide an ability to quickly modelling database schema when we start the new project with Diesel.
Why we need it?
As of now, we create a new project with Diesel and start modelling our database with the command:
After this step, the two new
.sql
files will be created inmigrations/XXXX_create_something_here
folder, and we have to write the actual SQL queries to create our tables.But why do we need to actually write the SQL queries in the first place? Why don't we just have the ability to automatically generate it from something?
What we want to do here is design the database schema, not writing lengthy queries.
What do you want to do?
So, instead of starting with generating migration, we will start defining our database schema in a text file. Let's say, in
yaml
orJSON
format:YAML version: db.yaml
JSON version: db.json
The example above illustrates the simple database with 2 tables: roles and users and their relationship.
The default field type should be
VARCHAR
, and you can specify your type with this syntax:The
db.yaml
ordb.json
file should be stored in project's root.After finished with your database schema, now it's time to
generate
the migration, with the newfrom <table_name>
argument:Then the magic happen,
diesel_cli
will generate the new migration folder with the automatically generated SQL queries from the schema and populate it intoup.sql
anddown.sql
files.For example:
XXX_create_roles/up.sql
How do you expect Diesel to support you with that?
I expect to have the new argument in
diesel_cli
:Which will read from
db.yaml
ordb.json
and generate the corresponding SQL query.How do you think this can be added to Diesel?
Since
diesel_cli
source code is very simple and straightforward, this feature can be added as a new module, for example,query_generator
.Then we can call it from here:
We also need to have the parser to read from the schema file as well.
For the
query_generator
, we need to handle the different in the data type between backend types. For example, in Postgres, we useSERIAL
but in MySQL, it should beINT
andAUTO_INCREMENT
.What are the possible alternatives?
I could not find any alternatives to my proposal.
Are there any disadvantages?
diesel_cli
source code.The text was updated successfully, but these errors were encountered: