diff --git a/dns/migrations/001_initial.sql b/dns/migrations/001_initial.sql index aec7346..cc6634c 100644 --- a/dns/migrations/001_initial.sql +++ b/dns/migrations/001_initial.sql @@ -12,10 +12,12 @@ CREATE INDEX idx_users_username ON users(username); CREATE TABLE IF NOT EXISTS invite_codes ( 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_by INTEGER, + used_by INTEGER, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - used_at TIMESTAMP + used_at TIMESTAMP, + FOREIGN KEY (created_by) REFERENCES users(id), + FOREIGN KEY (used_by) REFERENCES users(id) ); CREATE INDEX idx_invite_codes_code ON invite_codes(code); @@ -23,10 +25,12 @@ CREATE INDEX idx_invite_codes_code ON invite_codes(code); CREATE TABLE IF NOT EXISTS domain_invite_codes ( 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_by INTEGER, + used_by INTEGER, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - used_at TIMESTAMP + used_at TIMESTAMP, + FOREIGN KEY (created_by) REFERENCES users(id), + FOREIGN KEY (used_by) REFERENCES users(id) ); CREATE INDEX idx_domain_invite_codes_code ON domain_invite_codes(code); @@ -38,11 +42,12 @@ CREATE TABLE IF NOT EXISTS domains ( name VARCHAR(100) NOT NULL, tld VARCHAR(20) NOT NULL, ip VARCHAR(255) NOT NULL, - user_id INTEGER REFERENCES users(id), + user_id INTEGER, status VARCHAR(20) DEFAULT 'pending', denial_reason TEXT, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - UNIQUE(name, tld) + UNIQUE(name, tld), + FOREIGN KEY (user_id) REFERENCES users(id) ); CREATE INDEX idx_domains_name_tld ON domains(name, tld); @@ -51,13 +56,14 @@ CREATE INDEX idx_domains_status ON domains(status); CREATE TABLE IF NOT EXISTS dns_records ( id INT AUTO_INCREMENT PRIMARY KEY, - domain_id INTEGER NOT NULL REFERENCES domains(id) ON DELETE CASCADE, + domain_id INTEGER NOT NULL, 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, priority INTEGER, -- For MX records - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE ); CREATE INDEX idx_dns_records_domain_type ON dns_records(domain_id, record_type); diff --git a/dns/migrations/005_add_certificate_challenges.sql b/dns/migrations/005_add_certificate_challenges.sql index 8cd48d9..abe0104 100644 --- a/dns/migrations/005_add_certificate_challenges.sql +++ b/dns/migrations/005_add_certificate_challenges.sql @@ -18,13 +18,14 @@ CREATE INDEX idx_certificate_challenges_expires_at ON certificate_challenges(exp CREATE TABLE IF NOT EXISTS issued_certificates ( id INT AUTO_INCREMENT PRIMARY KEY, domain VARCHAR(255) NOT NULL, - user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + user_id INTEGER NOT NULL, certificate_pem TEXT NOT NULL, private_key_pem TEXT NOT NULL, issued_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, expires_at TIMESTAMP NOT NULL, revoked_at TIMESTAMP, - serial_number VARCHAR(255) UNIQUE NOT NULL + serial_number VARCHAR(255) UNIQUE NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE ); CREATE INDEX idx_issued_certificates_domain ON issued_certificates(domain); diff --git a/dns/migrations/007_cleanup_invalid_records.sql b/dns/migrations/007_cleanup_invalid_records.sql index f0ff418..d96d29e 100644 --- a/dns/migrations/007_cleanup_invalid_records.sql +++ b/dns/migrations/007_cleanup_invalid_records.sql @@ -2,7 +2,8 @@ DELETE FROM dns_records WHERE record_type NOT IN ('A', 'AAAA', 'CNAME', 'TXT'); -- Now apply the constraint -ALTER TABLE dns_records DROP CONSTRAINT dns_records_record_type_check; +-- MySQL doesn't support DROP CONSTRAINT syntax for CHECK constraints +-- ALTER TABLE dns_records DROP CONSTRAINT dns_records_record_type_check; -- MySQL doesn't support table-level CHECK constraints, using trigger instead DELIMITER // diff --git a/dns/migrations/010_add_search_crawl_status.sql b/dns/migrations/010_add_search_crawl_status.sql index edc1e99..2cd4181 100644 --- a/dns/migrations/010_add_search_crawl_status.sql +++ b/dns/migrations/010_add_search_crawl_status.sql @@ -1,12 +1,13 @@ -- Search engine domain crawl status tracking CREATE TABLE IF NOT EXISTS domain_crawl_status ( - domain_id INT PRIMARY KEY, FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE, + domain_id INT PRIMARY KEY, last_crawled_at TIMESTAMP, next_crawl_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, crawl_status VARCHAR(20) DEFAULT 'pending', error_message TEXT, pages_found INTEGER DEFAULT 0, - updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + FOREIGN KEY (domain_id) REFERENCES domains(id) ON DELETE CASCADE ); CREATE INDEX idx_domain_crawl_status_next_crawl ON domain_crawl_status(next_crawl_at);