annotate sat/memory/sqla_mapping.py @ 3752:5f546dd910e0

core (memory/param): fix unproper use of `Failure`
author Goffi <goffi@goffi.org>
date Fri, 13 May 2022 17:57:24 +0200
parents 658ddbabaf36
children 24c1c06c865b
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
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
19 import pickle
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
20 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
21 from datetime import datetime
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
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
23 import enum
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from sqlalchemy import (
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
25 MetaData, Column, Integer, Text, Float, Boolean, DateTime, Enum, JSON, ForeignKey,
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
26 UniqueConstraint, Index, DDL, event
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
27 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
28
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sqlalchemy.orm import declarative_base, relationship
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sqlalchemy.types import TypeDecorator
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
31 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
32 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
33 from wokkel import generic
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
35
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
36 Base = declarative_base(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
37 metadata=MetaData(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
38 naming_convention={
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
39 "ix": 'ix_%(column_0_label)s',
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
40 "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
41 "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
42 "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
43 "pk": "pk_%(table_name)s"
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
44 }
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
45 )
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
46 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
47 # 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
48 # because those values are stored in separate fields
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
49 NOT_IN_EXTRA = ('stanza_id', 'received_timestamp', 'update_uid')
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
50
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
51
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
52 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
53 #: 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
54 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
55 #: 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
56 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
57 #: 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
58 ERROR = 3
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
59 #: 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
60 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
61
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
62
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
63 class SubscriptionState(enum.Enum):
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
64 SUBSCRIBED = 1
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
65 PENDING = 2
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
66
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
67
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
68 class LegacyPickle(TypeDecorator):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
69 """Handle troubles with data pickled by former version of SàT
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
70
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
71 This type is temporary until we do migration to a proper data type
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
72 """
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
73 # 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
74 impl = Text
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
75 cache_ok = True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
76
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
77 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
78 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return pickle.dumps(value, 0)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
81
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
82 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
83 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
85 # value types are inconsistent (probably a consequence of Python 2/3 port
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
86 # and/or SQLite dynamic typing)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
87 try:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
88 value = value.encode()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
89 except AttributeError:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
90 pass
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
91 # "utf-8" encoding is needed to handle Python 2 pickled data
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
92 return pickle.loads(value, encoding="utf-8")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
93
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 class Json(TypeDecorator):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """Handle JSON field in DB independant way"""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
97 # 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
98 impl = Text
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
99 cache_ok = True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
100
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
101 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
102 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
103 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return json.dumps(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
105
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
106 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
107 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
108 return None
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
109 return json.loads(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
110
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
111
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
112 class JsonDefaultDict(Json):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
113 """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
114
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
115 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
116 if value is None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
117 return {}
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
118 return json.loads(value)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
119
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
120
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
121 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
122 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
123 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
124
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
125 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
126 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
127 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
128 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
129
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
130 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
131 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
132 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
133 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
134
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
135
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
136 class JID(TypeDecorator):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
137 """Store twisted JID in text fields"""
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
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
144 return value.full()
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 jid.JID(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 Profile(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
153 __tablename__ = "profiles"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
154
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
155 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
156 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
157 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
158 nullable=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
159 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
160 name = Column(Text, unique=True)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
161
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
162 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
163 private_data = relationship(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
164 "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
165 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
166 private_bin_data = relationship(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
167 "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
168 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
169
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
170
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
171 class Component(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
172 __tablename__ = "components"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
173
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
174 profile_id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
175 ForeignKey("profiles.id", ondelete="CASCADE"),
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
176 nullable=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
177 primary_key=True
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
178 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
179 entry_point = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
180 profile = relationship("Profile")
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
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
183 class History(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
184 __tablename__ = "history"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
185 __table_args__ = (
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
186 UniqueConstraint("profile_id", "stanza_id", "source", "dest"),
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
187 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
188 Index(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
189 "history__profile_id_received_timestamp", "profile_id", "received_timestamp"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
190 )
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
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
193 uid = Column(Text, primary_key=True)
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
194 stanza_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
195 update_uid = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
196 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
197 source = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
198 dest = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
199 source_res = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
200 dest_res = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
201 timestamp = Column(Float, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
202 received_timestamp = Column(Float)
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
203 type = Column(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
204 Enum(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
205 "chat",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
206 "error",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
207 "groupchat",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
208 "headline",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
209 "normal",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
210 # 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
211 # in a MUC
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
212 "info",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
213 name="message_type",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
214 create_constraint=True,
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
215 ),
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
216 nullable=False,
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
217 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
218 extra = Column(LegacyPickle)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
219
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
220 profile = relationship("Profile")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
221 messages = relationship("Message", backref="history", passive_deletes=True)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
222 subjects = relationship("Subject", backref="history", passive_deletes=True)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
223 thread = relationship(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
224 "Thread", uselist=False, back_populates="history", passive_deletes=True
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
225 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
226
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
227 def __init__(self, *args, **kwargs):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
228 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
229 if source_jid is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
230 kwargs["source"] = source_jid.userhost()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
231 kwargs["source_res"] = source_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
232 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
233 if dest_jid is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
234 kwargs["dest"] = dest_jid.userhost()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
235 kwargs["dest_res"] = dest_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
236 super().__init__(*args, **kwargs)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
237
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
238 @property
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
239 def source_jid(self) -> jid.JID:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
240 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
241
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
242 @source_jid.setter
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
243 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
244 self.source = source_jid.userhost
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
245 self.source_res = source_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
246
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
247 @property
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
248 def dest_jid(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
249 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
250
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
251 @dest_jid.setter
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
252 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
253 self.dest = dest_jid.userhost
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
254 self.dest_res = dest_jid.resource
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
255
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
256 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
257 dt = datetime.fromtimestamp(self.timestamp)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
258 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
259
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
260 def serialise(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
261 extra = self.extra
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
262 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
263 extra["stanza_id"] = self.stanza_id
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
264 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
265 extra["update_uid"] = self.update_uid
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
266 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
267 extra["received_timestamp"] = self.received_timestamp
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
268 if self.thread is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
269 extra["thread"] = self.thread.thread_id
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
270 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
271 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
272
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
273
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
274 return {
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
275 "from": f"{self.source}/{self.source_res}" if self.source_res
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
276 else self.source,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
277 "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
278 "uid": self.uid,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
279 "message": {m.language or '': m.message for m in self.messages},
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
280 "subject": {m.language or '': m.subject for m in self.subjects},
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
281 "type": self.type,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
282 "extra": extra,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
283 "timestamp": self.timestamp,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
284 }
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 as_tuple(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
287 d = self.serialise()
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
288 return (
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
289 d['uid'], d['timestamp'], d['from'], d['to'], d['message'], d['subject'],
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
290 d['type'], d['extra']
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
291 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
292
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
293 @staticmethod
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
294 def debug_collection(history_collection):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
295 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
296 history.debug_msg(idx)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
297
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
298 def debug_msg(self, idx=None):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
299 """Print messages"""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
300 dt = datetime.fromtimestamp(self.timestamp)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
301 if idx is not None:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
302 dt = f"({idx}) {dt}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
303 parts = []
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
304 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
305 for message in self.messages:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
306 if message.language:
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
307 parts.append(f"[{message.language}] ")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
308 parts.append(f"{message.message}\n")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
309 print("".join(parts))
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
310
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
311
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
312 class Message(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
313 __tablename__ = "message"
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
314 __table_args__ = (
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
315 Index("message__history_uid", "history_uid"),
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
316 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
317
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
318 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
319 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
320 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
321 )
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
322 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
323 message = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
324 language = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
325
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
326 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
327 lang_str = f"[{self.language}]" if self.language else ""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
328 msg = f"{self.message[:20]}…" if len(self.message)>20 else self.message
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
329 content = f"{lang_str}{msg}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
330 return f"Message<{content}>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
331
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
332
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
333 class Subject(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
334 __tablename__ = "subject"
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
335 __table_args__ = (
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
336 Index("subject__history_uid", "history_uid"),
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
337 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
338
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
339 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
340 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
341 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
342 )
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
343 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
344 subject = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
345 language = Column(Text)
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 __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
348 lang_str = f"[{self.language}]" if self.language else ""
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
349 msg = f"{self.subject[:20]}…" if len(self.subject)>20 else self.subject
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
350 content = f"{lang_str}{msg}"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
351 return f"Subject<{content}>"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
352
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
353
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
354 class Thread(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
355 __tablename__ = "thread"
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
356 __table_args__ = (
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
357 Index("thread__history_uid", "history_uid"),
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
358 )
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
359
3581
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
360 id = Column(
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
361 Integer,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
362 primary_key=True,
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
363 )
84ea57a8d6b3 memory (storage): adjustements to SQLAlchemy mapping to match current state of database
Goffi <goffi@goffi.org>
parents: 3537
diff changeset
364 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
365 thread_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
366 parent_id = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
367
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
368 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
369
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
370 def __repr__(self):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
371 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
372
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
373
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
374 class ParamGen(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
375 __tablename__ = "param_gen"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
376
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
377 category = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
378 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
379 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
380
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
381
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
382 class ParamInd(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
383 __tablename__ = "param_ind"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
384
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
385 category = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
386 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
387 profile_id = Column(
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
388 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
389 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
390 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
391
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
392 profile = relationship("Profile", back_populates="params")
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
393
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
394
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
395 class PrivateGen(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
396 __tablename__ = "private_gen"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
397
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
398 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
399 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
400 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
401
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
402
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
403 class PrivateInd(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
404 __tablename__ = "private_ind"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
405
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
406 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
407 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
408 profile_id = Column(
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
409 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
410 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
411 value = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
412
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
413 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
414
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
415
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
416 class PrivateGenBin(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
417 __tablename__ = "private_gen_bin"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
418
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
419 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
420 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
421 value = Column(LegacyPickle)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
422
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
423
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
424 class PrivateIndBin(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
425 __tablename__ = "private_ind_bin"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
426
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
427 namespace = Column(Text, primary_key=True)
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
428 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
429 profile_id = Column(
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
430 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
431 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
432 value = Column(LegacyPickle)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
433
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
434 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
435
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
436
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
437 class File(Base):
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
438 __tablename__ = "files"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
439 __table_args__ = (
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
440 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
441 Index(
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
442 "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
443 "profile_id",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
444 "owner",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
445 "media_type",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
446 "media_subtype"
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
447 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
448 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
449
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
450 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
451 public_id = Column(Text, unique=True)
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
452 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
453 parent = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
454 type = Column(
3583
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
455 Enum(
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
456 "file", "directory",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
457 name="file_type",
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
458 create_constraint=True
16ade4ad63f3 core (memory/sqla_mapping): fix some technical debt:
Goffi <goffi@goffi.org>
parents: 3581
diff changeset
459 ),
3537
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
460 nullable=False,
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
461 server_default="file",
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
462 )
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
463 file_hash = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
464 hash_algo = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
465 name = Column(Text, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
466 size = Column(Integer)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
467 namespace = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
468 media_type = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
469 media_subtype = Column(Text)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
470 created = Column(Float, nullable=False)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
471 modified = Column(Float)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
472 owner = Column(JID)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
473 access = Column(JsonDefaultDict)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
474 extra = Column(JsonDefaultDict)
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
475 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
476
f9a5b810f14d core (memory/storage): backend storage is now based on SQLAlchemy
Goffi <goffi@goffi.org>
parents:
diff changeset
477 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
478
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
479
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
480 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
481 __tablename__ = "pubsub_nodes"
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
482 __table_args__ = (
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
483 UniqueConstraint("profile_id", "service", "name"),
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
484 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
485
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
486 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
487 profile_id = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
488 ForeignKey("profiles.id", ondelete="CASCADE")
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
489 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
490 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
491 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
492 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
493 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
494 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
495 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
496 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
497 Enum(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
498 SyncState,
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
499 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
500 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
501 ),
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
502 nullable=True
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
503 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
504 sync_state_updated = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
505 Float,
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
506 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
507 default=time.time()
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
508 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
509 type_ = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
510 Text, name="type", nullable=True
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
511 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
512 subtype = Column(
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
513 Text, nullable=True
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
514 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
515 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
516
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
517 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
518 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
519
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
520 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
521 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
522
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
523
3744
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
524 class PubsubSub(Base):
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
525 """Subscriptions to pubsub nodes
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
526
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
527 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
528 """
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
529 __tablename__ = "pubsub_subs"
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
530 __table_args__ = (
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
531 UniqueConstraint("node_id", "subscriber"),
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
532 )
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
533
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
534 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
535 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
536 subscriber = Column(JID)
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
537 state = Column(
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
538 Enum(
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
539 SubscriptionState,
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
540 name="state",
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
541 create_constraint=True,
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
542 ),
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
543 nullable=True
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
544 )
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
545
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
546 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
547
658ddbabaf36 core (memory/sqla): new table/mapping to handle Pubsub node subscriptions:
Goffi <goffi@goffi.org>
parents: 3663
diff changeset
548
3593
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
549 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
550 __tablename__ = "pubsub_items"
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
551 __table_args__ = (
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
552 UniqueConstraint("node_id", "name"),
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
553 )
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
554 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
555 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
556 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
557 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
558 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
559 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
560 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
561
cb8d0e8b917f core (memory/sqla_mapping): mapping for PubsubNode and PubsubItem (will be used for caching)
Goffi <goffi@goffi.org>
parents: 3583
diff changeset
562 node = relationship("PubsubNode", back_populates="items")
3663
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
563
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
564
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
565 ## Full-Text Search
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
566
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
567 # create
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
568
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
569 @event.listens_for(PubsubItem.__table__, "after_create")
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
570 def fts_create(target, connection, **kw):
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
571 """Full-Text Search table creation"""
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
572 if connection.engine.name == "sqlite":
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
573 # Using SQLite FTS5
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
574 queries = [
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
575 "CREATE VIRTUAL TABLE pubsub_items_fts "
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
576 "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
577 "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
578 " 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
579 "END",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
580 "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
581 " 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
582 "VALUES('delete', old.id, old.data);"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
583 "END",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
584 "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
585 " 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
586 "('delete', old.id, old.data);"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
587 " 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
588 "END"
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
589 ]
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
590 for q in queries:
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
591 connection.execute(DDL(q))
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
592
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
593 # drop
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
594
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
595 @event.listens_for(PubsubItem.__table__, "before_drop")
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
596 def fts_drop(target, connection, **kw):
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
597 "Full-Text Search table drop" ""
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
598 if connection.engine.name == "sqlite":
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
599 # Using SQLite FTS5
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
600 queries = [
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
601 "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
602 "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
603 "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
604 "DROP TABLE IF EXISTS pubsub_items_fts",
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
605 ]
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
606 for q in queries:
162866ca4be7 db (schema): create virtual table for FTS + migration
Goffi <goffi@goffi.org>
parents: 3639
diff changeset
607 connection.execute(DDL(q))