diff --git a/src/lib/server/databases/configuration.ts b/src/lib/server/databases/configuration.ts index 807eaea..01c3989 100644 --- a/src/lib/server/databases/configuration.ts +++ b/src/lib/server/databases/configuration.ts @@ -88,6 +88,13 @@ export async function configureDatabase() { ` } + if (version.number < 8) { + await db` + ALTER TABLE IF EXISTS texte_version + ADD COLUMN IF NOT EXISTS nature_et_num text + ` + } + // Types // Tables @@ -160,6 +167,7 @@ export async function configureDatabase() { commission_fond_assemblee_uid text, est_texte_principal boolean, nature text, + nature_et_num text, text_search tsvector NOT NULL ) ` @@ -244,6 +252,26 @@ export async function configureDatabase() { ` } + if (version.number < 8) { + // Fill "nature_et_num" column of table texte_version; + for await (const rows of db< + Array<{ data: TexteVersion; id: string }> + >`SELECT data, id FROM texte_version`.cursor(100)) { + for (const { data, id } of rows) { + const natureEtNum = + data.META.META_COMMUN.NATURE !== undefined && + data.META.META_SPEC.META_TEXTE_CHRONICLE.NUM !== undefined + ? `${data.META.META_COMMUN.NATURE.toUpperCase()}.${data.META.META_SPEC.META_TEXTE_CHRONICLE.NUM}` + : null + await db` + UPDATE texte_version + SET nature_et_num = ${natureEtNum} + WHERE id = ${id} + ` + } + } + } + // Add indexes once every table and column exists. await db` @@ -257,8 +285,8 @@ export async function configureDatabase() { ` await db` - CREATE INDEX IF NOT EXISTS texte_version_nature_key - ON texte_version (nature) + CREATE INDEX IF NOT EXISTS texte_version_nature_et_num_key + ON texte_version (nature_et_num) ` // await db` diff --git a/src/lib/server/databases/index.ts b/src/lib/server/databases/index.ts index a842c3a..2fee606 100644 --- a/src/lib/server/databases/index.ts +++ b/src/lib/server/databases/index.ts @@ -23,7 +23,7 @@ export const db = postgres({ port: config.db.port, user: config.db.user, }) -export const versionNumber = 7 +export const versionNumber = 8 /// Check that assemblee database exists and is up to date. export async function checkAssembleeDb(): Promise { diff --git a/src/scripts/import_jorf.ts b/src/scripts/import_jorf.ts index dcf62ef..61bea68 100644 --- a/src/scripts/import_jorf.ts +++ b/src/scripts/import_jorf.ts @@ -355,16 +355,24 @@ async function importJorf( texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITRE, texteVersion.META.META_SPEC.META_TEXTE_VERSION.TITREFULL, ].filter((text) => text !== undefined) + const natureEtNum = + texteVersion.META.META_COMMUN.NATURE !== undefined && + texteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NUM !== + undefined + ? `${texteVersion.META.META_COMMUN.NATURE.toUpperCase()}.${texteVersion.META.META_SPEC.META_TEXTE_CHRONICLE.NUM}` + : null await db` INSERT INTO texte_version ( id, data, nature, + nature_et_num, text_search ) VALUES ( ${texteVersion.META.META_COMMUN.ID}, ${db.json(texteVersion as unknown as JSONValue)}, ${texteVersion.META.META_COMMUN.NATURE ?? null}, + ${natureEtNum}, setweight(to_tsvector('french', ${textAFragments.join( " ", )}), 'A') @@ -373,6 +381,7 @@ async function importJorf( DO UPDATE SET data = ${db.json(texteVersion as unknown as JSONValue)}, nature = ${texteVersion.META.META_COMMUN.NATURE ?? null}, + nature_et_num = ${natureEtNum} text_search = setweight(to_tsvector('french', ${textAFragments.join( " ", )}), 'A')