# Bug OP-14030 - Migration from 12.x to 13.1 fails for installations already having file links in the database 

Project: OpenProject
Status: closed
URL: https://community.openproject.org/work_packages/OP-14030

## Open points

_No work packages_

## Details

| Attribute | Value |
| --- | --- |
| Date |  |
| Priority | High |
| Module |  |
| Target versions | 13.1.2 |
| Observed in versions |  |
| Sprint | 13.1.2 |
| Backlog bucket |  |
| Bug found in version (deprecated) |  |
| Found in mobile app version |  |
| Follow-up feedback e-mail |  |
| Requires doc change | No |
| Accessibility | No |
| Pull preview |  |
| List |  |
| Labels |  |

## People

| Attribute | Value |
| --- | --- |
| Assignee |  |
| Accountable |  |
| Product manager |  |
| Developers |  |
| Designer |  |
| Ordered by |  |
| Signed off by |  |
| Interest by |  |

## Planning

| Attribute | Value |
| --- | --- |
| Relevance |  |

## Costs

| Attribute | Value |
| --- | --- |
| Votes |  |
| Work |  |
| Labor costs |  |

## Description

### Steps to reproduce

1.  Have an OpenProject 12.x instance (or earlier) where file links have been created for at least one work package.
2.  Try to migrate to 13.1

### What is the buggy behavior?

  - Migration fails

<!-- end list -->

``` bash
== 20230731153909 AddFileLinkJournalsToExistingContainers: migrating ==========
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled: (StandardError)

PG::UndefinedTable: ERROR:  relation "meeting_agenda_item_journals" does not exist
LINE 142:   meeting_agenda_item_journals

/opt/openproject/app/services/journals/create_service.rb:96:in `block in create_journal'
/opt/openproject/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/query_cache.rb:79:in `uncached'
/opt/openproject/app/services/journals/create_service.rb:93:in `create_journal'
/opt/openproject/app/services/journals/create_service.rb:57:in `block in call'
/opt/openproject/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:314:in `transaction'
/opt/openproject/vendor/bundle/ruby/3.2.0/gems/activerecord-7.0.8/lib/active_record/transactions.rb:209:in `transaction'
/opt/openproject/app/services/journals/create_service.rb:56:in `call'
/opt/openproject/db/migrate/20230731153909_add_file_link_journals_to_existing_containers.rb:40:in `block in up'
/opt/openproject/db/migrate/20230731153909_add_file_link_journals_to_existing_containers.rb:36:in `each'
/opt/openproject/db/migrate/20230731153909_add_file_link_journals_to_existing_containers.rb:36:in `up'
```

### What is the expected behavior?

1.  Migration runs without problems

### Notes

The problem is caused by a [migration for file links](https://github.com/opf/openproject/blob/release/13.1/db/migrate/20230731153909_add_file_link_journals_to_existing_containers.rb) calling the [`Journals::CreateService`](https://github.com/opf/openproject/blob/dev/app/services/journals/create_service.rb#L719-L746) before the migration creating the [`meeting_agenda_item_journals` table is created](https://github.com/opf/openproject/blob/release/13.1/db/migrate/20231003151656_create_meeting_agenda_item_journals.rb). Ideally the CreateService would not be calling the table at all since the objects that are being migrated can only be `WorkPackage` containers which can never have an agenda item. So the bug also points out the potential for a performance improvement as it is already done for parts of the CreateService [where only journals are tried to be written for associations the journaled object actually has](https://github.com/opf/openproject/blob/release/13.1/app/services/journals/create_service.rb#L229-L246). The same now needs to happen for the [part checking for changes](https://github.com/opf/openproject/blob/release/13.1/app/services/journals/create_service.rb#L614-L637).

### Workaround

The problem can be circumvented by having the faulty migration run later. This can be done by connecting to the openproject console:

`sudo openproject run console`

Then inside the console issue:

`ActiveRecord::Base.connection.execute("INSERT INTO schema_migrations (version) VALUES (20230731153909)")`

Afterwards, the migration can be executed just as always

`sudo openproject configure`

After that has run its course connect to the openproject console again and this time issue:

`ActiveRecord::Base.connection.execute("DELETE FROM schema_migrations WHERE version='20230731153909'")`

Afterwards, execute the migrations again:

`sudo openproject configure`
