cache/flush-schema command for specific database connection in yii2-advanced - php

I'm running the yii cache/flush-schema command on the root of yii-advanced project but it's working on only one and default db connection. Whereas, the application is having multiple database connections and I need to run the command on specific database.

Running the command with specifying the database connection name in it, would do the job, as:
yii cache/flush-schema db_connection_name

Related

Laravel keeps connecting to old database connection

Context :
I have DB1 and DB2 and a laravel application using DB1 so far
I want now that laravel (8) application to use DB2 instead (both DB1 & 2 are mysql)
I edited the .env file to change the DB_HOST and cleared the config cache
Expected : Laravel using DB2
Problem : Laravel still connects to DB1
Things I've tried :
php artisan config:cache / clear
php artisan optimize:clear
restarted DB1/DB2
restarted nginx
restarted php-fpm
redeployed the whole app (with composer install etc.)
tried adding a new connection in config/database.php instead of editing the existing one : same issue
hardcoding the values in config/database.php instead of referencing the env file : same issue
php artisan db sql --> connects to the RIGHT DB (this is what's driving me insane)
... but php artisan tinker doesn't seem to. I created a dummy table in DB2 only (so not present in DB1) and getting that table with \DB::connection('mysql')->table('dummy')->get(); shows an error via tinker
obviously, stopping DB1 makes the application go offline ("No such file or directory" blabla ie. no database PDO)
I don't even know what to try anymore. Every post somewhat related online are solved after a simple artisan config:clear/cache ....
Any thoughts appreciated ?
The answer was DB_SOCKET=/tmp/mysql.sock in my .env file that locked the connection to the previous (local) DB. Removing that from my env file solved the issue
to change the database you need to write the database name on DB_DATABASE instead on DB_HOST...

Laravel - Catch php artisan commands

I've did some changes in my config/app to use multiple databases selected by front-end,
now I have to tell in \Request()->header('database') which database I want access.
It's work perfectly, the problem is: when I try to do any artisan commands my logic dies, because isn't informed the database.
So I need to inform the database in artisan commands, like that:
php artisan migrate --database=sandiego_school
php artisan migrate:rollback --database=newyork_school
How can I observer all commands to get the argument?
In this case I guess you should create your own commands that overrides the commands you want to call, then in the handle method of the command you could specify the connexion you want to work on:
\DB::setDefaultConnection($connexion);
or also you can simply add header to request:
request()->headers->set('database', $dbname)

Restore a postgresql dump (.sql file) without the command line?

Scenario:
I have built a PHP framework that uses a postgresql database. The framework comes shipped with a .sql file which is a dump of the default tables and data that the framework requires.
I want to be able to run the sql file from the client (PHP), rather than the command line, in order to import the data. This is because I have come across some server setups where accessing the command line is not always a possibility, and/or running certain commands isn't possible (pg_restore may not be accessible to the PHP user for example).
I have tried simply splitting up the .sql file and running it as a query using the pg_sql PHP extension, however because the dump file uses COPY commands to create the data, this doesn't seem to work. It seems to be that because COPY is used, the .sql file expects to be imported using the pg_restore command (unless I am missing something?).
Question:
So the question is, how can I restore the .sql dump, or create the .sql dump in a way that it can be restored via the client (PHP) rather than the command line?
For example:
<?php pg_query(file_get_contents($sqlFile)); ?>
Rather than:
$ pg_restore -d dbname filename
Example of the error:
I am using pgAdmin III to generate the .sql dump, using the "plain" setting. In the .sql file, the data that will be inserted into a table looks like this:
COPY core_classes_models_api (id, label, class, namespace, description, "extensionName", "readAccess") FROM stdin;
1 data Data \\Core\\Components\\Apis\\Data The data api Core 310
\.
If I then run the above sql within a pgAdmin III query window, I get the following error:
ERROR: syntax error at or near "1"
LINE 708: 1 data Data \\Core\\Components\\Apis\\Data The data api Core...
This was a bit tricky to find, but after some investigation, it appears that pg_dump's "plain" format (which generates a plain-text SQL file) generates COPY commands rather than INSERT commands by default.
Looking at the specification for the pg_dump here, I found the option for --inserts. Configuring this option will allow the dump to create INSERT commands where it would normally create COPY commands.
The specification does state:
This will make restoration very slow; it is mainly useful for making dumps that can be loaded into non-PostgreSQL databases. However, since this option generates a separate command for each row, an error in reloading a row causes only that row to be lost rather than the entire table contents.
This works for my purposes however, and hopefully will help others with the same problem!

how to custom connect the database from yii

Im trying to connect the db in yii connection how can i do that ?
In below screen shot there is an cron.php i have trying to connect the database from root folder how to d that ?
Cron.php
require('framework/db/CDbConnection.php');
$connection=new CDbConnection($dsn,'root','');
$connection->active=true;
I need to use Yii connection instead of mysql
$mysql_con = mysql_connect('localhost','root','');
mysql_select_db('som',$mysql_con); //local DB
If you want to use cron for some tasks, consider creating yii console application commands. Configure it to use your db settings and call tasks that you need like that yiic <command-name> <action-name>

creating mysql db from exported sql with php exec

All I want to do is create a new database off of a dump created by phpmyadmin.
This dump is located in : /var/www/iadmin/wikifresh/db/template.sql and won't ever change.
I ran this command once, and the database was created fine
mysql test < /var/www/iadmin/wikifresh/db/template.sql
and it created the database. So, I put that script inside of a php exec command:
(note: $wikiname is the name of the new wiki being created)
$dbwikiname = escapeshellarg($wikiname);
exec("mysql $dbwikiname < /var/www/iadmin/wikifresh/db/template.sql");
now, when this script runs, I get:
ERROR 1049 (42000): Unknown database 'test'
even if I try to run it from my command line I have this issue.
what am I doing wrong?
For mysql to be invoked with a default database name, the database must exist.
Some distributions of MySQL happen to ship with a database called test, which explains why your command succeeded at first. One presumes you have since dropped that database? You will need to recreate it before attempting to select it:
CREATE DATABASE IF NOT EXISTS test;
Of course, you could instead place the above command, followed by USE test; atop your template.sql file and then invoke mysql without specifying a default database.
it seems you don't have the database 'test' created in the MySQL server of that machine, and the template.sql doesn't have the 'CREATE DATABASE' command. So you have to either create it first, or add the 'CREATE DATABASE' to the template.sql

Categories