WIP: Migrate from GORM to sqlc - partial migration, build broken
This commit is contained in:
parent
560b40503a
commit
dad96978e0
20 changed files with 1241 additions and 709 deletions
20
backend/internal/db/queries/aliases.sql
Normal file
20
backend/internal/db/queries/aliases.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
-- name: GetAllAliases :many
|
||||
SELECT * FROM virtual_aliases;
|
||||
|
||||
-- name: GetAliasesByDomain :many
|
||||
SELECT * FROM virtual_aliases WHERE domain_id = ?;
|
||||
|
||||
-- name: GetAliasByID :one
|
||||
SELECT * FROM virtual_aliases WHERE id = ?;
|
||||
|
||||
-- name: GetAliasBySource :one
|
||||
SELECT * FROM virtual_aliases WHERE source = ?;
|
||||
|
||||
-- name: CreateAlias :exec
|
||||
INSERT INTO virtual_aliases (domain_id, source, destination) VALUES (?, ?, ?);
|
||||
|
||||
-- name: DeleteAlias :exec
|
||||
DELETE FROM virtual_aliases WHERE id = ?;
|
||||
|
||||
-- name: CountAliasesByDomain :one
|
||||
SELECT COUNT(*) as count FROM virtual_aliases WHERE domain_id = ?;
|
||||
26
backend/internal/db/queries/domains.sql
Normal file
26
backend/internal/db/queries/domains.sql
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-- name: GetAllDomains :many
|
||||
SELECT * FROM virtual_domains ORDER BY name ASC;
|
||||
|
||||
-- name: GetDomainByID :one
|
||||
SELECT * FROM virtual_domains WHERE id = ?;
|
||||
|
||||
-- name: GetDomainByName :one
|
||||
SELECT * FROM virtual_domains WHERE name = ?;
|
||||
|
||||
-- name: CreateDomain :exec
|
||||
INSERT INTO virtual_domains (name) VALUES (?);
|
||||
|
||||
-- name: DeleteDomain :exec
|
||||
DELETE FROM virtual_domains WHERE id = ?;
|
||||
|
||||
-- name: GetAllDomainsWithCounts :many
|
||||
SELECT
|
||||
d.id,
|
||||
d.name,
|
||||
d.created_at,
|
||||
COALESCE(u.user_count, 0) as user_count,
|
||||
COALESCE(a.alias_count, 0) as alias_count
|
||||
FROM virtual_domains d
|
||||
LEFT JOIN (SELECT domain_id, COUNT(*) as user_count FROM virtual_users GROUP BY domain_id) u ON u.domain_id = d.id
|
||||
LEFT JOIN (SELECT domain_id, COUNT(*) as alias_count FROM virtual_aliases GROUP BY domain_id) a ON a.domain_id = d.id
|
||||
ORDER BY d.name ASC;
|
||||
36
backend/internal/db/queries/imc_users.sql
Normal file
36
backend/internal/db/queries/imc_users.sql
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
-- name: GetImcUserByUsername :one
|
||||
SELECT * FROM imc_users WHERE username = ?;
|
||||
|
||||
-- name: GetImcUserByID :one
|
||||
SELECT * FROM imc_users WHERE id = ?;
|
||||
|
||||
-- name: CreateImcUser :exec
|
||||
INSERT INTO imc_users (username, password_hash, role, domain_id) VALUES (?, ?, ?, ?);
|
||||
|
||||
-- name: UpdateImcUserPassword :exec
|
||||
UPDATE imc_users SET password_hash = ? WHERE id = ?;
|
||||
|
||||
-- name: DeleteImcUser :exec
|
||||
DELETE FROM imc_users WHERE id = ?;
|
||||
|
||||
-- name: GetUsersForDomain :many
|
||||
SELECT imc_users.* FROM imc_users WHERE domain_id = ?;
|
||||
|
||||
-- name: GetUsersForDomainViaJoin :many
|
||||
SELECT imc_users.* FROM imc_users
|
||||
JOIN imc_users2domains ON imc_users.id = imc_users2domains.user_id
|
||||
WHERE imc_users2domains.domain_id = ?;
|
||||
|
||||
-- name: AddUserToDomain :exec
|
||||
INSERT INTO imc_users2domains (user_id, domain_id) VALUES (?, ?);
|
||||
|
||||
-- name: RemoveUserFromDomain :exec
|
||||
DELETE FROM imc_users2domains WHERE user_id = ? AND domain_id = ?;
|
||||
|
||||
-- name: IsUserInDomain :one
|
||||
SELECT COUNT(*) as count FROM imc_users2domains WHERE user_id = ? AND domain_id = ?;
|
||||
|
||||
-- name: GetAdditionalDomainsForUser :many
|
||||
SELECT virtual_domains.* FROM virtual_domains
|
||||
JOIN imc_users2domains ON virtual_domains.id = imc_users2domains.domain_id
|
||||
WHERE imc_users2domains.user_id = ?;
|
||||
52
backend/internal/db/queries/schema.sql
Normal file
52
backend/internal/db/queries/schema.sql
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
-- ISPmail tables (existing)
|
||||
CREATE TABLE IF NOT EXISTS virtual_domains (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(50) NOT NULL UNIQUE,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS virtual_users (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
domain_id INT UNSIGNED NOT NULL,
|
||||
email VARCHAR(100) NOT NULL UNIQUE,
|
||||
password VARCHAR(150) NOT NULL,
|
||||
quota BIGINT DEFAULT 0,
|
||||
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS virtual_aliases (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
domain_id INT UNSIGNED NOT NULL,
|
||||
source VARCHAR(100) NOT NULL,
|
||||
destination VARCHAR(100) NOT NULL,
|
||||
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
-- IMC application tables
|
||||
CREATE TABLE IF NOT EXISTS imc_users (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) NOT NULL UNIQUE,
|
||||
password_hash VARCHAR(255) NOT NULL,
|
||||
role ENUM('admin','user') DEFAULT 'user',
|
||||
domain_id INT UNSIGNED NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS imc_login_attempts (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
username VARCHAR(100) NOT NULL,
|
||||
ip_address VARCHAR(45) NOT NULL,
|
||||
attempted_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
successful BOOLEAN DEFAULT FALSE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS imc_users2domains (
|
||||
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT UNSIGNED NOT NULL,
|
||||
domain_id INT UNSIGNED NOT NULL,
|
||||
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
||||
UNIQUE KEY unique_user_domain (user_id, domain_id),
|
||||
FOREIGN KEY (user_id) REFERENCES imc_users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (domain_id) REFERENCES virtual_domains(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
26
backend/internal/db/queries/users.sql
Normal file
26
backend/internal/db/queries/users.sql
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
-- name: GetAllMailUsers :many
|
||||
SELECT * FROM virtual_users;
|
||||
|
||||
-- name: GetUsersByDomain :many
|
||||
SELECT * FROM virtual_users WHERE domain_id = ?;
|
||||
|
||||
-- name: GetUserByID :one
|
||||
SELECT * FROM virtual_users WHERE id = ?;
|
||||
|
||||
-- name: GetUserByEmail :one
|
||||
SELECT * FROM virtual_users WHERE email = ?;
|
||||
|
||||
-- name: CreateUser :exec
|
||||
INSERT INTO virtual_users (domain_id, email, password, quota) VALUES (?, ?, ?, ?);
|
||||
|
||||
-- name: UpdateUserPassword :exec
|
||||
UPDATE virtual_users SET password = ? WHERE id = ?;
|
||||
|
||||
-- name: UpdateUserQuota :exec
|
||||
UPDATE virtual_users SET quota = ? WHERE id = ?;
|
||||
|
||||
-- name: DeleteUser :exec
|
||||
DELETE FROM virtual_users WHERE id = ?;
|
||||
|
||||
-- name: CountUsersByDomain :one
|
||||
SELECT COUNT(*) as count FROM virtual_users WHERE domain_id = ?;
|
||||
Loading…
Add table
Add a link
Reference in a new issue