Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/goit/db.go (154 lines, 3.2 KiB) -rw-r--r-- file download

570144e Jakob Wakeling 2023-12-15 23:28:06
0
package goit
570144e Jakob Wakeling 2023-12-15 23:28:06
1
570144e Jakob Wakeling 2023-12-15 23:28:06
2
import (
570144e Jakob Wakeling 2023-12-15 23:28:06
3
	"database/sql"
570144e Jakob Wakeling 2023-12-15 23:28:06
4
	"fmt"
570144e Jakob Wakeling 2023-12-15 23:28:06
5
	"log"
778d467 Jakob Wakeling 2024-01-17 18:29:55
6
778d467 Jakob Wakeling 2024-01-17 18:29:55
7
	"github.com/Jamozed/Goit/src/util"
570144e Jakob Wakeling 2023-12-15 23:28:06
8
)
570144e Jakob Wakeling 2023-12-15 23:28:06
9
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
10
/*
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
11
Current database schema:
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
12
users:
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
13
	id INTEGER PRIMARY KEY AUTOINCREMENT
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
14
	name TEXT UNIQUE NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
15
	name_full TEXT NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
16
	pass BLOB NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
17
	pass_algo TEXT NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
18
	salt BLOB NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
19
	is_admin BOOLEAN NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
20
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
21
repos:
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
22
	id INTEGER PRIMARY KEY AUTOINCREMENT
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
23
	owner_id INTEGER NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
24
	name TEXT UNIQUE NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
25
	name_lower TEXT UNIQUE NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
26
	description TEXT NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
27
	default_branch TEXT NOT NULL DEFAULT 'master'
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
28
	upstream TEXT NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
29
	visibility INTEGER NOT NULL DEFAULT 0
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
30
	is_mirror BOOLEAN NOT NULL
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
31
*/
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
32
2954edd Jakob Wakeling 2024-06-16 22:57:19
33
func updateDatabase(db *sql.DB) error {
2954edd Jakob Wakeling 2024-06-16 22:57:19
34
	const LATEST_VERSION = 3
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
35
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
36
	tx, err := db.Begin()
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
37
	if err != nil {
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
38
		return err
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
39
	}
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
40
	defer tx.Rollback()
570144e Jakob Wakeling 2023-12-15 23:28:06
41
570144e Jakob Wakeling 2023-12-15 23:28:06
42
	var version int
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
43
	if err := tx.QueryRow("PRAGMA user_version").Scan(&version); err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
44
		return err
570144e Jakob Wakeling 2023-12-15 23:28:06
45
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
46
2954edd Jakob Wakeling 2024-06-16 22:57:19
47
	if version > LATEST_VERSION {
2954edd Jakob Wakeling 2024-06-16 22:57:19
48
		return fmt.Errorf("database version is newer than supported (%d > %d)", version, LATEST_VERSION)
570144e Jakob Wakeling 2023-12-15 23:28:06
49
	}
570144e Jakob Wakeling 2023-12-15 23:28:06
50
2954edd Jakob Wakeling 2024-06-16 22:57:19
51
	logMigration := true
2954edd Jakob Wakeling 2024-06-16 22:57:19
52
2954edd Jakob Wakeling 2024-06-16 22:57:19
53
	if version <= 0 {
2954edd Jakob Wakeling 2024-06-16 22:57:19
54
		/* Database is empty or new, initialise from scratch */
2954edd Jakob Wakeling 2024-06-16 22:57:19
55
		log.Println("Initialising database at version", LATEST_VERSION)
2954edd Jakob Wakeling 2024-06-16 22:57:19
56
		logMigration = false
570144e Jakob Wakeling 2023-12-15 23:28:06
57
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
58
		if _, err := tx.Exec(
570144e Jakob Wakeling 2023-12-15 23:28:06
59
			`CREATE TABLE IF NOT EXISTS users (
570144e Jakob Wakeling 2023-12-15 23:28:06
60
				id INTEGER PRIMARY KEY AUTOINCREMENT,
570144e Jakob Wakeling 2023-12-15 23:28:06
61
				name TEXT UNIQUE NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
62
				name_full TEXT NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
63
				pass BLOB NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
64
				pass_algo TEXT NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
65
				salt BLOB NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
66
				is_admin BOOLEAN NOT NULL
570144e Jakob Wakeling 2023-12-15 23:28:06
67
			)`,
570144e Jakob Wakeling 2023-12-15 23:28:06
68
		); err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
69
			return err
570144e Jakob Wakeling 2023-12-15 23:28:06
70
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
71
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
72
		if _, err := tx.Exec(
570144e Jakob Wakeling 2023-12-15 23:28:06
73
			`CREATE TABLE IF NOT EXISTS repos (
570144e Jakob Wakeling 2023-12-15 23:28:06
74
				id INTEGER PRIMARY KEY AUTOINCREMENT,
570144e Jakob Wakeling 2023-12-15 23:28:06
75
				owner_id INTEGER NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
76
				name TEXT UNIQUE NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
77
				name_lower TEXT UNIQUE NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
78
				description TEXT NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
79
				upstream TEXT NOT NULL,
2954edd Jakob Wakeling 2024-06-16 22:57:19
80
				is_private BOOLEAN NOT NULL,
570144e Jakob Wakeling 2023-12-15 23:28:06
81
				is_mirror BOOLEAN NOT NULL
570144e Jakob Wakeling 2023-12-15 23:28:06
82
			)`,
570144e Jakob Wakeling 2023-12-15 23:28:06
83
		); err != nil {
570144e Jakob Wakeling 2023-12-15 23:28:06
84
			return err
570144e Jakob Wakeling 2023-12-15 23:28:06
85
		}
570144e Jakob Wakeling 2023-12-15 23:28:06
86
2954edd Jakob Wakeling 2024-06-16 22:57:19
87
		version = 1
570144e Jakob Wakeling 2023-12-15 23:28:06
88
	}
2954edd Jakob Wakeling 2024-06-16 22:57:19
89
	if version <= 1 {
2954edd Jakob Wakeling 2024-06-16 22:57:19
90
		if logMigration {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
91
			log.Println("Migrating database from version 1 to 2")
2954edd Jakob Wakeling 2024-06-16 22:57:19
92
		}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
93
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
94
		if _, err := tx.Exec("ALTER TABLE repos ADD COLUMN default_branch TEXT NOT NULL DEFAULT 'master'"); err != nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
95
			return err
2954edd Jakob Wakeling 2024-06-16 22:57:19
96
		}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
97
2954edd Jakob Wakeling 2024-06-16 22:57:19
98
		version = 2
2954edd Jakob Wakeling 2024-06-16 22:57:19
99
	}
2954edd Jakob Wakeling 2024-06-16 22:57:19
100
	if version <= 2 {
2954edd Jakob Wakeling 2024-06-16 22:57:19
101
		if logMigration {
778d467 Jakob Wakeling 2024-01-17 18:29:55
102
			log.Println("Migrating database from version 2 to 3")
2954edd Jakob Wakeling 2024-06-16 22:57:19
103
		}
778d467 Jakob Wakeling 2024-01-17 18:29:55
104
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
105
		if _, err := tx.Exec("ALTER TABLE repos ADD COLUMN visibility INTEGER NOT NULL DEFAULT 0"); err != nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
106
			return err
2954edd Jakob Wakeling 2024-06-16 22:57:19
107
		}
778d467 Jakob Wakeling 2024-01-17 18:29:55
108
2954edd Jakob Wakeling 2024-06-16 22:57:19
109
		/* Set values for each repo according to is_private */
2954edd Jakob Wakeling 2024-06-16 22:57:19
110
		var visibilities = map[int64]Visibility{}
778d467 Jakob Wakeling 2024-01-17 18:29:55
111
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
112
		if rows, err := tx.Query("SELECT id, is_private FROM repos"); err != nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
113
			return err
2954edd Jakob Wakeling 2024-06-16 22:57:19
114
		} else {
2954edd Jakob Wakeling 2024-06-16 22:57:19
115
			for rows.Next() {
2954edd Jakob Wakeling 2024-06-16 22:57:19
116
				var id int64
2954edd Jakob Wakeling 2024-06-16 22:57:19
117
				var isPrivate bool
778d467 Jakob Wakeling 2024-01-17 18:29:55
118
2954edd Jakob Wakeling 2024-06-16 22:57:19
119
				if err := rows.Scan(&id, &isPrivate); err != nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
120
					return err
778d467 Jakob Wakeling 2024-01-17 18:29:55
121
				}
778d467 Jakob Wakeling 2024-01-17 18:29:55
122
2954edd Jakob Wakeling 2024-06-16 22:57:19
123
				visibilities[id] = util.If(isPrivate, Private, Public)
778d467 Jakob Wakeling 2024-01-17 18:29:55
124
			}
778d467 Jakob Wakeling 2024-01-17 18:29:55
125
2954edd Jakob Wakeling 2024-06-16 22:57:19
126
			rows.Close()
2954edd Jakob Wakeling 2024-06-16 22:57:19
127
		}
778d467 Jakob Wakeling 2024-01-17 18:29:55
128
2954edd Jakob Wakeling 2024-06-16 22:57:19
129
		for id, visibility := range visibilities {
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
130
			if _, err := tx.Exec("UPDATE repos SET visibility = ? WHERE id = ?", visibility, id); err != nil {
778d467 Jakob Wakeling 2024-01-17 18:29:55
131
				return err
778d467 Jakob Wakeling 2024-01-17 18:29:55
132
			}
2954edd Jakob Wakeling 2024-06-16 22:57:19
133
		}
778d467 Jakob Wakeling 2024-01-17 18:29:55
134
2954edd Jakob Wakeling 2024-06-16 22:57:19
135
		/* Remove is_private column */
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
136
		if _, err := tx.Exec("ALTER TABLE repos DROP COLUMN is_private"); err != nil {
2954edd Jakob Wakeling 2024-06-16 22:57:19
137
			return err
570144e Jakob Wakeling 2023-12-15 23:28:06
138
		}
2954edd Jakob Wakeling 2024-06-16 22:57:19
139
2954edd Jakob Wakeling 2024-06-16 22:57:19
140
		version = 3
570144e Jakob Wakeling 2023-12-15 23:28:06
141
	}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
142
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
143
	if _, err := tx.Exec(fmt.Sprint("PRAGMA user_version = ", version)); err != nil {
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
144
		return err
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
145
	}
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
146
32e8b4d Jakob Wakeling 2024-07-07 17:50:40
147
	if err := tx.Commit(); err != nil {
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
148
		return err
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
149
	}
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
150
28a8f13 Jakob Wakeling 2023-12-25 19:52:40
151
	return nil
570144e Jakob Wakeling 2023-12-15 23:28:06
152
}
153