Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres identifier 63byte #309

Open
itsumura-h opened this issue Feb 7, 2025 · 0 comments
Open

Postgres identifier 63byte #309

itsumura-h opened this issue Feb 7, 2025 · 0 comments

Comments

@itsumura-h
Copy link
Owner

import strutils, hashes, os

# UTF8 でのバイト数を返す補助関数
proc byteCount(s: string): int =
  return s.encodeUtf8.len

# 識別子を 63 バイト以下に調整する関数
proc adjustIdentifier(id: string): string =
  const maxBytes = 63
  let idBytes = id.encodeUtf8
  if idBytes.len <= maxBytes:
    return id
  else:
    # ハッシュ値(例: SHA-256 の先頭 6 桁の16進文字列)
    let hashSuffix = sha256(id).toHex[0..5]
    # 区切り用の "_" を含めるので、利用可能な長さは maxBytes - (hashSuffix.len + 1)
    let allowedBytes = maxBytes - (hashSuffix.len + 1)
    # 単純に文字単位で切るとバイト数がずれる可能性があるので、
    # UTF8 バイト列として allowedBytes だけ取り出し、そこから再度文字列に戻す(ここは実装依存)
    let truncated = idBytes[0..<allowedBytes].reinterpret(string)
    return truncated & "_" & hashSuffix
let rawIndexName = tableName & "_" & columnName & "_key"
let indexName = adjustIdentifier(rawIndexName)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant