aboutsummaryrefslogtreecommitdiffstats
path: root/src/database.rs
blob: 09fedd71497bdfd08d1cd457fadb17f746cead34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright (c) 2021, 2022  Teddy Wing
//
// This file is part of Reflectub.
//
// Reflectub is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Reflectub is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Reflectub. If not, see <https://www.gnu.org/licenses/>.


use r2d2;
use r2d2_sqlite::SqliteConnectionManager;
use rusqlite::{self, OptionalExtension};
use thiserror;

use crate::github;


/// Repository metadata mapped to the database.
#[derive(Debug)]
pub struct Repo {
    id: i64,
    name: Option<String>,
    description: Option<String>,
    pub default_branch: Option<String>,
    updated_at: Option<String>,
}

impl Repo {
    pub fn description(&self) -> &str {
        self.description
            .as_deref()
            .unwrap_or("")
    }
}

impl From<&github::Repo> for Repo {
    fn from(repo: &github::Repo) -> Self {
        use chrono::DateTime;

        let repo_updated_at = DateTime::parse_from_rfc3339(&repo.updated_at).ok();
        let repo_pushed_at = DateTime::parse_from_rfc3339(&repo.pushed_at).ok();

        // Set `updated_at` to the most recent of `repo_updated_at` or
        // `repo_pushed_at`.
        let updated_at =
            if repo_updated_at.is_none() && repo_pushed_at.is_none() {
                repo.updated_at.clone()

            // `repo_updated_at` and `repo_pushed_at` are both Some.
            } else if repo_pushed_at.unwrap() > repo_updated_at.unwrap() {
                repo.pushed_at.clone()

            // Default to `repo.updated_at`.
            } else {
                repo.updated_at.clone()
            };

        Self {
            id: repo.id,
            name: Some(repo.name.clone()),
            description: repo.description.clone(),
            default_branch: Some(repo.default_branch.clone()),
            updated_at: Some(updated_at),
        }
    }
}


#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("database error")]
    Db(#[from] rusqlite::Error),

    #[error("connection pool error")]
    Pool(#[from] r2d2::Error),
}


#[derive(Debug)]
pub struct Db {
    pool: r2d2::Pool<SqliteConnectionManager>,
}

impl Db {
    /// Open a connection to the database.
    pub fn connect(path: &str) -> Result<Self, Error> {
        let manager = SqliteConnectionManager::file(path)
            .with_flags(
                rusqlite::OpenFlags::SQLITE_OPEN_READ_WRITE
                | rusqlite::OpenFlags::SQLITE_OPEN_CREATE,
            );

        Ok(
            Db {
                pool: r2d2::Pool::new(manager)?,
            }
        )
    }

    /// Initialise the database with tables and indexes.
    pub fn create(&self) -> Result<(), Error> {
        let mut pool = self.pool.get()?;
        let tx = pool.transaction()?;

        tx.execute(
            r#"
                CREATE TABLE IF NOT EXISTS repositories (
                    id INTEGER PRIMARY KEY,
                    name TEXT NOT NULL,
                    description TEXT,
                    default_branch TEXT,
                    updated_at TEXT NOT NULL
                );
            "#,
            [],
        )?;

        tx.execute(
            r#"
                CREATE UNIQUE INDEX IF NOT EXISTS idx_repositories_id
                    ON repositories (id);
            "#,
            [],
        )?;

        tx.commit()?;

        Ok(())
    }

    /// Get a repository by its ID.
    ///
    /// Returns a `rusqlite::Error::QueryReturnedNoRows` error if the row
    /// doesn't exist.
    pub fn repo_get(&self, id: i64) -> Result<Repo, Error> {
        let mut pool = self.pool.get()?;
        let tx = pool.transaction()?;

        let repo = tx.query_row(
            r#"
            SELECT
                id,
                name,
                description,
                default_branch,
                updated_at
            FROM repositories
            WHERE id = ?
            "#,
            [id],
            |row| {
                Ok(
                    Repo {
                        id: row.get(0)?,
                        name: Some(row.get(1)?),
                        description: row.get(2)?,
                        default_branch: row.get(3)?,
                        updated_at: Some(row.get(4)?),
                    }
                )
            },
        )?;

        tx.commit()?;

        Ok(repo)
    }

    /// Insert a new repository.
    pub fn repo_insert(&self, repo: Repo) -> Result<(), Error> {
        let mut pool = self.pool.get()?;
        let tx = pool.transaction()?;

        tx.execute(
            r#"
            INSERT INTO repositories
                (id, name, description, default_branch, updated_at)
                VALUES
                (?, ?, ?, ?, ?)
            "#,
            rusqlite::params![
                repo.id,
                &repo.name,
                &repo.description,
                &repo.default_branch,
                &repo.updated_at,
            ],
        )?;

        tx.commit()?;

        Ok(())
    }

    /// Check if the given repository is newer than the one in the repository.
    ///
    /// Compares the `updated_at` field to find out whether the repository was
    /// updated.
    pub fn repo_is_updated(
        &self,
        repo: &Repo,
    ) -> Result<bool, Error> {
        let mut pool = self.pool.get()?;
        let tx = pool.transaction()?;

        let is_updated = match tx.query_row(
            r#"
            SELECT 1
            FROM repositories
            WHERE id = ?
                AND datetime(updated_at) < datetime(?)
            "#,
            rusqlite::params![
                repo.id,
                &repo.updated_at,
            ],
            |row| row.get::<usize, u8>(0),
        )
            .optional()
        {
            Ok(Some(_)) => Ok(true),
            Ok(None) => Ok(false),
            Err(e) => Err(e.into()),
        };

        tx.commit()?;

        is_updated
    }

    /// Update an existing repository.
    pub fn repo_update(&self, repo: &Repo) -> Result<(), Error> {
        let mut pool = self.pool.get()?;
        let tx = pool.transaction()?;

        tx.execute(
            r#"
            UPDATE repositories
            SET
                name = ?,
                description = ?,
                default_branch = ?,
                updated_at = ?
            WHERE id = ?
            "#,
            rusqlite::params![
                &repo.name,
                &repo.description,
                &repo.default_branch,
                &repo.updated_at,
                repo.id,
            ],
        )?;

        tx.commit()?;

        Ok(())
    }
}