Skip to content

Commit 426f312

Browse files
committed
Add Grafana schema and views for document state management
1 parent c3c8667 commit 426f312

4 files changed

Lines changed: 31 additions & 5 deletions

File tree

welearn_database/alembic/env.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,16 @@ def run_migrations_online() -> None:
7979
if "prod" in os.getenv("PG_HOST").lower():
8080
print("Connecting to production database for migrations!")
8181
input("Press Enter to continue...")
82-
elif "dev" in os.getenv("PG_HOST" "").lower():
83-
print("Connecting to development database for migrations!")
82+
elif (
83+
"dev" in os.getenv("PG_HOST" "").lower()
84+
and "staging" in os.getenv("PG_DB" "").lower()
85+
):
86+
print("Connecting to staging database for migrations!")
87+
elif (
88+
"dev" in os.getenv("PG_HOST" "").lower()
89+
and not "staging" in os.getenv("PG_DB" "").lower()
90+
):
91+
print("Connecting to dev database for migrations!")
8492
else:
8593
print("Connecting to unknown database for migrations!")
8694

welearn_database/alembic/versions/16ff997426d3_remove_error_retrieval_unique_constraint.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
from typing import Sequence, Union
1010

1111
import sqlalchemy as sa
12-
from sqlalchemy.dialects import postgresql
13-
1412
from alembic import op
13+
from sqlalchemy.dialects import postgresql
1514

1615
# revision identifiers, used by Alembic.
1716
revision: str = "16ff997426d3"
@@ -21,9 +20,10 @@
2120

2221

2322
def upgrade() -> None:
24-
op.drop_index(
23+
op.drop_constraint(
2524
"error_retrieval_document_id_http_error_code_idx",
2625
"error_retrieval",
26+
type_="unique",
2727
schema="document_related",
2828
)
2929

welearn_database/alembic/versions/4c7161819e5a_grafana_views.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919

2020
def upgrade():
21+
op.execute("CREATE SCHEMA IF NOT EXISTS grafana;")
22+
2123
op.execute(
2224
"""
2325
CREATE OR REPLACE VIEW grafana.corpus
@@ -162,6 +164,21 @@ def upgrade():
162164
op.execute(
163165
"""
164166
CREATE OR REPLACE VIEW grafana.document_latest_state
167+
AS SELECT DISTINCT ON (ps.document_id) ps.id,
168+
ps.document_id,
169+
wd.corpus_id,
170+
wd.lang,
171+
ps.title,
172+
ps.created_at,
173+
ps.operation_order
174+
FROM document_related.process_state ps
175+
JOIN document_related.welearn_document wd ON ps.document_id = wd.id
176+
ORDER BY ps.document_id, ps.operation_order DESC;
177+
"""
178+
)
179+
op.execute(
180+
"""
181+
CREATE OR REPLACE VIEW grafana.test_document_latest_state
165182
AS SELECT DISTINCT ON (ps.document_id) ps.id,
166183
ps.document_id,
167184
wd.corpus_id,

welearn_database/alembic/versions/821173cf9c5d_initial_migration.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ def upgrade() -> None:
258258
sa.ForeignKeyConstraint(
259259
["user_id"],
260260
["user_related.user_profile.id"],
261+
name="message_user_id_fkey",
261262
),
262263
sa.PrimaryKeyConstraint("id"),
263264
schema="user_related",

0 commit comments

Comments
 (0)