-- NOTE: This file is auto generated by ./sql-generator

-- SyncRepository.album.getCreatedAfter
select
  "albumsId" as "id",
  "createId"
from
  "albums_shared_users_users"
where
  "usersId" = $1
  and "createId" >= $2
  and "createdAt" < now() - interval '1 millisecond'
order by
  "createId" asc

-- SyncRepository.album.getDeletes
select
  "id",
  "albumId"
from
  "albums_audit"
where
  "userId" = $1
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.album.getUpserts
select distinct
  on ("albums"."id", "albums"."updateId") "albums"."id",
  "albums"."ownerId",
  "albums"."albumName" as "name",
  "albums"."description",
  "albums"."createdAt",
  "albums"."updatedAt",
  "albums"."albumThumbnailAssetId" as "thumbnailAssetId",
  "albums"."isActivityEnabled",
  "albums"."order",
  "albums"."updateId"
from
  "albums"
  left join "albums_shared_users_users" as "album_users" on "albums"."id" = "album_users"."albumsId"
where
  "albums"."updatedAt" < now() - interval '1 millisecond'
  and (
    "albums"."ownerId" = $1
    or "album_users"."usersId" = $2
  )
order by
  "albums"."updateId" asc

-- SyncRepository.albumAsset.getBackfill
select
  "assets"."id",
  "assets"."ownerId",
  "assets"."originalFileName",
  "assets"."thumbhash",
  "assets"."checksum",
  "assets"."fileCreatedAt",
  "assets"."fileModifiedAt",
  "assets"."localDateTime",
  "assets"."type",
  "assets"."deletedAt",
  "assets"."isFavorite",
  "assets"."visibility",
  "assets"."duration",
  "assets"."updateId"
from
  "assets"
  inner join "albums_assets_assets" as "album_assets" on "album_assets"."assetsId" = "assets"."id"
where
  "album_assets"."albumsId" = $1
  and "assets"."updatedAt" < now() - interval '1 millisecond'
  and "assets"."updateId" <= $2
  and "assets"."updateId" >= $3
order by
  "assets"."updateId" asc

-- SyncRepository.albumAsset.getUpserts
select
  "assets"."id",
  "assets"."ownerId",
  "assets"."originalFileName",
  "assets"."thumbhash",
  "assets"."checksum",
  "assets"."fileCreatedAt",
  "assets"."fileModifiedAt",
  "assets"."localDateTime",
  "assets"."type",
  "assets"."deletedAt",
  "assets"."isFavorite",
  "assets"."visibility",
  "assets"."duration",
  "assets"."updateId"
from
  "assets"
  inner join "albums_assets_assets" as "album_assets" on "album_assets"."assetsId" = "assets"."id"
  inner join "albums" on "albums"."id" = "album_assets"."albumsId"
  left join "albums_shared_users_users" as "album_users" on "album_users"."albumsId" = "album_assets"."albumsId"
where
  "assets"."updatedAt" < now() - interval '1 millisecond'
  and (
    "albums"."ownerId" = $1
    or "album_users"."usersId" = $2
  )
order by
  "assets"."updateId" asc

-- SyncRepository.albumAssetExif.getBackfill
select
  "exif"."assetId",
  "exif"."description",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "exif"."fileSizeInByte",
  "exif"."orientation",
  "exif"."dateTimeOriginal",
  "exif"."modifyDate",
  "exif"."timeZone",
  "exif"."latitude",
  "exif"."longitude",
  "exif"."projectionType",
  "exif"."city",
  "exif"."state",
  "exif"."country",
  "exif"."make",
  "exif"."model",
  "exif"."lensModel",
  "exif"."fNumber",
  "exif"."focalLength",
  "exif"."iso",
  "exif"."exposureTime",
  "exif"."profileDescription",
  "exif"."rating",
  "exif"."fps",
  "exif"."updateId"
from
  "exif"
  inner join "albums_assets_assets" as "album_assets" on "album_assets"."assetsId" = "exif"."assetId"
where
  "album_assets"."albumsId" = $1
  and "exif"."updatedAt" < now() - interval '1 millisecond'
  and "exif"."updateId" <= $2
  and "exif"."updateId" >= $3
order by
  "exif"."updateId" asc

