I want to make an application using Laravel.
this application has 2 databases (production and sandbox).
when we log in, we can change production or sandbox by using the dropdown.
my question is:
How to achieve this?
Where I must place the table user? one of that databases or create a third database? how about the table relation?
You could set the config to a session variable using a middleware like
config(['database.default' => Session::get('db_to_use')]);
Keep all the users in one table and then set the connection in the User model like
/**
* The database connection that should be used by the model.
*
* #var string
*/
protected $connection = 'prod';
(from https://laravel.com/docs/9.x/eloquent#database-connections)
However
My 2 cents:
This seems quite dangerous having changes to production be so easily accessible. I would keep the databases separated by more than a dropdown.
ex. You are making changes to the sandbox site and open a new tab, switch to production from the dropdown, and make other changes. You then go back to your original tab and see the dropdown says 'sandbox' and make some changes, but UHOH, you deleted someone's report that is due today.
Related
Laravel 6. CentOS 7
I have an existing user table that was used with standard Laravel Auth.
I've converted to LDAP but if a user logs in, the system tries to create a new user rather than syncing the existing entry unless there is an objectguid. This fails of course because of duplicate constraints.
I have another app that I'm doing the same thing and it seems to be working as expected. I cant figure out what's different on the configurations outside of some column names are different so the sync config looks a little different.
How does ldap2 determine a match in the local table before creating a new one?
Thanks
We have an Apex application (version 20.1) and our users must be able to change the database schema at runtime via button click (preferably without logging in again).
Currently we are solving this by installing our application multiple times, once per schema.
We recently discovered the function apex_export.get_application. We intend to use this function to bring our frontend under version control (finally!). We would like to deploy our application directly from the exported files. Having a single application, we would not have to mess with the internal component ids from the exported files.
Is it possible to install the application once and change the default schema via Pl/SQL code? Thank you!
I don't think this can be done, but perhaps the following is a reasonable compromise
add all the schemas you need to support to the workspace schema list
Any SQL (and I do mean any) in your app would be prefixed with an application item, eg
Before: select * from my_table
After: select * from &my_schema..my_table
At login time (or when a user selects it) you modify the MY_SCHEMA application item
(I've not tried this...so test/tread carefully)
So I'm working on a little web application in which you can manage your database.
Now I can use the following function to retrieve all databases
DB::select('SHOW DATABASES')
But I now want to be able to get the tables for each of those databases and eventually do more with those databases, but I figured if I get this working that wouldn't be a problem.
Normally you'd have the different database in your config, but since I want my application work with "any" database and make sure I don't have to manually add all the databases etc since that's the kind of work I want my web app done for me.
I've tried tricking around it a bit without success for example.
DB:select('USE dbName; SHOW TABLES');
DB::select('SELECT dbName(); SHOW TABLES');
Obviously this didn't work, but is there any "proper" solution to this? I thought editing the .env variable on the fly might've been an option, but I can't seem to find a "legit" way to do that either.
You don't need this.
I thought editing the .env variable on the fly might've been an
option, but I can't seem to find a "legit" way to do that either.
What you need is this
DB::purge('mysql');//IMP
Config::set('database.connections.mysql.host', $host);
Config::set('database.connections.mysql.database', $database);
Config::set('database.connections.mysql.username', $username);
Config::set('database.connections.mysql.password', $password);
You just need to figure out a way to get the values for $host, $database, $username, and $password dynamically.
One way to do that is have a database which stores all these values and point the default database connection (say mysql in config/database.php) to it. Then read values from it on the fly and set the connections accordingly.
I'm a bit lost how I should get the entity framework to work with automatic migration. I want:
The database to be created automatically when it doesnt exist
The database to be updated automatically when the model changed
For the latter I'm using DbMigrator. It is rather slow so I don't want to run it every request, and also I have multiple databases in the same application so it cant go in Application_Start which is why I put it in Session_Start like this:
if (Session["started"] == null)
{
// this takes care of any database updates that might be necessary.
MigrationConfiguration configuration = new MigrationConfiguration();
DbMigrator migrator = new DbMigrator(configuration);
List<string> pm = migrator.GetPendingMigrations().ToList();
if (pm.Count > 0)
{
migrator.Update();
}
}
else
{
Session["started"] = "started";
}
Not sure if this is the right way to do it but it seems to work, however it doesnt actually generate the database when it doesnt exist. It gives me a "Cannot open database "db" requested by the login"
I had this working before with the following:
Database.SetInitializer<DbContext>(new InitializerIfModelChange());
This drops the database and generates seed data which is fine for when the database doesnt exist but it also is triggers when the database is changed (in which case I would like DbMigrator to handle it) This was in Application_Start before but I'm not sure what to do with it. I'm afraid that it will conflict with the DbMigrator. How would I set this all up in order to achieve the two things described earlier?
I manually run the Update-Database in the package manager whenever the database needs to be changed. Initially I used the Database.SetInitializer like you did to create the database but have it commented out now.
Checkout Entity Framework 4.3 Automatic Migrations Walkthrough for more advanced help.
This should work for what you want, then if you need to create a new database just add Database.SetInitializer<NewDBContext>(new NewDBInitializer()); like you had, build and run. Then comment it out so it doesn't run in the future on a model change and instead use the Update-Database command in the package manager.
In Models context file it was mentioned
If you want Entity Framework to drop and regenerate your database
automatically whenever you change your model schema, add the following
code to the Application_Start method in your Global.asax file. Note:
this will destroy and re-create your database with every model change.
System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<HMS.Models.HMSContext>());
so I added this code in the Global.asax file inside protected void Application_Start().
Adding this line drops whole database when I do any changes in model class. Is there any alternate way to avoid this and still I can do Model changes?
i.e After performing changes in model when I rebuild and run my application, it drops my database and regenerate all the empty model tables. So I loose all my entered data and I want to preserve table data.
If you're using EF Code First 4.1, then, no, it will only drop and recreate. EF version 5 apparently supports dynamically changing the underlying database. It's in beta at the moment, but is available for production use if you want to try it out. You can install it through NuGet.