Skip to content

Commit

Permalink
support PostgreSQL
Browse files Browse the repository at this point in the history
  • Loading branch information
Monirzadeh committed Jul 17, 2023
1 parent b872736 commit e950ff4
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
3 changes: 2 additions & 1 deletion internal/database/migrations/postgres/0001_initial.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS account(
username VARCHAR(250) NOT NULL,
password BYTEA NOT NULL,
owner BOOLEAN NOT NULL DEFAULT FALSE,
configures VARCHAR(500) NOT NULL,
PRIMARY KEY (id),
CONSTRAINT account_username_UNIQUE UNIQUE (username));

Expand Down Expand Up @@ -33,4 +34,4 @@ CREATE TABLE IF NOT EXISTS bookmark_tag(
CONSTRAINT bookmark_tag_tag_id_FK FOREIGN KEY (tag_id) REFERENCES tag (id));

CREATE INDEX IF NOT EXISTS bookmark_tag_bookmark_id_FK ON bookmark_tag (bookmark_id);
CREATE INDEX IF NOT EXISTS bookmark_tag_tag_id_FK ON bookmark_tag (tag_id);
CREATE INDEX IF NOT EXISTS bookmark_tag_tag_id_FK ON bookmark_tag (tag_id);
2 changes: 1 addition & 1 deletion internal/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func (db *MySQLDatabase) SaveAccount(ctx context.Context, account model.Account)
return errors.WithStack(err)
}

// SaveAccount saves new account to database. Returns error if any happened.
// SaveSettings update settings for specific account in database. Returns error if any happened
func (db *MySQLDatabase) SaveSettings(ctx context.Context, account model.Account) (err error) {
err = IsJson(account.Configures)
if err != nil {
Expand Down
24 changes: 20 additions & 4 deletions internal/database/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,11 +542,27 @@ func (db *PGDatabase) SaveAccount(ctx context.Context, account model.Account) (e

// Insert account to database
_, err = db.ExecContext(ctx, `INSERT INTO account
(username, password, owner) VALUES ($1, $2, $3)
(username, password, owner, configures) VALUES ($1, $2, $3, $4)
ON CONFLICT(username) DO UPDATE SET
password = $2,
owner = $3`,
account.Username, hashedPassword, account.Owner)
account.Username, hashedPassword, account.Owner, account.Configures)

return errors.WithStack(err)
}

// SaveSettings update settings for specific account in database. Returns error if any happened
func (db *PGDatabase) SaveSettings(ctx context.Context, account model.Account) (err error) {
err = IsJson(account.Configures)
if err != nil {
return errors.WithStack(err)
}

// Insert account to database
_, err = db.ExecContext(ctx, `UPDATE account
SET configures = $1
WHERE username = $2`,
account.Configures, account.Username)

return errors.WithStack(err)
}
Expand All @@ -555,7 +571,7 @@ func (db *PGDatabase) SaveAccount(ctx context.Context, account model.Account) (e
func (db *PGDatabase) GetAccounts(ctx context.Context, opts GetAccountsOptions) ([]model.Account, error) {
// Create query
args := []interface{}{}
query := `SELECT id, username, owner FROM account WHERE TRUE`
query := `SELECT id, username, owner, configures FROM account WHERE TRUE`

if opts.Keyword != "" {
query += " AND username LIKE $1"
Expand Down Expand Up @@ -583,7 +599,7 @@ func (db *PGDatabase) GetAccounts(ctx context.Context, opts GetAccountsOptions)
func (db *PGDatabase) GetAccount(ctx context.Context, username string) (model.Account, bool, error) {
account := model.Account{}
if err := db.GetContext(ctx, &account, `SELECT
id, username, password, owner FROM account WHERE username = $1`,
id, username, password, owner, configures FROM account WHERE username = $1`,
username,
); err != nil {
return account, false, errors.WithStack(err)
Expand Down

0 comments on commit e950ff4

Please sign in to comment.