Add lens make/model to db w/ migrations

This commit is contained in:
Sam Becker 2024-06-06 11:44:21 -05:00
parent 668f529ff0
commit d8d6c68fc8
3 changed files with 30 additions and 0 deletions

View File

@ -39,6 +39,8 @@ const createPhotosTable = () =>
model VARCHAR(255),
focal_length SMALLINT,
focal_length_in_35mm_format SMALLINT,
lens_make VARCHAR(255),
lens_model VARCHAR(255),
f_number REAL,
iso SMALLINT,
exposure_time DOUBLE PRECISION,
@ -65,6 +67,15 @@ const runMigration01 = () =>
ADD COLUMN IF NOT EXISTS semantic_description TEXT
`;
// Migration 02
const MIGRATION_FIELDS_02 = ['lens_make', 'lens_model'];
const runMigration02 = () =>
sql`
ALTER TABLE photos
ADD COLUMN IF NOT EXISTS lens_make VARCHAR(255),
ADD COLUMN IF NOT EXISTS lens_model VARCHAR(255)
`;
// Wrapper for most queries for JIT table creation/migration running
const safelyQueryPhotos = async <T>(
callback: () => Promise<T>,
@ -84,6 +95,13 @@ const safelyQueryPhotos = async <T>(
console.log('Running migration 01 ...');
await runMigration01();
result = await callback();
} else if (MIGRATION_FIELDS_02.some(field => new RegExp(
`column "${field}" of relation "photos" does not exist`,
'i',
).test(e.message))) {
console.log('Running migration 02 ...');
await runMigration02();
result = await callback();
} else if (/relation "photos" does not exist/i.test(e.message)) {
// If the table does not exist, create it
console.log('Creating photos table ...');
@ -131,6 +149,8 @@ export const insertPhoto = (photo: PhotoDbInsert) =>
model,
focal_length,
focal_length_in_35mm_format,
lens_make,
lens_model,
f_number,
iso,
exposure_time,
@ -158,6 +178,8 @@ export const insertPhoto = (photo: PhotoDbInsert) =>
${photo.model},
${photo.focalLength},
${photo.focalLengthIn35MmFormat},
${photo.lensMake},
${photo.lensModel},
${photo.fNumber},
${photo.iso},
${photo.exposureTime},
@ -188,6 +210,8 @@ export const updatePhoto = (photo: PhotoDbInsert) =>
model=${photo.model},
focal_length=${photo.focalLength},
focal_length_in_35mm_format=${photo.focalLengthIn35MmFormat},
lens_make=${photo.lensMake},
lens_model=${photo.lensModel},
f_number=${photo.fNumber},
iso=${photo.iso},
exposure_time=${photo.exposureTime},

View File

@ -103,6 +103,8 @@ const FORM_METADATA = (
},
focalLength: { label: 'focal length' },
focalLengthIn35MmFormat: { label: 'focal length 35mm-equivalent' },
lensMake: { label: 'lens make' },
lensModel: { label: 'lens model' },
fNumber: { label: 'aperture' },
iso: { label: 'ISO' },
exposureTime: { label: 'exposure time' },
@ -195,6 +197,8 @@ export const convertExifToFormData = (
model: data.tags?.Model,
focalLength: data.tags?.FocalLength?.toString(),
focalLengthIn35MmFormat: data.tags?.FocalLengthIn35mmFormat?.toString(),
lensMake: data.tags?.LensMake,
lensModel: data.tags?.LensModel,
fNumber: data.tags?.FNumber?.toString(),
iso: data.tags?.ISO?.toString(),
exposureTime: data.tags?.ExposureTime?.toString(),

View File

@ -48,6 +48,8 @@ export interface PhotoExif {
model?: string
focalLength?: number
focalLengthIn35MmFormat?: number
lensMake?: string
lensModel?: string
fNumber?: number
iso?: number
exposureTime?: number