fix(db): add every column an upgraded pre-ORM install can lack

The legacy baseline records the positional runner's steps up to
schema_version as executed without running them, so a column that runner
never added stays missing for good. Two things in its history left such
gaps: a step inserted below a version a build had already shipped (step 53,
users.mfa_backup_codes, so turning on MFA failed there) and a column added
to createTables() with no step (users.immich_access_token).

Migration20200101046460 adds, where missing, every column the schema held
at legacy step 258 that head still holds and ALTER TABLE ADD COLUMN can
add, declared as head declares it; on Postgres every one of them is in the
baseline, so nothing is added there. The list is derived again from the
migrations in tests/unit/db/legacy-column-repair.test.ts, which also adds
all of them to a database lacking them and upgrades a v244 install shaped
like the one whose move to Postgres was refused.
This commit is contained in:
Maurice
2026-11-18 15:22:36 +01:00
parent 6bc27a259b
commit ca7cdcebcc
4 changed files with 1121 additions and 8 deletions
@@ -0,0 +1,790 @@
import { addColumnIfMissing, columnNames, tableExists } from '../migration-utils';
import { Migration } from '@mikro-orm/migrations';
/**
* An install the retired positional runner upgraded can lack columns that its
* `schema_version` says it has, and the legacy baseline (`legacy-baseline.ts`)
* records every step up to that version as executed without running it, so
* no later boot added them. Two things in that runner's history left such gaps:
*
* - A step inserted below a version a build had already shipped. On
* 30 March 2026 a build of the main branch carried the MCP token table as
* step 53; the next one put `users.mfa_backup_codes` at step 53 and moved
* the rest up by one. An install that ran the first build counted step 53
* as done and never got the column, so turning on MFA failed there.
* - A column added to `createTables()` (the baseline) with no step. The
* baseline only creates a table that is missing, so a table created before
* the column was added never got it: `users.immich_access_token` (v2.9.0).
*
* Which columns either gap hit on a given install cannot be told from its
* history, so this adds every column that can be missing this way: each
* column the schema held at the last legacy step (258) that it still holds
* and that `ALTER TABLE ... ADD COLUMN` can add (not part of the primary key,
* not NOT NULL without a default, no default that is an expression), with its
* definition as it stands now. A column that is there is left alone, and so is
* a table that is missing: the boot names what still lacks (`schema-gaps.ts`).
* `tests/unit/db/legacy-column-repair.test.ts` derives the list again from the
* migrations and fails on any difference.
*
* Postgres never ran the positional runner: its baseline holds every one of
* these columns, so nothing is added there. Each postgres member is the
* column as that baseline declares it.
*/
const LEGACY_COLUMNS: Readonly<Record<string, ReadonlyArray<readonly [sqlite: string, postgres: string]>>> = {
addons: [
['description TEXT', '"description" text null'],
["type TEXT NOT NULL DEFAULT 'global'", '"type" text not null default \'global\''],
["icon TEXT DEFAULT 'Puzzle'", '"icon" text null default \'Puzzle\''],
['enabled INTEGER DEFAULT 0', '"enabled" bigint null default 0'],
["config TEXT DEFAULT '{}'", '"config" jsonb null default \'{}\''],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
app_settings: [['value TEXT', '"value" text null']],
audit_log: [
['user_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"user_id" bigint null'],
['resource TEXT', '"resource" text null'],
['details TEXT', '"details" text null'],
['ip TEXT', '"ip" text null'],
],
bucket_list: [
['lat REAL', '"lat" double precision null'],
['lng REAL', '"lng" double precision null'],
['country_code TEXT', '"country_code" text null'],
['notes TEXT', '"notes" text null'],
['target_date TEXT DEFAULT NULL', '"target_date" text null default NULL'],
['visited_at TEXT', '"visited_at" text null'],
['visited_source TEXT', '"visited_source" text null'],
['region_code TEXT', '"region_code" text null'],
],
budget_category_order: [['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0']],
budget_item_members: [
['paid INTEGER NOT NULL DEFAULT 0', '"paid" bigint not null default 0'],
['amount REAL', '"amount" double precision null'],
],
budget_item_payers: [['amount REAL NOT NULL DEFAULT 0', '"amount" double precision not null default 0']],
budget_items: [
["category TEXT NOT NULL DEFAULT 'Other'", '"category" text not null default \'Other\''],
['total_price REAL NOT NULL DEFAULT 0', '"total_price" double precision not null default 0'],
['persons INTEGER DEFAULT NULL', '"persons" bigint null default NULL'],
['days INTEGER DEFAULT NULL', '"days" bigint null default NULL'],
['note TEXT', '"note" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['paid_by_user_id INTEGER REFERENCES users(id)', '"paid_by_user_id" bigint null'],
['expense_date TEXT DEFAULT NULL', '"expense_date" text null default NULL'],
[
'reservation_id INTEGER REFERENCES reservations(id) ON DELETE SET NULL DEFAULT NULL',
'"reservation_id" bigint null default NULL',
],
['currency TEXT', '"currency" text null'],
['exchange_rate REAL NOT NULL DEFAULT 1', '"exchange_rate" double precision not null default 1'],
['ticket_json TEXT', '"ticket_json" text null'],
['place_id INTEGER REFERENCES places(id) ON DELETE SET NULL DEFAULT NULL', '"place_id" bigint null default NULL'],
],
budget_settlements: [
['amount REAL NOT NULL DEFAULT 0', '"amount" double precision not null default 0'],
['created_by_user_id INTEGER REFERENCES users(id)', '"created_by_user_id" bigint null'],
['currency TEXT', '"currency" text null'],
['exchange_rate REAL NOT NULL DEFAULT 1', '"exchange_rate" double precision not null default 1'],
['settled_at TEXT', '"settled_at" text null'],
['note TEXT', '"note" text null'],
],
categories: [
["color TEXT DEFAULT '#6366f1'", '"color" text null default \'#6366f1\''],
["icon TEXT DEFAULT '📍'", '"icon" text null default \'📍\''],
['user_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"user_id" bigint null'],
],
collab_links: [['pinned INTEGER DEFAULT 0', '"pinned" bigint null default 0']],
collab_messages: [
['reply_to INTEGER REFERENCES collab_messages(id) ON DELETE SET NULL', '"reply_to" bigint null'],
['deleted INTEGER DEFAULT 0', '"deleted" bigint null default 0'],
],
collab_notes: [
["category TEXT DEFAULT 'General'", '"category" text null default \'General\''],
['content TEXT', '"content" text null'],
["color TEXT DEFAULT '#6366f1'", '"color" text null default \'#6366f1\''],
['pinned INTEGER DEFAULT 0', '"pinned" bigint null default 0'],
['website TEXT', '"website" text null'],
],
collab_polls: [
['multiple INTEGER DEFAULT 0', '"multiple" bigint null default 0'],
['closed INTEGER DEFAULT 0', '"closed" bigint null default 0'],
['deadline TEXT', '"deadline" text null'],
],
collection_labels: [
["color TEXT DEFAULT '#6366f1'", '"color" text null default \'#6366f1\''],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
collection_members: [
["status TEXT NOT NULL DEFAULT 'pending'", '"status" text not null default \'pending\''],
["role TEXT NOT NULL DEFAULT 'editor'", '"role" text not null default \'editor\''],
],
collection_places: [
['saved_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"saved_by" bigint null'],
['description TEXT', '"description" text null'],
['lat REAL', '"lat" double precision null'],
['lng REAL', '"lng" double precision null'],
['address TEXT', '"address" text null'],
['category_id INTEGER REFERENCES categories(id) ON DELETE SET NULL', '"category_id" bigint null'],
['price REAL', '"price" double precision null'],
['currency TEXT', '"currency" text null'],
['notes TEXT', '"notes" text null'],
['image_url TEXT', '"image_url" text null'],
['google_place_id TEXT', '"google_place_id" text null'],
['google_ftid TEXT', '"google_ftid" text null'],
['osm_id TEXT', '"osm_id" text null'],
['website TEXT', '"website" text null'],
['phone TEXT', '"phone" text null'],
["status TEXT NOT NULL DEFAULT 'idea'", '"status" text not null default \'idea\''],
['source_trip_id INTEGER', '"source_trip_id" bigint null'],
['source_place_id INTEGER', '"source_place_id" bigint null'],
['links TEXT', '"links" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
collections: [
['description TEXT', '"description" text null'],
["color TEXT DEFAULT '#6366f1'", '"color" text null default \'#6366f1\''],
["icon TEXT DEFAULT 'Bookmark'", '"icon" text null default \'Bookmark\''],
['cover_image TEXT', '"cover_image" text null'],
['links TEXT', '"links" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
dawarich_connections: [
['url TEXT', '"url" text null'],
['api_key TEXT', '"api_key" text null'],
['allow_insecure_tls INTEGER NOT NULL DEFAULT 0', '"allow_insecure_tls" bigint not null default 0'],
['sync_enabled INTEGER NOT NULL DEFAULT 1', '"sync_enabled" bigint not null default 1'],
['last_sync_at TEXT', '"last_sync_at" text null'],
["last_sync_state TEXT NOT NULL DEFAULT 'never'", '"last_sync_state" text not null default \'never\''],
['last_sync_error TEXT', '"last_sync_error" text null'],
['capabilities TEXT', '"capabilities" text null'],
],
dawarich_visit_suggestions: [
['trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL', '"trip_id" bigint null'],
['lat REAL', '"lat" double precision null'],
['lng REAL', '"lng" double precision null'],
['duration_minutes INTEGER NOT NULL DEFAULT 0', '"duration_minutes" bigint not null default 0'],
["source_status TEXT NOT NULL DEFAULT 'suggested'", '"source_status" text not null default \'suggested\''],
['confidence REAL', '"confidence" double precision null'],
['confidence_band TEXT', '"confidence_band" text null'],
['country_code TEXT', '"country_code" text null'],
["state TEXT NOT NULL DEFAULT 'new'", '"state" text not null default \'new\''],
['target TEXT', '"target" text null'],
['accepted_place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"accepted_place_id" bigint null'],
['accepted_journal_entry_id INTEGER', '"accepted_journal_entry_id" bigint null'],
[
'accepted_bucket_list_item_id INTEGER REFERENCES bucket_list(id) ON DELETE SET NULL',
'"accepted_bucket_list_item_id" bigint null',
],
[
'matched_bucket_list_item_id INTEGER REFERENCES bucket_list(id) ON DELETE SET NULL',
'"matched_bucket_list_item_id" bigint null',
],
['accepted_hash TEXT', '"accepted_hash" text null'],
['source_missing_at TEXT', '"source_missing_at" text null'],
],
day_accommodations: [
['place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"place_id" bigint null'],
['check_in TEXT', '"check_in" text null'],
['check_in_end TEXT', '"check_in_end" text null'],
['check_out TEXT', '"check_out" text null'],
['confirmation TEXT', '"confirmation" text null'],
['notes TEXT', '"notes" text null'],
],
day_assignments: [
['order_index INTEGER DEFAULT 0', '"order_index" bigint null default 0'],
['notes TEXT', '"notes" text null'],
["reservation_status TEXT DEFAULT 'none'", '"reservation_status" text null default \'none\''],
['reservation_notes TEXT', '"reservation_notes" text null'],
['reservation_datetime TEXT', '"reservation_datetime" text null'],
['assignment_time TEXT', '"assignment_time" text null'],
['assignment_end_time TEXT', '"assignment_end_time" text null'],
['leg_transport_mode TEXT', '"leg_transport_mode" text null'],
['incoming_leg_transport_mode TEXT', '"incoming_leg_transport_mode" text null'],
['end_day INTEGER NOT NULL DEFAULT 0', '"end_day" bigint not null default 0'],
['accommodation_id INTEGER', '"accommodation_id" bigint null'],
['route_excluded INTEGER NOT NULL DEFAULT 0', '"route_excluded" bigint not null default 0'],
],
day_notes: [
['time TEXT', '"time" text null'],
["icon TEXT DEFAULT '📝'", '"icon" text null default \'📝\''],
['sort_order REAL DEFAULT 0', '"sort_order" double precision null default 0'],
['color TEXT', '"color" text null'],
],
days: [
['date TEXT', '"date" text null'],
['notes TEXT', '"notes" text null'],
['title TEXT', '"title" text null'],
['default_transport_mode TEXT', '"default_transport_mode" text null'],
],
document_connections: [
['secrets TEXT', '"secrets" text null'],
["settings TEXT NOT NULL DEFAULT '{}'", '"settings" text not null default \'{}\''],
['allow_insecure_tls INTEGER NOT NULL DEFAULT 0', '"allow_insecure_tls" bigint not null default 0'],
['capabilities TEXT', '"capabilities" text null'],
['last_probe_at TEXT', '"last_probe_at" text null'],
["last_probe_state TEXT NOT NULL DEFAULT 'never'", '"last_probe_state" text not null default \'never\''],
['last_probe_error TEXT', '"last_probe_error" text null'],
],
document_provider_fields: [
["input_type TEXT NOT NULL DEFAULT 'text'", '"input_type" text not null default \'text\''],
['placeholder TEXT', '"placeholder" text null'],
['hint TEXT', '"hint" text null'],
['required INTEGER DEFAULT 0', '"required" bigint null default 0'],
['secret INTEGER DEFAULT 0', '"secret" bigint null default 0'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
document_providers: [
['description TEXT', '"description" text null'],
["icon TEXT DEFAULT 'FileText'", '"icon" text null default \'FileText\''],
['enabled INTEGER DEFAULT 0', '"enabled" bigint null default 0'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
document_sync_items: [
['file_id INTEGER REFERENCES trip_files(id) ON DELETE SET NULL', '"file_id" bigint null'],
['remote_id TEXT', '"remote_id" text null'],
['remote_name TEXT', '"remote_name" text null'],
['remote_version TEXT', '"remote_version" text null'],
['remote_size INTEGER', '"remote_size" bigint null'],
['remote_modified_at TEXT', '"remote_modified_at" text null'],
['content_sha256 TEXT', '"content_sha256" text null'],
['pushed_sha256 TEXT', '"pushed_sha256" text null'],
["state TEXT NOT NULL DEFAULT 'pending'", '"state" text not null default \'pending\''],
['error_code TEXT', '"error_code" text null'],
['attempts INTEGER NOT NULL DEFAULT 0', '"attempts" bigint not null default 0'],
['next_attempt_at TEXT', '"next_attempt_at" text null'],
['remote_missing_at TEXT', '"remote_missing_at" text null'],
['synced_at TEXT', '"synced_at" text null'],
['remote_trashed_at TEXT', '"remote_trashed_at" text null'],
],
file_links: [
['reservation_id INTEGER REFERENCES reservations(id) ON DELETE CASCADE', '"reservation_id" bigint null'],
['assignment_id INTEGER REFERENCES day_assignments(id) ON DELETE CASCADE', '"assignment_id" bigint null'],
['place_id INTEGER REFERENCES places(id) ON DELETE CASCADE', '"place_id" bigint null'],
['budget_item_id INTEGER REFERENCES budget_items(id) ON DELETE SET NULL', '"budget_item_id" bigint null'],
],
google_api_usage: [['calls INTEGER NOT NULL DEFAULT 0', '"calls" bigint not null default 0']],
google_place_photo_meta: [
['attribution TEXT', '"attribution" text null'],
['error_at INTEGER', '"error_at" bigint null'],
],
invite_tokens: [
['max_uses INTEGER NOT NULL DEFAULT 1', '"max_uses" bigint not null default 1'],
['used_count INTEGER NOT NULL DEFAULT 0', '"used_count" bigint not null default 0'],
['expires_at TEXT', '"expires_at" text null'],
['trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL', '"trip_id" bigint null'],
],
journey_books: [
["title TEXT NOT NULL DEFAULT ''", '"title" text not null default \'\''],
['version INTEGER NOT NULL DEFAULT 1', '"version" bigint not null default 1'],
['created_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"created_by" bigint null'],
['updated_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"updated_by" bigint null'],
],
journey_contributors: [['hide_skeletons INTEGER NOT NULL DEFAULT 0', '"hide_skeletons" bigint not null default 0']],
journey_entries: [
['source_trip_id INTEGER REFERENCES trips(id) ON DELETE SET NULL', '"source_trip_id" bigint null'],
['source_place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"source_place_id" bigint null'],
['title TEXT', '"title" text null'],
['story TEXT', '"story" text null'],
['entry_time TEXT', '"entry_time" text null'],
['location_name TEXT', '"location_name" text null'],
['location_lat REAL', '"location_lat" double precision null'],
['location_lng REAL', '"location_lng" double precision null'],
['mood TEXT', '"mood" text null'],
['weather TEXT', '"weather" text null'],
['tags TEXT', '"tags" text null'],
["visibility TEXT DEFAULT 'private'", '"visibility" text null default \'private\''],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['pros_cons TEXT', '"pros_cons" text null'],
['stats_excluded INTEGER NOT NULL DEFAULT 0', '"stats_excluded" bigint not null default 0'],
['dismissed INTEGER NOT NULL DEFAULT 0', '"dismissed" bigint not null default 0'],
['country_code TEXT', '"country_code" text null'],
['source_assignment_id INTEGER', '"source_assignment_id" bigint null'],
['is_draft INTEGER NOT NULL DEFAULT 0', '"is_draft" bigint not null default 0'],
],
journey_entry_photos: [['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0']],
journey_photos: [
['caption TEXT', '"caption" text null'],
['shared INTEGER DEFAULT 0', '"shared" bigint null default 0'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['provider TEXT', '"provider" text null'],
['asset_id TEXT', '"asset_id" text null'],
['owner_id INTEGER', '"owner_id" bigint null'],
],
journey_share_tokens: [
['share_timeline INTEGER DEFAULT 1', '"share_timeline" bigint null default 1'],
['share_gallery INTEGER DEFAULT 1', '"share_gallery" bigint null default 1'],
['share_map INTEGER DEFAULT 1', '"share_map" bigint null default 1'],
['newest_first INTEGER NOT NULL DEFAULT 0', '"newest_first" bigint not null default 0'],
],
journeys: [
['subtitle TEXT', '"subtitle" text null'],
['cover_gradient TEXT', '"cover_gradient" text null'],
["status TEXT DEFAULT 'draft'", '"status" text null default \'draft\''],
['cover_image TEXT', '"cover_image" text null'],
['show_trip_tracks INTEGER NOT NULL DEFAULT 0', '"show_trip_tracks" bigint not null default 0'],
['show_verdict INTEGER NOT NULL DEFAULT 1', '"show_verdict" bigint not null default 1'],
['show_mood INTEGER NOT NULL DEFAULT 1', '"show_mood" bigint not null default 1'],
['show_weather INTEGER NOT NULL DEFAULT 1', '"show_weather" bigint not null default 1'],
['status_override TEXT', '"status_override" text null'],
['photo_location INTEGER NOT NULL DEFAULT 0', '"photo_location" bigint not null default 0'],
],
mcp_tokens: [
['last_used_at DATETIME', '"last_used_at" text null'],
["kind TEXT NOT NULL DEFAULT 'mcp'", '"kind" text not null default \'mcp\''],
["scope_mode TEXT NOT NULL DEFAULT 'all'", '"scope_mode" text not null default \'all\''],
['api_scopes TEXT', '"api_scopes" text null'],
],
notification_channel_preferences: [['enabled INTEGER NOT NULL DEFAULT 1', '"enabled" bigint not null default 1']],
notifications: [
['sender_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"sender_id" bigint null'],
["title_params TEXT DEFAULT '{}'", '"title_params" text null default \'{}\''],
["text_params TEXT DEFAULT '{}'", '"text_params" text null default \'{}\''],
['positive_text_key TEXT', '"positive_text_key" text null'],
['negative_text_key TEXT', '"negative_text_key" text null'],
['positive_callback TEXT', '"positive_callback" text null'],
['negative_callback TEXT', '"negative_callback" text null'],
["response TEXT CHECK(response IN ('positive', 'negative'))", '"response" text null'],
['navigate_text_key TEXT', '"navigate_text_key" text null'],
['navigate_target TEXT', '"navigate_target" text null'],
['is_read INTEGER DEFAULT 0', '"is_read" bigint null default 0'],
],
oauth_clients: [
['user_id INTEGER REFERENCES users(id) ON DELETE CASCADE', '"user_id" bigint null'],
["redirect_uris TEXT NOT NULL DEFAULT '[]'", '"redirect_uris" text not null default \'[]\''],
["allowed_scopes TEXT NOT NULL DEFAULT '[]'", '"allowed_scopes" text not null default \'[]\''],
['is_public INTEGER NOT NULL DEFAULT 0', '"is_public" bigint not null default 0'],
["created_via TEXT NOT NULL DEFAULT 'settings_ui'", '"created_via" text not null default \'settings_ui\''],
['allows_client_credentials INTEGER NOT NULL DEFAULT 0', '"allows_client_credentials" bigint not null default 0'],
],
oauth_consents: [["scopes TEXT NOT NULL DEFAULT '[]'", '"scopes" text not null default \'[]\'']],
oauth_tokens: [
["scopes TEXT NOT NULL DEFAULT '[]'", '"scopes" text not null default \'[]\''],
['revoked_at DATETIME', '"revoked_at" text null'],
['parent_token_id INTEGER REFERENCES oauth_tokens(id)', '"parent_token_id" bigint null'],
['audience TEXT', '"audience" text null'],
],
packing_bags: [
["color TEXT NOT NULL DEFAULT '#6366f1'", '"color" text not null default \'#6366f1\''],
['weight_limit_grams INTEGER', '"weight_limit_grams" bigint null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['user_id INTEGER REFERENCES users(id) ON DELETE SET NULL DEFAULT NULL', '"user_id" bigint null default NULL'],
],
packing_item_contributors: [
["status TEXT NOT NULL DEFAULT 'accepted'", '"status" text not null default \'accepted\''],
],
packing_items: [
['checked INTEGER DEFAULT 0', '"checked" bigint null default 0'],
['category TEXT', '"category" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['weight_grams INTEGER', '"weight_grams" bigint null'],
['bag_id INTEGER REFERENCES packing_bags(id) ON DELETE SET NULL', '"bag_id" bigint null'],
['quantity INTEGER NOT NULL DEFAULT 1', '"quantity" bigint not null default 1'],
['updated_at DATETIME', '"updated_at" text null'],
['is_private INTEGER NOT NULL DEFAULT 0', '"is_private" bigint not null default 0'],
['owner_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"owner_id" bigint null'],
['packed_quantity INTEGER', '"packed_quantity" bigint null'],
],
packing_template_categories: [['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0']],
packing_template_items: [
['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0'],
['weight_grams INTEGER', '"weight_grams" bigint null'],
['quantity INTEGER NOT NULL DEFAULT 1', '"quantity" bigint not null default 1'],
['bag_name TEXT', '"bag_name" text null'],
],
password_reset_tokens: [
['consumed_at DATETIME', '"consumed_at" text null'],
['created_ip TEXT', '"created_ip" text null'],
],
photo_provider_fields: [
["input_type TEXT NOT NULL DEFAULT 'text'", '"input_type" text not null default \'text\''],
['placeholder TEXT', '"placeholder" text null'],
['hint TEXT', '"hint" text null'],
['required INTEGER DEFAULT 0', '"required" bigint null default 0'],
['secret INTEGER DEFAULT 0', '"secret" bigint null default 0'],
['settings_key TEXT', '"settings_key" text null'],
['payload_key TEXT', '"payload_key" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
photo_providers: [
['description TEXT', '"description" text null'],
["icon TEXT DEFAULT 'Image'", '"icon" text null default \'Image\''],
['enabled INTEGER DEFAULT 0', '"enabled" bigint null default 0'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
],
photos: [
['day_id INTEGER REFERENCES days(id) ON DELETE SET NULL', '"day_id" bigint null'],
['place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"place_id" bigint null'],
['file_size INTEGER', '"file_size" bigint null'],
['mime_type TEXT', '"mime_type" text null'],
['caption TEXT', '"caption" text null'],
['taken_at TEXT', '"taken_at" text null'],
],
place_shadow_picks: [
['lang TEXT', '"lang" text null'],
['bias_lat REAL', '"bias_lat" double precision null'],
['bias_lng REAL', '"bias_lng" double precision null'],
['picked_place_id TEXT', '"picked_place_id" text null'],
],
places: [
['description TEXT', '"description" text null'],
['lat REAL', '"lat" double precision null'],
['lng REAL', '"lng" double precision null'],
['address TEXT', '"address" text null'],
['category_id INTEGER REFERENCES categories(id) ON DELETE SET NULL', '"category_id" bigint null'],
['price REAL', '"price" double precision null'],
['currency TEXT', '"currency" text null'],
["reservation_status TEXT DEFAULT 'none'", '"reservation_status" text null default \'none\''],
['reservation_notes TEXT', '"reservation_notes" text null'],
['reservation_datetime TEXT', '"reservation_datetime" text null'],
['place_time TEXT', '"place_time" text null'],
['end_time TEXT', '"end_time" text null'],
['duration_minutes INTEGER DEFAULT 60', '"duration_minutes" bigint null default 60'],
['notes TEXT', '"notes" text null'],
['image_url TEXT', '"image_url" text null'],
['google_place_id TEXT', '"google_place_id" text null'],
['google_ftid TEXT', '"google_ftid" text null'],
['website TEXT', '"website" text null'],
['phone TEXT', '"phone" text null'],
["transport_mode TEXT DEFAULT 'walking'", '"transport_mode" text null default \'walking\''],
['osm_id TEXT', '"osm_id" text null'],
['route_geometry TEXT', '"route_geometry" text null'],
['route_color TEXT', '"route_color" text null'],
['stop_type TEXT', '"stop_type" text null'],
['fill_percent INTEGER', '"fill_percent" bigint null'],
['amap_poi_id TEXT', '"amap_poi_id" text null'],
['source TEXT', '"source" text null'],
['email TEXT', '"email" text null'],
['opening_hours TEXT', '"opening_hours" text null'],
],
plugin_actions: [
['hint TEXT', '"hint" text null'],
['danger INTEGER NOT NULL DEFAULT 0', '"danger" bigint not null default 0'],
['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0'],
["scope TEXT NOT NULL DEFAULT 'user'", '"scope" text not null default \'user\''],
],
plugin_capability_audit: [
['acting_user_id INTEGER', '"acting_user_id" bigint null'],
['resource TEXT', '"resource" text null'],
['prev_hash TEXT', '"prev_hash" text null'],
],
plugin_entity_metadata: [['value TEXT', '"value" text null']],
plugin_error_log: [
["level TEXT NOT NULL DEFAULT 'error'", '"level" text not null default \'error\''],
['message TEXT', '"message" text null'],
['stack TEXT', '"stack" text null'],
],
plugin_oauth_tokens: [
['access_token TEXT', '"access_token" text null'],
['refresh_token TEXT', '"refresh_token" text null'],
['expires_at INTEGER', '"expires_at" bigint null'],
['scope TEXT', '"scope" text null'],
],
plugin_scheduled_tasks: [
["payload TEXT NOT NULL DEFAULT 'null'", '"payload" text not null default \'null\''],
['every_ms INTEGER', '"every_ms" bigint null'],
],
plugin_settings_fields: [
['label TEXT', '"label" text null'],
["input_type TEXT NOT NULL DEFAULT 'text'", '"input_type" text not null default \'text\''],
['placeholder TEXT', '"placeholder" text null'],
['hint TEXT', '"hint" text null'],
['required INTEGER NOT NULL DEFAULT 0', '"required" bigint not null default 0'],
['secret INTEGER NOT NULL DEFAULT 0', '"secret" bigint not null default 0'],
["scope TEXT NOT NULL DEFAULT 'instance'", '"scope" text not null default \'instance\''],
['options TEXT', '"options" text null'],
['oauth_config TEXT', '"oauth_config" text null'],
['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0'],
['default_value TEXT', '"default_value" text null'],
],
plugin_user_config: [["config TEXT NOT NULL DEFAULT '{}'", '"config" text not null default \'{}\'']],
plugins: [
['description TEXT', '"description" text null'],
["type TEXT NOT NULL DEFAULT 'integration'", '"type" text not null default \'integration\''],
["icon TEXT DEFAULT 'Blocks'", '"icon" text null default \'Blocks\''],
['version TEXT', '"version" text null'],
['api_version INTEGER DEFAULT 1', '"api_version" bigint null default 1'],
['min_trek_version TEXT', '"min_trek_version" text null'],
["permissions TEXT DEFAULT '[]'", '"permissions" text null default \'[]\''],
["granted_permissions TEXT DEFAULT '[]'", '"granted_permissions" text null default \'[]\''],
["status TEXT NOT NULL DEFAULT 'inactive'", '"status" text not null default \'inactive\''],
["config TEXT DEFAULT '{}'", '"config" text null default \'{}\''],
['source_repo TEXT', '"source_repo" text null'],
['source_commit TEXT', '"source_commit" text null'],
['sha256 TEXT', '"sha256" text null'],
['crash_count INTEGER NOT NULL DEFAULT 0', '"crash_count" bigint not null default 0'],
['last_error TEXT', '"last_error" text null'],
['reviewed_at TEXT', '"reviewed_at" text null'],
['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0'],
['enabled INTEGER NOT NULL DEFAULT 0', '"enabled" bigint not null default 0'],
["capabilities TEXT NOT NULL DEFAULT '{}'", '"capabilities" text not null default \'{}\''],
['author_pubkey TEXT', '"author_pubkey" text null'],
["dependencies TEXT NOT NULL DEFAULT '{}'", '"dependencies" text not null default \'{}\''],
['operator_egress INTEGER NOT NULL DEFAULT 0', '"operator_egress" bigint not null default 0'],
['update_block_code TEXT', '"update_block_code" text null'],
['update_block_detail TEXT', '"update_block_detail" text null'],
['update_block_version TEXT', '"update_block_version" text null'],
['trek_range TEXT', '"trek_range" text null'],
['update_hold INTEGER NOT NULL DEFAULT 0', '"update_hold" bigint not null default 0'],
],
push_subscriptions: [
['user_agent TEXT', '"user_agent" text null'],
['last_success_at TEXT', '"last_success_at" text null'],
['failure_count INTEGER NOT NULL DEFAULT 0', '"failure_count" bigint not null default 0'],
],
reservation_endpoints: [
['sequence INTEGER NOT NULL DEFAULT 0', '"sequence" bigint not null default 0'],
['code TEXT', '"code" text null'],
['timezone TEXT', '"timezone" text null'],
['local_time TEXT', '"local_time" text null'],
['local_date TEXT', '"local_date" text null'],
],
reservations: [
['day_id INTEGER REFERENCES days(id) ON DELETE SET NULL', '"day_id" bigint null'],
['end_day_id INTEGER REFERENCES days(id) ON DELETE SET NULL', '"end_day_id" bigint null'],
['place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"place_id" bigint null'],
['assignment_id INTEGER REFERENCES day_assignments(id) ON DELETE SET NULL', '"assignment_id" bigint null'],
['accommodation_id TEXT', '"accommodation_id" text null'],
['reservation_time TEXT', '"reservation_time" text null'],
['reservation_end_time TEXT', '"reservation_end_time" text null'],
['location TEXT', '"location" text null'],
['confirmation_number TEXT', '"confirmation_number" text null'],
['notes TEXT', '"notes" text null'],
["status TEXT DEFAULT 'pending'", '"status" text null default \'pending\''],
["type TEXT DEFAULT 'other'", '"type" text null default \'other\''],
['metadata TEXT', '"metadata" text null'],
['day_plan_position REAL DEFAULT NULL', '"day_plan_position" double precision null default NULL'],
['needs_review INTEGER NOT NULL DEFAULT 0', '"needs_review" bigint not null default 0'],
['external_source TEXT', '"external_source" text null'],
['external_id TEXT', '"external_id" text null'],
['external_owner_user_id INTEGER', '"external_owner_user_id" bigint null'],
['external_synced_at TEXT', '"external_synced_at" text null'],
['sync_enabled INTEGER DEFAULT 1', '"sync_enabled" bigint null default 1'],
['external_hash TEXT', '"external_hash" text null'],
['url TEXT', '"url" text null'],
["ingest_state TEXT NOT NULL DEFAULT 'live'", '"ingest_state" text not null default \'live\''],
],
roadtrip_day_boundaries: [
['to_assignment_id INTEGER REFERENCES day_assignments(id) ON DELETE CASCADE', '"to_assignment_id" bigint null'],
],
roadtrip_day_tracks: [['stray_km REAL', '"stray_km" double precision null']],
roadtrip_vias: [['sequence INTEGER NOT NULL DEFAULT 0', '"sequence" bigint not null default 0']],
route_usage_daily: [
['requests INTEGER NOT NULL DEFAULT 0', '"requests" bigint not null default 0'],
['waypoints INTEGER NOT NULL DEFAULT 0', '"waypoints" bigint not null default 0'],
['km REAL NOT NULL DEFAULT 0', '"km" double precision not null default 0'],
['failed INTEGER NOT NULL DEFAULT 0', '"failed" bigint not null default 0'],
],
school_holiday_regions: [['revision INTEGER NOT NULL DEFAULT 1', '"revision" bigint not null default 1']],
settings: [['value TEXT', '"value" text null']],
share_tokens: [
['share_map INTEGER DEFAULT 1', '"share_map" bigint null default 1'],
['share_bookings INTEGER DEFAULT 1', '"share_bookings" bigint null default 1'],
['share_packing INTEGER DEFAULT 0', '"share_packing" bigint null default 0'],
['share_budget INTEGER DEFAULT 0', '"share_budget" bigint null default 0'],
['share_collab INTEGER DEFAULT 0', '"share_collab" bigint null default 0'],
['expires_at TEXT', '"expires_at" text null'],
['share_travel_only INTEGER NOT NULL DEFAULT 0', '"share_travel_only" bigint not null default 0'],
['share_hide_images INTEGER NOT NULL DEFAULT 0', '"share_hide_images" bigint not null default 0'],
],
tags: [["color TEXT DEFAULT '#10b981'", '"color" text null default \'#10b981\'']],
todo_items: [
['checked INTEGER DEFAULT 0', '"checked" bigint null default 0'],
['category TEXT', '"category" text null'],
['sort_order INTEGER DEFAULT 0', '"sort_order" bigint null default 0'],
['due_date TEXT', '"due_date" text null'],
['description TEXT', '"description" text null'],
['assigned_user_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"assigned_user_id" bigint null'],
['priority INTEGER DEFAULT 0', '"priority" bigint null default 0'],
['reminded_at DATETIME', '"reminded_at" text null'],
],
trek_photo_cache_meta: [
["content_type TEXT NOT NULL DEFAULT 'image/jpeg'", '"content_type" text not null default \'image/jpeg\''],
],
trek_photos: [
['asset_id TEXT', '"asset_id" text null'],
['owner_id INTEGER REFERENCES users(id) ON DELETE SET NULL', '"owner_id" bigint null'],
['file_path TEXT', '"file_path" text null'],
['thumbnail_path TEXT', '"thumbnail_path" text null'],
['width INTEGER', '"width" bigint null'],
['height INTEGER', '"height" bigint null'],
['passphrase TEXT DEFAULT NULL', '"passphrase" text null default NULL'],
["media_type TEXT NOT NULL DEFAULT 'image'", '"media_type" text not null default \'image\''],
['duration_ms INTEGER', '"duration_ms" bigint null'],
['taken_at TEXT', '"taken_at" text null'],
['lat REAL', '"lat" double precision null'],
['lng REAL', '"lng" double precision null'],
],
trip_album_links: [
["album_name TEXT NOT NULL DEFAULT ''", '"album_name" text not null default \'\''],
['sync_enabled INTEGER NOT NULL DEFAULT 1', '"sync_enabled" bigint not null default 1'],
['last_synced_at DATETIME', '"last_synced_at" text null'],
['passphrase TEXT DEFAULT NULL', '"passphrase" text null default NULL'],
],
trip_document_links: [
['remote_root_id TEXT', '"remote_root_id" text null'],
['remote_root_path TEXT', '"remote_root_path" text null'],
["remote_label TEXT NOT NULL DEFAULT ''", '"remote_label" text not null default \'\''],
["direction TEXT NOT NULL DEFAULT 'both'", '"direction" text not null default \'both\''],
["delete_policy TEXT NOT NULL DEFAULT 'unlink'", '"delete_policy" text not null default \'unlink\''],
["conflict_policy TEXT NOT NULL DEFAULT 'manual'", '"conflict_policy" text not null default \'manual\''],
['sync_enabled INTEGER NOT NULL DEFAULT 1', '"sync_enabled" bigint not null default 1'],
['webhook_token TEXT', '"webhook_token" text null'],
['webhook_secret TEXT', '"webhook_secret" text null'],
['webhook_subscription_id TEXT', '"webhook_subscription_id" text null'],
['remote_cursor TEXT', '"remote_cursor" text null'],
['last_sync_at TEXT', '"last_sync_at" text null'],
["last_sync_state TEXT NOT NULL DEFAULT 'never'", '"last_sync_state" text not null default \'never\''],
['last_sync_error TEXT', '"last_sync_error" text null'],
['failure_count INTEGER NOT NULL DEFAULT 0', '"failure_count" bigint not null default 0'],
['next_attempt_at TEXT', '"next_attempt_at" text null'],
['created_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"created_by" bigint null'],
],
trip_files: [
['place_id INTEGER REFERENCES places(id) ON DELETE SET NULL', '"place_id" bigint null'],
['reservation_id INTEGER REFERENCES reservations(id) ON DELETE SET NULL', '"reservation_id" bigint null'],
['file_size INTEGER', '"file_size" bigint null'],
['mime_type TEXT', '"mime_type" text null'],
['description TEXT', '"description" text null'],
['note_id INTEGER REFERENCES collab_notes(id) ON DELETE SET NULL', '"note_id" bigint null'],
['uploaded_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"uploaded_by" bigint null'],
['starred INTEGER DEFAULT 0', '"starred" bigint null default 0'],
['deleted_at TEXT', '"deleted_at" text null'],
['message_id INTEGER REFERENCES collab_messages(id) ON DELETE CASCADE', '"message_id" bigint null'],
],
trip_invite_tokens: [
['created_by INTEGER REFERENCES users(id) ON DELETE SET NULL', '"created_by" bigint null'],
['expires_at TEXT', '"expires_at" text null'],
],
trip_members: [['invited_by INTEGER REFERENCES users(id)', '"invited_by" bigint null']],
trip_photos: [
['shared INTEGER NOT NULL DEFAULT 1', '"shared" bigint not null default 1'],
['album_link_id INTEGER REFERENCES trip_album_links(id) ON DELETE SET NULL', '"album_link_id" bigint null'],
],
trips: [
['description TEXT', '"description" text null'],
['start_date TEXT', '"start_date" text null'],
['end_date TEXT', '"end_date" text null'],
["currency TEXT DEFAULT 'EUR'", '"currency" text null default \'EUR\''],
['cover_image TEXT', '"cover_image" text null'],
['is_archived INTEGER DEFAULT 0', '"is_archived" bigint null default 0'],
['reminder_days INTEGER DEFAULT 3', '"reminder_days" bigint null default 3'],
['feed_token TEXT', '"feed_token" text null'],
],
user_notice_dismissals: [['dismissed_app_version TEXT', '"dismissed_app_version" text null']],
users: [
["role TEXT NOT NULL DEFAULT 'user'", '"role" text not null default \'user\''],
['maps_api_key TEXT', '"maps_api_key" text null'],
['unsplash_api_key TEXT', '"unsplash_api_key" text null'],
['amap_api_key TEXT', '"amap_api_key" text null'],
['openweather_api_key TEXT', '"openweather_api_key" text null'],
['avatar TEXT', '"avatar" text null'],
['oidc_sub TEXT', '"oidc_sub" text null'],
['oidc_issuer TEXT', '"oidc_issuer" text null'],
['last_login DATETIME', '"last_login" text null'],
['mfa_enabled INTEGER DEFAULT 0', '"mfa_enabled" bigint null default 0'],
['mfa_secret TEXT', '"mfa_secret" text null'],
['mfa_backup_codes TEXT', '"mfa_backup_codes" text null'],
['immich_url TEXT', '"immich_url" text null'],
['immich_access_token TEXT', '"immich_access_token" text null'],
['synology_url TEXT', '"synology_url" text null'],
['synology_username TEXT', '"synology_username" text null'],
['synology_password TEXT', '"synology_password" text null'],
['synology_sid TEXT', '"synology_sid" text null'],
['must_change_password INTEGER DEFAULT 0', '"must_change_password" bigint null default 0'],
['password_version INTEGER NOT NULL DEFAULT 0', '"password_version" bigint not null default 0'],
['feed_token TEXT', '"feed_token" text null'],
['is_guest INTEGER NOT NULL DEFAULT 0', '"is_guest" bigint not null default 0'],
['immich_api_key TEXT', '"immich_api_key" text null'],
['synology_skip_ssl INTEGER NOT NULL DEFAULT 0', '"synology_skip_ssl" bigint not null default 0'],
['synology_did TEXT', '"synology_did" text null'],
["first_seen_version TEXT NOT NULL DEFAULT '0.0.0'", '"first_seen_version" text not null default \'0.0.0\''],
['login_count INTEGER NOT NULL DEFAULT 0', '"login_count" bigint not null default 0'],
['immich_auto_upload INTEGER NOT NULL DEFAULT 0', '"immich_auto_upload" bigint not null default 0'],
['airtrail_url TEXT', '"airtrail_url" text null'],
['airtrail_api_key TEXT', '"airtrail_api_key" text null'],
['airtrail_allow_insecure_tls INTEGER DEFAULT 0', '"airtrail_allow_insecure_tls" bigint null default 0'],
['airtrail_write_enabled INTEGER DEFAULT 0', '"airtrail_write_enabled" bigint null default 0'],
['display_name TEXT', '"display_name" text null'],
['immich_allow_insecure_tls INTEGER NOT NULL DEFAULT 0', '"immich_allow_insecure_tls" bigint not null default 0'],
],
vacay_company_holidays: [
["note TEXT DEFAULT ''", '"note" text null default \'\''],
['fraction REAL NOT NULL DEFAULT 1', '"fraction" double precision not null default 1'],
],
vacay_entries: [
["note TEXT DEFAULT ''", '"note" text null default \'\''],
['fraction REAL NOT NULL DEFAULT 1', '"fraction" double precision not null default 1'],
["kind TEXT NOT NULL DEFAULT 'vacation'", '"kind" text not null default \'vacation\''],
],
vacay_holiday_calendars: [
["type TEXT NOT NULL DEFAULT 'public_holiday'", '"type" text not null default \'public_holiday\''],
['label TEXT', '"label" text null'],
["color TEXT NOT NULL DEFAULT '#fecaca'", '"color" text not null default \'#fecaca\''],
['sort_order INTEGER NOT NULL DEFAULT 0', '"sort_order" bigint not null default 0'],
],
vacay_plan_members: [["status TEXT DEFAULT 'pending'", '"status" text null default \'pending\'']],
vacay_plans: [
['block_weekends INTEGER DEFAULT 1', '"block_weekends" bigint null default 1'],
['holidays_enabled INTEGER DEFAULT 0', '"holidays_enabled" bigint null default 0'],
["holidays_region TEXT DEFAULT ''", '"holidays_region" text null default \'\''],
['school_holidays_enabled INTEGER DEFAULT 0', '"school_holidays_enabled" bigint null default 0'],
['company_holidays_enabled INTEGER DEFAULT 1', '"company_holidays_enabled" bigint null default 1'],
['carry_over_enabled INTEGER DEFAULT 1', '"carry_over_enabled" bigint null default 1'],
["weekend_days TEXT DEFAULT '0,6'", '"weekend_days" text null default \'0,6\''],
['week_start INTEGER NOT NULL DEFAULT 1', '"week_start" bigint not null default 1'],
],
vacay_shares: [['hidden INTEGER NOT NULL DEFAULT 0', '"hidden" bigint not null default 0']],
vacay_user_colors: [["color TEXT DEFAULT '#6366f1'", '"color" text null default \'#6366f1\'']],
vacay_user_settings: [
["year_type TEXT NOT NULL DEFAULT 'calendar'", '"year_type" text not null default \'calendar\''],
['year_start_month INTEGER NOT NULL DEFAULT 1', '"year_start_month" bigint not null default 1'],
['year_start_day INTEGER NOT NULL DEFAULT 1', '"year_start_day" bigint not null default 1'],
['hire_date TEXT', '"hire_date" text null'],
],
vacay_user_years: [
['vacation_days INTEGER DEFAULT 30', '"vacation_days" bigint null default 30'],
['carried_over REAL DEFAULT 0', '"carried_over" bigint null default 0'],
],
visited_countries: [["source TEXT NOT NULL DEFAULT 'manual'", '"source" text not null default \'manual\'']],
webauthn_challenges: [['user_id INTEGER REFERENCES users(id) ON DELETE CASCADE', '"user_id" bigint null']],
webauthn_credentials: [
['counter INTEGER NOT NULL DEFAULT 0', '"counter" bigint not null default 0'],
['transports TEXT', '"transports" text null'],
['device_type TEXT', '"device_type" text null'],
['backed_up INTEGER NOT NULL DEFAULT 0', '"backed_up" bigint not null default 0'],
['name TEXT', '"name" text null'],
['aaguid TEXT', '"aaguid" text null'],
['last_used_at DATETIME', '"last_used_at" text null'],
],
};
/** The column a definition declares: its first word. */
function columnOf(definition: string): string {
return definition.slice(0, definition.indexOf(' '));
}
export class Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped extends Migration {
override name = 'Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped';
override async up(): Promise<void> {
// One table after the other: each ALTER TABLE changes the catalog the next check reads.
await Object.entries(LEGACY_COLUMNS).reduce<Promise<void>>(
(previous, [table, columns]) => previous.then(() => this.addMissing(table, columns)),
Promise.resolve(),
);
}
private async addMissing(table: string, columns: ReadonlyArray<readonly [string, string]>): Promise<void> {
if (!(await tableExists(this, table))) return;
const present = await columnNames(this, table);
await columns
.filter(([sqliteColumn]) => !present.has(columnOf(sqliteColumn)))
.reduce<Promise<void>>(
(previous, [sqliteColumn, postgresColumn]) =>
previous.then(() =>
addColumnIfMissing(this, table, columnOf(sqliteColumn), { sqlite: sqliteColumn, postgres: postgresColumn }),
),
Promise.resolve(),
);
}
}
@@ -25,16 +25,14 @@ describeLegacyUpgrade(
nightOrderIndex: 1,
id: 'LEGACYUP-066',
// Carried from the legacy chain itself, not caused by the upgrade: an install
// this old got `users` and `reservations` from its own createTables(), and no
// numbered step ever adds `users.immich_access_token` or retypes
// `reservations.accommodation_id` (INTEGER there, TEXT in the baseline). The
// legacy runner at 417d0445b would have left the same two differences.
// this old got `reservations` from its own createTables(), and no numbered
// step ever retypes `reservations.accommodation_id` (INTEGER there, TEXT in
// the baseline). It also lacked `users.immich_access_token`, which no step
// adds either; Migration20200101046460 adds that one, as every column an
// ALTER TABLE can add.
carriedSchemaDiffs: {
onlyUpgraded: ['reservations.accommodation_id INTEGER notnull=0 default= pk=0'],
onlyFresh: [
'reservations.accommodation_id TEXT notnull=0 default= pk=0',
'users.immich_access_token TEXT notnull=0 default= pk=0',
],
onlyFresh: ['reservations.accommodation_id TEXT notnull=0 default= pk=0'],
},
},
legacyDb,
@@ -113,6 +113,7 @@ const UNNUMBERED = [
'Migration20200101046430_a_paused_mail_import_can_tell_its_address_holders',
'Migration20200101046440_a_trip_owns_its_tags',
'Migration20200101046450_a_carried_over_half_day_stays_half',
'Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped',
];
// The last step the positional runner ever had; planLegacyBaseline refuses a
// schema_version past it.
@@ -0,0 +1,324 @@
/**
* LEGACYCOL: the columns an install the retired positional runner upgraded
* can lack, and the migration that adds them
* (`Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped`).
*
* The list in that migration is frozen like every migration, so it is derived
* here again from the migrations themselves: a database migrated to the last
* legacy step holds the columns such an install should have, the head schema
* says how each one is declared now, and the Postgres baseline how Postgres
* declares it. Then every entry is added to a database that lacks all of them,
* and an install shaped like the one whose move to Postgres was refused
* (`users` without `mfa_backup_codes` and `immich_access_token`) is upgraded.
*/
import { migrateToHead } from '../../../src/db/legacy-baseline';
import { Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped as Repair } from '../../../src/db/migrations/Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped';
import { openLegacyFixture } from '../../helpers/legacy-fixture';
import {
createMigrationOrm,
migrateTo,
migratorOf,
rawExec,
rawQuery,
runMigrationDirect,
} from '../../helpers/migration-step';
import { Migrator } from '@mikro-orm/migrations';
import { MikroORM } from '@mikro-orm/sqlite';
import fs from 'node:fs';
import path from 'node:path';
import ts from 'typescript';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
const SERVER = path.join(__dirname, '../../..');
const MIGRATIONS = path.join(SERVER, 'src/db/migrations');
const REPAIR_FILE = path.join(
MIGRATIONS,
'Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped.ts',
);
/** The migration that carries the last step the positional runner had (258). */
const LAST_LEGACY_STEP = 'Migration20200101041900_a_journey_that_puts_an_entry_on';
/** A table the list names, left out of LEGACYCOL-003's database to show a missing table is skipped. */
const LEFT_OUT = 'bucket_list';
type Entry = readonly [table: string, sqlite: string, postgres: string];
interface ColumnInfo {
tbl: string;
name: string;
type: string;
notnull: number;
dflt_value: string | null;
pk: number;
hidden: number;
}
interface ForeignKey {
tbl: string;
from: string;
table: string;
to: string;
on_delete: string;
}
/** The parts of a CREATE TABLE body, split on the commas outside parentheses and quotes. */
function splitDefinitions(body: string): string[] {
const parts: string[] = [];
let depth = 0;
let quote: string | null = null;
let current = '';
for (const ch of body) {
if (quote) {
current += ch;
if (ch === quote) quote = null;
continue;
}
if (ch === "'" || ch === '"' || ch === '`') quote = ch;
else if (ch === '(') depth++;
else if (ch === ')') depth--;
else if (ch === ',' && depth === 0) {
parts.push(current.trim());
current = '';
continue;
}
current += ch;
}
parts.push(current.trim());
return parts.filter((part) => part.length > 0);
}
const TABLE_CONSTRAINT = /^(?:CONSTRAINT|PRIMARY\s+KEY|UNIQUE|CHECK|FOREIGN\s+KEY)\b/i;
const unquote = (name: string) => name.replace(/^["`[]|["`\]]$/g, '');
const nameOf = (definition: string) => unquote(definition.split(/\s+/)[0] ?? '');
function tableBody(sql: string): string {
return sql.slice(sql.indexOf('(') + 1, sql.lastIndexOf(')'));
}
/** A default ALTER TABLE ... ADD COLUMN accepts: a literal, not an expression. */
function constantDefault(value: string | null): boolean {
return value === null || /^(?:-?\d+(?:\.\d+)?|'[^']*'|NULL|TRUE|FALSE)$/i.test(value);
}
/** What the migration's list holds, read from its source. */
function listedEntries(): Entry[] {
const source = ts.createSourceFile(REPAIR_FILE, fs.readFileSync(REPAIR_FILE, 'utf8'), ts.ScriptTarget.Latest, true);
const entries: Entry[] = [];
const visit = (node: ts.Node): void => {
if (ts.isVariableDeclaration(node) && node.name.getText() === 'LEGACY_COLUMNS') {
const object = node.initializer as ts.ObjectLiteralExpression;
for (const property of object.properties as ts.NodeArray<ts.PropertyAssignment>) {
for (const tuple of (property.initializer as ts.ArrayLiteralExpression).elements) {
const [sqlite, postgres] = (tuple as ts.ArrayLiteralExpression).elements as ts.NodeArray<ts.StringLiteral>;
entries.push([property.name.getText(), sqlite?.text ?? '', postgres?.text ?? '']);
}
}
}
ts.forEachChild(node, visit);
};
visit(source);
return entries;
}
/** Every column of `assets/postgres/baseline.sql`, as that file declares it. */
function postgresColumns(): Map<string, string> {
const columns = new Map<string, string>();
const sql = fs.readFileSync(path.join(SERVER, 'assets/postgres/baseline.sql'), 'utf8');
for (const match of sql.matchAll(/^create table "(\w+)" \((.*)\);$/gm)) {
for (const part of splitDefinitions(match[2] ?? '')) {
const column = /^"(\w+)"/.exec(part)?.[1];
if (column) columns.set(`${match[1]}.${column}`, part);
}
}
return columns;
}
let atLastStep: MikroORM;
let atHead: MikroORM;
function tables(orm: MikroORM): Promise<Array<{ name: string; sql: string }>> {
return rawQuery(
orm,
`SELECT name, sql FROM sqlite_master WHERE type = 'table' AND name NOT LIKE 'sqlite_%' AND name <> 'mikro_orm_migrations' ORDER BY name`,
);
}
/** Every column of every table, in one statement. */
function allColumns(orm: MikroORM): Promise<ColumnInfo[]> {
return rawQuery<ColumnInfo>(
orm,
`SELECT m.name AS tbl, p.* FROM sqlite_master m JOIN pragma_table_xinfo(m.name) p WHERE m.type = 'table'`,
);
}
/** Every foreign key of every table, in one statement. */
function allForeignKeys(orm: MikroORM): Promise<ForeignKey[]> {
return rawQuery<ForeignKey>(
orm,
`SELECT m.name AS tbl, f.* FROM sqlite_master m JOIN pragma_foreign_key_list(m.name) f WHERE m.type = 'table'`,
);
}
/** Runs `work` on each item, one after the other. */
function inSequence<T>(items: readonly T[], work: (item: T) => Promise<unknown>): Promise<void> {
return items.reduce<Promise<void>>(
(previous, item) =>
previous.then(async () => {
await work(item);
}),
Promise.resolve(),
);
}
/**
* The list as the migrations make it: each column the last legacy step's
* schema holds that head still holds and ALTER TABLE can add, declared as
* head declares it (a foreign key head declares on the table, on the column).
*/
async function derivedEntries(): Promise<Entry[]> {
const before = new Set((await allColumns(atLastStep)).map((column) => `${column.tbl}.${column.name}`));
const postgres = postgresColumns();
const headColumns = await allColumns(atHead);
const headKeys = await allForeignKeys(atHead);
return (await tables(atHead)).flatMap((table) => {
const definitions = new Map(
splitDefinitions(tableBody(table.sql))
.filter((part) => !TABLE_CONSTRAINT.test(part))
.map((part) => [nameOf(part), part.replace(/\s+/g, ' ')]),
);
return headColumns
.filter((column) => column.tbl === table.name && before.has(`${table.name}.${column.name}`))
.flatMap((column): Entry[] => {
let definition = definitions.get(column.name) ?? '';
const addable =
column.pk === 0 &&
column.hidden === 0 &&
!(column.notnull === 1 && column.dflt_value === null) &&
constantDefault(column.dflt_value) &&
!/\b(?:UNIQUE|PRIMARY\s+KEY)\b/i.test(definition);
if (!addable) return [];
const key = headKeys.find((fk) => fk.tbl === table.name && fk.from === column.name);
if (key && !/\bREFERENCES\b/i.test(definition)) {
definition += ` REFERENCES ${key.table}(${key.to}) ON DELETE ${key.on_delete}`;
}
return [[table.name, definition, postgres.get(`${table.name}.${column.name}`) ?? '(not in the baseline)']];
});
});
}
beforeAll(async () => {
atLastStep = await createMigrationOrm();
await migrateTo(atLastStep, LAST_LEGACY_STEP);
atHead = await createMigrationOrm();
await migratorOf(atHead).up();
}, 120_000);
afterAll(async () => {
await atLastStep.close();
await atHead.close();
});
describe('the list of columns', () => {
it('LEGACYCOL-001: holds exactly the columns the migrations derive, declared as head declares them', async () => {
const listed = listedEntries();
expect(listed.length).toBeGreaterThan(500);
expect(listed.map(([table, sqlite]) => `${table}: ${sqlite}`)).toEqual(
(await derivedEntries()).map(([table, sqlite]) => `${table}: ${sqlite}`),
);
expect(listed.map(([table, sqlite]) => `${table}.${nameOf(sqlite)}`)).toEqual(
expect.arrayContaining(['users.mfa_backup_codes', 'users.immich_access_token', `${LEFT_OUT}.lat`]),
);
});
it('LEGACYCOL-002: each postgres member is the column as the Postgres baseline declares it', () => {
const postgres = postgresColumns();
const wrong = listedEntries().filter(
([table, sqlite, declared]) => declared !== postgres.get(`${table}.${nameOf(sqlite)}`),
);
expect(wrong).toEqual([]);
});
});
describe('the migration', () => {
it('LEGACYCOL-003: adds every listed column to a database that lacks them all, as head has it, and skips a missing table', async () => {
const byTable = new Map<string, Set<string>>();
for (const [table, sqlite] of listedEntries()) {
byTable.set(table, (byTable.get(table) ?? new Set()).add(nameOf(sqlite)));
}
const bare = await createMigrationOrm();
try {
const creates = (await tables(atHead)).flatMap((table) => {
const removed = byTable.get(table.name);
if (!removed || table.name === LEFT_OUT) return [];
const kept = splitDefinitions(tableBody(table.sql)).filter((part) =>
TABLE_CONSTRAINT.test(part)
? ![...removed].some((column) => new RegExp(`\\b${column}\\b`).test(part))
: !removed.has(nameOf(part)),
);
return [`CREATE TABLE "${table.name}" (${kept.join(', ')})`];
});
await inSequence(creates, (sql) => rawExec(bare, sql));
await runMigrationDirect(bare, Repair);
const listed = (table: string, column: string) => table !== LEFT_OUT && Boolean(byTable.get(table)?.has(column));
const shape = async (orm: MikroORM) =>
(await allColumns(orm))
.filter((row) => listed(row.tbl, row.name))
.map(({ tbl, name, type, notnull, dflt_value }) => `${tbl}.${name} ${type} ${notnull} ${dflt_value}`)
.sort();
const keys = async (orm: MikroORM) =>
(await allForeignKeys(orm))
.filter((row) => listed(row.tbl, row.from))
.map(({ tbl, from, table, to, on_delete }) => `${tbl}.${from} -> ${table}.${to} ${on_delete}`)
.sort();
expect(await shape(bare)).toEqual(await shape(atHead));
expect(await keys(bare)).toEqual(await keys(atHead));
expect(await rawQuery(bare, `SELECT name FROM sqlite_master WHERE name = ?`, [LEFT_OUT])).toEqual([]);
} finally {
await bare.close();
}
});
it('LEGACYCOL-004: changes nothing on a database the migrations built', async () => {
const shape = async () => (await tables(atHead)).map((table) => `${table.name}: ${table.sql}`);
const before = await shape();
await runMigrationDirect(atHead, Repair);
expect(await shape()).toEqual(before);
});
it('LEGACYCOL-005: an install at schema_version 244 without users.mfa_backup_codes and users.immich_access_token gets both', async () => {
const fixture = openLegacyFixture('legacy-v244');
const file = fixture.name;
fixture.exec('ALTER TABLE users DROP COLUMN mfa_backup_codes');
fixture.exec('ALTER TABLE users DROP COLUMN immich_access_token');
fixture.close();
const orm = await MikroORM.init({
entities: [],
discovery: { warnWhenNoEntities: false },
extensions: [Migrator],
migrations: { path: MIGRATIONS, pathTs: MIGRATIONS, snapshot: false, silent: true },
dbName: file,
});
try {
const migrator = migratorOf(orm);
await migrateToHead(orm.em.getConnection(), migrator);
const executed = new Set((await migrator.getExecuted()).map((row) => row.name));
// The step that should have added the column is recorded, not run: the gap this closes.
expect(executed.has('Migration20200101005300_users_add_mfa_backup_codes')).toBe(true);
expect(executed.has('Migration20200101046460_an_upgraded_install_gets_the_columns_its_history_skipped')).toBe(
true,
);
const users = (await allColumns(orm)).filter(
(column) => column.tbl === 'users' && ['mfa_backup_codes', 'immich_access_token'].includes(column.name),
);
expect(users.map(({ name, type, notnull, dflt_value }) => ({ name, type, notnull, dflt_value }))).toEqual([
{ name: 'mfa_backup_codes', type: 'TEXT', notnull: 0, dflt_value: null },
{ name: 'immich_access_token', type: 'TEXT', notnull: 0, dflt_value: null },
]);
} finally {
await orm.close();
}
}, 120_000);
});