-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa5d4f9
commit fa2ed59
Showing
14 changed files
with
179 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CREATE TABLE IF NOT EXISTS topics ( | ||
id VARCHAR(36) PRIMARY KEY, | ||
name VARCHAR(150) NOT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP DEFAULT NULL | ||
); | ||
|
||
CREATE UNIQUE INDEX idx_topic_name_unique ON topics(name) WHERE deleted_at IS NULL; | ||
CREATE INDEX idx_topic_search_by_id ON topics (id); | ||
CREATE INDEX idx_topic_search_by_name ON topics (name, deleted_at); |
17 changes: 17 additions & 0 deletions
17
migration/pgsql/1721051648_create_topic_subscribers_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
CREATE TABLE IF NOT EXISTS topic_subscribers ( | ||
id VARCHAR(36) PRIMARY KEY, | ||
topic_id VARCHAR(36) NOT NULL, | ||
name VARCHAR(150) NOT NULL, | ||
option JSON DEFAULT NULL, | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
deleted_at TIMESTAMP DEFAULT NULL, | ||
FOREIGN KEY (topic_id) | ||
REFERENCES topics (id) | ||
ON DELETE CASCADE | ||
ON UPDATE NO ACTION | ||
); | ||
|
||
CREATE UNIQUE INDEX idx_subscriber_name_unique ON topic_subscribers(name, topic_id) WHERE deleted_at IS NULL; | ||
CREATE INDEX idx_topic_subscribers_search_by_topic_id ON topic_subscribers (topic_id, deleted_at); | ||
CREATE INDEX idx_topic_subscribers_search_by_name ON topic_subscribers (name, deleted_at); |
13 changes: 13 additions & 0 deletions
13
migration/pgsql/1721051691_create_topic_message_tables.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE IF NOT EXISTS topic_messages ( | ||
id VARCHAR(36) PRIMARY KEY, | ||
topic_id VARCHAR(36) NOT NULL, | ||
message TEXT NOT NULL, | ||
status VARCHAR DEFAULT 'waiting', | ||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, | ||
FOREIGN KEY (topic_id) | ||
REFERENCES topics (id) | ||
ON DELETE CASCADE | ||
ON UPDATE NO ACTION | ||
); | ||
|
||
CREATE INDEX idx_topic_messages_search_by_topic_and_status ON topic_messages (topic_id, status); |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package postgre | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
"net/url" | ||
|
||
"github.com/jmoiron/sqlx" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
type Postgre struct { | ||
Database *sqlx.DB | ||
} | ||
|
||
type Config struct { | ||
Host string | ||
Username string | ||
Password string | ||
Name string | ||
Port int | ||
Timezone string | ||
MaxOpenConns int | ||
MaxIdleConns int | ||
} | ||
|
||
func New(config Config) (*Postgre, error) { | ||
db, err := sqlx.Connect("postgres", fmt.Sprintf( | ||
"host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=%s", | ||
config.Host, | ||
config.Username, | ||
config.Password, | ||
config.Name, | ||
config.Port, | ||
url.QueryEscape(config.Timezone))) | ||
if err != nil { | ||
slog.Error("[pgx.NewConnPool] failed to connect to database", "error", err) | ||
return &Postgre{}, nil | ||
} | ||
|
||
return &Postgre{ | ||
Database: db, | ||
}, nil | ||
} | ||
|
||
func (pg *Postgre) Conn() *sqlx.DB { | ||
return pg.Database | ||
} | ||
|
||
func (pg *Postgre) Close() error { | ||
return pg.Database.Close() | ||
} |