-- SyncRepository.albumAssetExif.getUpserts
select
  "exif"."assetId",
  "exif"."description",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "exif"."fileSizeInByte",
  "exif"."orientation",
  "exif"."dateTimeOriginal",
  "exif"."modifyDate",
  "exif"."timeZone",
  "exif"."latitude",
  "exif"."longitude",
  "exif"."projectionType",
  "exif"."city",
  "exif"."state",
  "exif"."country",
  "exif"."make",
  "exif"."model",
  "exif"."lensModel",
  "exif"."fNumber",
  "exif"."focalLength",
  "exif"."iso",
  "exif"."exposureTime",
  "exif"."profileDescription",
  "exif"."rating",
  "exif"."fps",
  "exif"."updateId"
from
  "exif"
  inner join "albums_assets_assets" as "album_assets" on "album_assets"."assetsId" = "exif"."assetId"
  inner join "albums" on "albums"."id" = "album_assets"."albumsId"
  left join "albums_shared_users_users" as "album_users" on "album_users"."albumsId" = "album_assets"."albumsId"
where
  "exif"."updatedAt" < now() - interval '1 millisecond'
  and (
    "albums"."ownerId" = $1
    or "album_users"."usersId" = $2
  )
order by
  "exif"."updateId" asc

-- SyncRepository.albumToAsset.getBackfill
select
  "album_assets"."assetsId" as "assetId",
  "album_assets"."albumsId" as "albumId",
  "album_assets"."updateId"
from
  "albums_assets_assets" as "album_assets"
where
  "album_assets"."albumsId" = $1
  and "album_assets"."updatedAt" < now() - interval '1 millisecond'
  and "album_assets"."updateId" <= $2
  and "album_assets"."updateId" >= $3
order by
  "album_assets"."updateId" asc

-- SyncRepository.albumToAsset.getDeletes
select
  "id",
  "assetId",
  "albumId"
from
  "album_assets_audit"
