import { MigrateUpArgs, MigrateDownArgs, sql } from '@payloadcms/db-sqlite' export async function up({ db, payload, req }: MigrateUpArgs): Promise { await db.run(sql`CREATE TABLE \`projects\` ( \`id\` integer PRIMARY KEY NOT NULL, \`slug\` text NOT NULL, \`cover_id\` integer, \`order\` numeric DEFAULT 100, \`updated_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL, \`created_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL, FOREIGN KEY (\`cover_id\`) REFERENCES \`media\`(\`id\`) ON UPDATE no action ON DELETE set null ); `) await db.run(sql`CREATE UNIQUE INDEX \`projects_slug_idx\` ON \`projects\` (\`slug\`);`) await db.run(sql`CREATE INDEX \`projects_cover_idx\` ON \`projects\` (\`cover_id\`);`) await db.run(sql`CREATE INDEX \`projects_updated_at_idx\` ON \`projects\` (\`updated_at\`);`) await db.run(sql`CREATE INDEX \`projects_created_at_idx\` ON \`projects\` (\`created_at\`);`) await db.run(sql`CREATE TABLE \`projects_locales\` ( \`title\` text NOT NULL, \`summary\` text, \`body\` text, \`id\` integer PRIMARY KEY NOT NULL, \`_locale\` text NOT NULL, \`_parent_id\` integer NOT NULL, FOREIGN KEY (\`_parent_id\`) REFERENCES \`projects\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`CREATE UNIQUE INDEX \`projects_locales_locale_parent_id_unique\` ON \`projects_locales\` (\`_locale\`,\`_parent_id\`);`) await db.run(sql`CREATE INDEX \`projects_locales_parent_id_idx\` ON \`projects_locales\` (\`_parent_id\`);`) await db.run(sql`CREATE TABLE \`media\` ( \`id\` integer PRIMARY KEY NOT NULL, \`updated_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL, \`created_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL, \`url\` text, \`thumbnail_u_r_l\` text, \`filename\` text, \`mime_type\` text, \`filesize\` numeric, \`width\` numeric, \`height\` numeric, \`focal_x\` numeric, \`focal_y\` numeric ); `) await db.run(sql`CREATE INDEX \`media_updated_at_idx\` ON \`media\` (\`updated_at\`);`) await db.run(sql`CREATE INDEX \`media_created_at_idx\` ON \`media\` (\`created_at\`);`) await db.run(sql`CREATE UNIQUE INDEX \`media_filename_idx\` ON \`media\` (\`filename\`);`) await db.run(sql`CREATE TABLE \`media_locales\` ( \`alt\` text, \`id\` integer PRIMARY KEY NOT NULL, \`_locale\` text NOT NULL, \`_parent_id\` integer NOT NULL, FOREIGN KEY (\`_parent_id\`) REFERENCES \`media\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`CREATE UNIQUE INDEX \`media_locales_locale_parent_id_unique\` ON \`media_locales\` (\`_locale\`,\`_parent_id\`);`) await db.run(sql`CREATE INDEX \`media_locales_parent_id_idx\` ON \`media_locales\` (\`_parent_id\`);`) await db.run(sql`CREATE TABLE \`about\` ( \`id\` integer PRIMARY KEY NOT NULL, \`photo_id\` integer, \`updated_at\` text, \`created_at\` text, FOREIGN KEY (\`photo_id\`) REFERENCES \`media\`(\`id\`) ON UPDATE no action ON DELETE set null ); `) await db.run(sql`CREATE INDEX \`about_photo_idx\` ON \`about\` (\`photo_id\`);`) await db.run(sql`CREATE TABLE \`about_locales\` ( \`text\` text, \`id\` integer PRIMARY KEY NOT NULL, \`_locale\` text NOT NULL, \`_parent_id\` integer NOT NULL, FOREIGN KEY (\`_parent_id\`) REFERENCES \`about\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`CREATE UNIQUE INDEX \`about_locales_locale_parent_id_unique\` ON \`about_locales\` (\`_locale\`,\`_parent_id\`);`) await db.run(sql`CREATE INDEX \`about_locales_parent_id_idx\` ON \`about_locales\` (\`_parent_id\`);`) await db.run(sql`CREATE TABLE \`site_settings_socials\` ( \`_order\` integer NOT NULL, \`_parent_id\` integer NOT NULL, \`id\` text PRIMARY KEY NOT NULL, \`label\` text NOT NULL, \`url\` text NOT NULL, FOREIGN KEY (\`_parent_id\`) REFERENCES \`site_settings\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`CREATE INDEX \`site_settings_socials_order_idx\` ON \`site_settings_socials\` (\`_order\`);`) await db.run(sql`CREATE INDEX \`site_settings_socials_parent_id_idx\` ON \`site_settings_socials\` (\`_parent_id\`);`) await db.run(sql`CREATE TABLE \`site_settings\` ( \`id\` integer PRIMARY KEY NOT NULL, \`email\` text, \`phone\` text, \`updated_at\` text, \`created_at\` text ); `) await db.run(sql`CREATE TABLE \`site_settings_locales\` ( \`address\` text, \`id\` integer PRIMARY KEY NOT NULL, \`_locale\` text NOT NULL, \`_parent_id\` integer NOT NULL, FOREIGN KEY (\`_parent_id\`) REFERENCES \`site_settings\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`CREATE UNIQUE INDEX \`site_settings_locales_locale_parent_id_unique\` ON \`site_settings_locales\` (\`_locale\`,\`_parent_id\`);`) await db.run(sql`CREATE INDEX \`site_settings_locales_parent_id_idx\` ON \`site_settings_locales\` (\`_parent_id\`);`) await db.run(sql`ALTER TABLE \`news\` ADD \`cover_id\` integer REFERENCES media(id);`) await db.run(sql`CREATE INDEX \`news_cover_idx\` ON \`news\` (\`cover_id\`);`) await db.run(sql`ALTER TABLE \`news_locales\` ADD \`body\` text;`) await db.run(sql`ALTER TABLE \`payload_locked_documents_rels\` ADD \`projects_id\` integer REFERENCES projects(id);`) await db.run(sql`ALTER TABLE \`payload_locked_documents_rels\` ADD \`media_id\` integer REFERENCES media(id);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_projects_id_idx\` ON \`payload_locked_documents_rels\` (\`projects_id\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_media_id_idx\` ON \`payload_locked_documents_rels\` (\`media_id\`);`) } export async function down({ db, payload, req }: MigrateDownArgs): Promise { await db.run(sql`DROP TABLE \`projects\`;`) await db.run(sql`DROP TABLE \`projects_locales\`;`) await db.run(sql`DROP TABLE \`media\`;`) await db.run(sql`DROP TABLE \`media_locales\`;`) await db.run(sql`DROP TABLE \`about\`;`) await db.run(sql`DROP TABLE \`about_locales\`;`) await db.run(sql`DROP TABLE \`site_settings_socials\`;`) await db.run(sql`DROP TABLE \`site_settings\`;`) await db.run(sql`DROP TABLE \`site_settings_locales\`;`) await db.run(sql`PRAGMA foreign_keys=OFF;`) await db.run(sql`CREATE TABLE \`__new_news\` ( \`id\` integer PRIMARY KEY NOT NULL, \`kind\` text DEFAULT 'news', \`date\` text NOT NULL, \`updated_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL, \`created_at\` text DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')) NOT NULL ); `) await db.run(sql`INSERT INTO \`__new_news\`("id", "kind", "date", "updated_at", "created_at") SELECT "id", "kind", "date", "updated_at", "created_at" FROM \`news\`;`) await db.run(sql`DROP TABLE \`news\`;`) await db.run(sql`ALTER TABLE \`__new_news\` RENAME TO \`news\`;`) await db.run(sql`PRAGMA foreign_keys=ON;`) await db.run(sql`CREATE INDEX \`news_updated_at_idx\` ON \`news\` (\`updated_at\`);`) await db.run(sql`CREATE INDEX \`news_created_at_idx\` ON \`news\` (\`created_at\`);`) await db.run(sql`CREATE TABLE \`__new_payload_locked_documents_rels\` ( \`id\` integer PRIMARY KEY NOT NULL, \`order\` integer, \`parent_id\` integer NOT NULL, \`path\` text NOT NULL, \`users_id\` integer, \`news_id\` integer, \`inquiries_id\` integer, FOREIGN KEY (\`parent_id\`) REFERENCES \`payload_locked_documents\`(\`id\`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (\`users_id\`) REFERENCES \`users\`(\`id\`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (\`news_id\`) REFERENCES \`news\`(\`id\`) ON UPDATE no action ON DELETE cascade, FOREIGN KEY (\`inquiries_id\`) REFERENCES \`inquiries\`(\`id\`) ON UPDATE no action ON DELETE cascade ); `) await db.run(sql`INSERT INTO \`__new_payload_locked_documents_rels\`("id", "order", "parent_id", "path", "users_id", "news_id", "inquiries_id") SELECT "id", "order", "parent_id", "path", "users_id", "news_id", "inquiries_id" FROM \`payload_locked_documents_rels\`;`) await db.run(sql`DROP TABLE \`payload_locked_documents_rels\`;`) await db.run(sql`ALTER TABLE \`__new_payload_locked_documents_rels\` RENAME TO \`payload_locked_documents_rels\`;`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_order_idx\` ON \`payload_locked_documents_rels\` (\`order\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_parent_idx\` ON \`payload_locked_documents_rels\` (\`parent_id\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_path_idx\` ON \`payload_locked_documents_rels\` (\`path\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_users_id_idx\` ON \`payload_locked_documents_rels\` (\`users_id\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_news_id_idx\` ON \`payload_locked_documents_rels\` (\`news_id\`);`) await db.run(sql`CREATE INDEX \`payload_locked_documents_rels_inquiries_id_idx\` ON \`payload_locked_documents_rels\` (\`inquiries_id\`);`) await db.run(sql`ALTER TABLE \`news_locales\` DROP COLUMN \`body\`;`) }