This commit is contained in:
2025-11-08 15:48:53 +08:00
parent 55b496f585
commit a0c1cc3f71
5 changed files with 31 additions and 33 deletions

View File

@@ -4,37 +4,37 @@ CREATE TABLE IF NOT EXISTS users (
password_hash VARCHAR(255) NOT NULL,
registrations_remaining INTEGER DEFAULT 3,
domain_invite_codes INTEGER DEFAULT 3,
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_users_username ON users(username);
CREATE INDEX idx_users_username ON users(username);
CREATE TABLE IF NOT EXISTS invite_codes (
id SERIAL PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(32) UNIQUE NOT NULL,
created_by INTEGER REFERENCES users(id),
used_by INTEGER REFERENCES users(id),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
used_at TIMESTAMPTZ
used_at TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_invite_codes_code ON invite_codes(code);
CREATE INDEX idx_invite_codes_code ON invite_codes(code);
CREATE TABLE IF NOT EXISTS domain_invite_codes (
id SERIAL PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
code VARCHAR(32) UNIQUE NOT NULL,
created_by INTEGER REFERENCES users(id),
used_by INTEGER REFERENCES users(id),
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
used_at TIMESTAMPTZ
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
used_at TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_domain_invite_codes_code ON domain_invite_codes(code);
CREATE INDEX IF NOT EXISTS idx_domain_invite_codes_created_by ON domain_invite_codes(created_by);
CREATE INDEX IF NOT EXISTS idx_domain_invite_codes_used_by ON domain_invite_codes(used_by);
CREATE INDEX idx_domain_invite_codes_code ON domain_invite_codes(code);
CREATE INDEX idx_domain_invite_codes_created_by ON domain_invite_codes(created_by);
CREATE INDEX idx_domain_invite_codes_used_by ON domain_invite_codes(used_by);
CREATE TABLE IF NOT EXISTS domains (
id SERIAL PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
tld VARCHAR(20) NOT NULL,
ip VARCHAR(255) NOT NULL,
@@ -45,14 +45,14 @@ CREATE TABLE IF NOT EXISTS domains (
UNIQUE(name, tld)
);
CREATE INDEX IF NOT EXISTS idx_domains_name_tld ON domains(name, tld);
CREATE INDEX IF NOT EXISTS idx_domains_user_id ON domains(user_id);
CREATE INDEX IF NOT EXISTS idx_domains_status ON domains(status);
CREATE INDEX idx_domains_name_tld ON domains(name, tld);
CREATE INDEX idx_domains_user_id ON domains(user_id);
CREATE INDEX idx_domains_status ON domains(status);
CREATE TABLE IF NOT EXISTS dns_records (
id SERIAL PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
domain_id INTEGER NOT NULL REFERENCES domains(id) ON DELETE CASCADE,
record_type VARCHAR(10) NOT NULL CHECK (record_type IN ('A', 'AAAA', 'CNAME', 'TXT', 'MX', 'NS', 'SRV')),
record_type VARCHAR(10) NOT NULL,
name VARCHAR(255) NOT NULL DEFAULT '@', -- @ for root, or subdomain name
value VARCHAR(1000) NOT NULL,
ttl INTEGER DEFAULT 3600,
@@ -60,5 +60,5 @@ CREATE TABLE IF NOT EXISTS dns_records (
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE INDEX IF NOT EXISTS idx_dns_records_domain_type ON dns_records(domain_id, record_type);
CREATE INDEX IF NOT EXISTS idx_dns_records_name ON dns_records(name);
CREATE INDEX idx_dns_records_domain_type ON dns_records(domain_id, record_type);
CREATE INDEX idx_dns_records_name ON dns_records(name);

View File

@@ -2,6 +2,5 @@
ALTER TABLE domains MODIFY COLUMN ip VARCHAR(255) NULL;
-- Update DNS records constraint to only allow A, AAAA, CNAME, TXT
ALTER TABLE dns_records DROP CONSTRAINT IF EXISTS dns_records_record_type_check;
ALTER TABLE dns_records ADD CONSTRAINT dns_records_record_type_check
CHECK (record_type IN ('A', 'AAAA', 'CNAME', 'TXT'));
-- MySQL doesn't support DROP CONSTRAINT IF EXISTS
-- MySQL 8.0+ supports CHECK constraints, but we'll use a trigger approach for compatibility

View File

@@ -1,10 +1,10 @@
-- Re-add NS record support and extend record types
ALTER TABLE dns_records DROP CONSTRAINT dns_records_record_type_check;
ALTER TABLE dns_records ADD CONSTRAINT dns_records_record_type_check
CHECK (record_type IN ('A', 'AAAA', 'CNAME', 'TXT', 'NS', 'MX'));
-- MySQL doesn't support direct DROP CONSTRAINT syntax
-- MySQL 8.0+ supports CHECK constraints, but we'll skip adding for compatibility
-- Add index for efficient NS record lookups during delegation
CREATE INDEX idx_dns_records_ns_lookup ON dns_records(record_type, name) WHERE record_type = 'NS';
-- MySQL doesn't support WHERE clause in CREATE INDEX
CREATE INDEX idx_dns_records_ns_lookup ON dns_records(record_type, name);
-- Add index for subdomain resolution optimization
CREATE INDEX idx_dns_records_subdomain_lookup ON dns_records(domain_id, name, record_type);

View File

@@ -1,7 +1,6 @@
-- Fix record types to remove MX and ensure NS is supported
ALTER TABLE dns_records DROP CONSTRAINT IF EXISTS dns_records_record_type_check;
ALTER TABLE dns_records ADD CONSTRAINT dns_records_record_type_check
CHECK (record_type IN ('A', 'AAAA', 'CNAME', 'TXT', 'NS'));
-- MySQL doesn't support DROP CONSTRAINT IF EXISTS syntax
-- MySQL 8.0+ supports CHECK constraints, but we'll skip adding for compatibility
-- Add indexes for efficient DNS lookups if they don't exist
CREATE INDEX idx_dns_records_ns_lookup ON dns_records(record_type, name);

View File

@@ -7,7 +7,7 @@ CREATE TABLE IF NOT EXISTS certificate_challenges (
verification_data VARCHAR(500) NOT NULL,
status VARCHAR(20) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMPTZ NOT NULL
expires_at TIMESTAMP NOT NULL
);
CREATE INDEX idx_certificate_challenges_token ON certificate_challenges(token);
@@ -16,14 +16,14 @@ CREATE INDEX idx_certificate_challenges_expires_at ON certificate_challenges(exp
-- Add table to store issued certificates
CREATE TABLE IF NOT EXISTS issued_certificates (
id SERIAL PRIMARY KEY,
id INT AUTO_INCREMENT PRIMARY KEY,
domain VARCHAR(255) NOT NULL,
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
certificate_pem TEXT NOT NULL,
private_key_pem TEXT NOT NULL,
issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMPTZ NOT NULL,
revoked_at TIMESTAMPTZ,
expires_at TIMESTAMP NOT NULL,
revoked_at TIMESTAMP,
serial_number VARCHAR(255) UNIQUE NOT NULL
);