Skip to content

Commit 8d78041

Browse files
committed
Remove Account
1 parent da5a624 commit 8d78041

File tree

7 files changed

+48
-125
lines changed

7 files changed

+48
-125
lines changed

go.mod

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
module github.com/dnote/dnote-pg2sqlite
22

3-
go 1.23
3+
go 1.24.0
44

55
require (
66
github.com/lib/pq v1.10.9
7-
github.com/mattn/go-sqlite3 v1.14.24
7+
github.com/mattn/go-sqlite3 v1.14.32
88
gorm.io/driver/postgres v1.5.11
9-
gorm.io/driver/sqlite v1.5.7
10-
gorm.io/gorm v1.25.12
9+
gorm.io/driver/sqlite v1.6.0
10+
gorm.io/gorm v1.31.0
1111
)
1212

1313
require (
@@ -18,6 +18,6 @@ require (
1818
github.com/jinzhu/inflection v1.0.0 // indirect
1919
github.com/jinzhu/now v1.1.5 // indirect
2020
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
21+
golang.org/x/sync v0.17.0 // indirect
22+
golang.org/x/text v0.30.0 // indirect
2323
)

go.sum

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
1515
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
1616
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
1717
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
18-
github.com/mattn/go-sqlite3 v1.14.24 h1:tpSp2G2KyMnnQu99ngJ47EIkWVmliIizyZBfPrBWDRM=
19-
github.com/mattn/go-sqlite3 v1.14.24/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
18+
github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs=
19+
github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
2020
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2121
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2222
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -26,17 +26,17 @@ github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKs
2626
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
2727
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
2828
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=
29+
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
30+
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
31+
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
32+
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
3333
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3434
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3535
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3636
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3737
gorm.io/driver/postgres v1.5.11 h1:ubBVAfbKEUld/twyKZ0IYn9rSQh448EdelLYk9Mv314=
3838
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=
39+
gorm.io/driver/sqlite v1.6.0 h1:WHRRrIiulaPiPFmDcod6prc4l2VGVWHz80KspNsxSfQ=
40+
gorm.io/driver/sqlite v1.6.0/go.mod h1:AO9V1qIQddBESngQUKWL9yoH93HIeA1X6V633rBwyT8=
41+
gorm.io/gorm v1.31.0 h1:0VlycGreVhK7RF/Bwt51Fk8v0xLiiiFdbGDPIZQ7mJY=
42+
gorm.io/gorm v1.31.0/go.mod h1:XyQVbO2k6YkOis7C2437jSit3SsDK72s7n7rsSHd+Gs=

main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ func initSQLiteSchema(sqlitePath string) error {
125125
// AutoMigrate SQLite models
126126
if err := db.AutoMigrate(
127127
&SqliteUser{},
128-
&SqliteAccount{},
129128
&SqliteBook{},
130129
&SqliteNote{},
131130
&SqliteToken{},

migrate.go

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
type MigrationStats struct {
1010
Users int
11-
Accounts int
1211
Books int
1312
Notes int
1413
Tokens int
@@ -28,20 +27,13 @@ func migrate(pgDB, sqliteDB *sql.DB) error {
2827

2928
var stats MigrationStats
3029

31-
// Migrate users
30+
// Migrate users (with email/password from accounts table)
3231
fmt.Println("Migrating users...")
3332
if err := migrateUsers(pgDB, tx, &stats); err != nil {
3433
return fmt.Errorf("migrating users: %w", err)
3534
}
3635
fmt.Printf(" Migrated %d users\n", stats.Users)
3736

38-
// Migrate accounts
39-
fmt.Println("Migrating accounts...")
40-
if err := migrateAccounts(pgDB, tx, &stats); err != nil {
41-
return fmt.Errorf("migrating accounts: %w", err)
42-
}
43-
fmt.Printf(" Migrated %d accounts\n", stats.Accounts)
44-
4537
// Migrate books
4638
fmt.Println("Migrating books...")
4739
if err := migrateBooks(pgDB, tx, &stats); err != nil {
@@ -78,7 +70,6 @@ func migrate(pgDB, sqliteDB *sql.DB) error {
7870
// Print summary
7971
fmt.Println("\nMigration Summary:")
8072
fmt.Printf(" Users: %d\n", stats.Users)
81-
fmt.Printf(" Accounts: %d\n", stats.Accounts)
8273
fmt.Printf(" Books: %d\n", stats.Books)
8374
fmt.Printf(" Notes: %d\n", stats.Notes)
8475
fmt.Printf(" Tokens: %d\n", stats.Tokens)
@@ -89,18 +80,21 @@ func migrate(pgDB, sqliteDB *sql.DB) error {
8980

9081
func migrateUsers(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
9182
rows, err := pgDB.Query(`
92-
SELECT id, created_at, updated_at, uuid, last_login_at, max_usn, cloud
93-
FROM users
94-
ORDER BY id
83+
SELECT
84+
u.id, u.created_at, u.updated_at, u.uuid, u.last_login_at, u.max_usn,
85+
a.email, a.password
86+
FROM users u
87+
LEFT JOIN accounts a ON u.id = a.user_id
88+
ORDER BY u.id
9589
`)
9690
if err != nil {
9791
return err
9892
}
9993
defer rows.Close()
10094

10195
stmt, err := tx.Prepare(`
102-
INSERT INTO users (id, created_at, updated_at, uuid, last_login_at, max_usn)
103-
VALUES (?, ?, ?, ?, ?, ?)
96+
INSERT INTO users (id, created_at, updated_at, uuid, last_login_at, max_usn, email, password)
97+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
10498
`)
10599
if err != nil {
106100
return err
@@ -112,54 +106,16 @@ func migrateUsers(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
112106
var createdAt, updatedAt time.Time
113107
var uuid string
114108
var lastLoginAt sql.NullTime
115-
var cloud bool // Read but ignore
116-
117-
if err := rows.Scan(&id, &createdAt, &updatedAt, &uuid, &lastLoginAt, &maxUSN, &cloud); err != nil {
118-
return err
119-
}
120-
121-
if _, err := stmt.Exec(id, createdAt, updatedAt, uuid, lastLoginAt, maxUSN); err != nil {
122-
return err
123-
}
124-
stats.Users++
125-
}
126-
127-
return rows.Err()
128-
}
129-
130-
func migrateAccounts(pgDB *sql.DB, tx *sql.Tx, stats *MigrationStats) error {
131-
rows, err := pgDB.Query(`
132-
SELECT id, created_at, updated_at, user_id, email, password
133-
FROM accounts
134-
ORDER BY id
135-
`)
136-
if err != nil {
137-
return err
138-
}
139-
defer rows.Close()
140-
141-
stmt, err := tx.Prepare(`
142-
INSERT INTO accounts (id, created_at, updated_at, user_id, email, password)
143-
VALUES (?, ?, ?, ?, ?, ?)
144-
`)
145-
if err != nil {
146-
return err
147-
}
148-
defer stmt.Close()
149-
150-
for rows.Next() {
151-
var id, userID int
152-
var createdAt, updatedAt time.Time
153109
var email, password sql.NullString
154110

155-
if err := rows.Scan(&id, &createdAt, &updatedAt, &userID, &email, &password); err != nil {
111+
if err := rows.Scan(&id, &createdAt, &updatedAt, &uuid, &lastLoginAt, &maxUSN, &email, &password); err != nil {
156112
return err
157113
}
158114

159-
if _, err := stmt.Exec(id, createdAt, updatedAt, userID, email, password); err != nil {
115+
if _, err := stmt.Exec(id, createdAt, updatedAt, uuid, lastLoginAt, maxUSN, email, password); err != nil {
160116
return err
161117
}
162-
stats.Accounts++
118+
stats.Users++
163119
}
164120

165121
return rows.Err()

migrate_test.go

Lines changed: 9 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ func TestMigration(t *testing.T) {
233233
if sqliteUser1.MaxUSN != user1.MaxUSN {
234234
t.Errorf("User1 MaxUSN: expected %d, got %d", user1.MaxUSN, sqliteUser1.MaxUSN)
235235
}
236+
// Verify email/password from account were merged into user
237+
if sqliteUser1.Email.String != account1.Email.String {
238+
t.Errorf("User1 Email: expected %s, got %s", account1.Email.String, sqliteUser1.Email.String)
239+
}
240+
if sqliteUser1.Password.String != account1.Password.String {
241+
t.Errorf("User1 Password: expected %s, got %s", account1.Password.String, sqliteUser1.Password.String)
242+
}
236243

237244
// Verify user2
238245
var sqliteUser2 SqliteUser
@@ -251,38 +258,8 @@ func TestMigration(t *testing.T) {
251258
if sqliteUser2.MaxUSN != user2.MaxUSN {
252259
t.Errorf("User2 MaxUSN: expected %d, got %d", user2.MaxUSN, sqliteUser2.MaxUSN)
253260
}
254-
255-
// Verify account1
256-
var sqliteAccount1 SqliteAccount
257-
if err := sqliteDB.Where("user_id = ?", user1.ID).First(&sqliteAccount1).Error; err != nil {
258-
t.Fatalf("Failed to query account1: %v", err)
259-
}
260-
if sqliteAccount1.ID != account1.ID {
261-
t.Errorf("Account1 ID: expected %d, got %d", account1.ID, sqliteAccount1.ID)
262-
}
263-
if sqliteAccount1.UserID != account1.UserID {
264-
t.Errorf("Account1 UserID: expected %d, got %d", account1.UserID, sqliteAccount1.UserID)
265-
}
266-
if sqliteAccount1.Email.String != account1.Email.String {
267-
t.Errorf("Account1 Email: expected %s, got %s", account1.Email.String, sqliteAccount1.Email.String)
268-
}
269-
if sqliteAccount1.Password.String != account1.Password.String {
270-
t.Errorf("Account1 Password: expected %s, got %s", account1.Password.String, sqliteAccount1.Password.String)
271-
}
272-
if sqliteAccount1.CreatedAt.Unix() != account1.CreatedAt.Unix() {
273-
t.Errorf("Account1 CreatedAt: expected %v, got %v", account1.CreatedAt, sqliteAccount1.CreatedAt)
274-
}
275-
if sqliteAccount1.UpdatedAt.Unix() != account1.UpdatedAt.Unix() {
276-
t.Errorf("Account1 UpdatedAt: expected %v, got %v", account1.UpdatedAt, sqliteAccount1.UpdatedAt)
277-
}
278-
279-
// Verify account2
280-
var sqliteAccount2 SqliteAccount
281-
if err := sqliteDB.Where("user_id = ?", user2.ID).First(&sqliteAccount2).Error; err != nil {
282-
t.Fatalf("Failed to query account2: %v", err)
283-
}
284-
if sqliteAccount2.Email.String != account2.Email.String {
285-
t.Errorf("Account2 Email: expected %s, got %s", account2.Email.String, sqliteAccount2.Email.String)
261+
if sqliteUser2.Email.String != account2.Email.String {
262+
t.Errorf("User2 Email: expected %s, got %s", account2.Email.String, sqliteUser2.Email.String)
286263
}
287264

288265
// Verify book1

sqlite_models.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,18 @@ func (SqliteNote) TableName() string {
4848

4949
type SqliteUser struct {
5050
SqliteModel
51-
UUID string `json:"uuid" gorm:"type:text;index"`
52-
Account SqliteAccount `gorm:"foreignKey:UserID"`
53-
LastLoginAt *time.Time `json:"-"`
54-
MaxUSN int `json:"-" gorm:"default:0"`
51+
UUID string `json:"uuid" gorm:"type:text;index"`
52+
Email NullString `gorm:"index"`
53+
Password NullString `json:"-"`
54+
LastLoginAt *time.Time `json:"-"`
55+
MaxUSN int `json:"-" gorm:"default:0"`
56+
FullSyncBefore int64 `json:"-" gorm:"default:0"`
5557
}
5658

5759
func (SqliteUser) TableName() string {
5860
return "users"
5961
}
6062

61-
type SqliteAccount struct {
62-
SqliteModel
63-
UserID int `gorm:"index"`
64-
Email NullString
65-
Password NullString
66-
}
67-
68-
func (SqliteAccount) TableName() string {
69-
return "accounts"
70-
}
71-
7263
type SqliteToken struct {
7364
SqliteModel
7465
UserID int `gorm:"index"`

test_models.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func (PgNote) TableName() string {
5656

5757
type PgUser struct {
5858
PgModel
59-
UUID string `json:"uuid" gorm:"type:uuid;index;default:uuid_generate_v4()"`
60-
Account PgAccount `gorm:"foreignKey:UserID"`
61-
LastLoginAt *time.Time `json:"-"`
62-
MaxUSN int `json:"-" gorm:"default:0"`
63-
Cloud bool `json:"-" gorm:"default:false"`
59+
UUID string `json:"uuid" gorm:"type:uuid;index;default:uuid_generate_v4()"`
60+
Account PgAccount `gorm:"foreignKey:UserID"`
61+
LastLoginAt *time.Time `json:"-"`
62+
MaxUSN int `json:"-" gorm:"default:0"`
63+
Cloud bool `json:"-" gorm:"default:false"`
6464
}
6565

6666
func (PgUser) TableName() string {

0 commit comments

Comments
 (0)