where
  "albumId" in (
    select
      "id"
    from
      "albums"
    where
      "ownerId" = $1
    union
    (
      select
        "albumUsers"."albumsId" as "id"
      from
        "albums_shared_users_users" as "albumUsers"
      where
        "albumUsers"."usersId" = $2
    )
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.albumToAsset.getUpserts
select
  "album_assets"."assetsId" as "assetId",
  "album_assets"."albumsId" as "albumId",
  "album_assets"."updateId"
from
  "albums_assets_assets" as "album_assets"
  inner join "albums" on "albums"."id" = "album_assets"."albumsId"
  left join "albums_shared_users_users" as "album_users" on "album_users"."albumsId" = "album_assets"."albumsId"
where
  "album_assets"."updatedAt" < now() - interval '1 millisecond'
  and (
    "albums"."ownerId" = $1
    or "album_users"."usersId" = $2
  )
order by
  "album_assets"."updateId" asc

-- SyncRepository.albumUser.getBackfill
select
  "album_users"."albumsId" as "albumId",
  "album_users"."usersId" as "userId",
  "album_users"."role",
  "album_users"."updateId"
from
  "albums_shared_users_users" as "album_users"
where
  "albumsId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
  and "updateId" <= $2
  and "updateId" >= $3
order by
  "updateId" asc

-- SyncRepository.albumUser.getDeletes
select
  "id",
  "userId",
  "albumId"
from
  "album_users_audit"
where
  "albumId" in (
    select
      "id"
    from
      "albums"
    where
      "ownerId" = $1
    union
    (
      select
        "albumUsers"."albumsId" as "id"
      from
        "albums_shared_users_users" as "albumUsers"
      where
        "albumUsers"."usersId" = $2
    )
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.albumUser.getUpserts
select
  "album_users"."albumsId" as "albumId",
  "album_users"."usersId" as "userId",
  "album_users"."role",
  "album_users"."updateId"
from
  "albums_shared_users_users" as "album_users"
where
  "album_users"."updatedAt" < now() - interval '1 millisecond'
  and "album_users"."albumsId" in (
    select
      "id"
    from
      "albums"
    where
      "ownerId" = $1
    union
    (
      select
        "albumUsers"."albumsId" as "id"
      from
        "albums_shared_users_users" as "albumUsers"
      where
        "albumUsers"."usersId" = $2
    )
  )
order by
  "album_users"."updateId" asc

-- SyncRepository.asset.getDeletes
select
  "id",
  "assetId"
from
  "assets_audit"
where
  "ownerId" = $1
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.asset.getUpserts
select
  "assets"."id",
  "assets"."ownerId",
  "assets"."originalFileName",
  "assets"."thumbhash",
  "assets"."checksum",
  "assets"."fileCreatedAt",
  "assets"."fileModifiedAt",
  "assets"."localDateTime",
  "assets"."type",
  "assets"."deletedAt",
  "assets"."isFavorite",
  "assets"."visibility",
  "assets"."duration",
  "assets"."updateId"
from
  "assets"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.assetExif.getUpserts
select
  "exif"."assetId",
  "exif"."description",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "exif"."fileSizeInByte",
  "exif"."orientation",
  "exif"."dateTimeOriginal",
  "exif"."modifyDate",
  "exif"."timeZone",
  "exif"."latitude",
  "exif"."longitude",
  "exif"."projectionType",
  "exif"."city",
  "exif"."state",
  "exif"."country",
  "exif"."make",
  "exif"."model",
  "exif"."lensModel",
  "exif"."fNumber",
  "exif"."focalLength",
  "exif"."iso",
  "exif"."exposureTime",
  "exif"."profileDescription",
  "exif"."rating",
  "exif"."fps",
  "exif"."updateId"
from
  "exif"
where
  "assetId" in (
    select
      "id"
    from
      "assets"
    where
      "ownerId" = $1
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.memory.getDeletes
select
  "id",
  "memoryId"
from
  "memories_audit"
where
  "userId" = $1
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.memory.getUpserts
select
  "id",
  "createdAt",
  "updatedAt",
  "deletedAt",
  "ownerId",
  "type",
  "data",
  "isSaved",
  "memoryAt",
  "seenAt",
  "showAt",
  "hideAt",
  "updateId"
from
  "memories"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.memoryToAsset.getDeletes
select
  "id",
  "memoryId",
  "assetId"
from
  "memory_assets_audit"
where
  "memoryId" in (
    select
      "id"
    from
      "memories"
    where
      "ownerId" = $1
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.memoryToAsset.getUpserts
select
  "memoriesId" as "memoryId",
  "assetsId" as "assetId",
  "updateId"
from
  "memories_assets_assets"
where
  "memoriesId" in (
    select
      "id"
    from
      "memories"
    where
      "ownerId" = $1
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.partner.getCreatedAfter
select
  "sharedById",
  "createId"
from
  "partners"
where
  "sharedWithId" = $1
  and "createId" >= $2
  and "createdAt" < now() - interval '1 millisecond'
order by
  "partners"."createId" asc

-- SyncRepository.partner.getDeletes
select
  "id",
  "sharedById",
  "sharedWithId"
from
  "partners_audit"
where
  (
    "sharedById" = $1
    or "sharedWithId" = $2
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.partner.getUpserts
select
  "sharedById",
  "sharedWithId",
  "inTimeline",
  "updateId"
from
  "partners"
where
  (
    "sharedById" = $1
    or "sharedWithId" = $2
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.partnerAsset.getBackfill
select
  "assets"."id",
  "assets"."ownerId",
  "assets"."originalFileName",
  "assets"."thumbhash",
  "assets"."checksum",
  "assets"."fileCreatedAt",
  "assets"."fileModifiedAt",
  "assets"."localDateTime",
  "assets"."type",
  "assets"."deletedAt",
  "assets"."isFavorite",
  "assets"."visibility",
  "assets"."duration",
  "assets"."updateId"
from
  "assets"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
  and "updateId" <= $2
  and "updateId" >= $3
order by
  "updateId" asc

-- SyncRepository.partnerAsset.getDeletes
select
  "id",
  "assetId"
from
  "assets_audit"
where
  "ownerId" in (
    select
      "sharedById"
    from
      "partners"
    where
      "sharedWithId" = $1
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.partnerAsset.getUpserts
select
  "assets"."id",
  "assets"."ownerId",
  "assets"."originalFileName",
  "assets"."thumbhash",
  "assets"."checksum",
  "assets"."fileCreatedAt",
  "assets"."fileModifiedAt",
  "assets"."localDateTime",
  "assets"."type",
  "assets"."deletedAt",
  "assets"."isFavorite",
  "assets"."visibility",
  "assets"."duration",
  "assets"."updateId"
from
  "assets"
where
  "ownerId" in (
    select
      "sharedById"
    from
      "partners"
    where
      "sharedWithId" = $1
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.partnerAssetExif.getBackfill
select
  "exif"."assetId",
  "exif"."description",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "exif"."fileSizeInByte",
  "exif"."orientation",
  "exif"."dateTimeOriginal",
  "exif"."modifyDate",
  "exif"."timeZone",
  "exif"."latitude",
  "exif"."longitude",
  "exif"."projectionType",
  "exif"."city",
  "exif"."state",
  "exif"."country",
  "exif"."make",
  "exif"."model",
  "exif"."lensModel",
  "exif"."fNumber",
  "exif"."focalLength",
  "exif"."iso",
  "exif"."exposureTime",
  "exif"."profileDescription",
  "exif"."rating",
  "exif"."fps",
  "exif"."updateId"
from
  "exif"
  inner join "assets" on "assets"."id" = "exif"."assetId"
where
  "assets"."ownerId" = $1
  and "exif"."updatedAt" < now() - interval '1 millisecond'
  and "exif"."updateId" <= $2
  and "exif"."updateId" >= $3
order by
  "exif"."updateId" asc

-- SyncRepository.partnerAssetExif.getUpserts
select
  "exif"."assetId",
  "exif"."description",
  "exif"."exifImageWidth",
  "exif"."exifImageHeight",
  "exif"."fileSizeInByte",
  "exif"."orientation",
  "exif"."dateTimeOriginal",
  "exif"."modifyDate",
  "exif"."timeZone",
  "exif"."latitude",
  "exif"."longitude",
  "exif"."projectionType",
  "exif"."city",
  "exif"."state",
  "exif"."country",
  "exif"."make",
  "exif"."model",
  "exif"."lensModel",
  "exif"."fNumber",
  "exif"."focalLength",
  "exif"."iso",
  "exif"."exposureTime",
  "exif"."profileDescription",
  "exif"."rating",
  "exif"."fps",
  "exif"."updateId"
from
  "exif"
where
  "assetId" in (
    select
      "id"
    from
      "assets"
    where
      "ownerId" in (
        select
          "sharedById"
        from
          "partners"
        where
          "sharedWithId" = $1
      )
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.partnerStack.getDeletes
select
  "id",
  "stackId"
from
  "stacks_audit"
where
  "userId" in (
    select
      "sharedById"
    from
      "partners"
    where
      "sharedWithId" = $1
  )
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.partnerStack.getBackfill
select
  "asset_stack"."id",
  "asset_stack"."createdAt",
  "asset_stack"."updatedAt",
  "asset_stack"."primaryAssetId",
  "asset_stack"."ownerId",
  "updateId"
from
  "asset_stack"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
  and "updateId" <= $2
  and "updateId" >= $3
order by
  "updateId" asc

-- SyncRepository.partnerStack.getUpserts
select
  "asset_stack"."id",
  "asset_stack"."createdAt",
  "asset_stack"."updatedAt",
  "asset_stack"."primaryAssetId",
  "asset_stack"."ownerId",
  "updateId"
from
  "asset_stack"
where
  "ownerId" in (
    select
      "sharedById"
    from
      "partners"
    where
      "sharedWithId" = $1
  )
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.people.getDeletes
select
  "id",
  "personId"
from
  "person_audit"
where
  "ownerId" = $1
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.people.getUpserts
select
  "id",
  "createdAt",
  "updatedAt",
  "ownerId",
  "name",
  "birthDate",
  "thumbnailPath",
  "isHidden",
  "isFavorite",
  "color",
  "updateId",
  "faceAssetId"
from
  "person"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.stack.getDeletes
select
  "id",
  "stackId"
from
  "stacks_audit"
where
  "userId" = $1
  and "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.stack.getUpserts
select
  "asset_stack"."id",
  "asset_stack"."createdAt",
  "asset_stack"."updatedAt",
  "asset_stack"."primaryAssetId",
  "asset_stack"."ownerId",
  "updateId"
from
  "asset_stack"
where
  "ownerId" = $1
  and "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc

-- SyncRepository.user.getDeletes
select
  "id",
  "userId"
from
  "users_audit"
where
  "deletedAt" < now() - interval '1 millisecond'
order by
  "id" asc

-- SyncRepository.user.getUpserts
select
  "id",
  "name",
  "email",
  "deletedAt",
  "updateId"
from
  "users"
where
  "updatedAt" < now() - interval '1 millisecond'
order by
  "updateId" asc
