1 package org
.asamk
.signal
.manager
.storage
;
3 import com
.zaxxer
.hikari
.HikariDataSource
;
5 import org
.asamk
.signal
.manager
.storage
.groups
.GroupStore
;
6 import org
.asamk
.signal
.manager
.storage
.identities
.IdentityKeyStore
;
7 import org
.asamk
.signal
.manager
.storage
.prekeys
.PreKeyStore
;
8 import org
.asamk
.signal
.manager
.storage
.prekeys
.SignedPreKeyStore
;
9 import org
.asamk
.signal
.manager
.storage
.recipients
.RecipientStore
;
10 import org
.asamk
.signal
.manager
.storage
.sendLog
.MessageSendLogStore
;
11 import org
.asamk
.signal
.manager
.storage
.sessions
.SessionStore
;
12 import org
.asamk
.signal
.manager
.storage
.stickers
.StickerStore
;
13 import org
.slf4j
.Logger
;
14 import org
.slf4j
.LoggerFactory
;
17 import java
.sql
.Connection
;
18 import java
.sql
.SQLException
;
20 public class AccountDatabase
extends Database
{
22 private final static Logger logger
= LoggerFactory
.getLogger(AccountDatabase
.class);
23 private static final long DATABASE_VERSION
= 7;
25 private AccountDatabase(final HikariDataSource dataSource
) {
26 super(logger
, DATABASE_VERSION
, dataSource
);
29 public static AccountDatabase
init(File databaseFile
) throws SQLException
{
30 return initDatabase(databaseFile
, AccountDatabase
::new);
34 protected void createDatabase(final Connection connection
) throws SQLException
{
35 RecipientStore
.createSql(connection
);
36 MessageSendLogStore
.createSql(connection
);
37 StickerStore
.createSql(connection
);
38 PreKeyStore
.createSql(connection
);
39 SignedPreKeyStore
.createSql(connection
);
40 GroupStore
.createSql(connection
);
41 SessionStore
.createSql(connection
);
42 IdentityKeyStore
.createSql(connection
);
46 protected void upgradeDatabase(final Connection connection
, final long oldVersion
) throws SQLException
{
48 logger
.debug("Updating database: Creating recipient table");
49 try (final var statement
= connection
.createStatement()) {
50 statement
.executeUpdate("""
51 CREATE TABLE recipient (
52 _id INTEGER PRIMARY KEY AUTOINCREMENT,
56 profile_key_credential BLOB,
62 expiration_time INTEGER NOT NULL DEFAULT 0,
63 blocked BOOLEAN NOT NULL DEFAULT FALSE,
64 archived BOOLEAN NOT NULL DEFAULT FALSE,
65 profile_sharing BOOLEAN NOT NULL DEFAULT FALSE,
67 profile_last_update_timestamp INTEGER NOT NULL DEFAULT 0,
68 profile_given_name TEXT,
69 profile_family_name TEXT,
71 profile_about_emoji TEXT,
72 profile_avatar_url_path TEXT,
73 profile_mobile_coin_address BLOB,
74 profile_unidentified_access_mode TEXT,
75 profile_capabilities TEXT
81 logger
.debug("Updating database: Creating sticker table");
82 try (final var statement
= connection
.createStatement()) {
83 statement
.executeUpdate("""
84 CREATE TABLE sticker (
85 _id INTEGER PRIMARY KEY,
86 pack_id BLOB UNIQUE NOT NULL,
87 pack_key BLOB NOT NULL,
88 installed BOOLEAN NOT NULL DEFAULT FALSE
94 logger
.debug("Updating database: Creating pre key tables");
95 try (final var statement
= connection
.createStatement()) {
96 statement
.executeUpdate("""
97 CREATE TABLE signed_pre_key (
98 _id INTEGER PRIMARY KEY,
99 account_id_type INTEGER NOT NULL,
100 key_id INTEGER NOT NULL,
101 public_key BLOB NOT NULL,
102 private_key BLOB NOT NULL,
103 signature BLOB NOT NULL,
104 timestamp INTEGER DEFAULT 0,
105 UNIQUE(account_id_type, key_id)
107 CREATE TABLE pre_key (
108 _id INTEGER PRIMARY KEY,
109 account_id_type INTEGER NOT NULL,
110 key_id INTEGER NOT NULL,
111 public_key BLOB NOT NULL,
112 private_key BLOB NOT NULL,
113 UNIQUE(account_id_type, key_id)
118 if (oldVersion
< 5) {
119 logger
.debug("Updating database: Creating group tables");
120 try (final var statement
= connection
.createStatement()) {
121 statement
.executeUpdate("""
122 CREATE TABLE group_v2 (
123 _id INTEGER PRIMARY KEY,
124 group_id BLOB UNIQUE NOT NULL,
125 master_key BLOB NOT NULL,
127 distribution_id BLOB UNIQUE NOT NULL,
128 blocked BOOLEAN NOT NULL DEFAULT FALSE,
129 permission_denied BOOLEAN NOT NULL DEFAULT FALSE
131 CREATE TABLE group_v1 (
132 _id INTEGER PRIMARY KEY,
133 group_id BLOB UNIQUE NOT NULL,
134 group_id_v2 BLOB UNIQUE,
137 expiration_time INTEGER NOT NULL DEFAULT 0,
138 blocked BOOLEAN NOT NULL DEFAULT FALSE,
139 archived BOOLEAN NOT NULL DEFAULT FALSE
141 CREATE TABLE group_v1_member (
142 _id INTEGER PRIMARY KEY,
143 group_id INTEGER NOT NULL REFERENCES group_v1 (_id) ON DELETE CASCADE,
144 recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,
145 UNIQUE(group_id, recipient_id)
150 if (oldVersion
< 6) {
151 logger
.debug("Updating database: Creating session tables");
152 try (final var statement
= connection
.createStatement()) {
153 statement
.executeUpdate("""
154 CREATE TABLE session (
155 _id INTEGER PRIMARY KEY,
156 account_id_type INTEGER NOT NULL,
157 recipient_id INTEGER NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,
158 device_id INTEGER NOT NULL,
159 record BLOB NOT NULL,
160 UNIQUE(account_id_type, recipient_id, device_id)
165 if (oldVersion
< 7) {
166 logger
.debug("Updating database: Creating identity table");
167 try (final var statement
= connection
.createStatement()) {
168 statement
.executeUpdate("""
169 CREATE TABLE identity (
170 _id INTEGER PRIMARY KEY,
171 recipient_id INTEGER UNIQUE NOT NULL REFERENCES recipient (_id) ON DELETE CASCADE,
172 identity_key BLOB NOT NULL,
173 added_timestamp INTEGER NOT NULL,
174 trust_level INTEGER NOT NULL