Skip to content

Instantly share code, notes, and snippets.

@capaj
Created October 8, 2017 12:48
Show Gist options
  • Save capaj/f159a9f2a0be21f5f9221ac0a5c75bf8 to your computer and use it in GitHub Desktop.
Save capaj/f159a9f2a0be21f5f9221ac0a5c75bf8 to your computer and use it in GitHub Desktop.
iterate a column in all tables postgre
import db from './src/db'
const af = async () => {
const res = await db.raw(
`SELECT tablename FROM pg_tables WHERE schemaname='looop'`
)
console.log('res: ', res)
res.rows.forEach(async table => {
const { tablename } = table
const schema = await db.raw(
`select *
from ${tablename}
where false;`
)
const fields = schema.fields.map(({ name }) => name)
if (fields.includes('updated_at')) {
console.log(tablename)
await db.schema.table(tablename, table => {
table
.timestamp('updated_at')
.nullable()
.alter()
})
}
})
}
af()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment