We're using Doctrine Migrations to keep the application DB in sync across versions. The app will be installed each time we get a new customer meaning a new DB for each installation.
Now, the new DB will have all DB changes in the migrations file in place but will try to execute the migration files as they are not registered in the migrations table.
What's the best way to handle this situation?
Just after fresh install to fill current schema use this commands:
Create new empty chema of current version
./bin/console doctrine:schema:create
Fill out the migration records with the current version (without actually run the migrations - schema already has current version after first command)
./bin/console doctrine:migrations:version --add --all
This command adds records of the migrations and doctrine:migrations:status will show you that there is no migrations need for the current verions.
Thats all!
As mentioned in my comment, I've successfully created a blank database schema just by using Doctrine migrations. I believe it's necessary to create the empty database first (php app/console doctrine:database:create) and then run the migrations task.
The only potential issue I can think of is that any base data that your app requires to function will either need to be in the migrations files or will need to be inserted separately.
As an aside, running the all the migrations in one go has picked up a couple of small errors that I'd missed when just executing one at a time.
I think you can pick the very first migration class, copy it and rename with just one second before.
So, if your first class is 2016060712284351 you should create a new class called 2016060712284350. This way it is executed just before the real migrations.
In this newly created class you setup your database creating all needed tables. This way, when you run migrations the first thing is done is to create the database with all the tables and then the real migrations are run.
Anyway I'm not sure this is the real way to proceed.
Migrations are needed to update the database schema, but if you are creating a new database for each user, each of the new users will have the new schema as per entities mapping. So a migration is not needed.
You need to run migration only for OLD USERS' DATABASES, am I right?
Related
I've been following a tutorial to create a database by having a context class, and running the following commands:
dotnet ef migrations add InitialMigration
dotnet ef database update
This did initially work for me, and the expected database was created. Since then I have deleted the migration file and ran the process again, the update command seems to run fine, but when I check the database, the tables have not been created.
If I look in the migration file, I can see that it looks fine and appears to have the correct code in it to create the tables I want.
I don't think I've actually changed anything, so why has the update command stopped working as it did before?
deleting you migration file didn't delete your database
so your database must be still existed however , if your create a new migration with the same name and update your database this will not effect you database since the migration is have been applied to your database
try to delete your database manually from SQL explorer then try update-database
Remove all migrations from your Migration folder in dot net project.
Create a new migration from CLI or from wherever you are making. (dotnet-ef migrations add..)
In the .cs file of this new migration file, comment all the CREATE TABLES commands for existing tables i.e. migration.Create...this one (These will be in the Up method of your migration file)
Just keep CREATE commands for only required tables.
Apply the update.(dotnet-ef database update)
Will work like a charm.
I am working with Symfony and Doctrine. In the middle of the project I need to implement the Doctrine migrations, because the DB changes were too much and I need a better way to handle it.
How is the best way to start with the migrations, when there is already data on prod and it need to stay there and not to be touched?
My plan will be:
On my test system
drop all tables
run php bin/console doctrine:migrations:diff
The new automatic migration file, which I become holds all the table structures of my current state
go live to the table "migration_versions" and add the ID of this migration, so it will be skipped by the first run of the
migrations
run the migration php bin/console doctrine:migrations:migrate
In this way, I have all the structures from my entities, but I will not destroy my live data.
What do you thing?
If there is already some data on production, then your best take is to do a make:migration or doc:mig:diff from the current schema state. That will generate only the necessary sql that will update the new changes, but nothing else. Your first migration version will countain only the sql to update from the current state, and not from the beginning of times.
And also, once you have adopted this, you have to do every database modification under migrations. For example, if you need to add a non-nullable field, you usually add the new field with not null, then fill all the rows of that field with a default or calculated one, and then alter the table to make the field not nullable. Migrations will generate some boilerplate code to make your life easier, but it also requires a lot of care from the development team. Always test them first in a database that you can get rid of. And you will run into FK constraints and lots of other issues, but basically you have to solve them doing SQL.
Old thread here, but what I do in cases like this:
Backup dev database (structure and data, but with table create statements protected with checking so they are only created if they don’t already exist)
Drop all tables so the database is empty
Generate migration (since database is empty, the generated migration will constitute all commands necessary to generate your entire schema)
Run migration you just generated to build schema
Import test data from your dump
That puts you right back where you started but with an initial migration that can build your schema from nothing.
I am trying to use migrations with a existing database and to create a new one, if not exists.
The EF Core documentations says:
If you created the initial migration when the database already exists, the database creation code is generated but it doesn't have to run because the database already matches the data model. When you deploy the app to another environment where the database doesn't exist yet, this code will run to create your database
From EFCore docs
I did initial migrations, it creates a up and down methods to create my tables. When I run it to a new database (new database name in connection string) it creates the database and tables as expected.
But if I run it to an existing database (not created by migrations), it fails at the first up method that tries to create a table that already exists, and the migrations stops to run.
Docs says "it doesn't have to run", but first thing migrations is doing is try to create an existing table.
How to prevent migrations to try to create existing tables? There is something like "create if not exists" built in on migrations? Is documentations right? It should works as expected/describe in docs?
If I'm doing it wrong, what is the strategy to work with migrations to run with existing and new on databases?
Dotnet Core version: 1.1.
EFCore version: 1.1.2.
Thanks in advance.
You need a baseline migration for the existing database. In EF 6 you used the -IgnoreChanges flag, but that doesn't exist in Core right now. See here.
You can accomplish the same thing in EF Core by commenting out all the Up() code and applying that migration. This will create the __MigrationHistory table and insert a record denoting it has been applied.
Subsequent migrations will be applied to both and if you need to create a new database you are covered.
I am working with doctrine:migrations:diff in order to prepare database evolutions.
This command creates files into app/DoctrineMigrations
Thoses files contains sql commands in order to upgrade or downgrade database scructure.
I want to store those sql commands into the database itself. In fact, i have several instances of databases. If sql commands are store into files, it is a big problem.
I have read somewhere that DoctrineMigrations bundle can create a table called "migration_versions", but i do not manage to find where i have read this...
I cannot really understand what you're trying to do.
Migrations are used when your code needs altered database structure. For example, a new table or a new column. These new requirements for a table or column comes from your newly written code, so it's only natural to place the migrations also as a code in your repository.
How and when would migrations even get to your database? How would you guarantee that migration is executed before the code changes, which use that new structure?
Generally, migrations are used in this way:
You develop your code, add new features, change existing ones. Your code needs changes to database.
You generate doctrine migration class, which contains needed SQL statements for your current database to get to the needed state.
You alter the class adding any more required SQL statements. For example, UPDATE statements to migrate your data, not only the structure.
You execute migration locally.
You test your code with database changes. If you need more changes, you either add new migration, or execute migration down, delete it and regenerate it. Never change the migration class, as you'll loose what's supposed to be in the database and what's not.
You commit your migration together with code that uses it.
Then comes the deployment part:
- For each server, upload the code, clear and warm-up cache, run other installation scripts. Then run migrations. And only then switch to the new code.
This way your database is always in-sync with current code in the server that uses that database.
migration_versions database table is created automatically by doctrine migrations. It holds only the version numbers of migration classes - it's used for keeping track which migrations were already run and which was not.
This way when you run doctrine:migrations:migrate all not-yet-ran migrations are executed. This allows to migrate few commits at once, have several migrations in a commit etc.
I have a .net MVC project which works with Code first approach, I need to add a new table and the migration folder already exists and contains a lot of migrations files that have been made before; when I run Add-Migration:
Unable to generate an explicit migration because the following
explicit migrations are pending:
[201304230714010_InformationalMessage, 201305231312259_Remove
hardcoded currencies ]. Apply the pending explicit migrations before
attempting to generate a new explicit migration.
So I run Update-Database –Verbose it gives me error:
There is already an object named 'InformationalMessage' in the
database.
which seems that it goes to execute the migrations files again and it is normal to give me that error as it already exist.
Can anyone help me to how to update this code with my new table?
Error says you are already having pending migration so first run the pending migration and then run your migration.
update-database -verbose (run first pending migration).
Add-migration <nameOfMigration> (add your migration).
Update-database -verbose (run your migration in the last).
I've run into that occasionally. What I do is comment out the CreateTable command for that migration in the Up() and Down() methods. When I need a production script I am usually rolling my changes up into a single update anyways.
Temporarily add DropTable("InformationalMessage") to your copy of the 201304230714010_InformationalMessage migration – be warned, this will lose any data currently in that table, but should get you to a point where you can add your new migration … if this is a concern, make a backup of the actual table data while working on your migration.
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 ([201304230714010_left]) from your Migrations folder and then re-run "add-migration" to create a brand new migration based off of your edits.