]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/AccountDatabase.java
ed4008c30ab27fd2a7b53c55baf598cc6f477bf4
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / storage / AccountDatabase.java
1 package org.asamk.signal.manager.storage;
2
3 import com.zaxxer.hikari.HikariDataSource;
4
5 import org.asamk.signal.manager.storage.prekeys.PreKeyStore;
6 import org.asamk.signal.manager.storage.prekeys.SignedPreKeyStore;
7 import org.asamk.signal.manager.storage.recipients.RecipientStore;
8 import org.asamk.signal.manager.storage.sendLog.MessageSendLogStore;
9 import org.asamk.signal.manager.storage.stickers.StickerStore;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12
13 import java.io.File;
14 import java.sql.Connection;
15 import java.sql.SQLException;
16
17 public class AccountDatabase extends Database {
18
19 private final static Logger logger = LoggerFactory.getLogger(AccountDatabase.class);
20 private static final long DATABASE_VERSION = 4;
21
22 private AccountDatabase(final HikariDataSource dataSource) {
23 super(logger, DATABASE_VERSION, dataSource);
24 }
25
26 public static AccountDatabase init(File databaseFile) throws SQLException {
27 return initDatabase(databaseFile, AccountDatabase::new);
28 }
29
30 @Override
31 protected void createDatabase(final Connection connection) throws SQLException {
32 RecipientStore.createSql(connection);
33 MessageSendLogStore.createSql(connection);
34 StickerStore.createSql(connection);
35 PreKeyStore.createSql(connection);
36 SignedPreKeyStore.createSql(connection);
37 }
38
39 @Override
40 protected void upgradeDatabase(final Connection connection, final long oldVersion) throws SQLException {
41 if (oldVersion < 2) {
42 logger.debug("Updating database: Creating recipient table");
43 try (final var statement = connection.createStatement()) {
44 statement.executeUpdate("""
45 CREATE TABLE recipient (
46 _id INTEGER PRIMARY KEY AUTOINCREMENT,
47 number TEXT UNIQUE,
48 uuid BLOB UNIQUE,
49 profile_key BLOB,
50 profile_key_credential BLOB,
51
52 given_name TEXT,
53 family_name TEXT,
54 color TEXT,
55
56 expiration_time INTEGER NOT NULL DEFAULT 0,
57 blocked BOOLEAN NOT NULL DEFAULT FALSE,
58 archived BOOLEAN NOT NULL DEFAULT FALSE,
59 profile_sharing BOOLEAN NOT NULL DEFAULT FALSE,
60
61 profile_last_update_timestamp INTEGER NOT NULL DEFAULT 0,
62 profile_given_name TEXT,
63 profile_family_name TEXT,
64 profile_about TEXT,
65 profile_about_emoji TEXT,
66 profile_avatar_url_path TEXT,
67 profile_mobile_coin_address BLOB,
68 profile_unidentified_access_mode TEXT,
69 profile_capabilities TEXT
70 );
71 """);
72 }
73 }
74 if (oldVersion < 3) {
75 logger.debug("Updating database: Creating sticker table");
76 try (final var statement = connection.createStatement()) {
77 statement.executeUpdate("""
78 CREATE TABLE sticker (
79 _id INTEGER PRIMARY KEY,
80 pack_id BLOB UNIQUE NOT NULL,
81 pack_key BLOB NOT NULL,
82 installed BOOLEAN NOT NULL DEFAULT FALSE
83 );
84 """);
85 }
86 }
87 if (oldVersion < 4) {
88 logger.debug("Updating database: Creating pre key tables");
89 try (final var statement = connection.createStatement()) {
90 statement.executeUpdate("""
91 CREATE TABLE signed_pre_key (
92 _id INTEGER PRIMARY KEY,
93 account_id_type INTEGER NOT NULL,
94 key_id INTEGER NOT NULL,
95 public_key BLOB NOT NULL,
96 private_key BLOB NOT NULL,
97 signature BLOB NOT NULL,
98 timestamp INTEGER DEFAULT 0,
99 UNIQUE(account_id_type, key_id)
100 );
101 CREATE TABLE pre_key (
102 _id INTEGER PRIMARY KEY,
103 account_id_type INTEGER NOT NULL,
104 key_id INTEGER NOT NULL,
105 public_key BLOB NOT NULL,
106 private_key BLOB NOT NULL,
107 UNIQUE(account_id_type, key_id)
108 );
109 """);
110 }
111 }
112 }
113 }