Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: deleted time added to the database #903

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
4 changes: 4 additions & 0 deletions internal/database/migrations/mysql/0005_deleted_at.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS deleted(
id INT(11) NOT NULL,
deleted_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
4 changes: 4 additions & 0 deletions internal/database/migrations/postgres/0002_deleted_at.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS deleted(
id INTEGER NOT NULL,
deleted_at TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
4 changes: 4 additions & 0 deletions internal/database/migrations/sqlite/0004_deleted_at.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS deleted(
id INTEGER NOT NULL,
deleted_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);
13 changes: 13 additions & 0 deletions internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@

return nil
}),
newFileMigration("0.7.0", "0.8.0", "mysql/0005_deleted_at"),
}

// MySQLDatabase is implementation of Database interface
Expand Down Expand Up @@ -518,6 +519,7 @@
// Prepare queries
delBookmark := `DELETE FROM bookmark`
delBookmarkTag := `DELETE FROM bookmark_tag`
delBookmarkTime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)`

Check warning on line 522 in internal/database/mysql.go

View check run for this annotation

Codecov / codecov/patch

internal/database/mysql.go#L522

Added line #L522 was not covered by tests

// Delete bookmark(s)
if len(ids) == 0 {
Expand All @@ -530,9 +532,15 @@
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)

Check warning on line 538 in internal/database/mysql.go

View check run for this annotation

Codecov / codecov/patch

internal/database/mysql.go#L535-L538

Added lines #L535 - L538 were not covered by tests
}
} else {
delBookmark += ` WHERE id = ?`
delBookmarkTag += ` WHERE bookmark_id = ?`
delBookmarkTime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)`

Check warning on line 543 in internal/database/mysql.go

View check run for this annotation

Codecov / codecov/patch

internal/database/mysql.go#L543

Added line #L543 was not covered by tests

stmtDelBookmark, _ := tx.Preparex(delBookmark)
stmtDelBookmarkTag, _ := tx.Preparex(delBookmarkTag)
Expand All @@ -547,6 +555,11 @@
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)

Check warning on line 561 in internal/database/mysql.go

View check run for this annotation

Codecov / codecov/patch

internal/database/mysql.go#L558-L561

Added lines #L558 - L561 were not covered by tests
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

return nil
}),
newFileMigration("0.3.0", "0.4.0", "postgres/0002_deleted_at"),
}

// PGDatabase is implementation of Database interface
Expand Down Expand Up @@ -518,6 +519,7 @@
// Prepare queries
delBookmark := `DELETE FROM bookmark`
delBookmarkTag := `DELETE FROM bookmark_tag`
delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES ($1, $2) `

Check warning on line 522 in internal/database/pg.go

View check run for this annotation

Codecov / codecov/patch

internal/database/pg.go#L522

Added line #L522 was not covered by tests

// Delete bookmark(s)
if len(ids) == 0 {
Expand All @@ -530,9 +532,15 @@
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)

Check warning on line 538 in internal/database/pg.go

View check run for this annotation

Codecov / codecov/patch

internal/database/pg.go#L535-L538

Added lines #L535 - L538 were not covered by tests
}
} else {
delBookmark += ` WHERE id = $1`
delBookmarkTag += ` WHERE bookmark_id = $1`
delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES ($1, $2) `

Check warning on line 543 in internal/database/pg.go

View check run for this annotation

Codecov / codecov/patch

internal/database/pg.go#L543

Added line #L543 was not covered by tests

stmtDelBookmark, err := tx.Preparex(delBookmark)
if err != nil {
Expand All @@ -553,6 +561,11 @@
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)

Check warning on line 567 in internal/database/pg.go

View check run for this annotation

Codecov / codecov/patch

internal/database/pg.go#L564-L567

Added lines #L564 - L567 were not covered by tests
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions internal/database/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
}),
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
Expand Down Expand Up @@ -601,6 +602,7 @@
delBookmark := `DELETE FROM bookmark`
delBookmarkTag := `DELETE FROM bookmark_tag`
delBookmarkContent := `DELETE FROM bookmark_content`
delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)`

Check warning on line 605 in internal/database/sqlite.go

View check run for this annotation

Codecov / codecov/patch

internal/database/sqlite.go#L605

Added line #L605 was not covered by tests

// Delete bookmark(s)
if len(ids) == 0 {
Expand All @@ -618,10 +620,16 @@
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)

Check warning on line 626 in internal/database/sqlite.go

View check run for this annotation

Codecov / codecov/patch

internal/database/sqlite.go#L623-L626

Added lines #L623 - L626 were not covered by tests
}
} else {
delBookmark += ` WHERE id = ?`
delBookmarkTag += ` WHERE bookmark_id = ?`
delBookmarkContent += ` WHERE docid = ?`
delBookmarktime := `INSERT INTO deleted (id, deleted_at) VALUES(?,?)`

Check warning on line 632 in internal/database/sqlite.go

View check run for this annotation

Codecov / codecov/patch

internal/database/sqlite.go#L632

Added line #L632 was not covered by tests

stmtDelBookmark, err := tx.Preparex(delBookmark)
if err != nil {
Expand Down Expand Up @@ -653,6 +661,11 @@
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)

Check warning on line 667 in internal/database/sqlite.go

View check run for this annotation

Codecov / codecov/patch

internal/database/sqlite.go#L664-L667

Added lines #L664 - L667 were not covered by tests
}
}
}

Expand Down
Loading