forked from nextauthjs/next-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres.nein
73 lines (62 loc) · 2.56 KB
/
postgres.nein
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* eslint-disable */
// Placeholder for schema test (will use test framework, this is temporary)
const fs = require('fs')
const path = require('path')
const { Client } = require('pg')
const { compareSchemas } = require('./lib/db')
const Adapters = require('../adapters')
const TABLES = ['users', 'accounts', 'sessions', 'verification_requests']
const SCHEMA_FILE = path.join(__dirname, '/fixtures/schemas/postgres.json')
function printSchema () {
return new Promise(async (resolve) => {
// Invoke adapter to sync schema
const adapter = Adapters.Default('postgres://nextauth:[email protected]:5432/nextauth?synchronize=true')
await adapter.getAdapter()
const connection = new Client({
host: '127.0.0.1',
user: 'nextauth',
password: 'password',
database: 'nextauth',
port: 5432
})
connection.connect()
connection.query(
TABLES.map(table => `SELECT column_name, data_type, character_maximum_length, is_nullable, column_default FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '${table}' ORDER BY ordinal_position`).join(';'),
(error, result) => {
if (error) { throw error }
const getColumnSchema = (column) => {
const nullable = column.is_nullable === 'YES' ? true : false
return {
type: column.data_type,
nullable,
default: nullable ? column.column_default : undefined
}
}
const users = {}
const accounts = {}
const sessions = {}
const verification_requests = {}
result[0].rows.forEach(column => { users[column.column_name] = getColumnSchema(column) })
result[1].rows.forEach(column => { accounts[column.column_name] = getColumnSchema(column) })
result[2].rows.forEach(column => { sessions[column.column_name] = getColumnSchema(column) })
result[3].rows.forEach(column => { verification_requests[column.column_name] = getColumnSchema(column) })
connection.end()
resolve({ users, accounts, sessions, verification_requests })
}
)
})
}
(async () => {
const expectedSchema = JSON.parse(fs.readFileSync(SCHEMA_FILE))
const testResultSchema = await printSchema()
const compareResult = compareSchemas(expectedSchema, testResultSchema)
if (compareResult === true) {
console.log('Postgres schema ok')
process.exit()
} else {
console.error('Postgres schema errors')
compareResult.forEach(error => console.log(` * ${error}`))
console.log('Postgres schema found:', JSON.stringify(testResultSchema, null, 2))
process.exit(1)
}
})()