Seeding in laravel for multiple databases - laravel

I am working on a school project with multiyear database built in laravel.
My requirement is to feed data for every new academic year in some tables. I have a main (superadmin) db and separate school's db for every school. I need to connect to school db, n process is i have kept superadmin db details in .env file then it fetches particular school's db details and makes a connection to that school db through middleware.
My question is when i execute migration and seeding command it connects to superadmin db and performs respective operation. But i want to execute migration/seeding one by one for every school's db.

Add a extra entry into config/database.php For example mysql2
On each of your models specify the database it is related to. For example on your School Model
protected $connection = 'mysql2';
Now you can just run your seeder like you have 1 database , since the connection is specified on your models the correct database will be seeded.

you can run the command
php:artisan db:seed --class="className" --database=mysql2

Related

How to stop Auto Migration of Database table in Go

I am trying to build an api in beego. I have to make a controller and a model for this project. Model has a database table but the table is not in my database. Beego automatically creates the table in the database. How can I stop it.
I imagine you are using this command err := orm.RunSyncdb(name, force, verbose). This command looks for changes in your models and if there are any drops the database and creates a new one. If you want to stop it simply don't use this command and write database migrations for your app with bee generate migration migration_name. In the file you have to run the required sql to create the tables. After writing your migrations run bee migrate -conn="{your_database_connection_string}".

Laravel: last migration

i'm using laravel and working on migrations.
I am looking for records of the implementation of migrations. How does Laravel find out how far the migrations have run? Because I have checked that each migraine only runs once and will not run in subsequent commands.
My software has one source and several databases (per user). I would like to know what effect will this have on other users if one of my users executes migraine?
Migrations, once processed are held in the database in a migrations table. If your users have separate databases then they will have their own migrations table. Can't imagine a scenario though where users of the application would be running migrate?

EF Core Migration - multiple databases

Is there a way to run EF Core migrations on multiple databases having the same set of tables. This is for multi-tenancy architecture where there's a master database (has metadata of all tenant databases including the tenant database connection string) and one database per tenant having the same set of database objects. We need to be able to run these migrations when a new tenant database is created automatically in SaaS model and also run these migrations whenever there are changes to the database structure (new columns, data type changes, new indexes etc.)
I've posted this exact same question on EF Core's GitHub.
The answer is, it can't be done at design time. You basically need to run your migration scripts manually on each tenant's database.
Executing migrations at runtime, however, is easy. You can instantiate a dbContext for each of your connection strings when your app launches (before WebHost.Run() if it's a web app) and execute your migrations like this: dbContext.Database.Migrate();
This is not ideal, of course, because it makes it harder for you to rollback your migrations to a certain point using Visual Studio Package Manager Console or CLI using dotnet ef commands.
The CLI command can be provided a connection string. So you could run it once per db, providing the connection string for each.
The command would look like this:
dotnet ef database update --connection "Server=client1.db;Database=client1"
Our team has about 10 developers, our application is one front-end connect to 20 databases(same scheme), and new database will be add when there is new client. Time to time someone will need update DB scheme, we end up doing this.
if you need scheme change, create SQL script and create the change request by email
only one person in the team run those script, and update database access layer
git push
tell the team dinner is ready
The person doing this created a EXE project for DB migration, he keep adding script to a folder, so the folder will contain all the script
0001.InitTables.sql
0002.MoreTabels.sql
0003.UpdateDropdowns.sql
.
.
.
then he use a library like DbUp (https://dbup.readthedocs.io/en/latest/) help him track those scripts and run on DB server.
He will run for DEV server first, on the release date, he will run this for production.
List<string> connectionStrings=new List<string>{
"ConStr1","ConStr2", "ConStr3"
};
foreach(var conStr in connectionStrings){
var upgrader = DeployChanges.To
.SqlDatabase(conStr)
.WithScriptsEmbeddedInAssembly(
Assembly.GetExecutingAssembly()
)
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
}

Laravel Migrations - already a table with name "migrations"

I have a problem with Laravel (5.1.x) and Migrations. I am running it on an windows server with an integrated service account - so dont the application pool runs as the same account as the database.
Do get the correct scheme of my database, ill changed in the database.php file the name for the migrations table to:
'migrations' => 'dbo.migrations',
Because otherwise it will use my Scheme (because the command line runs as my account)
But when ill use that and run
php artisan migrate
It works only the first time, when i run it again, i tells me that there is already a migrations table in my database.
When ill use with:
'migrations' => 'migrations',
And the schema is now username.migrations it works when ill run the commands with my user.
It looks like that laravel is checking anywhere else if the migrations table exists, and try to create it again.
How can ill use the global database scheme with migrations (ill use that for every other tables too, and here it works fine as expected)
Any Ideas? Thanks in advance.

incomplete migrations- stuck in generating new migrations

I am having a very bad situation here, working on a Laravel 5 project. previously developed by another developer. That developer at start created couple of tables using migration generators and added some columns using migrations. After that, he added table columns straight away using some sql GUI. I was given the sql dump which i imported and set it up on my local machine, now when i created a table using php artisan make:migration create_myTableName_table --create="myTableName" the table migration is created successfully, but, when i did php artisan migrate it's giving me SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'someTable' already exists I checked migrations folder and matched it with current version of someTable and i can see the columns are different, same with other tables aswell.
What should be the best case to handle this in this situation, i want to keep up with Laravel migrations generator so that in future if any other developer want to work on this project he just has to run migration command to migrate database or to create tables or create columns... Should i re-write all migrations ? pleas help. Thanks
Given your situation, I’d put the .sql export in your repository, clear out the old, broken migrations, and create a new one that initially imports the database dump. Then just create migrations as normal going forward.
Quick and dirty?
Delete current migration files. Clear your migrations table and export the whole DB. Put the SQL dump in the repo and instruct other devs to import it before they run php artisan migrate. With the dump imported and migrations table truncated, you can create new migrations with no further collisions with the legacy migrations.
Feeling ambitious?
Use a package like migrations-generator to generate migrations based on your current DB structure. Then, migrate away.

Resources