Fix for postgres compatibility

This commit is contained in:
Tiago vasconcelos 2021-08-13 16:31:07 +01:00 committed by fiatjaf
parent da7f7fb618
commit 20ecfb4810
3 changed files with 78 additions and 20 deletions

View file

@ -29,14 +29,14 @@ async def create_copilot(
lnurl_title: Optional[str] = None, lnurl_title: Optional[str] = None,
show_message: Optional[int] = 0, show_message: Optional[int] = 0,
show_ack: Optional[int] = 0, show_ack: Optional[int] = 0,
show_price: Optional[int] = 0, show_price: Optional[str] = None,
amount_made: Optional[int] = None, amount_made: Optional[int] = None,
) -> Copilots: ) -> Copilots:
copilot_id = urlsafe_short_hash() copilot_id = urlsafe_short_hash()
await db.execute( await db.execute(
""" """
INSERT INTO copilots ( INSERT INTO copilot.copilots (
id, id,
"user", "user",
lnurl_toggle, lnurl_toggle,
@ -55,15 +55,14 @@ async def create_copilot(
show_message, show_message,
show_ack, show_ack,
show_price, show_price,
lnurl_title,
amount_made amount_made
) )
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""", """,
( (
copilot_id, copilot_id,
user, user,
lnurl_toggle, int(lnurl_toggle),
wallet, wallet,
title, title,
animation1, animation1,
@ -76,10 +75,9 @@ async def create_copilot(
animation2webhook, animation2webhook,
animation3webhook, animation3webhook,
lnurl_title, lnurl_title,
show_message, int(show_message),
show_ack, int(show_ack),
show_price, show_price,
lnurl_title,
0, 0,
), ),
) )
@ -89,21 +87,21 @@ async def create_copilot(
async def update_copilot(copilot_id: str, **kwargs) -> Optional[Copilots]: async def update_copilot(copilot_id: str, **kwargs) -> Optional[Copilots]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()]) q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute( await db.execute(
f"UPDATE copilots SET {q} WHERE id = ?", (*kwargs.values(), copilot_id) f"UPDATE copilot.copilots SET {q} WHERE id = ?", (*kwargs.values(), copilot_id)
) )
row = await db.fetchone("SELECT * FROM copilots WHERE id = ?", (copilot_id,)) row = await db.fetchone("SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,))
return Copilots.from_row(row) if row else None return Copilots.from_row(row) if row else None
async def get_copilot(copilot_id: str) -> Copilots: async def get_copilot(copilot_id: str) -> Copilots:
row = await db.fetchone("SELECT * FROM copilots WHERE id = ?", (copilot_id,)) row = await db.fetchone("SELECT * FROM copilot.copilots WHERE id = ?", (copilot_id,))
return Copilots.from_row(row) if row else None return Copilots.from_row(row) if row else None
async def get_copilots(user: str) -> List[Copilots]: async def get_copilots(user: str) -> List[Copilots]:
rows = await db.fetchall("""SELECT * FROM copilots WHERE "user" = ?""", (user,)) rows = await db.fetchall("""SELECT * FROM copilot.copilots WHERE "user" = ?""", (user,))
return [Copilots.from_row(row) for row in rows] return [Copilots.from_row(row) for row in rows]
async def delete_copilot(copilot_id: str) -> None: async def delete_copilot(copilot_id: str) -> None:
await db.execute("DELETE FROM copilots WHERE id = ?", (copilot_id,)) await db.execute("DELETE FROM copilot.copilots WHERE id = ?", (copilot_id,))

View file

@ -31,3 +31,46 @@ async def m001_initial(db):
); );
""" """
) )
async def m002_fix_data_types(db):
"""
Fix data types.
"""
if(db.type != "SQLITE"):
await db.execute("ALTER TABLE copilot.copilots ALTER COLUMN show_price TYPE TEXT;")
# If needed, migration for SQLite (RENAME not working properly)
#
# await db.execute(
# f"""
# CREATE TABLE copilot.new_copilots (
# id TEXT NOT NULL PRIMARY KEY,
# "user" TEXT,
# title TEXT,
# lnurl_toggle INTEGER,
# wallet TEXT,
# animation1 TEXT,
# animation2 TEXT,
# animation3 TEXT,
# animation1threshold INTEGER,
# animation2threshold INTEGER,
# animation3threshold INTEGER,
# animation1webhook TEXT,
# animation2webhook TEXT,
# animation3webhook TEXT,
# lnurl_title TEXT,
# show_message INTEGER,
# show_ack INTEGER,
# show_price TEXT,
# amount_made INTEGER,
# fullscreen_cam INTEGER,
# iframe_url TEXT,
# timestamp TIMESTAMP NOT NULL DEFAULT {db.timestamp_now}
# );
# """
# )
#
# await db.execute("INSERT INTO copilot.new_copilots SELECT * FROM copilot.copilots;")
# await db.execute("DROP TABLE IF EXISTS copilot.copilots;")
# await db.execute("ALTER TABLE copilot.new_copilots RENAME TO copilot.copilots;")

