Fix migration error when migrating from scratch

This commit is contained in:
rubenwardy 2020-09-01 14:57:03 +01:00
parent 934d581737
commit 5b086bb559
4 changed files with 68 additions and 32 deletions

@ -4,31 +4,78 @@
Content database for Minetest mods, games, and more.\ Content database for Minetest mods, games, and more.\
Developed by rubenwardy, license GPLv3.0+. Developed by rubenwardy, license GPLv3.0+.
## Getting started (debug/dev)
Docker is the recommended way to develop and deploy ContentDB.
1. Install `docker` and `docker-compose`.
sudo apt install docker-ce docker-compose
1. Copy `config.example.cfg` to `config.cfg`.
2. Fill out `config.cfg`
1. Set `SQLALCHEMY_DATABASE_URI` = "postgres://contentdb:password@db:5432/contentdb"
2. Optionally, set the MAIL settings.
3. (Optional) Set up GitHub integration
1. Make a Github OAuth Client at <https://github.com/settings/developers>:
2. Homepage URL - `http://localhost:5123/`
3. Authorization callback URL - `http://localhost:5123/user/github/callback/`
4. Put client id and client secret in `GITHUB_CLIENT_ID` and `GITHUB_CLIENT_SECRET` in config.cfg.
4. Create config.env:
POSTGRES_USER=contentdb
POSTGRES_PASSWORD=password
POSTGRES_DB=contentdb
FLASK_DEBUG=1
5. Start docker images:
docker-compose up --build
6. Setup database:
./utils/run_migrations.sh
8. (Optional) create initial data
1. `./utils/bash.sh`
2. Either `python setup.py -o` or `python setup.py -t`
1. `-o` creates just the admin, and static data like tags, and licenses.
2. `-t` will create test pacakges.
9. View at <http://localhost:5123>.
The admin username is `rubenwardy` and the password is `tuckfrump`.
In the future, starting CDB is as simple as:
docker-compose up --build
To hot/live update CDB whilst it is running, use:
./utils/reload.sh
This will only work with python code and templates, it won't update tasks or config.
## How-tos ## How-tos
Note: you should first read one of the guides on the [Github repo wiki](https://github.com/minetest/contentdb/wiki)
```sh ```sh
# Run celery worker
FLASK_CONFIG=../config.cfg celery -A app.tasks.celery worker
# if sqlite
python utils/setup.py -t
rm db.sqlite && python setup.py -t && FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db stamp head
# Create migration
FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db migrate
# Run migration
FLASK_CONFIG=../config.cfg FLASK_APP=app/__init__.py flask db upgrade
# Enter docker
docker exec -it contentdb_app_1 bash
# Hot/live reload (only works with FLASK_DEBUG=1) # Hot/live reload (only works with FLASK_DEBUG=1)
./utils/reload.sh ./utils/reload.sh
# Cold update a running version of CDB with minimal downtime # Cold update a running version of CDB with minimal downtime (production)
./utils/update.sh ./utils/update.sh
# Enter docker
./utils/bash.sh
# Run migrations
./utils/run_migrations.sh
# Create new migration
./utils/create_migration.sh
``` ```
## Database ## Database

@ -1,11 +1,11 @@
USER_APP_NAME = "ContentDB" USER_APP_NAME = "ContentDB"
SERVER_NAME = "content.minetest.net" SERVER_NAME = "localhost:5123"
BASE_URL = "http://" + SERVER_NAME BASE_URL = "http://" + SERVER_NAME
SECRET_KEY = "" SECRET_KEY = ""
WTF_CSRF_SECRET_KEY = "" WTF_CSRF_SECRET_KEY = ""
SQLALCHEMY_DATABASE_URI = "sqlite:///../db.sqlite" SQLALCHEMY_DATABASE_URI = "postgres://contentdb:password@db:5432/contentdb"
SQLALCHEMY_TRACK_MODIFICATIONS = False SQLALCHEMY_TRACK_MODIFICATIONS = False
GITHUB_CLIENT_ID = "" GITHUB_CLIENT_ID = ""

@ -21,16 +21,6 @@ depends_on = None
def upgrade(): def upgrade():
op.add_column('package', sa.Column('downloads', sa.Integer(), nullable=False, server_default="0")) op.add_column('package', sa.Column('downloads', sa.Integer(), nullable=False, server_default="0"))
bind = op.get_bind()
session = orm.Session(bind=bind)
for package in session.query(Package).all():
downloads_result = session.query(func.sum(PackageRelease.downloads)).filter_by(package_id=package.id).one_or_none()
downloads = 0 if not downloads_result or not downloads_result[0] else downloads_result[0]
package.downloads = downloads
session.commit()
def downgrade(): def downgrade():
# ### commands auto generated by Alembic - please adjust! ### # ### commands auto generated by Alembic - please adjust! ###

@ -31,8 +31,7 @@ def upgrade():
bind = op.get_bind() bind = op.get_bind()
session = orm.Session(bind=bind) session = orm.Session(bind=bind)
for package in session.query(Package).all(): op.execute('INSERT INTO maintainers (package_id, user_id) SELECT id, author_id FROM package;')
package.maintainers.append(package.author)
session.commit() session.commit()