Relying heavily on migrations I need to create an additional user with readonly permissions to my database web in a yii2 migration.
Is this possible since Yii2 does not offer to create users directly in a Migration? How can this be done?
I tried this SO answer Crate a new, read-only user in postgres but did not succeed to transfer it to Yii2.
Yes! It is possible, you have to use the createCommand method and use raw SQL:
$user_created = Yii::$app->db
->createCommand("CREATE USER :db_username#'%' WITH PASSWORD :db_userpwd")
->bindValue( ':db_username', $db_username )
->bindValue( ':db_userpwd', $db_userpwd )
->execute();
Related
I want to create authentication apis in Hasura. My user can have differrent roles when signing up. Thinking of maintaining an Enum table for the same. So that I can have a foreign key/type from it in the user table. However, I intend to create a postgress trigger on this enum table, such that everytime, new role is added, a new hasura role should also be created to allow for JWT authentication and authorization accordingly.
Where does hasura stores its Hasrua role.
Answer 1 (direct answer)
Not sure this is something the app developer should edit.
All Hasura metadata (including roles/permissions) is in Postgres.
The schema is "hdb_catalog". The table is "hdb_metadata".
You can query this using:
SELECT * FROM "hdb_catalog"."hdb_metadata" WHERE id = 1;
It contains a large JSON document. It's better to look at it using PGAdmin.
Answer 2 (dynamic roles)
It looks like you're trying to get dynamic roles in place.
There is a great Youtube video that explains how to model it:
https://youtu.be/-18zZO3DrLY?t=1370
I use 'doctrine/DoctrineORMModule' module for zend framework 3 (MVC). I have configured 'orm_default' and can configure 'orm_old' but don't know how to use 'orm_old' within migration file.
I can do this within migration file:
public function up(Schema $schema) : void
{
$sql = "INSERT INTO `some_table` VALUES ('some_value','','',NULL,NULL,'1');";
$this->addSql($sql);
//...
But in general I need run something like this:
INSERT INTO DB2.T2(id, title, description)
SELECT id, title, description FROM DB1.T1;
How to do that?
If I understand correctly, you'd like to use Doctrine Migrations for two database connections: orm_default and orm_old.
This is possible in Doctrine, but not with the Zend Framework DoctrineORMModule. This is mentioned very shortly in the official documentation: https://github.com/doctrine/DoctrineORMModule/blob/master/docs/migrations.rst#multiple-migration-configurations
The best thing you can do is to use two separate cli-config files with the same database connections, and put the migrations in two seperate folders. You can then use the 'default' doctrine CLI tools (vendor/bin/doctrine migrations:migrate ) to run migrations for the two connections.
Adding this functionality to the DoctrineORMModule was requested, but never implemented. You can read more about it right here:
https://github.com/doctrine/DoctrineORMModule/issues/537
What's the best approach to create a database in EF core programmatically and then run the migrations to make sure the new database has all database objects?
We're building a SaaS based application where subscribers can signup on the website themselves and when they signup providing correct payment details, the application needs to create a new database (in SQL Azure) for the subscriber automatically and execute all migrations on the new database.
I have looked at this question here:
auto create database in Entity Framework Core
It does not give details on the following:
1) how to specify the name of the new database
2) create a new database login
3) add that new database login to the database as a user with dbo permissions
Create an Azure SQL Database Basic tier (for example, it cost $5 a month, you can also create a free one available for a year) with all the objects needed on the database, which you can copy asynchronously with a statement like below or using PowerShell.
CREATE DATABASE db_copy
AS COPY OF ozabzw7545.db_original ( SERVICE_OBJECTIVE = 'P2' );
You can then monitor when the copy finishes with below statement:
Select
[sys].[databases].[name],
[sys].[databases].[state_desc],
[sys].[dm_database_copies].[start_date],
[sys].[dm_database_copies].[modify_date],
[sys].[dm_database_copies].[percent_complete],
[sys].[dm_database_copies].[error_code],
[sys].[dm_database_copies].[error_desc],
[sys].[dm_database_copies].[error_severity],
[sys].[dm_database_copies].[error_state]
From
[sys].[databases]
Left
Outer
Join
[sys].[dm_database_copies]
On
[sys].[databases].[database_id] = [sys].[dm_database_copies].[database_id]
Where
[sys].[databases].[name] = 'db_copy'
I think the question speaks for itself. I have a fairly typical case where I've created a new entity class on which I've specified the [Table] attribute. The Add-Migration command has generated the corresponding DbMigration.CreateTable, etc.
However, I would like to ensure that the table is create in SQL Server with the select permission assigned to group... let's call it ABCGRP.
Can this be done via attributes, the Fluent API or will I need to simply create a SQL script with a GRANT operation?
Does Zend Framework provide an elegant way to check if the username already exist in the database?
Or is my only option to code a validator using a combination of php if/else and mysql select statements?
You can add a validator which will check in database if username already exist, if so it will notice the user. I am assuming you are using Zend_Form and Zend_Db_Table with at least one default db table database.
For example:
$element = new Zend_Form_Element_Text('username');
$element->setLabel('User:')
->addValidator(new Zend_Validate_Db_NoRecordExists('user', 'username'))
->setRequired(true);
$this->addElement($element);
You will pass 2 parameters to validator, first is table name and second is the column you want to check.
That's it!