Repository navigation
Expand file tree
/
Copy pathdb.go
More file actions
239 lines (200 loc) · 5.54 KB
/
Copy pathdb.go
File metadata and controls
239 lines (200 loc) · 5.54 KB
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
package main
import (
"fmt"
"io"
"strconv"
"github.com/jmoiron/sqlx"
)
type LinkDB struct {
db *sqlx.DB
}
func (l *LinkDB) GetLinks() ([]Link, error) {
links := []Link{}
if err := l.db.Select(&links,
"SELECT * FROM links ORDER BY weight DESC, link_id ASC;"); err != nil {
return nil, err
}
return links, nil
}
func (l *LinkDB) UpdateWeight(id int, action string) error {
var queryAction string
switch action {
case "up":
queryAction = "+ 1"
case "down":
queryAction = "- 1"
default:
return fmt.Errorf("unsupported action: %s", action)
}
res, err := l.db.Exec("UPDATE links set weight = weight " + queryAction + " where link_id = " + strconv.Itoa(id) + ";")
if err != nil {
return err
}
if c, _ := res.RowsAffected(); c == 0 {
return fmt.Errorf("item not found: %d", id)
}
return nil
}
func (l *LinkDB) InsertLink(text, description, url, imageURL string) error {
query := `INSERT INTO links (message, description, url, image_url) VALUES (?, ?, ?, ?);`
_, err := l.db.Exec(query, text, description, url, imageURL)
if err != nil {
return err
}
return nil
}
func (l *LinkDB) UpdateLink(id int, text, description, url, image string) error {
query := `UPDATE links SET message=?, description=?, url=?, image_url=? WHERE link_id=?;`
_, err := l.db.Exec(query, text, description, url, image, id)
if err != nil {
return err
}
return nil
}
func (l *LinkDB) DeleteLink(id int) error {
query := `DELETE FROM links where link_id = ?;`
_, err := l.db.Exec(query, id)
if err != nil {
return err
}
return nil
}
func (l *LinkDB) IncrementHit(id int) error {
_, err := l.db.Exec("UPDATE links set hits = hits + 1 where link_id = " + strconv.Itoa(id) + ";")
if err != nil {
return err
}
return nil
}
func (app *App) execSchema() error {
schemaFile, err := setupFS.Open("schema.sql")
if err != nil {
return err
}
schema, err := io.ReadAll(schemaFile)
if err != nil {
return err
}
if err := schemaFile.Close(); err != nil {
return err
}
if _, err := app.DB.db.Exec(string(schema)); err != nil {
return err
}
// Seed with example data
if err := app.DB.seedExampleData(); err != nil {
return fmt.Errorf("failed to seed example data: %v", err)
}
return nil
}
// seedExampleData inserts example links for new installations
func (l *LinkDB) seedExampleData() error {
exampleLinks := []struct {
text string
description string
url string
imageURL string
}{
{
text: "Getting Started with LinkPage",
description: "Learn how to customize your LinkPage and add your own links through the admin panel",
url: "https://github.com/rhnvrm/linkpage",
imageURL: "",
},
{
text: "View Documentation",
description: "Comprehensive documentation covering installation, configuration, and deployment options",
url: "https://github.com/rhnvrm/linkpage#features",
imageURL: "",
},
{
text: "Get Started",
description: "Quick setup guide to get your LinkPage running in minutes",
url: "https://github.com/rhnvrm/linkpage#get-started",
imageURL: "",
},
}
for _, link := range exampleLinks {
err := l.InsertLink(link.text, link.description, link.url, link.imageURL)
if err != nil {
return err
}
}
return nil
}
// Migration represents a database migration
type Migration struct {
Version int
Name string
Up string
}
// migrations contains all database migrations in order
var migrations = []Migration{
{
Version: 1,
Name: "add_description_column",
Up: `ALTER TABLE links ADD COLUMN description TEXT DEFAULT '' NOT NULL;`,
},
// Future migrations go here
}
// runMigrations checks and applies necessary schema migrations
func (l *LinkDB) runMigrations() error {
// Create migrations table if it doesn't exist
_, err := l.db.Exec(`
CREATE TABLE IF NOT EXISTS schema_migrations (
version INTEGER PRIMARY KEY,
name TEXT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
`)
if err != nil {
return fmt.Errorf("failed to create migrations table: %v", err)
}
// Get current version
var currentVersion int
err = l.db.Get(¤tVersion, `SELECT COALESCE(MAX(version), 0) FROM schema_migrations;`)
if err != nil {
return fmt.Errorf("failed to get current migration version: %v", err)
}
// Apply pending migrations
for _, migration := range migrations {
if migration.Version <= currentVersion {
continue
}
// Check if the migration needs to be applied
// For version 1 (description column), check if column already exists
if migration.Version == 1 {
var columnExists int
err := l.db.Get(&columnExists, `
SELECT COUNT(*)
FROM pragma_table_info('links')
WHERE name='description';
`)
if err != nil {
return fmt.Errorf("failed to check for description column: %v", err)
}
// Skip if column already exists
if columnExists > 0 {
// Mark as applied
_, err := l.db.Exec(`INSERT INTO schema_migrations (version, name) VALUES (?, ?);`,
migration.Version, migration.Name)
if err != nil {
return fmt.Errorf("failed to record migration %d: %v", migration.Version, err)
}
continue
}
}
// Execute migration
_, err := l.db.Exec(migration.Up)
if err != nil {
return fmt.Errorf("failed to apply migration %d (%s): %v", migration.Version, migration.Name, err)
}
// Record migration
_, err = l.db.Exec(`INSERT INTO schema_migrations (version, name) VALUES (?, ?);`,
migration.Version, migration.Name)
if err != nil {
return fmt.Errorf("failed to record migration %d: %v", migration.Version, err)
}
}
return nil
}