I am building a product that is based on the Yii2 advanced template.
As part of this product and its future deployments, I am trying to automatically create the tables related to Authorization in a regular Yii2 migration.
E.g, when the end user installs the product and runs the regular Yii migration commands he should have a fully functional user management AND authorization active.
For authorization to work, the Yii2 RBAC documentation page states that 4 tables are needed (auth_*). The documentation states that they are created by running the following migration:
yii migrate --migrationPath=#yii/rbac/migrations
I'd like to offset this extra hassle from the end user by running this specific migration code for him inside a regular migration that will be stored in common/migrations.
Any easy solution for this?
I have created a migrate.sh file where I put my migration commands that I need to run. This allows me to migrate from multiple places in the same time. It is quite simple, take a look here: https://github.com/Mihai-P/yii2-app-advanced/blob/master/migrate.sh
Instead of running ./yii migrate/up i just run sh migrate.sh that will update everything from any place.
The actual point of this is: you do not have to stick to exactly what Yii gave you. That is just a template for you to build on. Fork it, modify it, make it your own.
Try to add in console/config/main.php:
'controllerMap' => [
'migrate' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => [
'#console/migrations',
'#yii/rbac/migrations',
]
]
],
Another approach (not using *.sh file) is to copy the rbac_init migration to your migrations folder:
cp vendor/yiisoft/yii2/rbac/migrations/m???????_rbac_init.php console/migrations/
Now, when you run php yii migrate it will be included the rbac_init migration.
I know this is pretty old question, but here is easy solution, create migration file and have this code in that file
<?php
require(Yii::getAlias('#yii/rbac/migrations/m140506_102106_rbac_init.php'));
/**
* Class m220225_133725_init_rbac
*/
class m220225_133725_init_rbac extends m140506_102106_rbac_init
{
}
Related
I'm able to create a new service which creates a table in the MySQL DB just fine, but the feathersJS model file simply creates 1 text field in the model by default,
when I modify the fields add more etc...this does not reflect in the database.
Is there a migration script i must run ? can someone show me how to run a command that will reflect the changes i make to the model so I can sync the two.
right now I have to manually modify the table to match the model.
In your src/sequelize.(js|ts), there should be a call like sequelize.sync(). This updates the structure of the database according to the model definitions every time the app gets started. So in theory, restarting the app should be enough.
If that doesn't work, try .sync({ alter: true }) or even .sync({ force: true }). (This might be fine during initial development, but is not recommended for production use as it can result in data loss. See also the docs about .sync() options.)
You already mentioned Migrations, which would be a better alternative for sure. With Umzug they can be automatically executed during startup as well.
The CakePHP docs on using the FormHelper suggest I should be able to create new templates for its output once, and use this throughout my project.
As described it requires modifying the loading of the FormHelper, from this:
$this->loadHelper('Form');
to this:
$this->loadHelper('Form', [
'templates' => 'app_form',
]);
where app_form refers to a file in the config directory. However, I haven't had to actually load the FormHelper at all, it's magically available already, and if I try to do the above I get an error to the effect the helper has already been loaded:
The "Form" alias has already been loaded with the following config: array (...
Don't know if the docs are out of sync with the current version of CakePHP, but I can't see how to use this functionality - anyone know?
I've played with Laravel and love the database migration system.
I also work a lot with wordpress and wonder if there is a similar way to describe database changes in text files that can be added to git and have those changes push up to a remote version of the db in staging or production environments.
Perhaps the local machine could connect to the remote mysql db and make the changes automatically.
There must be something but i cant find anything through google or other routes.
The closest i've found is http://dbv.vizuina.com/ but im not sure its quite the same thing.
Anyone have any tools or suggestions?
I will recomend you to use lib https://phinx.org/. We are using this lib in our company and we are quite with that.
You can install this library using composer and configure using php files(is an option - good to include different config files - e.g. from wordpress). The big advantage of this is that you write your migration in php code. You just have to define method up(make migration) and down(to rollback - you can do it to the selected stage). Look at an example below:
<?php
use Phinx\Migration\AbstractMigration;
class AddColumnsToVisitorTable extends AbstractMigration
{
public function up()
{
$table = $this->table('visitor');
$table->addColumn('send_at', 'datetime');
$table->addColumn('visitor_email', 'string');
$table->addColumn('visitor_source', 'string');
$table->update();
}
public function down()
{
$table = $this->table('visitor');
$table->removeColumn('send_at');
$table->removeColumn('visitor_email');
$table->removeColumn('visitor_source');
$table->update();
}
}
If you merge changes from develop branch to master you also merge migration. After merge we have deploy script which run migrations on live env. so you are sure that with your code you have changed database.
Liquibase's changesets. Doesn't solve problem of rolling back history, bring big headache on merges - but document all data- and structure-changes
I need to setup demo sites quickly with demo data, including hourly reset of data for a public demo site. Since our data uses timestamps relative to "now" (e.g. archived_timestamp), we cannot just restore an sql dump with fixed timestamps.
My idea is to use Yii2 migrations for that task with PHP code generating timestamps and inserting the demo data.
How to achieve that?
Are Yii2 migrations the right tool for that?
Is it recommended to store the migration file in a seperate subdirectory that our demo setup does not interfere with ordinary "migrate/up" and "migrate/down" processes?
Is this migration bound to a file naming scheme or can this be e.g. demo-data-setup.php ?
Are Yii2 migrations the right tool for that?
Could be if you need a proper sequence of sql command and instruction for create and populate a specific set of table and data you can use the funtcion up for creation an popluation and the function down for drop delete (or delete) what you need.
I*s it recommended to store the migration file in a seperate subdirectory that our demo setup does not interfere with ordinary "migrate/up" and "migrate/down" processes? Of course
Is this migration bound to a file naming scheme or can this be e.g. demo-data-setup.php ? In yii2 (but also in the other migration tools ) the migration file are related to a proper template, tipically datetime_migration_name.php
But for my experience for a proper and recurrent create/populate and drop/update/delete could be useful in some situation use a controller, especially if these activities are to be launched by web page or a URL without having to launch console commands use a controller with the appropriate action can be even up and down and possibly a view to an appropriate echo the results of operations
Yes, you could assign command name using controller map, configure migration path & table and use it without interfering with the original migration command.
In the console app's config, add a controller map with demo
'controllerMap' => [
'demo-setup' => [
'class' => 'yii\console\controllers\MigrateController',
'migrationPath' => '#common/migrations/demo',
'migrationTable' => '{{%demo_setup_migration}}'
],
cd to your console app directory and use demo-setup command as you would use migrate command.
./yii demo-setup/create schema
./yii demo-setup/create sample-data
./yii demo-setup
You may wish to setup a cron job to re-create db and apply migration or re-apply migration to existing db to override demo data.
Are Yii2 migrations the right tool for that?
As long as you store your demo migrations in a separate location than your main migration. To be honest, when I start a new Yii2 project I always start with creating migrations for demo data (I also create the first user with migration). I usually use faker, and some of my own classes to generate demo data.
Is it recommended to store the migration file in a seperate subdirectory that our demo setup does not interfere with ordinary "migrate/up" and "migrate/down" processes? Yes, you should!
Is this migration bound to a file naming scheme or can this be e.g. demo-data-setup.php? From the official Yii2 documentation about naming migrations:
Note: Because the name argument will be used as part of the generated migration class name, it should only contain letters, digits, and/or underscore characters.
Ps.: don't overengineer your projects
Are Yii2 migrations the right tool for that?
No, migrations are for database structure changes (add a column, set index,..) more than fill tables with data. In your case, I would write a component, that have delete / create function for every model you need to restore. Then, you can call your component with a cron task.
You can use fixtures.
Fixtures are an important part of testing. Their main purpose is to set up the environment in a fixed/known state so that your tests are repeatable and run in an expected way. Yii provides a fixture framework that allows you to define your fixtures precisely and use them easily both when running your tests with Codeception and independently.
I have created a new MySQL DB app through ruby console giving below command
rails new -demo -d mysql
Then I created 1 table through MySQL editor. In Ruby Console I started rails server and its working fine (checked localhost:3000). Now I am unable to create controller class for this table. I tried many commands given on net but nothing seems to be working.
From Ruby command console I switched to rails console giving below command
rails console
Then I entered below command to create controller class which in turn returned nil
irb(main):003:0> class CategoryController < ApplicationController
irb(main):004:1> def index
irb(main):005:2> end
irb(main):006:1> def show
irb(main):007:2> end
irb(main):008:1> end
=> nil
But no such class got created in app/controller folder of my application.
I read couple of tutorials and it was referred that controller classes gets automatically created in rails. I tried those too but they didn't run.
I am not sure if I am missing something. Could someone please help to guide further steps. I am using ROR only on server side. My Android app will be using this DB.
It will be really helpful if somebody can provide relevant Beginner tutorial or sample examples using ROR for server side coding with MySQL.
Thanks.
The Rails console is used to test code at runtime, not to generate code which is actually stored in files to be used again. The correct way to generate a controller is via the rails generate controller command from your system's command line (not from the Rails console or irb):
This will create your CategoryController file with two actions, index, show. You may omit those actions or add additional actions.
rails generate controller CategoryController index show
This will result in output similar to what's below, assuming all your gem dependencies are correctly met.
create app/controllers/category_controller_controller.rb
route get "category_controller/show"
route get "category_controller/index"
invoke erb
create app/views/category_controller
create app/views/category_controller/index.html.erb
create app/views/category_controller/show.html.erb
invoke test_unit
create test/controllers/category_controller_controller_test.rb
invoke helper
create app/helpers/category_controller_helper.rb
invoke test_unit
create test/helpers/category_controller_helper_test.rb
invoke assets
invoke js
create app/assets/javascripts/category_controller.js
invoke scss
create app/assets/stylesheets/category_controller.css.scss