#database #development #devops #laravel #mysql #php #postgresql

At work, I recently had the need to use the Laravel migrations for multiple databases. Long story short, our main database is MySQL but we also use PostgreSQL as an AI vector store. I wanted to use the Laravel migrations to create the tables in both databases.

It turns out to be really straightforward to do this.

The first step is to ensure you have the database connections set up in your config/database.php file:

 1return [
 2    'default' => env('DB_CONNECTION', 'mysql'),
 3
 4    'connections' => [
 5        'mysql' => [
 6            'driver' => 'mysql',
 7            'host' => env('DB_HOST', ''),
 8            'port' => env('DB_PORT', ''),
 9            'database' => env('DB_DATABASE', ''),
10            'username' => env('DB_USERNAME', ''),
11            'password' => env('DB_PASSWORD', ''),
12        ],
13
14        'vectordb' => [
15            'driver' => 'pgsql',
16            'host' => env('DB_HOST', ''),
17            'port' => env('DB_PORT', ''),
18            'database' => env('DB_DATABASE', ''),
19            'username' => env('DB_USERNAME', ''),
20            'password' => env('DB_PASSWORD', ''),
21        ],
22    ],
23];

As you can see, the default one points to MySQL, the other one points to PostgreSQL.

Next, you need to create a new migration file. You can do this by running the following command:

1php artisan make:migration add_vectordb_tables

Then, in there, you can do this:

1Schema::connection('vectordb')->create('embeddings', function (Blueprint $table) {
2    $table->id();
3});

By using Schema::connection('vectordb'), you can specify which connection to use for the migration.

Finally, you can run the migration like this:

1php artisan migrate

The `migrations' table will be stored in the main database by the way.

You can read more about this in the Laravel documetation.