postgres support.

This commit is contained in:
fiatjaf
2021-07-02 17:34:31 -03:00
parent eeb3e66529
commit 2f309c9863
54 changed files with 624 additions and 540 deletions
+21 -17
View File
@@ -9,7 +9,7 @@ from .models import Livestream, Track, Producer
async def create_livestream(*, wallet_id: str) -> int:
result = await db.execute(
"""
INSERT INTO livestreams (wallet)
INSERT INTO livestream.livestreams (wallet)
VALUES (?)
""",
(wallet_id,),
@@ -18,14 +18,16 @@ async def create_livestream(*, wallet_id: str) -> int:
async def get_livestream(ls_id: int) -> Optional[Livestream]:
row = await db.fetchone("SELECT * FROM livestreams WHERE id = ?", (ls_id,))
row = await db.fetchone(
"SELECT * FROM livestream.livestreams WHERE id = ?", (ls_id,)
)
return Livestream(**dict(row)) if row else None
async def get_livestream_by_track(track_id: int) -> Optional[Livestream]:
row = await db.fetchone(
"""
SELECT livestreams.* FROM livestreams
SELECT livestreams.* FROM livestream.livestreams
INNER JOIN tracks ON tracks.livestream = livestreams.id
WHERE tracks.id = ?
""",
@@ -35,7 +37,9 @@ async def get_livestream_by_track(track_id: int) -> Optional[Livestream]:
async def get_or_create_livestream_by_wallet(wallet: str) -> Optional[Livestream]:
row = await db.fetchone("SELECT * FROM livestreams WHERE wallet = ?", (wallet,))
row = await db.fetchone(
"SELECT * FROM livestream.livestreams WHERE wallet = ?", (wallet,)
)
if not row:
# create on the fly
@@ -47,14 +51,14 @@ async def get_or_create_livestream_by_wallet(wallet: str) -> Optional[Livestream
async def update_current_track(ls_id: int, track_id: Optional[int]):
await db.execute(
"UPDATE livestreams SET current_track = ? WHERE id = ?",
"UPDATE livestream.livestreams SET current_track = ? WHERE id = ?",
(track_id, ls_id),
)
async def update_livestream_fee(ls_id: int, fee_pct: int):
await db.execute(
"UPDATE livestreams SET fee_pct = ? WHERE id = ?",
"UPDATE livestream.livestreams SET fee_pct = ? WHERE id = ?",
(fee_pct, ls_id),
)
@@ -68,7 +72,7 @@ async def add_track(
) -> int:
result = await db.execute(
"""
INSERT INTO tracks (livestream, name, download_url, price_msat, producer)
INSERT INTO livestream.tracks (livestream, name, download_url, price_msat, producer)
VALUES (?, ?, ?, ?, ?)
""",
(livestream, name, download_url, price_msat, producer),
@@ -86,7 +90,7 @@ async def update_track(
) -> int:
result = await db.execute(
"""
UPDATE tracks SET
UPDATE livestream.tracks SET
name = ?,
download_url = ?,
price_msat = ?,
@@ -105,7 +109,7 @@ async def get_track(track_id: Optional[int]) -> Optional[Track]:
row = await db.fetchone(
"""
SELECT id, download_url, price_msat, name, producer
FROM tracks WHERE id = ?
FROM livestream.tracks WHERE id = ?
""",
(track_id,),
)
@@ -116,7 +120,7 @@ async def get_tracks(livestream: int) -> List[Track]:
rows = await db.fetchall(
"""
SELECT id, download_url, price_msat, name, producer
FROM tracks WHERE livestream = ?
FROM livestream.tracks WHERE livestream = ?
""",
(livestream,),
)
@@ -126,7 +130,7 @@ async def get_tracks(livestream: int) -> List[Track]:
async def delete_track_from_livestream(livestream: int, track_id: int):
await db.execute(
"""
DELETE FROM tracks WHERE livestream = ? AND id = ?
DELETE FROM livestream.tracks WHERE livestream = ? AND id = ?
""",
(livestream, track_id),
)
@@ -137,7 +141,7 @@ async def add_producer(livestream: int, name: str) -> int:
existing = await db.fetchall(
"""
SELECT id FROM producers
SELECT id FROM livestream.producers
WHERE livestream = ? AND lower(name) = ?
""",
(livestream, name.lower()),
@@ -150,7 +154,7 @@ async def add_producer(livestream: int, name: str) -> int:
result = await db.execute(
"""
INSERT INTO producers (livestream, name, user, wallet)
INSERT INTO livestream.producers (livestream, name, "user", wallet)
VALUES (?, ?, ?, ?)
""",
(livestream, name, user.id, wallet.id),
@@ -161,8 +165,8 @@ async def add_producer(livestream: int, name: str) -> int:
async def get_producer(producer_id: int) -> Optional[Producer]:
row = await db.fetchone(
"""
SELECT id, user, wallet, name
FROM producers WHERE id = ?
SELECT id, "user", wallet, name
FROM livestream.producers WHERE id = ?
""",
(producer_id,),
)
@@ -172,8 +176,8 @@ async def get_producer(producer_id: int) -> Optional[Producer]:
async def get_producers(livestream: int) -> List[Producer]:
rows = await db.fetchall(
"""
SELECT id, user, wallet, name
FROM producers WHERE livestream = ?
SELECT id, "user", wallet, name
FROM livestream.producers WHERE livestream = ?
""",
(livestream,),
)
+13 -13
View File
@@ -3,9 +3,9 @@ async def m001_initial(db):
Initial livestream tables.
"""
await db.execute(
"""
CREATE TABLE IF NOT EXISTS livestreams (
id INTEGER PRIMARY KEY AUTOINCREMENT,
f"""
CREATE TABLE livestream.livestreams (
id {db.serial_primary_key},
wallet TEXT NOT NULL,
fee_pct INTEGER NOT NULL DEFAULT 10,
current_track INTEGER
@@ -14,11 +14,11 @@ async def m001_initial(db):
)
await db.execute(
"""
CREATE TABLE IF NOT EXISTS producers (
livestream INTEGER NOT NULL REFERENCES livestreams (id),
id INTEGER PRIMARY KEY AUTOINCREMENT,
user TEXT NOT NULL,
f"""
CREATE TABLE livestream.producers (
livestream INTEGER NOT NULL REFERENCES {db.references_schema}livestreams (id),
id {db.serial_primary_key},
"user" TEXT NOT NULL,
wallet TEXT NOT NULL,
name TEXT NOT NULL
);
@@ -26,14 +26,14 @@ async def m001_initial(db):
)
await db.execute(
"""
CREATE TABLE IF NOT EXISTS tracks (
livestream INTEGER NOT NULL REFERENCES livestreams (id),
id INTEGER PRIMARY KEY AUTOINCREMENT,
f"""
CREATE TABLE livestream.tracks (
livestream INTEGER NOT NULL REFERENCES {db.references_schema}livestreams (id),
id {db.serial_primary_key},
download_url TEXT,
price_msat INTEGER NOT NULL DEFAULT 0,
name TEXT,
producer INTEGER REFERENCES producers (id) NOT NULL
producer INTEGER REFERENCES {db.references_schema}producers (id) NOT NULL
);
"""
)
+1 -1
View File
@@ -49,7 +49,7 @@ async def on_invoice_paid(payment: Payment) -> None:
# and reduce it by the amount we're going to send to the producer
await core_db.execute(
"""
UPDATE apipayments
UPDATE livestream.apipayments
SET extra = ?, amount = ?
WHERE hash = ?
AND checking_id NOT LIKE 'internal_%'