View file

@ -49,6 +49,7 @@
<q-th style="width: 5%"></q-th> <q-th style="width: 5%"></q-th>
<q-th style="width: 5%"></q-th> <q-th style="width: 5%"></q-th>
<q-th style="width: 5%"></q-th> <q-th style="width: 5%"></q-th>
<q-th style="width: 5%"></q-th>
<q-th <q-th
v-for="col in props.cols" v-for="col in props.cols"
@ -59,13 +60,13 @@
<div v-if="col.name == 'id'"></div> <div v-if="col.name == 'id'"></div>
<div v-else>{{ col.label }}</div> <div v-else>{{ col.label }}</div>
</q-th> </q-th>
<q-th auto-width></q-th> <!-- <q-th auto-width></q-th> -->
</q-tr> </q-tr>
</template> </template>
<template v-slot:body="props"> <template v-slot:body="props">
<q-tr :props="props"> <q-tr :props="props">
<q-td auto-width> <q-td>
<q-btn <q-btn
unelevated unelevated
dense dense
@ -77,7 +78,7 @@
<q-tooltip> Panel </q-tooltip> <q-tooltip> Panel </q-tooltip>
</q-btn> </q-btn>
</q-td> </q-td>
<q-td auto-width> <q-td>
<q-btn <q-btn
unelevated unelevated
dense dense
@ -89,7 +90,7 @@
<q-tooltip> Compose window </q-tooltip> <q-tooltip> Compose window </q-tooltip>
</q-btn> </q-btn>
</q-td> </q-td>
<q-td auto-width> <q-td>
<q-btn <q-btn
flat flat
dense dense
@ -101,7 +102,7 @@
<q-tooltip> Delete copilot </q-tooltip> <q-tooltip> Delete copilot </q-tooltip>
</q-btn> </q-btn>
</q-td> </q-td>
<q-td auto-width> <q-td>
<q-btn <q-btn
flat flat
dense dense
@ -206,8 +207,9 @@
dense dense
v-model.trim="formDialogCopilot.data.animation1threshold" v-model.trim="formDialogCopilot.data.animation1threshold"
type="number" type="number"
label="From *sats" step="1"
:min="10" label="From *sats (min. 10)"
:rules="[ val => val >= 10 || 'Please use minimum 10' ]"
> >
</q-input> </q-input>
</div> </div>
@ -254,6 +256,7 @@
dense dense
v-model="formDialogCopilot.data.animation2threshold" v-model="formDialogCopilot.data.animation2threshold"
type="number" type="number"
step="1"
label="From *sats" label="From *sats"
:min="formDialogCopilot.data.animation1threshold" :min="formDialogCopilot.data.animation1threshold"
> >
@ -302,6 +305,7 @@
dense dense
v-model="formDialogCopilot.data.animation3threshold" v-model="formDialogCopilot.data.animation3threshold"
type="number" type="number"
step="1"
label="From *sats" label="From *sats"
:min="formDialogCopilot.data.animation2threshold" :min="formDialogCopilot.data.animation2threshold"
> >
@ -469,8 +473,10 @@
cancelCopilot: function (data) { cancelCopilot: function (data) {
var self = this var self = this
self.formDialogCopilot.show = false self.formDialogCopilot.show = false
self.clearFormDialogCopilot()
}, },
closeFormDialog: function () { closeFormDialog: function () {
this.clearFormDialogCopilot()
this.formDialog.data = { this.formDialog.data = {
is_unique: false is_unique: false
} }
@ -512,6 +518,7 @@
.then(function (response) { .then(function (response) {
self.CopilotLinks.push(mapCopilot(response.data)) self.CopilotLinks.push(mapCopilot(response.data))
self.formDialogCopilot.show = false self.formDialogCopilot.show = false
self.clearFormDialogCopilot()
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
@ -621,11 +628,21 @@
}) })
self.CopilotLinks.push(mapCopilot(response.data)) self.CopilotLinks.push(mapCopilot(response.data))
self.formDialogCopilot.show = false self.formDialogCopilot.show = false
self.clearFormDialogCopilot()
}) })
.catch(function (error) { .catch(function (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
}) })
}, },
clearFormDialogCopilot(){
this.formDialogCopilot.data = {
lnurl_toggle: false,
show_message: false,
show_ack: false,
show_price: 'None',
title: ''
}
},
exportcopilotCSV: function () { exportcopilotCSV: function () {
var self = this var self = this
LNbits.utils.exportCSV(self.CopilotsTable.columns, this.CopilotLinks) LNbits.utils.exportCSV(self.CopilotsTable.columns, this.CopilotLinks)