Skip to content

Migrate all IDs to bigint - #67

Merged
sjha4 merged 1 commit into
Katello:mainfrom
sjha4:bigint
Sep 4, 2026
Merged

sjha4 merged 1 commit into
Katello:mainfrom
sjha4:bigint

Conversation

@sjha4

@sjha4 sjha4 commented Aug 25, 2026

Copy link
Copy Markdown
Member

Migrate all ID fields in DB to bigint.

To test:

Check out branch
Set up a conf to mount repo's lib/ to installed gem's lib/ inside container using https://gist.github.com/sjha4/4b539989d89640109d562e95475173e7#file-smart_proxy_development-md
Restart:

systemctl daemon-reload
systemctl restart foreman-proxy.service

Check all DB table ID fields to make sure those are migrated to bigint.

up do
# SQLite's INTEGER type already supports 64-bit integers, skip for SQLite
# Check database adapter from environment variable set by Database class
db_adapter = ENV['CONTAINER_GATEWAY_DB_ADAPTER'] || ''

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like we get database_type for free:

Suggested change
db_adapter = ENV['CONTAINER_GATEWAY_DB_ADAPTER'] || ''
db_adapter = database_type || ''
     2: require 'pry'
     3: Sequel.migration do
     4:   up do
     5:     # SQLite's INTEGER type already supports 64-bit integers, skip for SQLite
     6:     # Check database adapter from environment variable set by Database class
 =>  7:     binding.pry
     8:     db_adapter = database_type || ''
     9: 
    10:     # Skip for SQLite (adapter will be 'sqlite', 'jdbc:sqlite', etc.)
    11:     unless db_adapter.to_s.downcase.include?('sqlite')
    12:       alter_table(:repositories) do

[1] pry(#<Sequel::SQLite::Database>)> database_type
=> :sqlite

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember rejecting this edit on claude 🙈 ..Will update..

@ianballou ianballou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The migration is working well on a live smart proxy with postgres:

container_gateway=# SELECT
    child_ns.nspname AS fk_schema,
    child_tbl.relname AS fk_table,
    child_attr.attname AS fk_column,
    format_type(child_attr.atttypid, NULL) AS fk_type,
    parent_ns.nspname AS pk_schema,
    parent_tbl.relname AS pk_table,
    parent_attr.attname AS pk_column,
    format_type(parent_attr.atttypid, NULL) AS pk_type,
    CASE 
        WHEN format_type(child_attr.atttypid, NULL) = 'bigint' 
         AND format_type(parent_attr.atttypid, NULL) = 'bigint' 
        THEN 'PASS'
        ELSE 'FAIL'
    END AS status
FROM pg_constraint c
JOIN pg_class child_tbl ON c.conrelid = child_tbl.oid
JOIN pg_namespace child_ns ON child_tbl.relnamespace = child_ns.oid
JOIN pg_class parent_tbl ON c.confrelid = parent_tbl.oid
JOIN pg_namespace parent_ns ON parent_tbl.relnamespace = parent_ns.oid
CROSS JOIN LATERAL unnest(c.conkey, c.confkey) AS k(child_attnum, parent_attnum)
JOIN pg_attribute child_attr ON child_attr.attrelid = child_tbl.oid AND child_attr.attnum = k.child_attnum
JOIN pg_attribute parent_attr ON parent_attr.attrelid = parent_tbl.oid AND parent_attr.attnum = k.parent_attnum
WHERE c.contype = 'f'
  AND child_ns.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY status ASC, fk_schema, fk_table;
 fk_schema |       fk_table        |   fk_column   | fk_type | pk_schema |   pk_table   | pk_column | pk_type | status 
-----------+-----------------------+---------------+---------+-----------+--------------+-----------+---------+--------
 public    | authentication_tokens | user_id       | bigint  | public    | users        | id        | bigint  | PASS
 public    | hosts_repositories    | repository_id | bigint  | public    | repositories | id        | bigint  | PASS
 public    | hosts_repositories    | host_id       | bigint  | public    | hosts        | id        | bigint  | PASS
 public    | repositories_users    | user_id       | bigint  | public    | users        | id        | bigint  | PASS
 public    | repositories_users    | repository_id | bigint  | public    | repositories | id        | bigint  | PASS
(5 rows)

container_gateway=# SELECT
    n.nspname AS schema_name,
    t.relname AS table_name,
    a.attname AS pk_column,
    format_type(a.atttypid, NULL) AS data_type,
    CASE 
        WHEN format_type(a.atttypid, NULL) = 'bigint' THEN 'PASS'
        ELSE 'FAIL'
    END AS status
FROM pg_constraint c
JOIN pg_class t ON c.conrelid = t.oid
JOIN pg_namespace n ON t.relnamespace = n.oid
JOIN pg_attribute a ON a.attrelid = t.oid AND a.attnum = ANY(c.conkey)
WHERE c.contype = 'p'
  AND n.nspname NOT IN ('pg_catalog', 'information_schema')
ORDER BY status ASC, schema_name, table_name;
 schema_name |      table_name       |   pk_column   | data_type | status 
-------------+-----------------------+---------------+-----------+--------
 public      | authentication_tokens | id            | bigint    | PASS
 public      | hosts                 | id            | bigint    | PASS
 public      | hosts_repositories    | host_id       | bigint    | PASS
 public      | hosts_repositories    | repository_id | bigint    | PASS
 public      | repositories          | id            | bigint    | PASS
 public      | repositories_users    | user_id       | bigint    | PASS
 public      | repositories_users    | repository_id | bigint    | PASS
 public      | users                 | id            | bigint    | PASS
(8 rows)

@ianballou ianballou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me!

@sjha4
sjha4 merged commit 1d885cb into Katello:main Sep 4, 2026
11 checks passed
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

Successfully merging this pull request may close these issues.

2 participants