Seeding tenant database in Laravel 5 - php

I am working on building a system using a main database and multiple tenant databases in laravel 5. I have a database migration and seed for the main database no problem, using php artisan migrate:refresh --seed.
Tenants then get their own database upon registration. I need to run a migration and seed on the tenants database.
The tenant migration files are stored in a separate folder. The migration runs (unfortunately on the main database) with the following command
\Artisan::call('migrate', [
'--path' => "database/migrations_system"
);
However I need the migration to occur on the tenant database, say DB_1.
I read the following should work
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'db_1'
]);
However I end up with an error
InvalidArgumentException in DatabaseManager.php line 238:
Database [db_1] not configured.
Stuck.... How can I specify the migration to run on a specific database?
UPDATE:
I have found that changing config/database.php and adding db_1 gets me past this error....
'db_1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db_1',
'username' => env('DB_USERNAME', 'root'),
'password' => env('DB_PASSWORD', 'pass'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
],
However this connection is unknown and needs to be done on the fly, which I am also stuck on how to accomplish....
UPDATE Again and working solution.... Modify the config on the fly...
$connections = \Config::get('database.connections');
$tenant_database = 'db_1'; //assign from your main database
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $tenant_database ,
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections['tenant'] = $tenant_connection;
\Config::set('database.connections', $connections);
\Artisan::call('migrate', [
'--path' => "database/migrations_system",
'--database' => 'tenant'
]);
So it looks like I have answered my own question, however maybe some can comment on this procedure.

I'll put my own solution down....
1) Create the database name
For example I name the new database db_1 where 1 represents the ID of the system in the main database. I have considered storing a unique scrambled name in the main database. In the end that seemed like overkill.
2) Create the connection - for this I use the exact same name as the new database.
public function createConnection()
{
$connections = \Config::get('database.connections');
if(!isset($connections[$this->getSystemName()]))
{
$tenant_connection = [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => $this->getSystemName(),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'port' => '3306'
];
$connections[$this->getSystemName()] = $tenant_connection;
\Config::set('database.connections', $connections);
//dd(\Config::get('database.connections'));
}
}
3) Create the database, which you do using your main connection
$sql = "CREATE DATABASE " . $this->tenant_name;
DB::connection('main_db')->statement($sql);
4)run the migration
$r = \Artisan::call('migrate', [
'--path' => "database/migrations_tenant",
'--database' => $this->tenant_name
]);
5) finally you can seed test data or load some default data. Access the connection like
DB::connection($this->system_name)->table($table)->insert($csv);

Related

How to dynamically create multiple database in laravel

I am creating Laravel 6.6 project,
my query is how to create new multiple databases in laravel and then how to handle it,
if I fetch data from the new database that I was recently created then how to register a new database in .env file dynamically?
I am doing this the following way.
I do have a dummy entry for my client connections in my config/database.php like this:
'clientDb' => [
'driver' => 'mysql',
'host' => env('DB_CLIENT_HOST', '127.0.0.1'),
'port' => env('DB_CLIENT_PORT', '3306'),
'database' => env('DB_CLIENT_DATABASE', 'some_default_client_name'),
'username' => env('DB_CLIENT_USERNAME', 'root'),
'password' => env('DB_CLIENT_PASSWORD', ''),
'unix_socket' => env('DB_CLIENT_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
'dump' => [],
],
So, all my client-related models are used with $connection = 'clientDb';, the others using the default connection.
When changing a client (so i need to switch the connection), i just set the new connection by calling:
Config::set('database.connections.clientDb', [
'database' => NEW_DATABASE_NAME,
// all the other params from config
]);
After that, i figured out to call DB::reconnect('clientDb'); to get the connection really running.
I hope, this will lead you to the right direction 😉
You should usage from database.php in config directory. for example I have another mysql database which is different from main db:
so I add this code to database.php and connections section:
'mysql_second_db' => [
'driver' => 'mysql',
'host' => env('DB_HOST_SECOND_DB', '127.0.0.1'),
'port' => env('DB_PORT_SECOND_DB', '3306'),
'database' => env('DB_DATABASE_SECOND_DB', 'forge'),
'username' => env('DB_USERNAME_SECOND_DB', 'forge'),
'password' => env('DB_PASSWORD_SECOND_DB', ''),
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
'prefix_indexes' => true,
'strict' => true,
'engine' => null,
],
You can use that:
$names=['name1','name2'];
Schema::create('t1', function($table)
{
$table->increments('id');
});
To set env look at that: ENV

SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "migrations" already exists

Laravel Version: 5.1
PHP Version:5.6
Database Driver & Version: psql
Description:
I run php artisan migrate the first time, and it work perfectly fine.
Then:
I've added 1 more migration script to alter one of my table.
As soon as I ran php artisan migrate, I started to see this error.
I have this setting in my database.php
'default' => env('DB_CONNECTION', 'pgsql'),
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
],
Please kindly let me know what else, I can provide.
It is an old question but i faced this problem recently and it seems after copying my migration table from another database owner is changed.
So check your database and table owners and make sure tables owned by same user.
The actual solution is to append 'schema' => 'public', to the end of the psql DB_CONNECTION.
'pgsql' => [
'driver' => 'pgsql',
'host' => env('DB_HOST'),
'database' => env('DB_DATABASE'),
'username' => env('DB_USERNAME'),
'password' => env('DB_PASSWORD'),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'schema' => 'public', <------- ADD HERE
],
Now, php artisan migrate works perfectly fine.

