cpc / openasip

Open Application-Specific Instruction Set processor tools (OpenASIP)
http://openasip.org
Other
138 stars 41 forks source link

HDB Database has invalid foreign key constrains #212

Closed nrother closed 1 year ago

nrother commented 1 year ago

When creating a new HDB the database has invalid foreign key relations in them. This currently does not cause any error since foreign keys are apparently not checked, but nevertheless makes editing the DB in tools like SQLiteBrowser harder. (Also it somehow defeats the purpose of having the foreign key relations in the first place).

For example the table operation_implementation_variable has the following relation, mentioning the non-existing table operation_implementation_old:

CREATE TABLE "operation_implementation_variable" (
    [...]
    PRIMARY KEY("id"),
    FOREIGN KEY("operation") REFERENCES "operation_implementation_old"("id")
);

It is cause the migration code for version 6:

dbConnection_->updateQuery(std::string(
    "ALTER TABLE operation_implementation RENAME TO "
    "operation_implementation_old"));
dbConnection_->updateQuery(std::string(
    "CREATE TABLE operation_implementation ( "
//...
dbConnection_->updateQuery(std::string(
    "INSERT INTO operation_implementation "
        "(id, name, bus_definition, post_op_vhdl, post_op_verilog, "
        "initial_vhdl, initial_verilog) "
    "SELECT id, name, bus_definition, post_op_vhdl, post_op_verilog, "
           "default_vhdl, default_verilog "
    "FROM operation_implementation_old"));
dbConnection_->updateQuery(std::string(
    "DROP TABLE operation_implementation_old"));

This renames the existing table (which updates the foreign key relation to the new name) and then creates a new table with the old name, moves all the data and drops the old table. This causes the foreign keys to reference the non-existing old table.

The original intention of the code seems to be to rename a column. According to this answer on StackOverflow this is now possible using the ALTER TABLE query.