lnbits-legend/lnbits/extensions/market/migrations.py

157 lines
3.8 KiB
Python
Raw Normal View History

2022-01-27 12:24:38 +00:00
async def m001_initial(db):
2022-12-22 18:26:34 +00:00
"""
2023-01-04 21:08:28 +00:00
Initial Market settings table.
2022-12-22 18:26:34 +00:00
"""
await db.execute(
"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.settings (
2022-12-22 18:26:34 +00:00
"user" TEXT PRIMARY KEY,
currency TEXT DEFAULT 'sat',
fiat_base_multiplier INTEGER DEFAULT 1
);
"""
)
2022-01-27 12:24:38 +00:00
"""
2022-07-25 10:11:03 +01:00
Initial stalls table.
2022-01-27 12:24:38 +00:00
"""
await db.execute(
"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.stalls (
2022-01-27 12:24:38 +00:00
id TEXT PRIMARY KEY,
2022-07-25 10:11:03 +01:00
wallet TEXT NOT NULL,
name TEXT NOT NULL,
currency TEXT,
2022-07-25 10:11:03 +01:00
publickey TEXT,
relays TEXT,
2022-08-18 10:49:04 +01:00
shippingzones TEXT NOT NULL,
rating INTEGER DEFAULT 0
2022-01-27 12:24:38 +00:00
);
"""
)
"""
2022-07-25 10:11:03 +01:00
Initial products table.
2022-01-27 12:24:38 +00:00
"""
await db.execute(
2022-08-18 10:49:04 +01:00
f"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.products (
2022-01-27 12:24:38 +00:00
id TEXT PRIMARY KEY,
2022-12-22 10:45:56 +00:00
stall TEXT NOT NULL REFERENCES {db.references_schema}stalls (id) ON DELETE CASCADE,
2022-07-25 10:11:03 +01:00
product TEXT NOT NULL,
categories TEXT,
description TEXT,
image TEXT,
price INTEGER NOT NULL,
quantity INTEGER NOT NULL,
2022-08-18 10:49:04 +01:00
rating INTEGER DEFAULT 0
2022-01-27 12:24:38 +00:00
);
"""
)
"""
Initial zones table.
"""
await db.execute(
"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.zones (
2022-01-27 12:24:38 +00:00
id TEXT PRIMARY KEY,
"user" TEXT NOT NULL,
2022-01-27 12:24:38 +00:00
cost TEXT NOT NULL,
countries TEXT NOT NULL
);
"""
)
"""
Initial orders table.
"""
await db.execute(
2022-08-18 10:49:04 +01:00
f"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.orders (
2022-07-25 10:11:03 +01:00
id {db.serial_primary_key},
2022-08-16 12:08:27 +01:00
wallet TEXT NOT NULL,
2022-08-18 10:49:04 +01:00
username TEXT,
2022-08-16 12:08:27 +01:00
pubkey TEXT,
2022-08-18 10:49:04 +01:00
shippingzone TEXT NOT NULL,
2022-01-27 12:24:38 +00:00
address TEXT NOT NULL,
email TEXT NOT NULL,
2022-08-16 12:08:27 +01:00
total INTEGER NOT NULL,
2022-01-27 12:24:38 +00:00
invoiceid TEXT NOT NULL,
paid BOOLEAN NOT NULL,
shipped BOOLEAN NOT NULL,
time TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
+ """
2022-01-27 12:24:38 +00:00
);
"""
)
2022-07-25 10:11:03 +01:00
"""
Initial order details table.
"""
await db.execute(
2022-08-18 10:49:04 +01:00
f"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.order_details (
2022-07-25 10:11:03 +01:00
id TEXT PRIMARY KEY,
2022-12-22 10:45:56 +00:00
order_id INTEGER NOT NULL REFERENCES {db.references_schema}orders (id) ON DELETE CASCADE,
product_id TEXT NOT NULL REFERENCES {db.references_schema}products (id) ON DELETE CASCADE,
2022-08-16 12:08:27 +01:00
quantity INTEGER NOT NULL
2022-07-25 10:11:03 +01:00
);
"""
)
2022-07-27 15:28:58 +01:00
"""
Initial market table.
"""
await db.execute(
"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.markets (
2022-07-27 15:28:58 +01:00
id TEXT PRIMARY KEY,
usr TEXT NOT NULL,
name TEXT
);
"""
)
"""
Initial market stalls table.
"""
await db.execute(
2022-08-18 10:49:04 +01:00
f"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.market_stalls (
2022-07-27 15:28:58 +01:00
id TEXT PRIMARY KEY,
2022-12-22 10:45:56 +00:00
marketid TEXT NOT NULL REFERENCES {db.references_schema}markets (id) ON DELETE CASCADE,
stallid TEXT NOT NULL REFERENCES {db.references_schema}stalls (id) ON DELETE CASCADE
2022-07-27 15:28:58 +01:00
);
"""
)
2022-09-30 10:16:34 +01:00
"""
Initial chat messages table.
"""
await db.execute(
f"""
2023-01-04 21:08:28 +00:00
CREATE TABLE market.messages (
2022-09-30 10:16:34 +01:00
id {db.serial_primary_key},
msg TEXT NOT NULL,
pubkey TEXT NOT NULL,
id_conversation TEXT NOT NULL,
timestamp TIMESTAMP NOT NULL DEFAULT """
+ db.timestamp_now
2023-01-21 15:30:11 +00:00
+ """
2022-09-30 10:16:34 +01:00
);
"""
)
2022-11-15 00:59:41 +00:00
if db.type != "SQLITE":
"""
Create indexes for message fetching
"""
await db.execute(
2023-01-04 21:08:28 +00:00
"CREATE INDEX idx_messages_timestamp ON market.messages (timestamp DESC)"
2022-11-15 00:59:41 +00:00
)
await db.execute(
2023-01-04 21:08:28 +00:00
"CREATE INDEX idx_messages_conversations ON market.messages (id_conversation)"
2022-11-15 00:59:41 +00:00
)