Maintain order on languages.

main
Joshua Potter 2023-12-05 16:06:04 -07:00
parent f20fc76081
commit 0b9a721368
3 changed files with 12 additions and 6 deletions

View File

@ -52,19 +52,19 @@ def load_languages(conn):
cursor = None cursor = None
try: try:
cursor = conn.cursor() cursor = conn.cursor()
for code, name in code_to_lang.items(): for pos, (code, name) in enumerate(list(code_to_lang.items())):
cursor.execute( cursor.execute(
f""" f"""
INSERT INTO {SCHEMA_NAME}.{LANG_TABLE_NAME} INSERT INTO {SCHEMA_NAME}.{LANG_TABLE_NAME}
(code, name) (code, name, pos)
VALUES VALUES
(%s, %s) (%s, %s, %s)
ON CONFLICT ON CONFLICT
(code) (code)
DO UPDATE SET DO UPDATE SET
name = EXCLUDED.name; name = EXCLUDED.name;
""", """,
[code, name], [code, name, pos],
) )
conn.commit() conn.commit()
finally: finally:

View File

@ -1,4 +1,5 @@
import enum import enum
from collections import OrderedDict
class Site(enum.Enum): class Site(enum.Enum):
@ -109,6 +110,10 @@ class Language(enum.Enum):
zu_ZA = "isiZulu" zu_ZA = "isiZulu"
code_to_lang = {lang.name.replace("_", "-"): lang.value for lang in Language} code_to_lang = OrderedDict(
[(lang.name.replace("_", "-"), lang.value) for lang in Language]
)
lang_to_code = {lang.value: lang.name.replace("_", "-") for lang in Language} lang_to_code = OrderedDict(
[(lang.value, lang.name.replace("_", "-")) for lang in Language]
)

View File

@ -27,6 +27,7 @@ CREATE TABLE coach_scraper.languages
( id SERIAL PRIMARY KEY ( id SERIAL PRIMARY KEY
, code VARCHAR(8) NOT NULL , code VARCHAR(8) NOT NULL
, name VARCHAR(128) NOT NULL , name VARCHAR(128) NOT NULL
, pos INTEGER NOT NULL
); );
CREATE UNIQUE INDEX IF NOT EXISTS CREATE UNIQUE INDEX IF NOT EXISTS