Move all tables into a schema
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m19s
All checks were successful
Build and Push Docker Image / build (push) Successful in 7m19s
This commit is contained in:
@@ -11,18 +11,18 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const getClicks = `-- name: GetClicks :many
|
const getClicks = `-- name: GetClicks :many
|
||||||
SELECT click_id, url_id, clicked_at, referrer, user_agent, ip_address FROM click_logs
|
SELECT click_id, url_id, clicked_at, referrer, user_agent, ip_address FROM lss.click_logs
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetClicks(ctx context.Context) ([]ClickLog, error) {
|
func (q *Queries) GetClicks(ctx context.Context) ([]LssClickLog, error) {
|
||||||
rows, err := q.db.QueryContext(ctx, getClicks)
|
rows, err := q.db.QueryContext(ctx, getClicks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
var items []ClickLog
|
var items []LssClickLog
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var i ClickLog
|
var i LssClickLog
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.ClickID,
|
&i.ClickID,
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
@@ -45,7 +45,7 @@ func (q *Queries) GetClicks(ctx context.Context) ([]ClickLog, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const trackClick = `-- name: TrackClick :one
|
const trackClick = `-- name: TrackClick :one
|
||||||
INSERT INTO click_logs (url_id, referrer, user_agent, ip_address)
|
INSERT INTO lss.click_logs (url_id, referrer, user_agent, ip_address)
|
||||||
VALUES (
|
VALUES (
|
||||||
$1,
|
$1,
|
||||||
$2,
|
$2,
|
||||||
@@ -62,14 +62,14 @@ type TrackClickParams struct {
|
|||||||
IpAddress sql.NullString
|
IpAddress sql.NullString
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) TrackClick(ctx context.Context, arg TrackClickParams) (ClickLog, error) {
|
func (q *Queries) TrackClick(ctx context.Context, arg TrackClickParams) (LssClickLog, error) {
|
||||||
row := q.db.QueryRowContext(ctx, trackClick,
|
row := q.db.QueryRowContext(ctx, trackClick,
|
||||||
arg.UrlID,
|
arg.UrlID,
|
||||||
arg.Referrer,
|
arg.Referrer,
|
||||||
arg.UserAgent,
|
arg.UserAgent,
|
||||||
arg.IpAddress,
|
arg.IpAddress,
|
||||||
)
|
)
|
||||||
var i ClickLog
|
var i LssClickLog
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.ClickID,
|
&i.ClickID,
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ClickLog struct {
|
type LssClickLog struct {
|
||||||
ClickID int64
|
ClickID int64
|
||||||
UrlID int32
|
UrlID int32
|
||||||
ClickedAt time.Time
|
ClickedAt time.Time
|
||||||
@@ -18,7 +18,7 @@ type ClickLog struct {
|
|||||||
IpAddress sql.NullString
|
IpAddress sql.NullString
|
||||||
}
|
}
|
||||||
|
|
||||||
type Url struct {
|
type LssUrl struct {
|
||||||
UrlID int32
|
UrlID int32
|
||||||
LongUrl string
|
LongUrl string
|
||||||
ShortCode string
|
ShortCode string
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const createURL = `-- name: CreateURL :one
|
const createURL = `-- name: CreateURL :one
|
||||||
INSERT INTO urls (created_at, expires_at, long_url, short_code, is_active)
|
INSERT INTO lss.urls (created_at, expires_at, long_url, short_code, is_active)
|
||||||
VALUES (
|
VALUES (
|
||||||
$1,
|
$1,
|
||||||
$2,
|
$2,
|
||||||
@@ -31,7 +31,7 @@ type CreateURLParams struct {
|
|||||||
IsActive bool
|
IsActive bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) CreateURL(ctx context.Context, arg CreateURLParams) (Url, error) {
|
func (q *Queries) CreateURL(ctx context.Context, arg CreateURLParams) (LssUrl, error) {
|
||||||
row := q.db.QueryRowContext(ctx, createURL,
|
row := q.db.QueryRowContext(ctx, createURL,
|
||||||
arg.CreatedAt,
|
arg.CreatedAt,
|
||||||
arg.ExpiresAt,
|
arg.ExpiresAt,
|
||||||
@@ -39,7 +39,7 @@ func (q *Queries) CreateURL(ctx context.Context, arg CreateURLParams) (Url, erro
|
|||||||
arg.ShortCode,
|
arg.ShortCode,
|
||||||
arg.IsActive,
|
arg.IsActive,
|
||||||
)
|
)
|
||||||
var i Url
|
var i LssUrl
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
&i.LongUrl,
|
&i.LongUrl,
|
||||||
@@ -52,12 +52,12 @@ func (q *Queries) CreateURL(ctx context.Context, arg CreateURLParams) (Url, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
const deleteURL = `-- name: DeleteURL :one
|
const deleteURL = `-- name: DeleteURL :one
|
||||||
DELETE FROM urls WHERE url_id = $1 RETURNING url_id, long_url, short_code, created_at, expires_at, is_active
|
DELETE FROM lss.urls WHERE url_id = $1 RETURNING url_id, long_url, short_code, created_at, expires_at, is_active
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) DeleteURL(ctx context.Context, urlID int32) (Url, error) {
|
func (q *Queries) DeleteURL(ctx context.Context, urlID int32) (LssUrl, error) {
|
||||||
row := q.db.QueryRowContext(ctx, deleteURL, urlID)
|
row := q.db.QueryRowContext(ctx, deleteURL, urlID)
|
||||||
var i Url
|
var i LssUrl
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
&i.LongUrl,
|
&i.LongUrl,
|
||||||
@@ -70,7 +70,7 @@ func (q *Queries) DeleteURL(ctx context.Context, urlID int32) (Url, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getURLByShortCode = `-- name: GetURLByShortCode :one
|
const getURLByShortCode = `-- name: GetURLByShortCode :one
|
||||||
SELECT url_id, long_url FROM urls WHERE is_active = true AND short_code = $1
|
SELECT url_id, long_url FROM lss.urls WHERE is_active = true AND short_code = $1
|
||||||
`
|
`
|
||||||
|
|
||||||
type GetURLByShortCodeRow struct {
|
type GetURLByShortCodeRow struct {
|
||||||
@@ -86,18 +86,18 @@ func (q *Queries) GetURLByShortCode(ctx context.Context, shortCode string) (GetU
|
|||||||
}
|
}
|
||||||
|
|
||||||
const getURLs = `-- name: GetURLs :many
|
const getURLs = `-- name: GetURLs :many
|
||||||
SELECT url_id, long_url, short_code, created_at, expires_at, is_active FROM urls
|
SELECT url_id, long_url, short_code, created_at, expires_at, is_active FROM lss.urls
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) GetURLs(ctx context.Context) ([]Url, error) {
|
func (q *Queries) GetURLs(ctx context.Context) ([]LssUrl, error) {
|
||||||
rows, err := q.db.QueryContext(ctx, getURLs)
|
rows, err := q.db.QueryContext(ctx, getURLs)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
var items []Url
|
var items []LssUrl
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var i Url
|
var i LssUrl
|
||||||
if err := rows.Scan(
|
if err := rows.Scan(
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
&i.LongUrl,
|
&i.LongUrl,
|
||||||
@@ -120,7 +120,7 @@ func (q *Queries) GetURLs(ctx context.Context) ([]Url, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const setActive = `-- name: SetActive :one
|
const setActive = `-- name: SetActive :one
|
||||||
UPDATE urls SET is_active = $1 WHERE url_id = $2 RETURNING url_id, long_url, short_code, created_at, expires_at, is_active
|
UPDATE lss.urls SET is_active = $1 WHERE url_id = $2 RETURNING url_id, long_url, short_code, created_at, expires_at, is_active
|
||||||
`
|
`
|
||||||
|
|
||||||
type SetActiveParams struct {
|
type SetActiveParams struct {
|
||||||
@@ -128,9 +128,9 @@ type SetActiveParams struct {
|
|||||||
UrlID int32
|
UrlID int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) SetActive(ctx context.Context, arg SetActiveParams) (Url, error) {
|
func (q *Queries) SetActive(ctx context.Context, arg SetActiveParams) (LssUrl, error) {
|
||||||
row := q.db.QueryRowContext(ctx, setActive, arg.IsActive, arg.UrlID)
|
row := q.db.QueryRowContext(ctx, setActive, arg.IsActive, arg.UrlID)
|
||||||
var i Url
|
var i LssUrl
|
||||||
err := row.Scan(
|
err := row.Scan(
|
||||||
&i.UrlID,
|
&i.UrlID,
|
||||||
&i.LongUrl,
|
&i.LongUrl,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
-- name: TrackClick :one
|
-- name: TrackClick :one
|
||||||
INSERT INTO click_logs (url_id, referrer, user_agent, ip_address)
|
INSERT INTO lss.click_logs (url_id, referrer, user_agent, ip_address)
|
||||||
VALUES (
|
VALUES (
|
||||||
$1,
|
$1,
|
||||||
$2,
|
$2,
|
||||||
@@ -9,4 +9,4 @@ VALUES (
|
|||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
-- name: GetClicks :many
|
-- name: GetClicks :many
|
||||||
SELECT * FROM click_logs;
|
SELECT * FROM lss.click_logs;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
-- name: CreateURL :one
|
-- name: CreateURL :one
|
||||||
INSERT INTO urls (created_at, expires_at, long_url, short_code, is_active)
|
INSERT INTO lss.urls (created_at, expires_at, long_url, short_code, is_active)
|
||||||
VALUES (
|
VALUES (
|
||||||
$1,
|
$1,
|
||||||
$2,
|
$2,
|
||||||
@@ -10,13 +10,13 @@ VALUES (
|
|||||||
RETURNING *;
|
RETURNING *;
|
||||||
|
|
||||||
-- name: GetURLs :many
|
-- name: GetURLs :many
|
||||||
SELECT * FROM urls;
|
SELECT * FROM lss.urls;
|
||||||
|
|
||||||
-- name: GetURLByShortCode :one
|
-- name: GetURLByShortCode :one
|
||||||
SELECT url_id, long_url FROM urls WHERE is_active = true AND short_code = $1;
|
SELECT url_id, long_url FROM lss.urls WHERE is_active = true AND short_code = $1;
|
||||||
|
|
||||||
-- name: SetActive :one
|
-- name: SetActive :one
|
||||||
UPDATE urls SET is_active = $1 WHERE url_id = $2 RETURNING *;
|
UPDATE lss.urls SET is_active = $1 WHERE url_id = $2 RETURNING *;
|
||||||
|
|
||||||
-- name: DeleteURL :one
|
-- name: DeleteURL :one
|
||||||
DELETE FROM urls WHERE url_id = $1 RETURNING *;
|
DELETE FROM lss.urls WHERE url_id = $1 RETURNING *;
|
||||||
@@ -1,5 +1,7 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
CREATE TABLE urls (
|
CREATE SCHEMA IF NOT EXISTS lss;
|
||||||
|
|
||||||
|
CREATE TABLE lss.urls (
|
||||||
url_id SERIAL PRIMARY KEY,
|
url_id SERIAL PRIMARY KEY,
|
||||||
long_url TEXT NOT NULL,
|
long_url TEXT NOT NULL,
|
||||||
short_code VARCHAR(10) UNIQUE NOT NULL,
|
short_code VARCHAR(10) UNIQUE NOT NULL,
|
||||||
@@ -9,8 +11,8 @@ CREATE TABLE urls (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Crucial index for lightning-fast lookups when redirecting
|
-- Crucial index for lightning-fast lookups when redirecting
|
||||||
CREATE INDEX idx_urls_short_code ON urls(short_code);
|
CREATE INDEX idx_urls_short_code ON lss.urls(short_code);
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
DROP INDEX idx_urls_short_code;
|
DROP INDEX idx_urls_short_code;
|
||||||
DROP TABLE urls;
|
DROP TABLE lss.urls;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
-- +goose Up
|
-- +goose Up
|
||||||
CREATE TABLE click_logs (
|
CREATE TABLE lss.click_logs (
|
||||||
click_id BIGSERIAL PRIMARY KEY,
|
click_id BIGSERIAL PRIMARY KEY,
|
||||||
url_id INT NOT NULL REFERENCES urls(url_id) ON DELETE CASCADE,
|
url_id INT NOT NULL REFERENCES urls(url_id) ON DELETE CASCADE,
|
||||||
clicked_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
clicked_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
@@ -9,8 +9,8 @@ CREATE TABLE click_logs (
|
|||||||
);
|
);
|
||||||
|
|
||||||
-- Index to optimize analytics queries for a specific link
|
-- Index to optimize analytics queries for a specific link
|
||||||
CREATE INDEX idx_click_logs_url_id ON click_logs(url_id);
|
CREATE INDEX idx_click_logs_url_id ON lss.click_logs(url_id);
|
||||||
|
|
||||||
-- +goose Down
|
-- +goose Down
|
||||||
DROP INDEX idx_click_logs_url_id;
|
DROP INDEX idx_click_logs_url_id;
|
||||||
DROP TABLE click_logs;
|
DROP TABLE lss.click_logs;
|
||||||
Reference in New Issue
Block a user