annotate libervia/backend/memory/sqla_mapping.py @ 4212:5f2d496c633f

core: get rid of `pickle`: Use of `pickle` to serialise data was a technical legacy that was causing trouble to store in database, to update (if a class was serialised, a change could break update), and to security (pickle can lead to code execution). This patch remove all use of Pickle in favour in JSON, notably: - for caching data, a Pydantic model is now used instead - for SQLAlchemy model, the LegacyPickle is replaced by JSON serialisation - in XEP-0373 a class `PublicKeyMetadata` was serialised. New method `from_dict` and `to_dict` method have been implemented to do serialisation. - new methods to (de)serialise data can now be specified with Identity data types. It is notably used to (de)serialise `path` of avatars. A migration script has been created to convert data (for upgrade or downgrade), with special care for XEP-0373 case. Depending of size of database, this migration script can be long to run. rel 443
author Goffi <goffi@goffi.org>
date Fri, 23 Feb 2024 13:31:04 +0100
parents 2074b2bbe616
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
2
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
5
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
10
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
15
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
18
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
19 from datetime import datetime
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
20 import enum
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
21 import json
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
22 import time
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
23 from typing import Any, Dict
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
24
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from sqlalchemy import (
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
26 Boolean,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
27 Column,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
28 DDL,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
29 DateTime,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
30 Enum,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
31 Float,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
32 ForeignKey,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
33 Index,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
34 Integer,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
35 JSON,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
36 MetaData,
4152
23d21daed216 core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents: 4130
diff changeset
37 text,
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
38 Text,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
39 UniqueConstraint,
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
40 event,
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
41 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from sqlalchemy.orm import declarative_base, relationship
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
43 from sqlalchemy.sql.functions import now
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
44 from sqlalchemy.types import TypeDecorator
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
45 from twisted.words.protocols.jabber import jid
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
46 from wokkel import generic
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
47
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
48 from libervia.backend.core.constants import Const as C
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
49
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
50
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
51 Base = declarative_base(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
52 metadata=MetaData(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
53 naming_convention={
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
54 "ix": "ix_%(column_0_label)s",
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
55 "uq": "uq_%(table_name)s_%(column_0_name)s",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
56 "ck": "ck_%(table_name)s_%(constraint_name)s",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
57 "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
58 "pk": "pk_%(table_name)s",
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
59 }
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
60 )
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
61 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
62 # keys which are in message data extra but not stored in extra field this is
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
63 # because those values are stored in separate fields
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
64 NOT_IN_EXTRA = ("origin_id", "stanza_id", "received_timestamp", "update_uid")
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
65
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
66
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
67 class Profiles(dict):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
68 def __init__(self, *args, **kwargs):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
69 super().__init__(*args, **kwargs)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
70 self.id_to_profile = {v: k for k, v in self.items()}
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
71
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
72 def __setitem__(self, key, value):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
73 super().__setitem__(key, value)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
74 self.id_to_profile[value] = key
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
75
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
76 def __delitem__(self, key):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
77 del self.id_to_profile[self[key]]
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
78 super().__delitem__(key)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
79
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
80 def update(self, *args, **kwargs):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
81 super().update(*args, **kwargs)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
82 self.id_to_profile = {v: k for k, v in self.items()}
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
83
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
84 def clear(self):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
85 super().clear()
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
86 self.id_to_profile.clear()
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
87
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
88
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
89 profiles = Profiles()
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
90
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
91
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
92 def get_profile_by_id( profile_id):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
93 return profiles.id_to_profile.get(profile_id)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
94
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
95
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
96 class SyncState(enum.Enum):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
97 #: synchronisation is currently in progress
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
98 IN_PROGRESS = 1
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
99 #: synchronisation is done
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
100 COMPLETED = 2
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
101 #: something wrong happened during synchronisation, won't sync
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
102 ERROR = 3
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
103 #: synchronisation won't be done even if a syncing analyser matches
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
104 NO_SYNC = 4
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
105
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
106
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
107 class SubscriptionState(enum.Enum):
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
108 SUBSCRIBED = 1
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
109 PENDING = 2
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
110
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
111
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
112 class NotificationType(enum.Enum):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
113 chat = "chat"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
114 blog = "blog"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
115 calendar = "calendar"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
116 file = "file"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
117 call = "call"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
118 service = "service"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
119 other = "other"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
120
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
121
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
122 class NotificationStatus(enum.Enum):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
123 new = "new"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
124 read = "read"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
125
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
126
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
127 class NotificationPriority(enum.IntEnum):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
128 LOW = 10
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
129 MEDIUM = 20
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
130 HIGH = 30
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
131 URGENT = 40
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
132
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
133
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
134 class Json(TypeDecorator):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
135 """Handle JSON field in DB independant way"""
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
136
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # Blob is used on SQLite but gives errors when used here, while Text works fine
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
138 impl = Text
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
139 cache_ok = True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
140
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def process_bind_param(self, value, dialect):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
142 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
143 return None
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4161
diff changeset
144 return json.dumps(value, ensure_ascii=False)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
145
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
146 def process_result_value(self, value, dialect):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
148 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
149 return json.loads(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
150
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
151
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
152 class JsonDefaultDict(Json):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
153 """Json type which convert NULL to empty dict instead of None"""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
154
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
155 def process_result_value(self, value, dialect):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
156 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
157 return {}
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return json.loads(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
159
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
160
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
161 class Xml(TypeDecorator):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
162 impl = Text
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
163 cache_ok = True
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
164
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
165 def process_bind_param(self, value, dialect):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
166 if value is None:
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
167 return None
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
168 return value.toXml()
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
169
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
170 def process_result_value(self, value, dialect):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
171 if value is None:
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
172 return None
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
173 return generic.parseXml(value.encode())
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
174
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
175
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
176 class JID(TypeDecorator):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
177 """Store twisted JID in text fields"""
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
178
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
179 impl = Text
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
180 cache_ok = True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
181
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
182 def process_bind_param(self, value, dialect):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
183 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
184 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
185 return value.full()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
186
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
187 def process_result_value(self, value, dialect):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
188 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
189 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
190 return jid.JID(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
191
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
192
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
193 class Profile(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
194 __tablename__ = "profiles"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
195
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
196 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
197 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
198 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
199 nullable=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
200 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
201 name = Column(Text, unique=True)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
202
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
203 params = relationship("ParamInd", back_populates="profile", passive_deletes=True)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
204 private_data = relationship(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
205 "PrivateInd", back_populates="profile", passive_deletes=True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
206 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
207 private_bin_data = relationship(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
208 "PrivateIndBin", back_populates="profile", passive_deletes=True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
209 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
210
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
211
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
212 class Component(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
213 __tablename__ = "components"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
214
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
215 profile_id = Column(
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
216 ForeignKey("profiles.id", ondelete="CASCADE"), nullable=True, primary_key=True
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
217 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
218 entry_point = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
219 profile = relationship("Profile")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
220
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
221
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
222 class History(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
223 __tablename__ = "history"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
224 __table_args__ = (
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
225 UniqueConstraint("profile_id", "stanza_id", "source", "dest"),
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
226 UniqueConstraint("profile_id", "origin_id", "source", name="uq_origin_id"),
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
227 Index("history__profile_id_timestamp", "profile_id", "timestamp"),
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
228 Index(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
229 "history__profile_id_received_timestamp", "profile_id", "received_timestamp"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
230 ),
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
231 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
232
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
233 uid = Column(Text, primary_key=True)
4152
23d21daed216 core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents: 4130
diff changeset
234 # FIXME: version_id is only needed for changes in `extra` column. It would maybe be
23d21daed216 core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents: 4130
diff changeset
235 # better to use separate table for `extra` data instead.
23d21daed216 core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents: 4130
diff changeset
236 version_id = Column(Integer, nullable=False, server_default=text("1"))
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
237 origin_id = Column(Text)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
238 stanza_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
239 update_uid = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
240 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"))
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
241 source = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
242 dest = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
243 source_res = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
244 dest_res = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
245 timestamp = Column(Float, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
246 received_timestamp = Column(Float)
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
247 type = Column(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
248 Enum(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
249 "chat",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
250 "error",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
251 "groupchat",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
252 "headline",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
253 "normal",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
254 # info is not XMPP standard, but used to keep track of info like join/leave
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
255 # in a MUC
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
256 "info",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
257 name="message_type",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
258 create_constraint=True,
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
259 ),
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
260 nullable=False,
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
261 )
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4161
diff changeset
262 extra = Column(JSON)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
263
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
264 profile = relationship("Profile")
4161
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
265 messages = relationship(
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
266 "Message",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
267 backref="history",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
268 cascade="all, delete-orphan",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
269 passive_deletes=True
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
270 )
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
271 subjects = relationship(
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
272 "Subject",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
273 backref="history",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
274 cascade="all, delete-orphan",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
275 passive_deletes=True
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
276 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
277 thread = relationship(
4161
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
278 "Thread",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
279 uselist=False,
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
280 back_populates="history",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
281 cascade="all, delete-orphan",
2074b2bbe616 core (memory/sqla_mapping): `delete-orphan` in History:
Goffi <goffi@goffi.org>
parents: 4152
diff changeset
282 passive_deletes=True
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
283 )
4152
23d21daed216 core (memory/sqla_mapping): add a `version_id` column to detect race conditions.
Goffi <goffi@goffi.org>
parents: 4130
diff changeset
284 __mapper_args__ = {"version_id_col": version_id}
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
285
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
286 def __init__(self, *args, **kwargs):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
287 source_jid = kwargs.pop("source_jid", None)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
288 if source_jid is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
289 kwargs["source"] = source_jid.userhost()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
290 kwargs["source_res"] = source_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
291 dest_jid = kwargs.pop("dest_jid", None)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
292 if dest_jid is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
293 kwargs["dest"] = dest_jid.userhost()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
294 kwargs["dest_res"] = dest_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
295 super().__init__(*args, **kwargs)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
296
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
297 @property
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
298 def source_jid(self) -> jid.JID:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
299 return jid.JID(f"{self.source}/{self.source_res or ''}")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
300
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
301 @source_jid.setter
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
302 def source_jid(self, source_jid: jid.JID) -> None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
303 self.source = source_jid.userhost
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
304 self.source_res = source_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
305
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
306 @property
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
307 def dest_jid(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
308 return jid.JID(f"{self.dest}/{self.dest_res or ''}")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
309
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
310 @dest_jid.setter
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
311 def dest_jid(self, dest_jid: jid.JID) -> None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
312 self.dest = dest_jid.userhost
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
313 self.dest_res = dest_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
314
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
315 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
316 dt = datetime.fromtimestamp(self.timestamp)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
317 return f"History<{self.source_jid.full()}->{self.dest_jid.full()} [{dt}]>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
318
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
319 def serialise(self):
4026
fe4725bf42fb core (memory/sqla): be sure to have a dict when serialising History.extra:
Goffi <goffi@goffi.org>
parents: 3796
diff changeset
320 extra = self.extra or {}
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
321 if self.origin_id is not None:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
322 extra["origin_id"] = self.origin_id
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
323 if self.stanza_id is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
324 extra["stanza_id"] = self.stanza_id
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
325 if self.update_uid is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
326 extra["update_uid"] = self.update_uid
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
327 if self.received_timestamp is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
328 extra["received_timestamp"] = self.received_timestamp
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
329 if self.thread is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
330 extra["thread"] = self.thread.thread_id
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
331 if self.thread.parent_id is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
332 extra["thread_parent"] = self.thread.parent_id
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
333
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
334 return {
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
335 "from": f"{self.source}/{self.source_res}"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
336 if self.source_res
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
337 else self.source,
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
338 "to": f"{self.dest}/{self.dest_res}" if self.dest_res else self.dest,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
339 "uid": self.uid,
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
340 "message": {m.language or "": m.message for m in self.messages},
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
341 "subject": {m.language or "": m.subject for m in self.subjects},
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
342 "type": self.type,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
343 "extra": extra,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
344 "timestamp": self.timestamp,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
345 }
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
346
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
347 def as_tuple(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
348 d = self.serialise()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
349 return (
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
350 d["uid"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
351 d["timestamp"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
352 d["from"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
353 d["to"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
354 d["message"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
355 d["subject"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
356 d["type"],
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
357 d["extra"],
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
358 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
359
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
360 @staticmethod
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
361 def debug_collection(history_collection):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
362 for idx, history in enumerate(history_collection):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
363 history.debug_msg(idx)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
364
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
365 def debug_msg(self, idx=None):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
366 """Print messages"""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
367 dt = datetime.fromtimestamp(self.timestamp)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
368 if idx is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
369 dt = f"({idx}) {dt}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
370 parts = []
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
371 parts.append(f"[{dt}]<{self.source_jid.full()}->{self.dest_jid.full()}> ")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
372 for message in self.messages:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
373 if message.language:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
374 parts.append(f"[{message.language}] ")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
375 parts.append(f"{message.message}\n")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
376 print("".join(parts))
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
377
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
378
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
379 class Message(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
380 __tablename__ = "message"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
381 __table_args__ = (Index("message__history_uid", "history_uid"),)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
382
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
383 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
384 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
385 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
386 )
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
387 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"), nullable=False)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
388 message = Column(Text, nullable=False)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
389 language = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
390
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
391 def serialise(self) -> Dict[str, Any]:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
392 s = {}
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
393 if self.message:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
394 s["message"] = str(self.message)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
395 if self.language:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
396 s["language"] = str(self.language)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
397 return s
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
398
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
399 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
400 lang_str = f"[{self.language}]" if self.language else ""
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
401 msg = f"{self.message[:20]}…" if len(self.message) > 20 else self.message
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
402 content = f"{lang_str}{msg}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
403 return f"Message<{content}>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
404
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
405
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
406 class Subject(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
407 __tablename__ = "subject"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
408 __table_args__ = (Index("subject__history_uid", "history_uid"),)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
409
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
410 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
411 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
412 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
413 )
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
414 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"), nullable=False)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
415 subject = Column(Text, nullable=False)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
416 language = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
417
3796
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
418 def serialise(self) -> Dict[str, Any]:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
419 s = {}
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
420 if self.subject:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
421 s["subject"] = str(self.subject)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
422 if self.language:
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
423 s["language"] = str(self.language)
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
424 return s
24c1c06c865b core (memory/mapping): add `origin_id` column to History + constraints update:
Goffi <goffi@goffi.org>
parents: 3744
diff changeset
425
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
426 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
427 lang_str = f"[{self.language}]" if self.language else ""
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
428 msg = f"{self.subject[:20]}…" if len(self.subject) > 20 else self.subject
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
429 content = f"{lang_str}{msg}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
430 return f"Subject<{content}>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
431
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
432
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
433 class Thread(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
434 __tablename__ = "thread"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
435 __table_args__ = (Index("thread__history_uid", "history_uid"),)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
436
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
437 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
438 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
439 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
440 )
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
441 history_uid = Column(ForeignKey("history.uid", ondelete="CASCADE"))
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
442 thread_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
443 parent_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
444
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
445 history = relationship("History", uselist=False, back_populates="thread")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
446
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
447 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
448 return f"Thread<{self.thread_id} [parent: {self.parent_id}]>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
449
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
450
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
451 class Notification(Base):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
452 __tablename__ = "notifications"
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
453 __table_args__ = (Index("notifications_profile_id_status", "profile_id", "status"),)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
454
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
455 id = Column(Integer, primary_key=True, autoincrement=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
456 timestamp = Column(Float, nullable=False, default=time.time)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
457 expire_at = Column(Float, nullable=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
458
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
459 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), index=True, nullable=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
460 profile = relationship("Profile")
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
461
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
462 type = Column(Enum(NotificationType), nullable=False)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
463
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
464 title = Column(Text, nullable=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
465 body_plain = Column(Text, nullable=False)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
466 body_rich = Column(Text, nullable=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
467
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
468 requires_action = Column(Boolean, default=False)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
469 priority = Column(Integer, default=NotificationPriority.MEDIUM.value)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
470
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
471 extra_data = Column(JSON)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
472 status = Column(Enum(NotificationStatus), default=NotificationStatus.new)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
473
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
474 def serialise(self) -> dict[str, str | float | bool | int | dict]:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
475 """
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
476 Serialises the Notification instance to a dictionary.
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
477 """
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
478 result = {}
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
479 for column in self.__table__.columns:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
480 value = getattr(self, column.name)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
481 if value is not None:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
482 if column.name in ("type", "status"):
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
483 result[column.name] = value.name
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
484 elif column.name == "id":
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
485 result[column.name] = str(value)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
486 elif column.name == "profile_id":
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
487 if value is None:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
488 result["profile"] = C.PROF_KEY_ALL
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
489 else:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
490 result["profile"] = get_profile_by_id(value)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
491 else:
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
492 result[column.name] = value
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
493 return result
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
494
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
495
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
496 class ParamGen(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
497 __tablename__ = "param_gen"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
498
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
499 category = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
500 name = Column(Text, primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
501 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
502
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
503
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
504 class ParamInd(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
505 __tablename__ = "param_ind"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
506
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
507 category = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
508 name = Column(Text, primary_key=True)
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
509 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
510 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
511
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
512 profile = relationship("Profile", back_populates="params")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
513
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
514
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
515 class PrivateGen(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
516 __tablename__ = "private_gen"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
517
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
518 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
519 key = Column(Text, primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
520 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
521
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
522
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
523 class PrivateInd(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
524 __tablename__ = "private_ind"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
525
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
526 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
527 key = Column(Text, primary_key=True)
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
528 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
529 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
530
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
531 profile = relationship("Profile", back_populates="private_data")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
532
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
533
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
534 class PrivateGenBin(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
535 __tablename__ = "private_gen_bin"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
536
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
537 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
538 key = Column(Text, primary_key=True)
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4161
diff changeset
539 value = Column(JSON)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
540
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
541
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
542 class PrivateIndBin(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
543 __tablename__ = "private_ind_bin"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
544
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
545 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
546 key = Column(Text, primary_key=True)
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
547 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"), primary_key=True)
4212
5f2d496c633f core: get rid of `pickle`:
Goffi <goffi@goffi.org>
parents: 4161
diff changeset
548 value = Column(JSON)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
549
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
550 profile = relationship("Profile", back_populates="private_bin_data")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
551
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
552
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
553 class File(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
554 __tablename__ = "files"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
555 __table_args__ = (
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
556 Index("files__profile_id_owner_parent", "profile_id", "owner", "parent"),
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
557 Index(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
558 "files__profile_id_owner_media_type_media_subtype",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
559 "profile_id",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
560 "owner",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
561 "media_type",
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
562 "media_subtype",
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
563 ),
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
564 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
565
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
566 id = Column(Text, primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
567 public_id = Column(Text, unique=True)
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
568 version = Column(Text, primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
569 parent = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
570 type = Column(
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
571 Enum("file", "directory", name="file_type", create_constraint=True),
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
572 nullable=False,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
573 server_default="file",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
574 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
575 file_hash = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
576 hash_algo = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
577 name = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
578 size = Column(Integer)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
579 namespace = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
580 media_type = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
581 media_subtype = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
582 created = Column(Float, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
583 modified = Column(Float)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
584 owner = Column(JID)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
585 access = Column(JsonDefaultDict)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
586 extra = Column(JsonDefaultDict)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
587 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"))
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
588
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
589 profile = relationship("Profile")
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
590
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
591
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
592 class PubsubNode(Base):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
593 __tablename__ = "pubsub_nodes"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
594 __table_args__ = (UniqueConstraint("profile_id", "service", "name"),)
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
595
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
596 id = Column(Integer, primary_key=True)
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
597 profile_id = Column(ForeignKey("profiles.id", ondelete="CASCADE"))
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
598 service = Column(JID)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
599 name = Column(Text, nullable=False)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
600 subscribed = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
601 Boolean(create_constraint=True, name="subscribed_bool"), nullable=False
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
602 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
603 analyser = Column(Text)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
604 sync_state = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
605 Enum(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
606 SyncState,
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
607 name="sync_state",
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
608 create_constraint=True,
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
609 ),
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
610 nullable=True,
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
611 )
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
612 sync_state_updated = Column(Float, nullable=False, default=time.time())
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
613 type_ = Column(Text, name="type", nullable=True)
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
614 subtype = Column(Text, nullable=True)
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
615 extra = Column(JSON)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
616
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
617 items = relationship("PubsubItem", back_populates="node", passive_deletes=True)
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
618 subscriptions = relationship("PubsubSub", back_populates="node", passive_deletes=True)
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
619
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
620 def __str__(self):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
621 return f"Pubsub node {self.name!r} at {self.service}"
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
622
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
623
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
624 class PubsubSub(Base):
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
625 """Subscriptions to pubsub nodes
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
626
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
627 Used by components managing a pubsub service
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
628 """
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
629
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
630 __tablename__ = "pubsub_subs"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
631 __table_args__ = (UniqueConstraint("node_id", "subscriber"),)
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
632
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
633 id = Column(Integer, primary_key=True)
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
634 node_id = Column(ForeignKey("pubsub_nodes.id", ondelete="CASCADE"), nullable=False)
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
635 subscriber = Column(JID)
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
636 state = Column(
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
637 Enum(
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
638 SubscriptionState,
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
639 name="state",
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
640 create_constraint=True,
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
641 ),
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
642 nullable=True,
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
643 )
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
644
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
645 node = relationship("PubsubNode", back_populates="subscriptions")
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
646
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
647
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
648 class PubsubItem(Base):
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
649 __tablename__ = "pubsub_items"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
650 __table_args__ = (UniqueConstraint("node_id", "name"),)
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
651 id = Column(Integer, primary_key=True)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
652 node_id = Column(ForeignKey("pubsub_nodes.id", ondelete="CASCADE"), nullable=False)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
653 name = Column(Text, nullable=False)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
654 data = Column(Xml, nullable=False)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
655 created = Column(DateTime, nullable=False, server_default=now())
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
656 updated = Column(DateTime, nullable=False, server_default=now(), onupdate=now())
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
657 parsed = Column(JSON)
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
658
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
659 node = relationship("PubsubNode", back_populates="items")
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
660
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
661
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
662 ## Full-Text Search
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
664 # create
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
665
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
666
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
667 @event.listens_for(PubsubItem.__table__, "after_create")
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
668 def fts_create(target, connection, **kw):
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
669 """Full-Text Search table creation"""
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
670 if connection.engine.name == "sqlite":
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
671 # Using SQLite FTS5
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
672 queries = [
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
673 "CREATE VIRTUAL TABLE pubsub_items_fts "
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
674 "USING fts5(data, content=pubsub_items, content_rowid=id)",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
675 "CREATE TRIGGER pubsub_items_fts_sync_ins AFTER INSERT ON pubsub_items BEGIN"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
676 " INSERT INTO pubsub_items_fts(rowid, data) VALUES (new.id, new.data);"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
677 "END",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
678 "CREATE TRIGGER pubsub_items_fts_sync_del AFTER DELETE ON pubsub_items BEGIN"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
679 " INSERT INTO pubsub_items_fts(pubsub_items_fts, rowid, data) "
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
680 "VALUES('delete', old.id, old.data);"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
681 "END",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
682 "CREATE TRIGGER pubsub_items_fts_sync_upd AFTER UPDATE ON pubsub_items BEGIN"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
683 " INSERT INTO pubsub_items_fts(pubsub_items_fts, rowid, data) VALUES"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
684 "('delete', old.id, old.data);"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
685 " INSERT INTO pubsub_items_fts(rowid, data) VALUES(new.id, new.data);"
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
686 "END",
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
687 ]
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
688 for q in queries:
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
689 connection.execute(DDL(q))
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
690
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
691
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
692 # drop
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
693
4130
02f0adc745c6 core: notifications implementation, first draft:
Goffi <goffi@goffi.org>
parents: 4095
diff changeset
694
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
695 @event.listens_for(PubsubItem.__table__, "before_drop")
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
696 def fts_drop(target, connection, **kw):
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
697 "Full-Text Search table drop" ""
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
698 if connection.engine.name == "sqlite":
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
699 # Using SQLite FTS5
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
700 queries = [
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
701 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_ins",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
702 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_del",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
703 "DROP TRIGGER IF EXISTS pubsub_items_fts_sync_upd",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
704 "DROP TABLE IF EXISTS pubsub_items_fts",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
705 ]
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
706 for q in queries:
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
707 connection.execute(DDL(q))