Skip to content

Commit 47b2f41

Browse files
committed
Initial commit
0 parents  commit 47b2f41

File tree

6 files changed

+575
-0
lines changed

6 files changed

+575
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
dnote-pg2sqlite
2+
*.db
3+
*.db-shm
4+
*.db-wal

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# dnote-pg2sqlite
2+
3+
Migration tool from PostgreSQL to SQLite for Dnote.
4+
5+
## Installation
6+
7+
```bash
8+
go install github.com/dnote/dnote-pg2sqlite@latest
9+
```
10+
11+
## Usage
12+
13+
```bash
14+
dnote-pg2sqlite \
15+
--pg-host localhost \
16+
--pg-port 5432 \
17+
--pg-database dnote \
18+
--pg-user dnote \
19+
--pg-password yourpassword \
20+
--sqlite-path /path/to/server.db
21+
```
22+
23+
## Backup First
24+
25+
**Always backup PostgreSQL before migrating:**
26+
27+
```bash
28+
pg_dump -h localhost -U dnote -d dnote > dnote_backup.sql
29+
```
30+
31+
## What Gets Migrated
32+
33+
- Users & accounts
34+
- Books & notes
35+
- Sessions & tokens
36+
- Notifications & email preferences
37+
38+
Full-text search index rebuilds automatically.
39+
40+
## Verification
41+
42+
After migration:
43+
44+
1. Check the tool's output for record counts
45+
2. Start Dnote and verify login works
46+
3. Verify notes and search work correctly

go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/dnote/dnote-pg2sqlite
2+
3+
go 1.23
4+
5+
require (
6+
github.com/lib/pq v1.10.9
7+
github.com/mattn/go-sqlite3 v1.14.24
8+
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
2+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
3+
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
4+
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=

main.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package main
2+
3+
import (
4+
"database/sql"
5+
"flag"
6+
"fmt"
7+
"log"
8+
"os"
9+
10+
_ "github.com/lib/pq"
11+
_ "github.com/mattn/go-sqlite3"
12+
)
13+
14+
type Config struct {
15+
PgHost string
16+
PgPort string
17+
PgDatabase string
18+
PgUser string
19+
PgPassword string
20+
SqlitePath string
21+
}
22+
23+
func main() {
24+
var config Config
25+
26+
flag.StringVar(&config.PgHost, "pg-host", "", "PostgreSQL host")
27+
flag.StringVar(&config.PgPort, "pg-port", "5432", "PostgreSQL port")
28+
flag.StringVar(&config.PgDatabase, "pg-database", "", "PostgreSQL database name")
29+
flag.StringVar(&config.PgUser, "pg-user", "", "PostgreSQL user")
30+
flag.StringVar(&config.PgPassword, "pg-password", "", "PostgreSQL password")
31+
flag.StringVar(&config.SqlitePath, "sqlite-path", "", "SQLite database path")
32+
flag.Parse()
33+
34+
if err := validate(config); err != nil {
35+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
36+
flag.Usage()
37+
os.Exit(1)
38+
}
39+
40+
if err := run(config); err != nil {
41+
log.Fatalf("Migration failed: %v", err)
42+
}
43+
44+
fmt.Println("Migration completed successfully!")
45+
}
46+
47+
func validate(c Config) error {
48+
if c.PgHost == "" {
49+
return fmt.Errorf("--pg-host is required")
50+
}
51+
if c.PgDatabase == "" {
52+
return fmt.Errorf("--pg-database is required")
53+
}
54+
if c.PgUser == "" {
55+
return fmt.Errorf("--pg-user is required")
56+
}
57+
if c.SqlitePath == "" {
58+
return fmt.Errorf("--sqlite-path is required")
59+
}
60+
return nil
61+
}
62+
63+
func run(config Config) error {
64+
// Connect to PostgreSQL
65+
pgDSN := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable",
66+
config.PgHost, config.PgPort, config.PgDatabase, config.PgUser, config.PgPassword)
67+
68+
pgDB, err := sql.Open("postgres", pgDSN)
69+
if err != nil {
70+
return fmt.Errorf("connecting to PostgreSQL: %w", err)
71+
}
72+
defer pgDB.Close()
73+
74+
if err := pgDB.Ping(); err != nil {
75+
return fmt.Errorf("pinging PostgreSQL: %w", err)
76+
}
77+
78+
fmt.Println("Connected to PostgreSQL")
79+
80+
// Connect to SQLite
81+
sqliteDB, err := sql.Open("sqlite3", config.SqlitePath)
82+
if err != nil {
83+
return fmt.Errorf("opening SQLite: %w", err)
84+
}
85+
defer sqliteDB.Close()
86+
87+
if err := sqliteDB.Ping(); err != nil {
88+
return fmt.Errorf("pinging SQLite: %w", err)
89+
}
90+
91+
fmt.Println("Connected to SQLite")
92+
93+
// Run migration
94+
return migrate(pgDB, sqliteDB)
95+
}

0 commit comments

Comments
 (0)