Skip to content

Commit 7fdb94d

Browse files
committed
Test
1 parent 47b2f41 commit 7fdb94d

File tree

7 files changed

+641
-107
lines changed

7 files changed

+641
-107
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ pg_dump -h localhost -U dnote -d dnote > dnote_backup.sql
3333
- Users & accounts
3434
- Books & notes
3535
- Sessions & tokens
36-
- Notifications & email preferences
3736

3837
Full-text search index rebuilds automatically.
3938

go.mod

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,19 @@ go 1.23
55
require (
66
github.com/lib/pq v1.10.9
77
github.com/mattn/go-sqlite3 v1.14.24
8+
gorm.io/driver/postgres v1.5.11
9+
gorm.io/driver/sqlite v1.5.7
10+
gorm.io/gorm v1.25.12
11+
)
12+
13+
require (
14+
github.com/jackc/pgpassfile v1.0.0 // indirect
15+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
16+
github.com/jackc/pgx/v5 v5.5.5 // indirect
17+
github.com/jackc/puddle/v2 v2.2.1 // indirect
18+
github.com/jinzhu/inflection v1.0.0 // indirect
19+
github.com/jinzhu/now v1.1.5 // indirect
20+
golang.org/x/crypto v0.17.0 // indirect
21+
golang.org/x/sync v0.1.0 // indirect
22+
golang.org/x/text v0.14.0 // indirect
823
)

go.sum

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,42 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
5+
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
6+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
7+
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
8+
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
9+
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
10+
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
11+
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
12+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
13+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
14+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
15+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
116
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
217
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
318
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
419
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
20+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
21+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
22+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
23+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
24+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
25+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
26+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
27+
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
28+
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
29+
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
30+
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
31+
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
32+
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
33+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
35+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
36+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
37+
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
38+
gorm.io/driver/postgres v1.5.11/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
39+
gorm.io/driver/sqlite v1.5.7 h1:8NvsrhP0ifM7LX9G4zPB97NwovUakUxc+2V2uuf3Z1I=
40+
gorm.io/driver/sqlite v1.5.7/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4=
41+
gorm.io/gorm v1.25.12 h1:I0u8i2hWQItBq1WfE0o2+WuL9+8L21K9e2HHSTE/0f8=
42+
gorm.io/gorm v1.25.12/go.mod h1:xh7N7RHfYlNc5EmcI/El95gXusucDrQnHXe0+CgWcLQ=

migrate.go

Lines changed: 12 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@ import (
77
)
88

99
type MigrationStats struct {
10-
Users int
11-
Accounts int
12-
Books int
13-
Notes int
14-
Tokens int
15-
Sessions int
16-
Notifications int
17-
EmailPreferences int
10+
Users int
11+
Accounts int
12+
Books int
13+
Notes int
14+
Tokens int
15+
Sessions int
1816
}
1917

