How to remove deleted typeorm migration files from migration list - entity-framework-migrations

ran typeorm migration:generate -n <name>, but generated migration files did not run due to some issues, deleted the generated file and created another migration file and try to run the migration, but it gets blocked by the same migration file i deleted. I ran typeorm migration:show to get list of migrations, saw the deleted migration files and that is blocking my other migrations, can someone help me how to fix this.
More details:
There were migration info stored in my database but not the deleted migration file. Don't know where typeorm is pull the files from.
Cache disabled

Check you dist folder or whatever your folder name which has been created from build. In my case the migration folder from dist containing the pending migrations and blocking my new migrations to run.

In your TypeORM.config file, check which (path) your migration is pointing to and you will find the folder where the build migration is stored.
{
type: "mysql",
host: "localhost",
port: 3306,
username: "test",
password: "test",
database: "test",
entities: [/*...*/],
migrations: [/*...*/],
migrationsTableName: "custom_migration_table",
}

delete your folder dist and rebuild your project

Related

Ruby on Rails. Why schema.rb builded on existing data through db:schema:dump is almost empty?

I am trying to find the correct (any) method to create an application in Ruby on Rails having an existing database (PostgreSQL) with data and fresh app made with:
rails new --database=postgresql -J --skip-coffee .
I found https://github.com/frenesim/schema_to_scaffold but first I need to have a file with a database structure: schema.rb. I’m looking for a way to do it automatically.
In result of rake db:schema:dumpfile schema.rb is generated, but only with content like that:
ActiveRecord::Schema.define(version: 0) do
enable_extension "plpgsql"
end
And I stuck here. Why that file is empty? Why are there no tables here?
I have a connection with DB and no errors. I did rake db:create before to test. Creation of bases described in database.yml is successful.
At the beginning I used Docker containers and this is my goal. But to exclude the probability of error, I installed the environment in the system (macOS Mojave) based on the socket. And I’ve got the same effect.
How to generate schema.rb with structure of existing database? Or is there different way to build RoR app based on the existing data structure?
Update: Connection with the new database I only did for testing purposes. To verify configuration.
Here's what else I did:
Dump existing structure with
pg_dump --schema-only app_development > db/structure.sql
I changed name in database.yml to have fresh place to import.
rake db:setup created new DB
rake db:structure:load create tables from db/structure.sql file in DB correctly.
But rake db:schema:dump still generate empty file as earlier.
If you have set proper db config you can use rake db:migrate to regenerate the schema file.
edit:
Ok so lets check if I understood correctly:
you have an existing db with tables and data in it
you have brand new rails app
you want to reflect db structure in you schema.rb file
Is that correct? If yes then like I wrote before - without adding any new migrations to your codebase, run rake db:migrate. That task not only applies changes from the migration file but also updates your schema file to be in sync with the actual database.
I've got it! Two days of my life.
File used to import PostgreSQL database has at the beginning:
CREATE SCHEMA employees;
-- and later
CREATE TABLE employees.department;
I thought that since Rails generates database by rake db:structure:load , the file's syntax is correct.
But when I create manually table users in new empty database and then pg_dump that new base I don't have CREATE SCHEMA query there.
And finally rake db:schema:dump fills schema.rb with tables as I want:
create_table "users", id: :serial, force: :cascade do |t|
t.text "name"
end
Because that fresh pg_dumped file has CREATE TABLE public.users query. public.
I think the key is in comments in database.yml file:
# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public
One picture is more valuable than a thousand words: that's the differences Table users on the right goes to schema.rb after rake db:schema:dump
Thanks guys for the comments. It's made sure me that I do not make a terrible mistake.
It sounds like you did rake db:create which creates a new database for you and then did rake db:schema:dump which generated a schema.rb file for you from the newly created (empty) database.
If you have an existing database that you want to use you will need to modify your database.yml file to connect to it.
If you want to create a new database you will need to generate Active Record database migrations e.g.) rails generate migration CreateProducts name:string part_number:string and then run them rake db:migrate to update your database and generate your schema.rb.

Rails db migrate generate migrate file

When working on a project, I keep track of all the changes I make to a database in a notepad file. Then, later, I manually write all the changes in rails' db migration file.
But it should be possible to compare the schema of a backup of my database, with the new version of my database, and automatically detect the differences. And generate the rails db migration file automatically.
Is there a tool that can compare two database schema's and automatically generate rails' db migration files?
As far as I'm aware, there's no tool that will do it automatically, however, you can get most of the way there just using rake db:schema:dump with source control.
Create a new Rails project and do the following:
Update database.yml to connect to your first database.
Use rake db:schema:dump to populate schema.rb and commit schema.rb to git.
Update database.yml to connect to your second database and again run rake db:schema:dump
Use git diff on schema.rb to compare the changes. This can easily be mapped to a migration.
The benefit of using source control is that you can then test the migration by comparing schema.rb after the migration runs to the schema dump of the second database.

Rails migration rollback mistake - how to update schema?

I royally screwed up with version control and deleted a bunch of files including migrations. I tried then to rails generate the files with the same names but then got (error, record already exists). I realized the table must already exist in the database.
If I run rake db:migrate:status I get the following:
up 20150426172505 Remove columns from businesses categories sub categories
up 20150426172902 Change pdf name
up 20150520193556 ********** NO FILE **********
up 20150520194841 ********** NO FILE **********
up 20150527194215 ********** NO FILE **********
How do I get rid of the missing migrations and get the schema to use 20150426172902?
Well, there are two ways you can get rid of those lost migration files.
One is to run the following command:
rake db:migrate:reset db:seed
This will completely drop down your database, re-run all the migration, and run the seeds if you have any.
Second is to run the following query, and directly deleting the lost migration files that you do not want to see around.
DELETE FROM `schema_migrations` WHERE version='<MIGRATION_ID>';

How to create database from schema.rb without initializing Rails?

I am trying to create all my tables from schema.rb
I used the command: "rake db:schema:load"
However, this fails because in one of my initializers, it is referencing a model/table that obviously doesn't exist (since the database is empty)
I could comment out these lines, and then run schema:load again, but is there an alternative?
Probably the fastest way is to just move the offending initializer to a temporary directory that is outside of the app, and then run your schema load. But if that doesn't work, or isn't an option for some reason, you could always work around that by creating a bare bones rails app to do the schema load:
Create a new rails app: rails new (app)-fixer
Copy your gemfile (unless there are specific exceptions) to the fixer app.
Copy your database.yml config to the fixer app.
Copy your schema.rb file to the fixer app.
Do all appropriate "bundle install" commands as needed for your app.
Then run "rake db:drop db:create db:schema:load"
That will build up a new database from scratch, based on your current schema.
You can add a check for the table existance in your initializer.
if TheModel.table_exists?
// do something with the model
end

deleted migration file keeps coming back Rails 3

I have 2 versions of the same migration (bad habit, I know). I deleted old version many times, but after restarting my project (or do something with it, like rake db:test:prepare), the file shows up in my migrate folder. When I run rake db:migrate, it will complain about multiple migrations with the same name.
How can I delete a migration file completely? Is there a registry that I need to remove to prevent it from coming back?
Are you updating from a repo? I don't see how the original file could be restored otherwise.
You can also delete the entry from the schema_migration table, but that just tracks if it's been run or not (IIRC).
git add only adds new and changed files, it doesn't remove deleted ones. To delete:
git rm db/migrate/<filename>
or if you have already deleted it from the filesystem:
git add -u

Resources