Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign up[Proposal] Adding the ability to automatically generate migration queries #980
Comments
This comment has been minimized.
|
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. |
sgrif
closed this
Jul 4, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
huytd commentedJun 30, 2017
•
edited
Hi everybody, this is my proposal for the new feature of
diesel_climigration.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
.sqlfiles will be created inmigrations/XXXX_create_something_herefolder, 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
yamlorJSONformat:YAML version: db.yaml
JSON version: db.json
{ roles: [ { id: 'key' }, 'name', 'code' ], users: [ { id: 'key' }, 'username', 'email', 'passwod', { enabled: 'int' }, { role: 'roles' } ] }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.yamlordb.jsonfile should be stored in project's root.After finished with your database schema, now it's time to
generatethe migration, with the newfrom <table_name>argument:Then the magic happen,
diesel_cliwill generate the new migration folder with the automatically generated SQL queries from the schema and populate it intoup.sqlanddown.sqlfiles.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.yamlordb.jsonand generate the corresponding SQL query.How do you think this can be added to Diesel?
Since
diesel_clisource 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:
let migration_name = args.value_of("MIGRATION_NAME").unwrap(); + let table_name = args.value_of("FROM_TABLE").unwrap(); ... - up.write_all(b"-- Your SQL goes here").unwrap(); + up.write_all(query_generator.create_table(table_name)).unwrap(); ...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 useSERIALbut in MySQL, it should beINTandAUTO_INCREMENT.What are the possible alternatives?
I could not find any alternatives to my proposal.
Are there any disadvantages?
diesel_clisource code.