Illuminate Database connectivity with mysql router

I'm using Laravel's illuminate database library outside laravel with jessengers mongodb.
My requirement is multiple database connectivity through illuminate database.
Currently, I've added two connection one mysql and one mongodb.
To split the database load, I need to connect to mysql router instead of mysql db server directly. Also, in that I want one only for Read operation and one for Read/Write operation.
Kindly help me out on this.
Thanks in advance.
Current connections
$db = new Capsule;
$db->addConnection([
'driver' => 'mysql',
'host' => '127.0.0.1',
'database' => 'test',
'username' => 'test',
'password' => 'test#123#',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "default");
$db->addConnection([
'driver' => 'mongodb',
'host' => '127.0.0.1',
'port' => 27017,
'database' => 'test',
'username' => null,
'password' => null,
'options' => []
], "mongodb");
$db->getDatabaseManager()->extend('mongodb', function ($config) {
return new Connection($config);
});
$db->setEventDispatcher(new Dispatcher(new Container));
$db->setAsGlobal();
$db->bootEloquent();
I need to replace one mysql connection with two mysql connection for Read and Read/Write operation through mysql router.
You can define read/write options separately with either mysql host or mysql router host and port
$db->addConnection([
'driver' => 'mysql',
'read' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'write' => [
'host' => '<mysql_router_host_ip>',
'port' => '<mysql_router_host_port>'
],
'database' => '<mysql_database>',
'username' => '<mysql_database_user>',
'password' => '<mysql_database_password>',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
], "mysql");

Laravel 5.2 Database [xxx] not configured, while being defined in database.php

So I have 3 mysql DB's defined in my database.php file. I had 2 for the longest time and everything worked fine. I have one titled mysql, one mysql1 and one mysql2. The first 2 work fine, I copied the second one, changed the name to mysql2 and added the proper parameters but it still always says "Database mysql1 not configured. Is there some sort of cache? or somewhere else I have to define it? I am calling the DB through a model like this.
protected $connection = 'mysql1';
This is my database.php for clarification
'mysql' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql1' => [
'driver' => 'mysql',
'host' => 'localhost',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'mysql2' => [
'driver' => 'mysql',
'host' => 'localhost',
'port' => '3306',
'database' => '',
'username' => '',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
'engine' => null,
],
This was fixed by using the command
php artisan config:cache
I submitted a Laravel issue request to fix it since that absolutely should not be required to change the DB connections variable.
If you can not delete cache with php artisan config:cache because it throws error after you run it:
[InvalidArgumentException]
Database [] not configured
And if you are sure that your connections parameters are good then try to manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder.

how to create a PHP file on the server with dynamic input

I am using Laravel and I created a multi-tenant app using the database connections, now I need to be able to create the contents of the database.php automatically with my chosen input.
Sample of database.php:
'connections' => [
'db1' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db1',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
],
'db2' => [
'driver' => 'mysql',
'host' => env('DB_HOST', 'localhost'),
'database' => 'db2',
'username' => env('DB_USERNAME', 'forge'),
'password' => env('DB_PASSWORD', ''),
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
'strict' => false,
]
]
So basically I will have a form that will ask those required fields like db name, connection name, password, etc. My problem is that I dont have a fixed amount of connections, could be one, two, four, w/e and file_put_contents() requires a string not an array of data. So my question is how to create this database.php file with the input from my form, create the opening and closing bracket and everything needed? Is there a function that will convert an Array to a String retaining the brackets and operators?
How can I achieve this?
If you want to go down on that road, make sure you have a template file, and you can setup placeholders that you simple replace.
Your sample template file would hold all requirements + placeholders for each info you need.
'db{index}' => [
...other key+values and it's placeholders...
],
And simply you can loop and concat as many times this block in your final database.php file.
on the other hand you could do this on the fly, some snippet to guide you.
$nameKey = 'NameOfConnection';
$dataSync = AtlasBases::find($id_client);
Config::set('database.connections.' . $nameKey, array(
'driver' => 'mysql',
'host' => $dataSync->site_db_server,
'database' => $dataSync->site_db_base,
'username' => $dataSync->site_db_user,
'password' => $dataSync->site_db_password,
'charset' => 'utf8',
'collation' => 'utf8_general_ci',
'prefix' => '',
));
# And set the new connection to my models
$imobModel = new ImobImoveis;
$imobModel->setConnection($nameKey);

Categories