diff --git a/README.md b/README.md index 763ec31..d181b7b 100644 --- a/README.md +++ b/README.md @@ -134,7 +134,7 @@ src/main/java/com/mailengine/mailengine/ src/main/resources/ ├── application.yaml # All config (reads from environment variables) -├── db/migration/ # Flyway SQL migrations (V1 – V22) +├── db/migration/ # Flyway SQL migrations (V1 is the full schema) └── templates/ # CSV / XLSX import templates served at /template/csv and /template/xlsx ``` @@ -219,7 +219,7 @@ http://localhost:8080/swagger-ui.html → API docs ### 7. Log in with the seeded admin account -Migration `V21` seeds a default admin user: +Migration `V1` seeds a default admin user: ``` Email: admin@mailengine.internal @@ -303,37 +303,17 @@ Schema is managed entirely by **Flyway**. Never edit the database by hand — ad ``` src/main/resources/db/migration/ - V1__create_accounts.sql - V2__create_users.sql - V3__create_templates.sql - V4__create_recipient_lists.sql - V5__create_recipients.sql - V6__create_recipient_list_members.sql - V7__create_campaigns.sql - V8__create_campaign_recipient_lists.sql - V9__create_campaign_jobs.sql - V10__create_tracking_events.sql - V11__create_read_duration_events.sql - V12__create_engagement_scores.sql - V13__create_suppression_list.sql - V14__create_file_uploads.sql - V15__create_email_queue.sql - V16__fix_ip_address.sql - V17__add_tracking_tokens_to_email_queue.sql - V18__add_login_lockout.sql - V19__add_token_tables.sql - V20__add_message_id_to_email_queue.sql - V21__seed_admin.sql ← seeds the default admin account - V22__remove_multitenancy.sql ← dropped multi-tenant account_id columns - V23__fix_seed_admin_password.sql ← makes the seeded admin's password really ChangeMe123! + V1__initial_schema.sql ← the whole schema, plus the seeded admin account ``` +The original V1–V26 history was squashed into `V1` (no database had to be kept). Every change from now on is a new file. + ### Adding a migration Create the next file in sequence: ``` -src/main/resources/db/migration/V23__your_description.sql +src/main/resources/db/migration/V2__your_description.sql ``` Flyway applies it automatically on the next startup. **Never modify an already-applied migration** — Flyway checksums every file and will refuse to start if a previously applied file changes. diff --git a/src/main/java/com/mailengine/mailengine/entity/FileUpload.java b/src/main/java/com/mailengine/mailengine/entity/FileUpload.java index 10d6115..489d490 100644 --- a/src/main/java/com/mailengine/mailengine/entity/FileUpload.java +++ b/src/main/java/com/mailengine/mailengine/entity/FileUpload.java @@ -41,7 +41,7 @@ public class FileUpload { @Column(name = "skipped_rows") private Integer skippedRows; @Column(name = "duplicate_rows") private Integer duplicateRows; - // Plain json, not jsonb: jsonb reorders keys and the columns must stay in file order (V26) + // Plain json, not jsonb: jsonb reorders keys and the columns must stay in file order @JdbcType(PostgreSQLJsonPGObjectJsonType.class) @Column(name = "column_preview") private Map columnPreview; diff --git a/src/main/resources/db/migration/V10__create_tracking_events.sql b/src/main/resources/db/migration/V10__create_tracking_events.sql deleted file mode 100644 index e6f7ef1..0000000 --- a/src/main/resources/db/migration/V10__create_tracking_events.sql +++ /dev/null @@ -1,20 +0,0 @@ -CREATE TABLE tracking_events ( - id BIGSERIAL PRIMARY KEY, - campaign_id BIGINT REFERENCES campaigns(id), - recipient_id BIGINT REFERENCES recipients(id), - event_type VARCHAR(30) NOT NULL, - link_url TEXT, - link_position VARCHAR(50), - ip_address INET, - geo_country VARCHAR(5), - geo_city VARCHAR(100), - device_type VARCHAR(20), - email_client VARCHAR(50), - os VARCHAR(50), - user_agent TEXT, - is_apple_proxy BOOLEAN DEFAULT FALSE, - created_at TIMESTAMP DEFAULT NOW() -); -CREATE INDEX idx_tracking_campaign ON tracking_events(campaign_id); -CREATE INDEX idx_tracking_recipient ON tracking_events(recipient_id); -CREATE INDEX idx_tracking_type ON tracking_events(event_type); \ No newline at end of file diff --git a/src/main/resources/db/migration/V11__create_read_duration_events.sql b/src/main/resources/db/migration/V11__create_read_duration_events.sql deleted file mode 100644 index cf88c42..0000000 --- a/src/main/resources/db/migration/V11__create_read_duration_events.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE read_duration_events ( - id BIGSERIAL PRIMARY KEY, - campaign_id BIGINT REFERENCES campaigns(id), - recipient_id BIGINT REFERENCES recipients(id), - checkpoint INTEGER NOT NULL, - source VARCHAR(20), - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V12__create_engagement_scores.sql b/src/main/resources/db/migration/V12__create_engagement_scores.sql deleted file mode 100644 index 0b8d5ef..0000000 --- a/src/main/resources/db/migration/V12__create_engagement_scores.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE engagement_scores ( - recipient_id BIGINT PRIMARY KEY REFERENCES recipients(id), - account_id BIGINT REFERENCES accounts(id), - score DECIMAL(5,2) DEFAULT 0, - category VARCHAR(20), - last_open_at TIMESTAMP, - last_click_at TIMESTAMP, - campaigns_sent INTEGER DEFAULT 0, - campaigns_opened INTEGER DEFAULT 0, - campaigns_clicked INTEGER DEFAULT 0, - updated_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V13__create_suppression_list.sql b/src/main/resources/db/migration/V13__create_suppression_list.sql deleted file mode 100644 index fa5aa1a..0000000 --- a/src/main/resources/db/migration/V13__create_suppression_list.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE suppression_list ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - email VARCHAR(255) NOT NULL, - reason VARCHAR(20), - created_at TIMESTAMP DEFAULT NOW(), - UNIQUE(account_id, email) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V14__create_file_uploads.sql b/src/main/resources/db/migration/V14__create_file_uploads.sql deleted file mode 100644 index cf15547..0000000 --- a/src/main/resources/db/migration/V14__create_file_uploads.sql +++ /dev/null @@ -1,14 +0,0 @@ -CREATE TABLE file_uploads ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - recipient_list_id BIGINT REFERENCES recipient_lists(id), - file_name VARCHAR(255), - status VARCHAR(20) DEFAULT 'processing', - total_rows INTEGER, - imported_rows INTEGER, - skipped_rows INTEGER, - duplicate_rows INTEGER, - column_preview JSONB, - column_mapping JSONB, - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V15__create_email_queue.sql b/src/main/resources/db/migration/V15__create_email_queue.sql deleted file mode 100644 index d75a3d2..0000000 --- a/src/main/resources/db/migration/V15__create_email_queue.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE TABLE email_queue ( - id BIGSERIAL PRIMARY KEY, - campaign_id BIGINT REFERENCES campaigns(id), - recipient_id BIGINT REFERENCES recipients(id), - to_email VARCHAR(255) NOT NULL, - html_content TEXT NOT NULL, - subject_line VARCHAR(500) NOT NULL, - from_name VARCHAR(255) NOT NULL, - from_email VARCHAR(255) NOT NULL, - status VARCHAR(20) DEFAULT 'pending' - CHECK (status IN ('pending','sending','sent','failed')), - attempts INTEGER DEFAULT 0, - error_message TEXT, - locked_until TIMESTAMP, - created_at TIMESTAMP DEFAULT NOW(), - sent_at TIMESTAMP -); -CREATE INDEX idx_email_queue_status ON email_queue(status, locked_until); \ No newline at end of file diff --git a/src/main/resources/db/migration/V16__fix_ip_address.sql b/src/main/resources/db/migration/V16__fix_ip_address.sql deleted file mode 100644 index ef5e2f1..0000000 --- a/src/main/resources/db/migration/V16__fix_ip_address.sql +++ /dev/null @@ -1,2 +0,0 @@ -ALTER TABLE tracking_events - ALTER COLUMN ip_address TYPE VARCHAR(45); \ No newline at end of file diff --git a/src/main/resources/db/migration/V17__add_tracking_tokens_to_email_queue.sql b/src/main/resources/db/migration/V17__add_tracking_tokens_to_email_queue.sql deleted file mode 100644 index 7717c75..0000000 --- a/src/main/resources/db/migration/V17__add_tracking_tokens_to_email_queue.sql +++ /dev/null @@ -1,8 +0,0 @@ --- Adds unique tracking tokens to the email_queue table. --- These tokens are generated when emails are queued and embedded --- in the HTML so we can identify open, click, and unsubscribe events. - -ALTER TABLE email_queue - ADD COLUMN pixel_token VARCHAR(255) UNIQUE, - ADD COLUMN click_token VARCHAR(255) UNIQUE, - ADD COLUMN unsub_token VARCHAR(255) UNIQUE; \ No newline at end of file diff --git a/src/main/resources/db/migration/V18__add_login_lockout.sql b/src/main/resources/db/migration/V18__add_login_lockout.sql deleted file mode 100644 index a6e41b3..0000000 --- a/src/main/resources/db/migration/V18__add_login_lockout.sql +++ /dev/null @@ -1,3 +0,0 @@ -ALTER TABLE users - ADD COLUMN IF NOT EXISTS failed_login_attempts INTEGER NOT NULL DEFAULT 0, - ADD COLUMN IF NOT EXISTS locked_until TIMESTAMP; diff --git a/src/main/resources/db/migration/V19__add_token_tables.sql b/src/main/resources/db/migration/V19__add_token_tables.sql deleted file mode 100644 index b6bd89b..0000000 --- a/src/main/resources/db/migration/V19__add_token_tables.sql +++ /dev/null @@ -1,18 +0,0 @@ -CREATE TABLE IF NOT EXISTS email_verification_tokens ( - id BIGSERIAL PRIMARY KEY, - token VARCHAR(255) NOT NULL UNIQUE, - user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, - expires_at TIMESTAMP NOT NULL, - used BOOLEAN NOT NULL DEFAULT FALSE -); - -CREATE TABLE IF NOT EXISTS password_reset_tokens ( - id BIGSERIAL PRIMARY KEY, - token VARCHAR(255) NOT NULL UNIQUE, - user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE, - expires_at TIMESTAMP NOT NULL, - used BOOLEAN NOT NULL DEFAULT FALSE -); - -CREATE INDEX IF NOT EXISTS idx_evt_token ON email_verification_tokens(token); -CREATE INDEX IF NOT EXISTS idx_prt_token ON password_reset_tokens(token); diff --git a/src/main/resources/db/migration/V1__create_accounts.sql b/src/main/resources/db/migration/V1__create_accounts.sql deleted file mode 100644 index 73a48b0..0000000 --- a/src/main/resources/db/migration/V1__create_accounts.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE accounts ( - id BIGSERIAL PRIMARY KEY, - company_name VARCHAR(255) NOT NULL, - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V1__initial_schema.sql b/src/main/resources/db/migration/V1__initial_schema.sql new file mode 100644 index 0000000..659b8a4 --- /dev/null +++ b/src/main/resources/db/migration/V1__initial_schema.sql @@ -0,0 +1,239 @@ +-- MailEngine schema. Squashed from the original V1-V26 migrations: a new database gets the same schema in one +-- step. Times are TIMESTAMP (without time zone) holding UTC, which is how Hibernate writes Instant values. + +-- ── Users and one-time tokens ───────────────────────────────────────────────── + +CREATE TABLE users ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + email VARCHAR(255) NOT NULL UNIQUE, + password_hash VARCHAR(255) NOT NULL, + role VARCHAR(20) NOT NULL CHECK (role IN ('admin', 'editor', 'viewer')), + email_verified BOOLEAN DEFAULT false, + must_change_pwd BOOLEAN DEFAULT false, + -- users someone invited keep their accounts when that someone is deleted + invited_by BIGINT REFERENCES users (id) ON DELETE SET NULL, + last_login TIMESTAMP, + created_at TIMESTAMP DEFAULT NOW(), + failed_login_attempts INTEGER NOT NULL DEFAULT 0, + locked_until TIMESTAMP +); + +-- Tokens are stored as the SHA-256 of what was emailed (see TokenHashing) +CREATE TABLE email_verification_tokens ( + id BIGSERIAL PRIMARY KEY, + token VARCHAR(255) NOT NULL UNIQUE, + user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE, + expires_at TIMESTAMP NOT NULL, + used BOOLEAN NOT NULL DEFAULT false +); + +CREATE TABLE password_reset_tokens ( + id BIGSERIAL PRIMARY KEY, + token VARCHAR(255) NOT NULL UNIQUE, + user_id BIGINT NOT NULL REFERENCES users (id) ON DELETE CASCADE, + expires_at TIMESTAMP NOT NULL, + used BOOLEAN NOT NULL DEFAULT false +); + +-- ── Templates ───────────────────────────────────────────────────────────────── + +CREATE TABLE templates ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + category VARCHAR(100), + html_content TEXT NOT NULL, + json_design JSONB, + thumbnail_url VARCHAR(500), + -- content outlives the user who made it + created_by BIGINT REFERENCES users (id) ON DELETE SET NULL, + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +-- ── Recipients and lists ────────────────────────────────────────────────────── + +CREATE TABLE recipient_lists ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + description TEXT, + recipient_count INTEGER DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW() +); + +CREATE TABLE recipients ( + id BIGSERIAL PRIMARY KEY, + email VARCHAR(255) NOT NULL UNIQUE, + first_name VARCHAR(255), + last_name VARCHAR(255), + company VARCHAR(255), + custom_fields JSONB DEFAULT '{}', + status VARCHAR(20) DEFAULT 'active' CHECK (status IN ('active', 'unsubscribed', 'bounced', 'complained')), + created_at TIMESTAMP DEFAULT NOW() +); + +CREATE TABLE recipient_list_members ( + recipient_list_id BIGINT REFERENCES recipient_lists (id) ON DELETE CASCADE, + recipient_id BIGINT REFERENCES recipients (id) ON DELETE CASCADE, + PRIMARY KEY (recipient_list_id, recipient_id) +); + +CREATE TABLE suppression_list ( + id BIGSERIAL PRIMARY KEY, + email VARCHAR(255) NOT NULL UNIQUE, + reason VARCHAR(20), + created_at TIMESTAMP DEFAULT NOW() +); + +-- ── File imports ────────────────────────────────────────────────────────────── + +CREATE TABLE file_uploads ( + id BIGSERIAL PRIMARY KEY, + -- a list's upload history goes with the list + recipient_list_id BIGINT REFERENCES recipient_lists (id) ON DELETE CASCADE, + file_name VARCHAR(255), + status VARCHAR(20) DEFAULT 'processing', + total_rows INTEGER, + imported_rows INTEGER, + skipped_rows INTEGER, + duplicate_rows INTEGER, + -- JSON, not JSONB: JSONB reorders keys, and the columns must stay in the file's order + column_preview JSON, + column_mapping JSONB, + created_at TIMESTAMP DEFAULT NOW() +); + +-- The file of an upload whose columns need mapping (POST /api/uploads/{id}/map): deleted once imported, and +-- after a day if never mapped +CREATE TABLE pending_upload_files ( + upload_id BIGINT PRIMARY KEY REFERENCES file_uploads (id) ON DELETE CASCADE, + file_name VARCHAR(255), + content BYTEA NOT NULL, + created_at TIMESTAMP NOT NULL DEFAULT NOW() +); + +-- ── Campaigns ───────────────────────────────────────────────────────────────── + +CREATE TABLE campaigns ( + id BIGSERIAL PRIMARY KEY, + name VARCHAR(255) NOT NULL, + subject_line VARCHAR(500) NOT NULL, + from_name VARCHAR(255) NOT NULL, + from_email VARCHAR(255) NOT NULL, + -- a template in use can't be deleted (TemplateService checks first and explains) + template_id BIGINT REFERENCES templates (id), + status VARCHAR(20) DEFAULT 'draft' + CHECK (status IN ('draft', 'scheduled', 'sending', 'sent', 'cancelled', 'failed')), + scheduled_at TIMESTAMP, + timezone VARCHAR(50), + sent_at TIMESTAMP, + created_by BIGINT REFERENCES users (id) ON DELETE SET NULL, + total_recipients INTEGER DEFAULT 0, + total_sent INTEGER DEFAULT 0, + total_delivered INTEGER DEFAULT 0, + total_bounced INTEGER DEFAULT 0, + total_opened INTEGER DEFAULT 0, + total_clicked INTEGER DEFAULT 0, + total_complained INTEGER DEFAULT 0, + total_unsubscribed INTEGER DEFAULT 0, + created_at TIMESTAMP DEFAULT NOW() +); + +CREATE TABLE campaign_recipient_lists ( + campaign_id BIGINT REFERENCES campaigns (id) ON DELETE CASCADE, + -- a list in use can't be deleted (RecipientListService checks first and explains) + recipient_list_id BIGINT REFERENCES recipient_lists (id), + PRIMARY KEY (campaign_id, recipient_list_id) +); + +CREATE TABLE campaign_jobs ( + id BIGSERIAL PRIMARY KEY, + campaign_id BIGINT UNIQUE REFERENCES campaigns (id), + status VARCHAR(20) DEFAULT 'queued' + CHECK (status IN ('queued', 'processing', 'paused', 'completed', 'failed', 'cancelled')), + total_emails INTEGER DEFAULT 0, + queued_count INTEGER DEFAULT 0, + sent_count INTEGER DEFAULT 0, + failed_count INTEGER DEFAULT 0, + started_at TIMESTAMP, + completed_at TIMESTAMP, + error_message TEXT, + created_at TIMESTAMP DEFAULT NOW(), + updated_at TIMESTAMP DEFAULT NOW() +); + +-- One row per email to send; workers claim rows by status and locked_until +CREATE TABLE email_queue ( + id BIGSERIAL PRIMARY KEY, + campaign_id BIGINT REFERENCES campaigns (id), + recipient_id BIGINT REFERENCES recipients (id), + to_email VARCHAR(255) NOT NULL, + html_content TEXT NOT NULL, + subject_line VARCHAR(500) NOT NULL, + from_name VARCHAR(255) NOT NULL, + from_email VARCHAR(255) NOT NULL, + status VARCHAR(20) DEFAULT 'pending' CHECK (status IN ('pending', 'sending', 'sent', 'failed')), + attempts INTEGER DEFAULT 0, + error_message TEXT, + locked_until TIMESTAMP, + created_at TIMESTAMP DEFAULT NOW(), + sent_at TIMESTAMP, + pixel_token VARCHAR(255) UNIQUE, + click_token VARCHAR(255) UNIQUE, + unsub_token VARCHAR(255) UNIQUE, + message_id VARCHAR(255) +); + +CREATE INDEX idx_email_queue_status ON email_queue (status, locked_until); + +-- ── Tracking and engagement ─────────────────────────────────────────────────── + +CREATE TABLE tracking_events ( + id BIGSERIAL PRIMARY KEY, + campaign_id BIGINT REFERENCES campaigns (id), + recipient_id BIGINT REFERENCES recipients (id), + event_type VARCHAR(30) NOT NULL, + link_url TEXT, + link_position VARCHAR(50), + ip_address VARCHAR(45), + geo_country VARCHAR(5), + geo_city VARCHAR(100), + device_type VARCHAR(20), + email_client VARCHAR(50), + os VARCHAR(50), + user_agent TEXT, + is_apple_proxy BOOLEAN DEFAULT false, + created_at TIMESTAMP DEFAULT NOW() +); + +CREATE INDEX idx_tracking_campaign ON tracking_events (campaign_id); +CREATE INDEX idx_tracking_recipient ON tracking_events (recipient_id); +CREATE INDEX idx_tracking_type ON tracking_events (event_type); + +CREATE TABLE read_duration_events ( + id BIGSERIAL PRIMARY KEY, + campaign_id BIGINT REFERENCES campaigns (id), + recipient_id BIGINT REFERENCES recipients (id), + checkpoint INTEGER NOT NULL, + source VARCHAR(20), + created_at TIMESTAMP DEFAULT NOW() +); + +CREATE TABLE engagement_scores ( + recipient_id BIGINT PRIMARY KEY REFERENCES recipients (id), + score DECIMAL(5, 2) DEFAULT 0, + category VARCHAR(20), + last_open_at TIMESTAMP, + last_click_at TIMESTAMP, + campaigns_sent INTEGER DEFAULT 0, + campaigns_opened INTEGER DEFAULT 0, + campaigns_clicked INTEGER DEFAULT 0, + updated_at TIMESTAMP DEFAULT NOW() +); + +-- ── Seed ────────────────────────────────────────────────────────────────────── + +-- The first admin. Password: ChangeMe123! (BCrypt); it must be changed at first login. +INSERT INTO users (name, email, password_hash, role, email_verified, must_change_pwd) +VALUES ('Admin', 'admin@mailengine.internal', '$2a$10$5Y1P3x81Oypux6VObKy1bOKTLAfBAgnf69o8lHdzLQhXTSnUAeDFW', + 'admin', true, true); diff --git a/src/main/resources/db/migration/V20__add_message_id_to_email_queue.sql b/src/main/resources/db/migration/V20__add_message_id_to_email_queue.sql deleted file mode 100644 index 271848d..0000000 --- a/src/main/resources/db/migration/V20__add_message_id_to_email_queue.sql +++ /dev/null @@ -1 +0,0 @@ -ALTER TABLE email_queue ADD COLUMN message_id VARCHAR(255); \ No newline at end of file diff --git a/src/main/resources/db/migration/V21__seed_admin.sql b/src/main/resources/db/migration/V21__seed_admin.sql deleted file mode 100644 index 1c3263a..0000000 --- a/src/main/resources/db/migration/V21__seed_admin.sql +++ /dev/null @@ -1,18 +0,0 @@ --- Seed the single internal admin account -INSERT INTO accounts (company_name) -VALUES ('MailEngine Internal') -ON CONFLICT DO NOTHING; - --- Seed the admin user (password = ChangeMe123!) --- BCrypt hash of 'ChangeMe123!' -INSERT INTO users (name, email, password_hash, role, email_verified, must_change_pwd, account_id) -VALUES ( - 'Admin', - 'admin@mailengine.internal', - '$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2uheWG/igi.', - 'admin', - true, - true, - (SELECT id FROM accounts WHERE company_name = 'MailEngine Internal' LIMIT 1) - ) -ON CONFLICT (email) DO NOTHING; \ No newline at end of file diff --git a/src/main/resources/db/migration/V22__remove_multitenancy.sql b/src/main/resources/db/migration/V22__remove_multitenancy.sql deleted file mode 100644 index efb302f..0000000 --- a/src/main/resources/db/migration/V22__remove_multitenancy.sql +++ /dev/null @@ -1,21 +0,0 @@ --- Drop account_id from all tables -ALTER TABLE templates DROP COLUMN IF EXISTS account_id; -ALTER TABLE recipient_lists DROP COLUMN IF EXISTS account_id; -ALTER TABLE recipients DROP COLUMN IF EXISTS account_id; -ALTER TABLE suppression_list DROP COLUMN IF EXISTS account_id; -ALTER TABLE file_uploads DROP COLUMN IF EXISTS account_id; -ALTER TABLE campaigns DROP COLUMN IF EXISTS account_id; -ALTER TABLE engagement_scores DROP COLUMN IF EXISTS account_id; - --- Fix unique constraints scoped to account -ALTER TABLE recipients DROP CONSTRAINT IF EXISTS recipients_account_id_email_key; -ALTER TABLE recipients ADD CONSTRAINT recipients_email_key UNIQUE (email); - -ALTER TABLE suppression_list DROP CONSTRAINT IF EXISTS suppression_list_account_id_email_key; -ALTER TABLE suppression_list ADD CONSTRAINT suppression_list_email_key UNIQUE (email); - --- Drop account_id from users -ALTER TABLE users DROP COLUMN IF EXISTS account_id; - --- Drop accounts table -DROP TABLE IF EXISTS accounts; \ No newline at end of file diff --git a/src/main/resources/db/migration/V23__fix_seed_admin_password.sql b/src/main/resources/db/migration/V23__fix_seed_admin_password.sql deleted file mode 100644 index 29ef3dd..0000000 --- a/src/main/resources/db/migration/V23__fix_seed_admin_password.sql +++ /dev/null @@ -1,8 +0,0 @@ --- V21 seeded the admin with a BCrypt hash that does not match the documented password --- 'ChangeMe123!', so a fresh install had no way to log in. --- Only the untouched seed hash is replaced: an admin whose password was already set some other --- way (e.g. by hand in the database) keeps it. -UPDATE users -SET password_hash = '$2a$10$5Y1P3x81Oypux6VObKy1bOKTLAfBAgnf69o8lHdzLQhXTSnUAeDFW' -WHERE email = 'admin@mailengine.internal' - AND password_hash = '$2a$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2uheWG/igi.'; diff --git a/src/main/resources/db/migration/V24__keep_content_when_user_deleted.sql b/src/main/resources/db/migration/V24__keep_content_when_user_deleted.sql deleted file mode 100644 index 3a7ca9c..0000000 --- a/src/main/resources/db/migration/V24__keep_content_when_user_deleted.sql +++ /dev/null @@ -1,14 +0,0 @@ --- Deleting a user failed (foreign key violation, shown as a 409) as soon as they had created a template or a --- campaign. Their templates and campaigns now stay and simply lose their creator. -ALTER TABLE templates - DROP CONSTRAINT templates_created_by_fkey, - ADD CONSTRAINT templates_created_by_fkey FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL; - -ALTER TABLE campaigns - DROP CONSTRAINT campaigns_created_by_fkey, - ADD CONSTRAINT campaigns_created_by_fkey FOREIGN KEY (created_by) REFERENCES users (id) ON DELETE SET NULL; - --- Users this user invited keep their accounts too (UserService also clears it, this makes it hold everywhere) -ALTER TABLE users - DROP CONSTRAINT users_invited_by_fkey, - ADD CONSTRAINT users_invited_by_fkey FOREIGN KEY (invited_by) REFERENCES users (id) ON DELETE SET NULL; diff --git a/src/main/resources/db/migration/V25__hash_tokens_and_pending_uploads.sql b/src/main/resources/db/migration/V25__hash_tokens_and_pending_uploads.sql deleted file mode 100644 index c9ba092..0000000 --- a/src/main/resources/db/migration/V25__hash_tokens_and_pending_uploads.sql +++ /dev/null @@ -1,14 +0,0 @@ --- One-time tokens are now stored as their SHA-256 (see TokenHashing). Tokens issued before this change were --- stored as sent and could be read from the database; they are removed (a reset link lives one hour, so --- anyone affected just asks for a new one). -DELETE FROM password_reset_tokens; -DELETE FROM email_verification_tokens; - --- An upload whose headers don't match email/first_name/last_name keeps its file here until the user maps --- the columns (POST /api/uploads/{id}/map). Removed once imported, and after a day if never mapped. -CREATE TABLE pending_upload_files ( - upload_id BIGINT PRIMARY KEY REFERENCES file_uploads (id) ON DELETE CASCADE, - file_name VARCHAR(255), - content BYTEA NOT NULL, - created_at TIMESTAMP NOT NULL DEFAULT NOW() -); diff --git a/src/main/resources/db/migration/V26__ordered_upload_columns_and_list_delete.sql b/src/main/resources/db/migration/V26__ordered_upload_columns_and_list_delete.sql deleted file mode 100644 index e2d4e82..0000000 --- a/src/main/resources/db/migration/V26__ordered_upload_columns_and_list_delete.sql +++ /dev/null @@ -1,11 +0,0 @@ --- JSONB reorders object keys, so an upload's columns came back out of file order (the mapping screen showed --- them shuffled). JSON keeps the text as written, so the order is kept. -ALTER TABLE file_uploads - ALTER COLUMN column_preview TYPE JSON USING column_preview::json; - --- Deleting a recipient list failed (409) once anything had been uploaded to it: its upload history now goes --- with it (and any file still waiting for a column mapping, through pending_upload_files' own cascade). -ALTER TABLE file_uploads - DROP CONSTRAINT file_uploads_recipient_list_id_fkey, - ADD CONSTRAINT file_uploads_recipient_list_id_fkey - FOREIGN KEY (recipient_list_id) REFERENCES recipient_lists (id) ON DELETE CASCADE; diff --git a/src/main/resources/db/migration/V2__create_users.sql b/src/main/resources/db/migration/V2__create_users.sql deleted file mode 100644 index f00e691..0000000 --- a/src/main/resources/db/migration/V2__create_users.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE users ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - name VARCHAR(255) NOT NULL, - email VARCHAR(255) UNIQUE NOT NULL, - password_hash VARCHAR(255) NOT NULL, - role VARCHAR(20) NOT NULL CHECK (role IN ('admin','editor','viewer')), - email_verified BOOLEAN DEFAULT FALSE, - must_change_pwd BOOLEAN DEFAULT FALSE, - invited_by BIGINT REFERENCES users(id), - last_login TIMESTAMP, - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V3__create_templates.sql b/src/main/resources/db/migration/V3__create_templates.sql deleted file mode 100644 index 9d28d5a..0000000 --- a/src/main/resources/db/migration/V3__create_templates.sql +++ /dev/null @@ -1,12 +0,0 @@ -CREATE TABLE templates ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - name VARCHAR(255) NOT NULL, - category VARCHAR(100), - html_content TEXT NOT NULL, - json_design JSONB, - thumbnail_url VARCHAR(500), - created_by BIGINT REFERENCES users(id), - created_at TIMESTAMP DEFAULT NOW(), - updated_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V4__create_recipient_lists.sql b/src/main/resources/db/migration/V4__create_recipient_lists.sql deleted file mode 100644 index 0ac6172..0000000 --- a/src/main/resources/db/migration/V4__create_recipient_lists.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE TABLE recipient_lists ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - name VARCHAR(255) NOT NULL, - description TEXT, - recipient_count INTEGER DEFAULT 0, - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V5__create_recipients.sql b/src/main/resources/db/migration/V5__create_recipients.sql deleted file mode 100644 index b9f738d..0000000 --- a/src/main/resources/db/migration/V5__create_recipients.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE TABLE recipients ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - email VARCHAR(255) NOT NULL, - first_name VARCHAR(255), - last_name VARCHAR(255), - company VARCHAR(255), - custom_fields JSONB DEFAULT '{}', - status VARCHAR(20) DEFAULT 'active' - CHECK (status IN ('active','unsubscribed','bounced','complained')), - created_at TIMESTAMP DEFAULT NOW(), - UNIQUE(account_id, email) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V6__create_recipient_list_members.sql b/src/main/resources/db/migration/V6__create_recipient_list_members.sql deleted file mode 100644 index e9aa243..0000000 --- a/src/main/resources/db/migration/V6__create_recipient_list_members.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE recipient_list_members ( - recipient_list_id BIGINT REFERENCES recipient_lists(id) ON DELETE CASCADE, - recipient_id BIGINT REFERENCES recipients(id) ON DELETE CASCADE, - PRIMARY KEY (recipient_list_id, recipient_id) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V7__create_campaigns.sql b/src/main/resources/db/migration/V7__create_campaigns.sql deleted file mode 100644 index af93044..0000000 --- a/src/main/resources/db/migration/V7__create_campaigns.sql +++ /dev/null @@ -1,25 +0,0 @@ -CREATE TABLE campaigns ( - id BIGSERIAL PRIMARY KEY, - account_id BIGINT REFERENCES accounts(id), - name VARCHAR(255) NOT NULL, - subject_line VARCHAR(500) NOT NULL, - from_name VARCHAR(255) NOT NULL, - from_email VARCHAR(255) NOT NULL, - template_id BIGINT REFERENCES templates(id), - status VARCHAR(20) DEFAULT 'draft' - CHECK (status IN - ('draft','scheduled','sending','sent','cancelled','failed')), - scheduled_at TIMESTAMP, - timezone VARCHAR(50), - sent_at TIMESTAMP, - created_by BIGINT REFERENCES users(id), - total_recipients INTEGER DEFAULT 0, - total_sent INTEGER DEFAULT 0, - total_delivered INTEGER DEFAULT 0, - total_bounced INTEGER DEFAULT 0, - total_opened INTEGER DEFAULT 0, - total_clicked INTEGER DEFAULT 0, - total_complained INTEGER DEFAULT 0, - total_unsubscribed INTEGER DEFAULT 0, - created_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V8__create_campaign_recipient_lists.sql b/src/main/resources/db/migration/V8__create_campaign_recipient_lists.sql deleted file mode 100644 index f0fc607..0000000 --- a/src/main/resources/db/migration/V8__create_campaign_recipient_lists.sql +++ /dev/null @@ -1,5 +0,0 @@ -CREATE TABLE campaign_recipient_lists ( - campaign_id BIGINT REFERENCES campaigns(id) ON DELETE CASCADE, - recipient_list_id BIGINT REFERENCES recipient_lists(id), - PRIMARY KEY (campaign_id, recipient_list_id) -); \ No newline at end of file diff --git a/src/main/resources/db/migration/V9__create_campaign_jobs.sql b/src/main/resources/db/migration/V9__create_campaign_jobs.sql deleted file mode 100644 index 78679bd..0000000 --- a/src/main/resources/db/migration/V9__create_campaign_jobs.sql +++ /dev/null @@ -1,16 +0,0 @@ -CREATE TABLE campaign_jobs ( - id BIGSERIAL PRIMARY KEY, - campaign_id BIGINT REFERENCES campaigns(id) UNIQUE, - status VARCHAR(20) DEFAULT 'queued' - CHECK (status IN - ('queued','processing','paused','completed','failed','cancelled')), - total_emails INTEGER DEFAULT 0, - queued_count INTEGER DEFAULT 0, - sent_count INTEGER DEFAULT 0, - failed_count INTEGER DEFAULT 0, - started_at TIMESTAMP, - completed_at TIMESTAMP, - error_message TEXT, - created_at TIMESTAMP DEFAULT NOW(), - updated_at TIMESTAMP DEFAULT NOW() -); \ No newline at end of file diff --git a/src/test/java/com/mailengine/mailengine/integration/PostgresIntegrationSupport.java b/src/test/java/com/mailengine/mailengine/integration/PostgresIntegrationSupport.java index 35a625a..9c3d7b1 100644 --- a/src/test/java/com/mailengine/mailengine/integration/PostgresIntegrationSupport.java +++ b/src/test/java/com/mailengine/mailengine/integration/PostgresIntegrationSupport.java @@ -30,7 +30,7 @@ public abstract class PostgresIntegrationSupport { Runtime.getRuntime().addShutdownHook(new Thread(POSTGRES::stop, "stop-postgres")); } - /** The admin seeded by V21 has id 1; requests made "as" this user pass every role check. */ + /** The admin seeded by V1 has id 1; requests made "as" this user pass every role check. */ protected static final UsernamePasswordAuthenticationToken ADMIN; static {