]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/storage/AccountDatabase.java
df7f3019c81f114ea2eb77b7d330f63263390913
[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.recipients.RecipientStore;
6 import org.asamk.signal.manager.storage.sendLog.MessageSendLogStore;
7 import org.asamk.signal.manager.storage.stickers.StickerStore;
8 import org.slf4j.Logger;
9 import org.slf4j.LoggerFactory;
10
11 import java.io.File;
12 import java.sql.Connection;
13 import java.sql.SQLException;
14
15 public class AccountDatabase extends Database {
16
17 private final static Logger logger = LoggerFactory.getLogger(AccountDatabase.class);
18 private static final long DATABASE_VERSION = 3;
19
20 private AccountDatabase(final HikariDataSource dataSource) {
21 super(logger, DATABASE_VERSION, dataSource);
22 }
23
24 public static AccountDatabase init(File databaseFile) throws SQLException {
25 return initDatabase(databaseFile, AccountDatabase::new);
26 }
27
28 @Override
29 protected void createDatabase(final Connection connection) throws SQLException {
30 RecipientStore.createSql(connection);
31 MessageSendLogStore.createSql(connection);
32 StickerStore.createSql(connection);
33 }
34
35 @Override
36 protected void upgradeDatabase(final Connection connection, final long oldVersion) throws SQLException {
37 if (oldVersion < 2) {
38 logger.debug("Updating database: Creating recipient table");
39 try (final var statement = connection.createStatement()) {
40 statement.executeUpdate("""
41 CREATE TABLE recipient (
42 _id INTEGER PRIMARY KEY AUTOINCREMENT,
43 number TEXT UNIQUE,
44 uuid BLOB UNIQUE,
45 profile_key BLOB,
46 profile_key_credential BLOB,
47
48 given_name TEXT,
49 family_name TEXT,
50 color TEXT,
51
52 expiration_time INTEGER NOT NULL DEFAULT 0,
53 blocked BOOLEAN NOT NULL DEFAULT FALSE,
54 archived BOOLEAN NOT NULL DEFAULT FALSE,
55 profile_sharing BOOLEAN NOT NULL DEFAULT FALSE,
56
57 profile_last_update_timestamp INTEGER NOT NULL DEFAULT 0,
58 profile_given_name TEXT,
59 profile_family_name TEXT,
60 profile_about TEXT,
61 profile_about_emoji TEXT,
62 profile_avatar_url_path TEXT,
63 profile_mobile_coin_address BLOB,
64 profile_unidentified_access_mode TEXT,
65 profile_capabilities TEXT
66 );
67 """);
68 }
69 }
70 if (oldVersion < 3) {
71 logger.debug("Updating database: Creating sticker table");
72 try (final var statement = connection.createStatement()) {
73 statement.executeUpdate("""
74 CREATE TABLE sticker (
75 _id INTEGER PRIMARY KEY,
76 pack_id BLOB UNIQUE NOT NULL,
77 pack_key BLOB NOT NULL,
78 installed BOOLEAN NOT NULL DEFAULT FALSE
79 );
80 """);
81 }
82 }
83 }
84 }