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

Restoring a db dump from sqlite to postgres is incomplete #740

Closed
3 of 6 tasks
mjwwit opened this issue Jan 24, 2017 · 8 comments
Closed
3 of 6 tasks

Restoring a db dump from sqlite to postgres is incomplete #740

mjwwit opened this issue Jan 24, 2017 · 8 comments
Assignees
Labels

Comments

@mjwwit
Copy link
Contributor

mjwwit commented Jan 24, 2017

Description

The error in the gist linked above takes place when trying to create a repository after migrating from a sqlite3 database to a postgres database using gitea dump.
The issue is that the sequences aren't set to correct values in the dump. This means that they all start at 0. Any insertions get the next sequence number, 1, which is most likely already taken, causing the conflict.

@lunny lunny added this to the 1.1.0 milestone Jan 24, 2017
@lunny lunny added the type/bug label Jan 24, 2017
@lunny lunny self-assigned this Jan 25, 2017
@mjwwit
Copy link
Contributor Author

mjwwit commented Jan 26, 2017

I'm not sure if this will help, but a sequence in Postgres can be set to the last value present in the table by executing:

SELECT setval('table_id_seq', COALESCE((SELECT MAX(id) FROM table), 1), false);

@lunny
Copy link
Member

lunny commented Feb 9, 2017

I will send a PR to fix this tomorrow.

lunny added a commit to lunny/gitea that referenced this issue Feb 10, 2017
@lunny
Copy link
Member

lunny commented Feb 10, 2017

@mjwwit Please help to confirm #886 resolved this issue.

@lunny lunny closed this as completed in 284c016 Feb 10, 2017
@mjwwit
Copy link
Contributor Author

mjwwit commented Apr 4, 2017

This is still an issue, please re-open.

@lunny lunny reopened this Apr 4, 2017
@mjwwit
Copy link
Contributor Author

mjwwit commented Apr 4, 2017

I've come to realize that my previous fix suggestion doesn't work. In order to fix this issue the following (fixed) statement needs to be run for all sequences:

SELECT setval('table_id_seq', COALESCE((SELECT MAX(id) FROM "table"), 0) + 1, false);

Where table needs to be replaced with the table name.

This will set the sequence value to MAX(id) + 1 for tables with rows and 1 for tables without any rows.

@lunny lunny modified the milestones: 1.2.0, 1.1.0 Apr 4, 2017
@lunny lunny modified the milestones: 1.3.0, 1.2.0 Apr 30, 2017
@lunny
Copy link
Member

lunny commented Apr 30, 2017

The dump command will be deprecated and see #1637

@bkcsoft
Copy link
Member

bkcsoft commented Jun 15, 2017

Migrations between database providers is not supported and never has been (until #1637 is done). Closing

@nodiscc
Copy link
Contributor

nodiscc commented Sep 21, 2020

@mjwwit 's solution seems to have worked for me so far (migration from MySQL to PostgreSQL), gitea 12.4

# get a list of tables
$ sudo -u gitea psql -c '\dt' | cut -d " " -f 4

$ echo $tables
access access_token action attachment collaboration comment commit_status deleted_branch deploy_key email_address email_hash external_login_user follow gpg_key gpg_key_import hook_task issue issue_assignees issue_dependency issue_label issue_user issue_watch label language_stat lfs_lock lfs_meta_object login_source milestone mirror notice notification oauth2_application oauth2_authorization_code oauth2_grant oauth2_session org_user protected_branch public_key pull_request reaction release repo_indexer_status repo_redirect repository repo_topic repo_unit review star stopwatch task team team_repo team_unit team_user topic tracked_time two_factor u2f_registration upload user user_open_id version watch webhook

# update all sequences
$ for table in $tables; do sudo -u gitea psql --echo-all -c "SELECT setval('${table}_id_seq', COALESCE((SELECT MAX(id) FROM \"$table\"), 0) + 1, false);"; done

@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants