Several months ago, I was working late and needed to create a new column for a postgres table. I added the column through the command line using the standard heroku/postgres commands. I quickly realized how silly this was, deleted the column and then created the necessary migration to add it.
I recently took that laravel app and created a pipeline with it as the production app. I created a new staging app and set up a database for it. When I ran the migrations, I got an error saying that a column already existed. As you may have guessed, this is the same column that I had messed up before.
Since the migration failed after the database was created, I was able to delete the column in question, rerun the migration, and everything was OK. I do not understand why the column already existed except to guess that it was some artifact of my adding it manually (there was no error in the migrations).
I am worried that this same error will happen when I push the staging code to production and have to rerun my migrations. Obviously, in the production app I can't just delete the column in question as it has data.
Any ideas as to why this would happen and what I could do to fix the issue if it arises when I push the code into production?
Related
Until now, I used the hasura console to update my database schema (new column, table and manage permissions). Every time a change is made, a new migration is created.
Today I added two columns and updated the permissions (select/insert/update).
For some reason, I deleted and recreated my database. Every migrations worked fine but not the permissions. I have my new columns in my tables but the permissions are not persisted and I can't understand why...
The last time I updated permissions, the metadata file changed but not now. I don't really understand this part of hasura, even with the documentation.
Any idea?
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.
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.
so I went to create some migrations of some tables
and was hitting the up arrow and changing the --create"name" but was leaving the class name the same on accident after I already created some I noticed and tried renaming the files and classes but ran into problem I migrated and got errors I tried renaming them back and resseting and rolling back but everything is too messed up I dropped the tables manually and when I try to reset or rollback now I get a table does not exist error is there any easy way to get out of this mess with out having to create a new project?
If you mess up your migrations (can happen if you have migrated and change directly in the migrations), you can always just drop all tables and run a fresh php artisan migrate.
It sounds like you had migrated, and changed the tablenames of some tables. When you do that and try to rollback (reset starts out with rolling back) it tries to drop the tables, but when it gets to the changed tablename it can't find it in the database and you get an error saying the table doesn't exist :)
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.