Skip to content

Commit

Permalink
More compatibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
jvoisin committed Jan 2, 2025
1 parent cf5369c commit 92be012
Showing 1 changed file with 44 additions and 18 deletions.
62 changes: 44 additions & 18 deletions internal/database/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -733,17 +733,31 @@ var migrations = []func(tx *sql.Tx, driver string) error{
return err
},
func(tx *sql.Tx, _ string) (err error) {
// TODO sqlite doesn't support arrays
_, err = tx.Exec(`
ALTER TABLE entries ADD COLUMN tags text[] default '{}';
`)
return
},
func(tx *sql.Tx, _ string) (err error) {
sql := `
func(tx *sql.Tx, driver string) (err error) {
sql := ""
switch driver {
case "postgresql":
sql = `
ALTER TABLE users RENAME double_tap TO gesture_nav;
ALTER TABLE users ALTER COLUMN gesture_nav SET DATA TYPE text using case when gesture_nav = true then 'tap' when gesture_nav = false then 'none' end;
ALTER TABLE users ALTER COLUMN gesture_nav SET default 'tap';
`
`
case "sqlite3":
sql = `
ALTER TABLE users ADD COLUMN gesture_nav DEFAULT 'tap';
UPDATE users SET gesture_nav = 'none' WHERE double_tap = false;
ALTER TABLE users DROP COLUMN double_tap;
`
default:
panic(fmt.Sprintf("driver %s isn't supported", driver))
}

_, err = tx.Exec(sql)
return err
},
Expand All @@ -769,14 +783,17 @@ var migrations = []func(tx *sql.Tx, driver string) error{
_, err = tx.Exec(sql)
return err
},
func(tx *sql.Tx, _ string) (err error) {
func(tx *sql.Tx, driver string) (err error) {
// Delete duplicated rows
sql := `
DELETE FROM enclosures a USING enclosures b
WHERE a.id < b.id
AND a.user_id = b.user_id
AND a.entry_id = b.entry_id
AND a.url = b.url;
DELETE FROM enclosures
WHERE EXISTS (
SELECT 1 FROM enclosures e2
WHERE id < e2.id
AND user_id = e2.user_id
AND entry_id = e2.entry_id
AND url = e2.url
);
`
_, err = tx.Exec(sql)
if err != nil {
Expand All @@ -790,7 +807,14 @@ var migrations = []func(tx *sql.Tx, driver string) error{
}

// Create unique index
_, err = tx.Exec(`CREATE UNIQUE INDEX enclosures_user_entry_url_unique_idx ON enclosures(user_id, entry_id, md5(url))`)
switch driver {
case "postgresql":
_, err = tx.Exec(`CREATE UNIQUE INDEX enclosures_user_entry_url_unique_idx ON enclosures(user_id, entry_id, md5(url))`)
case "sqlite3":
_, err = tx.Exec(`CREATE UNIQUE INDEX enclosures_user_entry_url_unique_idx ON enclosures(user_id, entry_id, url)`)
default:
panic(fmt.Sprintf("driver %s isn't supported", driver))
}

Check failure on line 817 in internal/database/migrations.go

View workflow job for this annotation

GitHub Actions / Golang Linters

File is not properly formatted (gofmt)
if err != nil {
return err
}
Expand Down Expand Up @@ -971,11 +995,14 @@ var migrations = []func(tx *sql.Tx, driver string) error{
_, err = tx.Exec(sql)
return err
},
func(tx *sql.Tx, _ string) (err error) {
// the WHERE part speed-up the request a lot
sql := `UPDATE entries SET tags = array_remove(tags, '') WHERE '' = ANY(tags);`
_, err = tx.Exec(sql)
return err
func(tx *sql.Tx, driver string) (err error) {
if driver == "postgresql" {
// the WHERE part speed-up the request a lot
sql := `UPDATE entries SET tags = array_remove(tags, '') WHERE '' = ANY(tags);`
_, err = tx.Exec(sql)
return err
}
return nil
},
func(tx *sql.Tx, _ string) (err error) {
// Entry URLs can exceeds btree maximum size
Expand All @@ -1000,9 +1027,8 @@ var migrations = []func(tx *sql.Tx, driver string) error{
},
func(tx *sql.Tx, _ string) (err error) {
sql := `
ALTER TABLE users
ADD COLUMN block_filter_entry_rules text not null default '',
ADD COLUMN keep_filter_entry_rules text not null default ''
ALTER TABLE users ADD COLUMN block_filter_entry_rules text not null default '';
ALTER TABLE users ADD COLUMN keep_filter_entry_rules text not null default '';
`
_, err = tx.Exec(sql)
return err
Expand Down

0 comments on commit 92be012

Please sign in to comment.