-
Notifications
You must be signed in to change notification settings - Fork 22
/
migrations.py
191 lines (174 loc) · 5.01 KB
/
migrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
async def m001_initial(db):
"""
Initial merchants table.
"""
await db.execute(
"""
CREATE TABLE nostrmarket.merchants (
user_id TEXT NOT NULL,
id TEXT PRIMARY KEY,
private_key TEXT NOT NULL,
public_key TEXT NOT NULL,
meta TEXT NOT NULL DEFAULT '{}'
);
"""
)
"""
Initial stalls table.
"""
await db.execute(
"""
CREATE TABLE nostrmarket.stalls (
merchant_id TEXT NOT NULL,
id TEXT PRIMARY KEY,
wallet TEXT NOT NULL,
name TEXT NOT NULL,
currency TEXT,
zones TEXT NOT NULL DEFAULT '[]',
meta TEXT NOT NULL DEFAULT '{}'
);
"""
)
"""
Initial products table.
"""
await db.execute(
"""
CREATE TABLE nostrmarket.products (
merchant_id TEXT NOT NULL,
id TEXT PRIMARY KEY,
stall_id TEXT NOT NULL,
name TEXT NOT NULL,
image_urls TEXT DEFAULT '[]',
price REAL NOT NULL,
quantity INTEGER NOT NULL,
category_list TEXT DEFAULT '[]',
meta TEXT NOT NULL DEFAULT '{}'
);
"""
)
"""
Initial zones table.
"""
await db.execute(
"""
CREATE TABLE nostrmarket.zones (
id TEXT PRIMARY KEY,
merchant_id TEXT NOT NULL,
name TEXT NOT NULL,
currency TEXT NOT NULL,
cost REAL NOT NULL,
regions TEXT NOT NULL DEFAULT '[]'
);
"""
)
"""
Initial orders table.
"""
empty_object = "{}"
await db.execute(
f"""
CREATE TABLE nostrmarket.orders (
merchant_id TEXT NOT NULL,
id TEXT PRIMARY KEY,
event_id TEXT,
event_created_at INTEGER NOT NULL,
public_key TEXT NOT NULL,
merchant_public_key TEXT NOT NULL,
contact_data TEXT NOT NULL DEFAULT '{empty_object}',
extra_data TEXT NOT NULL DEFAULT '{empty_object}',
order_items TEXT NOT NULL,
address TEXT,
total REAL NOT NULL,
shipping_id TEXT NOT NULL,
stall_id TEXT NOT NULL,
invoice_id TEXT NOT NULL,
paid BOOLEAN NOT NULL DEFAULT false,
shipped BOOLEAN NOT NULL DEFAULT false,
time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
UNIQUE(event_id)
);
"""
)
"""
Initial chat messages table.
"""
await db.execute(
f"""
CREATE TABLE nostrmarket.direct_messages (
merchant_id TEXT NOT NULL,
id TEXT PRIMARY KEY,
event_id TEXT,
event_created_at INTEGER NOT NULL,
message TEXT NOT NULL,
public_key TEXT NOT NULL,
incoming BOOLEAN NOT NULL DEFAULT false,
time TIMESTAMP NOT NULL DEFAULT {db.timestamp_now},
UNIQUE(event_id)
);
"""
)
if db.type != "SQLITE":
"""
Create indexes for message fetching
"""
await db.execute(
"""
CREATE INDEX idx_messages_timestamp
ON nostrmarket.direct_messages (time DESC)
"""
)
await db.execute(
"CREATE INDEX idx_event_id ON nostrmarket.direct_messages (event_id)"
)
"""
Initial customers table.
"""
await db.execute(
"""
CREATE TABLE nostrmarket.customers (
merchant_id TEXT NOT NULL,
public_key TEXT NOT NULL,
event_created_at INTEGER,
unread_messages INTEGER NOT NULL DEFAULT 1,
meta TEXT NOT NULL DEFAULT '{}'
);
"""
)
async def m002_update_stall_and_product(db):
await db.execute(
"""
ALTER TABLE nostrmarket.stalls
ADD COLUMN pending BOOLEAN NOT NULL DEFAULT false;
"""
)
await db.execute("ALTER TABLE nostrmarket.stalls ADD COLUMN event_id TEXT;")
await db.execute(
"ALTER TABLE nostrmarket.stalls ADD COLUMN event_created_at INTEGER;"
)
await db.execute(
"""
ALTER TABLE nostrmarket.products
ADD COLUMN pending BOOLEAN NOT NULL DEFAULT false;
"""
)
await db.execute("ALTER TABLE nostrmarket.products ADD COLUMN event_id TEXT;")
await db.execute(
"ALTER TABLE nostrmarket.products ADD COLUMN event_created_at INTEGER;"
)
async def m003_update_direct_message_type(db):
await db.execute(
"""
ALTER TABLE nostrmarket.direct_messages
ADD COLUMN type INTEGER NOT NULL DEFAULT -1;
"""
)
async def m004_add_merchant_timestamp(db):
await db.execute("ALTER TABLE nostrmarket.merchants ADD COLUMN time TIMESTAMP;")
async def m005_update_product_activation(db):
await db.execute(
"""
ALTER TABLE nostrmarket.products
ADD COLUMN active BOOLEAN NOT NULL DEFAULT true;
"""
)