2018
func migrate(pgDB, sqliteDB *sql.DB) error {
@@ -65,20 +63,6 @@ func migrate(pgDB, sqliteDB *sql.DB) error {
6563
}
6664
fmt.Printf(" Migrated %d sessions\n", stats.Sessions)
6765

68-
// Migrate notifications
69-
fmt.Println("Migrating notifications...")
70-
if err := migrateNotifications(pgDB, tx, &stats); err != nil {
71-
return fmt.Errorf("migrating notifications: %w", err)
72-
}
73-
fmt.Printf(" Migrated %d notifications\n", stats.Notifications)
74-
75-
// Migrate email preferences
76-
fmt.Println("Migrating email preferences...")
77-
if err := migrateEmailPreferences(pgDB, tx, &stats); err != nil {
78-
return fmt.Errorf("migrating email preferences: %w", err)
79-
}
80-
fmt.Printf(" Migrated %d email preferences\n", stats.EmailPreferences)
81-
8266
// Migrate notes (last so FTS triggers work)
8367
fmt.Println("Migrating notes...")
8468
if err := migrateNotes(pgDB, tx, &stats); err != nil {
@@ -93,14 +77,12 @@ func migrate(pgDB, sqliteDB *sql.DB) error {
9377

9478
// Print summary
9579
fmt.Println("\nMigration Summary:")
96-
fmt.Printf(" Users: %d\n", stats.Users)
97-
fmt.Printf(" Accounts: %d\n", stats.Accounts)
98-
fmt.Printf(" Books: %d\n", stats.Books)
99-
fmt.Printf(" Notes: %d\n", stats.Notes)
100-
fmt.Printf(" Tokens: %d\n", stats.Tokens)
101-
fmt.Printf(" Sessions: %d\n", stats.Sessions)
102-
fmt.Printf(" Notifications: %d\n", stats.Notifications)
103-
fmt.Printf(" Email Preferences: %d\n", stats.EmailPreferences)
80+
fmt.Printf(" Users: %d\n", stats.Users)
81+
fmt.Printf(" Accounts: %d\n", stats.Accounts)
82+
fmt.Printf(" Books: %d\n", stats.Books)
83+
fmt.Printf(" Notes: %d\n", stats.Notes)
84+
fmt.Printf(" Tokens: %d\n", stats.Tokens)
85+
fmt.Printf(" Sessions: %d\n", stats.Sessions)
10486

10587
return nil
10688
}
@@ -340,79 +322,3 @@ func migrateSessions(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
340322

341323
return rows.Err()
342324
}
343-
344-
func migrateNotifications(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
345-
rows, err := pgDB.Query(`
346-
SELECT id, created_at, updated_at, type, user_id
347-
FROM notifications
348-
ORDER BY id
349-
`)
350-
if err != nil {
351-
return err
352-
}
353-
defer rows.Close()
354-
355-
stmt, err := tx.Prepare(`
356-
INSERT INTO notifications (id, created_at, updated_at, type, user_id)
357-
VALUES (?, ?, ?, ?, ?)
358-
`)
359-
if err != nil {
360-
return err
361-
}
362-
defer stmt.Close()
363-
364-
for rows.Next() {
365-
var id, userID int
366-
var createdAt, updatedAt time.Time
367-
var notifType string
368-
369-
if err := rows.Scan(&id, &createdAt, &updatedAt, &notifType, &userID); err != nil {
370-
return err
371-
}
372-
373-
if _, err := stmt.Exec(id, createdAt, updatedAt, notifType, userID); err != nil {
374-
return err
375-
}
376-
stats.Notifications++
377-
}
378-
379-
return rows.Err()
380-
}
381-
382-
func migrateEmailPreferences(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
383-
rows, err := pgDB.Query(`
384-
SELECT id, created_at, updated_at, user_id, inactive_reminder, product_update
385-
FROM email_preferences
386-
ORDER BY id
387-
`)
388-
if err != nil {
389-
return err
390-
}
391-
defer rows.Close()
392-
393-
stmt, err := tx.Prepare(`
394-
INSERT INTO email_preferences (id, created_at, updated_at, user_id, inactive_reminder, product_update)
395-
VALUES (?, ?, ?, ?, ?, ?)
396-
`)
397-
if err != nil {
398-
return err
399-
}
400-
defer stmt.Close()
401-
402-
for rows.Next() {
403-
var id, userID int
404-
var createdAt, updatedAt time.Time
405-
var inactiveReminder, productUpdate bool
406-
407-
if err := rows.Scan(&id, &createdAt, &updatedAt, &userID, &inactiveReminder, &productUpdate); err != nil {
408-
return err
409-
}
410-
411-
if _, err := stmt.Exec(id, createdAt, updatedAt, userID, inactiveReminder, productUpdate); err != nil {
412-
return err
413-
}
414-
stats.EmailPreferences++
415-
}
416-
417-
return rows.Err()
418-
}

0 commit comments

Comments
 (0)