Created
October 8, 2017 12:48
-
-
Save capaj/f159a9f2a0be21f5f9221ac0a5c75bf8 to your computer and use it in GitHub Desktop.
iterate a column in all tables postgre
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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