add constraint
This commit is contained in:
1 parent
8bb5d996b9
commit
0f993cffe3
1 file changed
+32
-11
+32
-11
@@ -1,8 +1,9 @@
|
|||||||
import createConnectionPool, { ConnectionPool, sql } from '@databases/pg'
|
import createConnectionPool, { ConnectionPool, sql } from '@databases/pg'
|
||||||
import sqlite3 from 'better-sqlite3'
|
import sqlite3 from 'better-sqlite3'
|
||||||
|
import S from 'jsonschema-definer'
|
||||||
import { PythonShell } from 'python-shell'
|
import { PythonShell } from 'python-shell'
|
||||||
|
|
||||||
async function main(db: ConnectionPool) {
|
export async function main(db: ConnectionPool) {
|
||||||
const s3 = sqlite3('./assets/edict.db')
|
const s3 = sqlite3('./assets/edict.db')
|
||||||
|
|
||||||
const rows = s3
|
const rows = s3
|
||||||
@@ -67,14 +68,18 @@ async function main(db: ConnectionPool) {
|
|||||||
await db.query(sql`
|
await db.query(sql`
|
||||||
INSERT INTO "entry" ("userId", "type", "entry", "key", "reading", "translation")
|
INSERT INTO "entry" ("userId", "type", "entry", "key", "reading", "translation")
|
||||||
VALUES ${sql.join(
|
VALUES ${sql.join(
|
||||||
sublot.map(
|
sublot.map((r) => {
|
||||||
(r) =>
|
const reading = JSON.parse(r.reading)
|
||||||
sql`(uuid_nil(), 'vocabulary', ${JSON.parse(
|
const english = JSON.parse(r.english)
|
||||||
r.entry
|
|
||||||
)}, ${`edict:${r.id}`}, ${{
|
return sql`(uuid_nil(), 'vocabulary', ${JSON.parse(
|
||||||
_: JSON.parse(r.reading),
|
r.entry
|
||||||
}}, ${{ en: JSON.parse(r.english) }})`
|
)}, ${`edict:${r.id}`}, ${{
|
||||||
),
|
_: reading.length ? reading : undefined,
|
||||||
|
}}, ${{
|
||||||
|
en: english.length ? english : undefined,
|
||||||
|
}})`
|
||||||
|
}),
|
||||||
','
|
','
|
||||||
)}
|
)}
|
||||||
ON CONFLICT DO NOTHING;
|
ON CONFLICT DO NOTHING;
|
||||||
@@ -82,6 +87,22 @@ async function main(db: ConnectionPool) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (require.main === module) {
|
export async function constraint(db: ConnectionPool) {
|
||||||
main(createConnectionPool({ bigIntMode: 'number' }))
|
const sReading = S.object().additionalProperties(
|
||||||
|
S.list(S.string()).minItems(1)
|
||||||
|
)
|
||||||
|
|
||||||
|
const colName = sql`translation`
|
||||||
|
const constraintName = sql`c_entry_${colName}`
|
||||||
|
|
||||||
|
await db.query(sql`
|
||||||
|
ALTER TABLE "entry" DROP CONSTRAINT IF EXISTS ${constraintName};
|
||||||
|
ALTER TABLE "entry" ADD CONSTRAINT ${constraintName} CHECK (validate_json_schema('${sql.__dangerous__rawValue(
|
||||||
|
JSON.stringify(sReading.valueOf())
|
||||||
|
)}', ${colName}))
|
||||||
|
`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
constraint(createConnectionPool({ bigIntMode: 'number' }))
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user