Skip to content

Commit 58a2137

Browse files
committed
Document
1 parent ed2f6d9 commit 58a2137

File tree

3 files changed

+72
-22
lines changed

3 files changed

+72
-22
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
Migration tool from PostgreSQL to SQLite for Dnote.
44

5+
This tool migrates data from **Dnote server v2.x** (PostgreSQL) to **Dnote server v3** (SQLite).
6+
7+
## Prerequisites
8+
9+
- You must be running **Dnote server v2.x**
10+
511
## Installation
612

713
```bash
@@ -17,9 +23,13 @@ dnote-pg2sqlite \
1723
--pg-database dnote \
1824
--pg-user dnote \
1925
--pg-password yourpassword \
20-
--sqlite-path /path/to/server.db
26+
--sqlite-path ~/.local/share/dnote/server.db
2127
```
2228

29+
**Note**: Dnote v3 uses XDG directories. The default SQLite database path is `~/.local/share/dnote/server.db` (or `$XDG_DATA_HOME/dnote/server.db`).
30+
31+
**Safety**: The migration tool will refuse to run if the SQLite file already exists, preventing accidental overwrites. Remove the existing file if you need to re-run the migration.
32+
2333
## Backup First
2434

2535
**Always backup PostgreSQL before migrating:**
@@ -36,10 +46,19 @@ pg_dump -h localhost -U dnote -d dnote > dnote_backup.sql
3646

3747
Full-text search index rebuilds automatically.
3848

49+
## Migration Workflow
50+
51+
1. **Ensure you're on v2.x**: Upgrade to Dnote server v2.x if needed
52+
2. **Stop your Dnote server** to ensure data consistency during migration
53+
3. **Backup your PostgreSQL database** (see above)
54+
4. **Run the migration tool** with your PostgreSQL credentials
55+
5. **Verify the migration** succeeded (check record counts)
56+
6. **Upgrade to Dnote v3** and configure it to use the new SQLite database
57+
3958
## Verification
4059

4160
After migration:
4261

4362
1. Check the tool's output for record counts
44-
2. Start Dnote and verify login works
63+
2. Start Dnote v3 and verify login works
4564
3. Verify notes and search work correctly

main.go

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
"fmt"
77
"log"
88
"os"
9+
"path/filepath"
910

1011
_ "github.com/lib/pq"
1112
_ "github.com/mattn/go-sqlite3"
13+
"gorm.io/driver/sqlite"
14+
"gorm.io/gorm"
1215
)
1316

1417
type Config struct {
@@ -61,6 +64,19 @@ func validate(c Config) error {
6164
}
6265

6366
func run(config Config) error {
67+
// Create directory if it doesn't exist
68+
dir := filepath.Dir(config.SqlitePath)
69+
if err := os.MkdirAll(dir, 0755); err != nil {
70+
return fmt.Errorf("creating database directory at %s: %w", dir, err)
71+
}
72+
73+
// Check if SQLite file already exists
74+
if _, err := os.Stat(config.SqlitePath); err == nil {
75+
return fmt.Errorf("SQLite database already exists at %s - refusing to overwrite. Please remove the file or choose a different path", config.SqlitePath)
76+
} else if !os.IsNotExist(err) {
77+
return fmt.Errorf("checking if SQLite file exists: %w", err)
78+
}
79+
6480
// Connect to PostgreSQL
6581
pgDSN := fmt.Sprintf("host=%s port=%s dbname=%s user=%s password=%s sslmode=disable",
6682
config.PgHost, config.PgPort, config.PgDatabase, config.PgUser, config.PgPassword)
@@ -77,7 +93,7 @@ func run(config Config) error {
7793

7894
fmt.Println("Connected to PostgreSQL")
7995

80-
// Connect to SQLite
96+
// Connect to SQLite with GORM
8197
sqliteDB, err := sql.Open("sqlite3", config.SqlitePath)
8298
if err != nil {
8399
return fmt.Errorf("opening SQLite: %w", err)
@@ -90,6 +106,33 @@ func run(config Config) error {
90106

91107
fmt.Println("Connected to SQLite")
92108

109+
// Initialize SQLite schema using GORM
110+
fmt.Println("Creating SQLite schema...")
111+
if err := initSQLiteSchema(config.SqlitePath); err != nil {
112+
return fmt.Errorf("initializing SQLite schema: %w", err)
113+
}
114+
93115
// Run migration
94116
return migrate(pgDB, sqliteDB)
95117
}
118+
119+
func initSQLiteSchema(sqlitePath string) error {
120+
db, err := gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
121+
if err != nil {
122+
return fmt.Errorf("opening SQLite with GORM: %w", err)
123+
}
124+
125+
// AutoMigrate SQLite models
126+
if err := db.AutoMigrate(
127+
&SqliteUser{},
128+
&SqliteAccount{},
129+
&SqliteBook{},
130+
&SqliteNote{},
131+
&SqliteToken{},
132+
&SqliteSession{},
133+
); err != nil {
134+
return fmt.Errorf("running AutoMigrate: %w", err)
135+
}
136+
137+
return nil
138+
}

migrate_test.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -185,25 +185,7 @@ func TestMigration(t *testing.T) {
185185
sqlitePath := "/tmp/dnote_test.db"
186186
os.Remove(sqlitePath) // Clean up any existing test db
187187

188-
// Create SQLite schema using GORM AutoMigrate
189-
sqliteDB, err := gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
190-
if err != nil {
191-
t.Fatalf("Failed to open SQLite: %v", err)
192-
}
193-
194-
// AutoMigrate SQLite models
195-
if err := sqliteDB.AutoMigrate(
196-
&SqliteUser{},
197-
&SqliteAccount{},
198-
&SqliteBook{},
199-
&SqliteNote{},
200-
&SqliteToken{},
201-
&SqliteSession{},
202-
); err != nil {
203-
t.Fatalf("Failed to migrate SQLite schema: %v", err)
204-
}
205-
206-
// Run migration
188+
// Run migration (will create schema automatically)
207189
config := Config{
208190
PgHost: pgHost,
209191
PgPort: pgPort,
@@ -217,6 +199,12 @@ func TestMigration(t *testing.T) {
217199
t.Fatalf("Migration failed: %v", err)
218200
}
219201

202+
// Open SQLite with GORM for verification
203+
sqliteDB, err := gorm.Open(sqlite.Open(sqlitePath), &gorm.Config{})
204+
if err != nil {
205+
t.Fatalf("Failed to open SQLite for verification: %v", err)
206+
}
207+
220208
// Verify SQLite data using GORM
221209
// Verify user1
222210
var sqliteUser1 SqliteUser

0 commit comments

Comments
 (0)