CREATE TABLE module_as_entry_old (
    id            TEXT PRIMARY KEY NOT NULL,
    module_id     TEXT NOT NULL REFERENCES module (id) ON DELETE CASCADE,
    asn           INTEGER,
    prefix        TEXT,
    community_id  TEXT REFERENCES bgp_community (id) ON DELETE SET NULL,
    created_at    TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    updated_at    TEXT NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ', 'now')),
    CHECK (asn IS NOT NULL OR prefix IS NOT NULL)
);

INSERT INTO module_as_entry_old (id, module_id, asn, prefix, community_id, created_at, updated_at)
SELECT id, module_id, asn, NULL, community_id, created_at, updated_at FROM module_as_entry;

DROP TABLE module_as_entry;
ALTER TABLE module_as_entry_old RENAME TO module_as_entry;

CREATE INDEX idx_module_as_entry_module ON module_as_entry (module_id);
