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
yii2 migration code style #3335
Comments
I think this would be usefull, some time ago i was discussing same idea with @samdark ) However i think that something like L4 or Rails approach here can be used . |
@Ragazzo |
@Asetss plz do that would be wonderfull ! |
|
I like the new approach. 👍 for me |
@samdark this is just an example. integer(intlen), string(strlen) etc,I think you understand. There such a thing if the other will look like a function and he as a static variable. |
@Asetss 👍 - would be nice if it is standardized for all column types... |
@kartik-v I think it will be so. Solution for samdark. So we are waiting :] |
I don't mind implementing it but since we're now focused very much on docs and bugfixes, it will probably wait for 2.1... or someone will make a pull request and I'll review/merge it. |
@Asetss if you will be implenting this please note that it should be something like in Rails or L4, so it should not be just a several methods in |
@Asetss let know ... I can work on a PR jointly if needed... taking some cue from L4.. |
@kartik-v I can help, I have the time not so much. We have to see how they are arranged on the L4. |
Maybe like this: $this->createTable('{{%users}}', [
Schema::field('id', Schema::TYPE_PK),
Schema::field('username', Schema::TYPE_STRING, 100, true),
Schema::field('status_id', 'tinyint', true, 0),
], $tableOptions);
public static function field($name, $type, $size=null, $notNull=false, $default=false); |
@gonimar nope, 4 or 5 params to method is wrong, Rails and L4 approach is better and more easy to read. However i dont see any difficulties on how to do this ) |
Do we really need methods for this? If you do not like writing of constants you can simply write strings which is much more readable imo: $this->createTable('{{%users}}', [
'id' => 'pk',
'username' => 'string(30) NOT NULL',
'email' => 'string(100) NOT NULL',
'password_hash' => 'string NOT NULL',
'role_id' => 'tinyint NOT NULL DEFAULT 0',
'status_id' => 'tinyint(4) NOT NULL DEFAULT 0',
], $tableOptions); What are the benefits of a new abstraction syntax? |
Support different databases. |
The type names are already being translated dependend on the database. Can you give an example of where 'NOT NULL' or 'DEFAULT ...' is different in different dbms? |
Unlikely |
so again, why do we need this feature then? :) |
@cebe the benefit is less concatenation and better autocomplete. Also it's possible to improve it further like the following: $this->createTable('{{%auth_item}}', [
'name' => Schema::string(64)->notNull(),
'type' => Schema::integer()->notNull()->default(10),
'description' => Schema::text(),
'rule_name' => Schema::string(64),
'data' => Schema::text(),
'created_at' => Schema::integer(),
'updated_at' => Schema::integer(),
])
->primaryKey('pk-auth_item', 'name')
->foreignKey('fk-auth_item-rule_name', 'rule_name', '{{%auth_rule}}', 'name', 'SET NULL', 'CASCADE')
->index('idx-auth_item-type', 'type'); |
This is more probably an aid to developer (as @samdark's code example points out) to reduce the typos when creating the migration code which can result when typing raw strings or concatenating constants with text. This will not be known until the migration is tested/run. Some of the common errors could be:
Using a function approach like above with an IDE... may help draft the column structure faster using autocomplete and with less coding typos IMO - since the errors may be known immediately through the IDE when coding itself. Of course as @cebe pointed out there are similarities to some extent... both of the above achieve the same thing in different ways - I feel this latter option just helps the developer minimize coding errors. |
@samdark I like. Only scares line ->primaryKey('pk-auth_item', 'name')
->foreignKey('fk-auth_item-rule_name', 'rule_name', '{{%auth_rule}}', 'name', 'SET NULL', 'CASCADE')
->index('idx-auth_item-type', 'type'); foreignKey type so,cascade etc ->foreign('user_id')->cascade(); index ->index('type'); I do not understand your last line of code |
@Asetss can't be done since we're not fixing the order in which the chain parts are specified. |
Also one thing should be noted is that we should avoid usage of direct |
@samdark how are things? I think it is time to do this. Migration code looks awful ... |
It's not showstopper so I'll come back to it after I'll deal with all the things for RC. Feel free to work on it if you want it faster. |
Would love to see it become real. It is really much easier to code and read. |
Some highlight in migration output will also be useful , just a hint |
is it also possible to generate migration code automatically? |
I found this to be useful with Rails. $ bin/rails generate scaffold HighScore game:string score:integer $ bin/rails generate model $ bin/rake db:migrate Source : http://guides.rubyonrails.org/command_line.html#rails-generate |
Yes, that's cool but the issue is not about it. If you want such syntax to be supported by migrate command, create additional issue. |
Hi, I'm trying to implement this issue, that helpers should be implemented,
Another helpers? it should be support helpers especific dbms (e.g. Schema::enum(['Monday', 'Thursday']) for mysql)?¿ any idea or tip? |
For now only common types should be implemented. |
In yii2 a great migration,on there lame coding style.
And an example of a new style of coding
Is an example of what I mean. What do you think about this?
The text was updated successfully, but these errors were encountered: