sequelize force mark migration done - node.js

I re-created my database on one of my dev environment and now when I run the migration via sequelize db:migrate, it tries to run the migrations from the first.
I don't want to re-sync/re-create the database since running migrations on the dev environment ensures that the migrations are correctly written.
Is there a way to force-mark some migrations as 'done'?

Sequelize-cli stores the migration data on a table called SequelizeMeta.
You can copy the migration filename from your existing DB and insert into the above mentioned table in new environment's DB.
All the migrations recorded would be considered as they have already ran.
Though this would stop selected migrations from running, it's not the best approach to be taken.
This metadata could also be stored in a json, though I am not very aware of the structure for it.
You can dig through the docs here

The actual way of doing is as follow,
There are two tables sequelizemeta and SequelizeMeta(Hidden)
And for skip or say migration already ran is enter value in both table like
INSERT INTO sequelizemeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
INSERT INTO SequelizeMeta (name) VALUES
('20181019072815-roles.js'),
('20181019093229-users.js')
Note- SequelizeMeta is hidden table but we can query it.
SELECT * from seerportal.SequelizeMeta

Related

Write data frame to hive table in spark

could you please tell me if this command could create problems with overwriting all tables in the DB:
df.write.option(“path”, “path_to_the_db/hive/”).mode(overwrite).saveAsTable("result_data")
table_name is a new table in the DB, it hasn't existed.
After these commands, all tables disappeared.
I was using Spark3 and tried to solve an error:
Can not create the managed table('result_data').
The associated location('dbfs:/user/hive/warehouse/result_data') already exists.
I expected that a new table will be created without any issues if it doesn’t exist.
If path_to_the_db/hive contains other tables, then you overwrite into that folder, it seems possible that the whole directory would be emptied first, yes. Perhaps you should instead use path_to_the_db/hive/result_data
According to the error, though, your table does already exist.
You can use Spark to register a temporary table in SQL code, then run INSERT OVERWRITE query for existing tables.

Quick question about sequelize migrations

I'm trying to do sequelize migrations on the models I created such as User.js, Blogs.js etc... What I've noticed is that I can add columns to Models like User.js and Blogs.js, but if I dont change the files in my models folder, the changes will be recognized in the database, but I wont be able to access the new columns when the server is running.
EG:
I add column permission: {type: Sequelize.STRING} to User.js.
I can't access this column because my original model file doesn't contain permission. Do I have to delete the models, and then create them using the sequelize-cli in order to get the migrations to also update the model files?
Your question is somewhat unclear.
In order to add a new column to a table using Sequelize migrations, you need to do the following steps:
Generate a new migration file.
Using queryInterface methods, make the changes to your tables in the migration file.
Execute your migrations.
Update your models with any new properties that are added.
Restart your server.
Keep in mind that migrations are done as an ALTERNATIVE to using the sequelize.sync() method. If you are using the sync() method, you cannot add columns to a table without using the { force: true } flag inside of sync(), which causes your tables to be dropped and data lost. Migrations are a process that requires a bit more attention to be paid, but they are a feasible long-term solution for a production database.

How to reflect dropped tables from models in the database using Sequelize commands?

I currently call a custom function dbinit() in terminal whenever I have updated my models and want my database to reflect those changes. This function includes the Sequelize method db.sequelize.sync(), which adds some changes to my database including new tables and columns.
Unfortunately, sync() doesn't remove tables from the database to reflect changes in my model. Is there some code I can add to my custom function that will make the database reflect tables that have been deleted?
(using PostgreSQL 9.4.4 and Node.js 5.1.0)

How to use migrations in Sequelize?

I have used ORMS such as EntityFramework, Waterline and Mongoose. I just started using Sequelize. I have created a sample model. Later I wanted to add another column to the table corresponding to the same model. When I edit the model, the new field doesn't seem to be reflecting in the table unless I drop the table. Am I supposed to use Migrations? I got confused after reading the documentation. Could someone help me out?
Yes, you should use migrations as if you edit a model in sequelize when you application starts it will only create the table if it doesn't exist. It does not check if the schema is different thus it doesn't update the schema of your table.
If I were you, I would install sequelize-cli and then run the sequelize init command. This sets up your project ready to use migrations.
Please note that we using sequelize migrations you have to explicitly define the primary key, updatedAt and createdAt columns otherwise they will not great created!
The docs are not too bad about migrations: http://sequelize.readthedocs.org/en/latest/docs/migrations/

MVC Switch from Code First to Database first - alter schema without dropping database

With Entity Framework it is possible to enable migrations and create migration steps. But is there an intermediate way where it is possible to change the models, and take care of database schema changes yourself? I don't want to drop the database, because there are future production schenario's.
Now - without enable migrations - I use a code first, and when I create another property in a DbSet - lets assume for example in table 'ExistingTable' int NewField {get; set;}
And when in SQL I update my schema with
Alter table ExistingTable add column NewField int not null
the database knows existence of the new field, the Entity Framework / C# knows the property, but when running, there is some hidden check that still want's to drop my database because of the model change.
Question: can I overwrite a certain setting, in such a way that intial 'Code First' can be transformed to database first?
Removing the __MigrationHistory table from the database (Azure) did work fine for me. I made my (simple) database changes myself and published the code. It all runs fine. There is an alternative see EF Code First Migrations Deployment to an Azure Cloud Service. For a simple one-way patch (and no change history needed) removing the __MigrationHistory works fine.

Resources