diff --git a/internal/database/migrations/mysql/0005_deleted_at.up.sql b/internal/database/migrations/mysql/0005_deleted_at.up.sql new file mode 100644 index 000000000..3cb33b46f --- /dev/null +++ b/internal/database/migrations/mysql/0005_deleted_at.up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS deleted( + id INT(11) NOT NULL, + deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); diff --git a/internal/database/migrations/postgres/0002_deleted_at.up.sql b/internal/database/migrations/postgres/0002_deleted_at.up.sql new file mode 100644 index 000000000..92eeccf49 --- /dev/null +++ b/internal/database/migrations/postgres/0002_deleted_at.up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS deleted( + id INTEGER NOT NULL, + deleted_at TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP + ); diff --git a/internal/database/migrations/sqlite/0004_deleted_at.up.sql b/internal/database/migrations/sqlite/0004_deleted_at.up.sql new file mode 100644 index 000000000..8258b9caa --- /dev/null +++ b/internal/database/migrations/sqlite/0004_deleted_at.up.sql @@ -0,0 +1,4 @@ +CREATE TABLE IF NOT EXISTS deleted( + id INTEGER NOT NULL, + deleted_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP + ); diff --git a/internal/database/mysql.go b/internal/database/mysql.go index 72aa18b97..2203827ec 100644 --- a/internal/database/mysql.go +++ b/internal/database/mysql.go @@ -61,6 +61,7 @@ var mysqlMigrations = []migration{ return nil }), + newFileMigration("0.7.0", "0.8.0", "mysql/0005_deleted_at"), } // MySQLDatabase is implementation of Database interface @@ -518,6 +519,7 @@ func (db *MySQLDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err e // Prepare queries delBookmark := `DELETE FROM bookmark` delBookmarkTag := `DELETE FROM bookmark_tag` + delBookmarkTime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)` // Delete bookmark(s) if len(ids) == 0 { @@ -530,9 +532,15 @@ func (db *MySQLDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err e if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarkTime, ids, deletedTime) + if err != nil { + return errors.WithStack(err) + } } else { delBookmark += ` WHERE id = ?` delBookmarkTag += ` WHERE bookmark_id = ?` + delBookmarkTime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)` stmtDelBookmark, _ := tx.Preparex(delBookmark) stmtDelBookmarkTag, _ := tx.Preparex(delBookmarkTag) @@ -547,6 +555,11 @@ func (db *MySQLDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err e if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarkTime, id, deletedTime) + if err != nil { + return errors.WithStack(err) + } } } diff --git a/internal/database/pg.go b/internal/database/pg.go index 22dc485e4..4e9ba7900 100644 --- a/internal/database/pg.go +++ b/internal/database/pg.go @@ -57,6 +57,7 @@ var postgresMigrations = []migration{ return nil }), + newFileMigration("0.3.0", "0.4.0", "postgres/0002_deleted_at"), } // PGDatabase is implementation of Database interface @@ -518,6 +519,7 @@ func (db *PGDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err erro // Prepare queries delBookmark := `DELETE FROM bookmark` delBookmarkTag := `DELETE FROM bookmark_tag` + delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES ($1, $2) ` // Delete bookmark(s) if len(ids) == 0 { @@ -530,9 +532,15 @@ func (db *PGDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err erro if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarktime, ids, deletedTime) + if err != nil { + return errors.WithStack(err) + } } else { delBookmark += ` WHERE id = $1` delBookmarkTag += ` WHERE bookmark_id = $1` + delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES ($1, $2) ` stmtDelBookmark, err := tx.Preparex(delBookmark) if err != nil { @@ -553,6 +561,11 @@ func (db *PGDatabase) DeleteBookmarks(ctx context.Context, ids ...int) (err erro if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarktime, id, deletedTime) + if err != nil { + return errors.WithStack(err) + } } } diff --git a/internal/database/sqlite.go b/internal/database/sqlite.go index 810c74638..244502ca3 100644 --- a/internal/database/sqlite.go +++ b/internal/database/sqlite.go @@ -60,6 +60,7 @@ var sqliteMigrations = []migration{ }), newFileMigration("0.3.0", "0.4.0", "sqlite/0002_denormalize_content"), newFileMigration("0.4.0", "0.5.0", "sqlite/0003_uniq_id"), + newFileMigration("0.5.0", "0.6.0", "sqlite/0004_deleted_at"), } // SQLiteDatabase is implementation of Database interface @@ -601,6 +602,7 @@ func (db *SQLiteDatabase) DeleteBookmarks(ctx context.Context, ids ...int) error delBookmark := `DELETE FROM bookmark` delBookmarkTag := `DELETE FROM bookmark_tag` delBookmarkContent := `DELETE FROM bookmark_content` + delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)` // Delete bookmark(s) if len(ids) == 0 { @@ -618,10 +620,16 @@ func (db *SQLiteDatabase) DeleteBookmarks(ctx context.Context, ids ...int) error if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarktime, ids, deletedTime) + if err != nil { + return errors.WithStack(err) + } } else { delBookmark += ` WHERE id = ?` delBookmarkTag += ` WHERE bookmark_id = ?` delBookmarkContent += ` WHERE docid = ?` + delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)` stmtDelBookmark, err := tx.Preparex(delBookmark) if err != nil { @@ -653,6 +661,11 @@ func (db *SQLiteDatabase) DeleteBookmarks(ctx context.Context, ids ...int) error if err != nil { return errors.WithStack(err) } + deletedTime := time.Now().UTC().Format(model.DatabaseDateFormat) + _, err = tx.ExecContext(ctx, delBookmarktime, id, deletedTime) + if err != nil { + return errors.WithStack(err) + } } }