Search code examples
node.jspostgresqlsequelize.jsdatabase-migration

Alter table inside schema - postgres + sequelize


I am using postgres + sequelize

const db = //db connection stuffs....

db.authenticate().then((err) => {
  if (err) {
    console.log('There is connection in ERROR.');
  } else {
    console.log('Postgres Client App Connection has been established successfully');
  }
});

db.sync().then(() => {
  console.log('Psql Client App Missing Table Created');
}, (err) => {
  console.log('An error occurred while creating postgres client app table:', err.message);
});

For example i have multiple schemas

  1. Public (default schema)
  2. tenant1
  3. tenant2 . . . .
  4. tenant100

I have table called users in my all schemas including public schema

I want to add extra column in my user table

After adding column using

ALTER TABLE ADD COLUMN.. more from postgres docs.

inside my code, i run my project with the help of this below code:

db.sync({ force: true }).then(() => {
  console.log('Psql Client App Missing Table Created');
}, (err) => {
  console.log('An error occurred while creating postgres client app table:', err.message);
});

//This will successfully added my column on the table inside public schema, without data loss.

But my tables inside other schemas does not changed.

How to change that to all schemas?

By postgres i can do Alter table tenant1.users ...bla..bla , but how to do with sequelize?

Also i dont want to lose my datas.


Solution

  • I finally did this using migrations in sequelize

    http://docs.sequelizejs.com/manual/tutorial/migrations.html