Is there a concise way to test Laravel migrations? I want establish the database up to the point of my new migration (including seeds etc) and then run my new migration and check the tests I have written for my seed data. The main issue is that I need to create my factory objects before the new migration is run, but after the other migrations run (creating all the original tables etc). As far as I know the only way to do it is either run every migration individually with the --path option, create the factory objects and finally run my new migration. Is there a better way?
Related
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}".
We are looking to implement Continuous Integration using Circle CI but we are not sure on how should we proceed with our test database. We have the following alternatives in mind:
Run the migrations from scratch (the problem is that we have a lot of migration files, our first migrations were moving everything from MySQL and PostgreSQL and using a legacy database, so, it's rather complex).
Recreate the current DB and have a .sql file that will create our current tables, and then, we create a seeder to fill the information that we need.
But we are not sure which is the best alternative or if we're missing something?
Thank you
I seen in laravel documentation that whenever i append new column in existing table at that time i need to create
php artisan make:migration append_tablename
it is fine this level. but next time i need to update any column structure of same table then i need to create new migration or i can add this below code in second migration file which i already used for append new column ?
$table->string('name', 50)->change();
any idea please share.
Migrations only run once, so adjusting an already-ran migration won't do you any good. During development, you can keep tweaking one and run php artisan migrate:rollback to undo it and re-run it repeatedly until you get it right, but once you a) push to production or b) push it somewhere other developers may run it you shouldn't touch the migration any more.
Every migration should be independent.
Unless you are explicitly refactoring for it to be more compact (If you know that you want to deploy a cleaner version in production and it won't mess with other developers work). In which case you should probably add it to your initial table creation.
It is your database history and versioning. Which allow's you to rollback to any previous state.
In my project, current approach is to create database if not already exists using CreateDatabaseIfNotExists and doing seeding initial data from that Intializer as well. I also added Code First Migration support after upgrade to Entity 4.4, so that in the future when we change the modle/database structure we can update client side database without drop their exist database.
However it didn't seems to working well, for example, I am now stuck on design time where forms wouldn't load and the error message is something like The model backing the 'myEntities' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).. But the model and database is indeed the updated version, just seems Migration didn't recognize the database generated by CreateDatabaseIfNotExists, but at the same time all seems working well at run time.
Also after that I noticed that if I let CreateDatabaseIfNotExists initialize a database, Add-migration afterwards will fail and complain that pending migration and ask me to do a update-database. When I try to do a the update-database, it will fail as well because the migration path seems assume the database is in initial setup state and will trying to running all the migration scripts while none should be run as the database generated by CreateDatabaseIfNotExists is indeed sync with current model and should not be migrated at all.
I discovered that there is a MigrationHistory table in System Tables, that table will always save the database initialize history regardless of whether the initializer is CreateDatabaseIfNotExists or MigrateDatabaseToLatestVersion. The difference is that if database is initialized by CreateDatabaseIfNotExists1, everytime the database initialized, the migriationId for that initialize record will be different, butMigrateDatabaseToLatestVersion` will always save same set of migrationId for each of the Migration steps. I guess that is how Entity Framework 5.0 works.
So in the end, I give up and rewrite my DB Access code to seeding the initial database data
in other part of my code rather than the CreateDatabaseIfNotExists or MigrationsConfiguration` class because either suit my need.
I have a local instance of a database that I recently created using DbContext.Database.Create(), so the __MigrationHistory table exists with an InitalCreate entry that matches the code at the moment.
Some code-based migrations exist in the Migrations folder, however. These will be run in our development and staging environments to bring those databases in line with the code. I don't need to apply them locally, however, since I created the database using the current code.
I now need to make a change to the model and create the corresponding migration. But when I run Add-Migration TestMigration, I get the following error
Unable to generate an explicit migration because the following explicit
migrations are pending:
[201203271113060_AddTableX,
201203290856574_AlterColumnY]
Apply the pending explicit migrations before attempting to generate
a new explicit migration.
What should I do in this case? I can't point the Add-Migration tool at another environment because it's not guaranteed that version matches what I have locally. I want a migration that matches only the changes I've made.
It seems I have a few options but none are ideal:
Delete the other migrations from the Migrations folder, run the Add-Migration command, upgrade the database, then restore the old migrations. This is simple but seems a bit hackish.
Revert to the version of the model in source control that the first migration was applied to, then build this and use it to create the database. Then get the latest version, apply all the migrations, then I'm ready to add my migration. This seems like a lot of effort!
Create the migration manually.
Does anyone have any suggestions about how to manage this?
We are planning to use a variant of your Option #1...
Our Standard Operating Procedure is to generate a SQL script for each migration (using the -script option of update-database), in order to have SQL scripts to be applied to end-user "production" databases by InstallShield (we plan to use EF update-database only for developer databases).
Thus, we have both the Migration .cs files and the corresponding .sql files for all migrations in our Migrations folder.
So rather than deleting the migrations from the Migrations folder (as you proposed in #1), we use SQL Mgmt Studio to manually apply just the parts of the .sql files that do the inserts into _MigrationHistory.
That brings the _MigrationHistory of the local database up-to-date with the changes that are already incorporated into that database.
But it's a kludge, and we're still looking for a better solution.
DadCat
What I've found works best is very simple: don't use DbContext.Database.Create() once you've enabled migrations. If you want to programmatically create a new database, use the migrations API instead.
var migrator = new DbMigrator(new Configuration());
migrator.Update();
Then you've got the full migration history and adding further migrations works just as expected.
You either need to run "update-database" from the package manager console to push your changes to the database OR you can delete the pending migration file ([201203271113060_AddTableX]) from your Migrations folder and then re-run "add-migration" to create a brand new migration based off of your edits.
I have encountered the same problem.
If you run
Update-database
and then run
Add-Migration YourMigrationName
This solves the problem
simply exclude the old migration file from the solution files.