From 343e0a9df021b4d10baca214b43f5cb4377e6c07 Mon Sep 17 00:00:00 2001 From: Chen Du Date: Mon, 10 May 2021 23:30:17 +0800 Subject: [PATCH] #32 Add mysql and pgsql database support --- Dockerfile | 2 +- README.md | 8 ++++++++ app/models.py | 8 ++++---- docker-compose.yml | 3 ++- migrations/versions/7bd1ee1840ca_meta_table.py | 6 +++--- migrations/versions/9ca5901af374_cleanup.py | 4 ++-- migrations/versions/a477f34dbaa4_initial_config.py | 6 +++--- requirements.txt | 4 +++- 8 files changed, 26 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index a6776c5..06f8ae1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,7 @@ WORKDIR /app COPY . . -RUN apk add build-base libffi-dev +RUN apk add build-base libffi-dev postgresql-dev RUN \ cd /app && \ diff --git a/README.md b/README.md index 428ff6b..5d9e6dd 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,14 @@ The recommended way of running is to pull the image from [Docker Hub](https://hu |---|---| | /app/config | Used to store DB and environment variables. This is not needed if you pass in all of the above environment variables. | +#### Database setup +**MySQL** +Set DATABASE_URI connect string as `mysql+pymysql://user:pass@some_mariadb/dbname?charset=utf8mb4` + + +**PostgreSQL** +Set DATABASE_URI connect string as `postgresql+psycopg2://user:pass@host/dbname?client_encoding=utf8` + #### Docker Run By default, the easiest way to get running is: diff --git a/app/models.py b/app/models.py index fb22b01..265cf9b 100644 --- a/app/models.py +++ b/app/models.py @@ -62,9 +62,9 @@ class Meta(db.Model): uuid = db.Column(GUID, primary_key=True, index=True, unique=True, default=lambda: uuid.uuid4()) user_id = db.Column(GUID, db.ForeignKey('user.uuid'), nullable=False) note_id = db.Column(GUID, db.ForeignKey('note.uuid'), nullable=False) - name_encrypted = db.Column('name', db.String) - name_compare = db.Column(db.String) - kind = db.Column(db.String) + name_encrypted = db.Column('name', db.String(512)) + name_compare = db.Column(db.String(512)) + kind = db.Column(db.String(128)) @hybrid_property def name(self): @@ -90,7 +90,7 @@ def serialize(self): class Note(db.Model): uuid = db.Column(GUID, primary_key=True, index=True, unique=True, default=lambda: uuid.uuid4()) user_id = db.Column(GUID, db.ForeignKey('user.uuid'), nullable=False) - data = db.Column(db.String) + data = db.Column(db.Text()) title = db.Column(db.String(128), nullable=False) date = db.Column(db.DateTime(timezone=True), server_default=func.now()) is_date = db.Column(db.Boolean, default=False) diff --git a/docker-compose.yml b/docker-compose.yml index d929bd8..f0486b6 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,4 +10,5 @@ services: - ./dailynotes-volume:/app/config environment: API_SECRET_KEY: CHANGE_THIS_WITH_SECURE_PASSWORD - DB_ENCRYPTION_KEY: CHANGE_THIS_WITH_SECURE_PASSWORD \ No newline at end of file + DB_ENCRYPTION_KEY: CHANGE_THIS_WITH_SECURE_PASSWORD + DATABASE_URI: CHANGE_THIS_WITH_YOUR_DATABASE_URL, OR REMOVE IT IF YOU USE SQLITE \ No newline at end of file diff --git a/migrations/versions/7bd1ee1840ca_meta_table.py b/migrations/versions/7bd1ee1840ca_meta_table.py index f75c804..db99a78 100644 --- a/migrations/versions/7bd1ee1840ca_meta_table.py +++ b/migrations/versions/7bd1ee1840ca_meta_table.py @@ -23,9 +23,9 @@ def upgrade(): sa.Column('uuid', app.model_types.GUID(), nullable=False), sa.Column('user_id', app.model_types.GUID(), nullable=False), sa.Column('note_id', app.model_types.GUID(), nullable=False), - sa.Column('name', sa.String(), nullable=True), - sa.Column('name_compare', sa.String(), nullable=True), - sa.Column('kind', sa.String(), nullable=True), + sa.Column('name', sa.String(128), nullable=True), + sa.Column('name_compare', sa.String(512), nullable=True), + sa.Column('kind', sa.String(128), nullable=True), sa.ForeignKeyConstraint(['note_id'], ['note.uuid'], ), sa.ForeignKeyConstraint(['user_id'], ['user.uuid'], ), sa.PrimaryKeyConstraint('uuid') diff --git a/migrations/versions/9ca5901af374_cleanup.py b/migrations/versions/9ca5901af374_cleanup.py index 652539c..da9873e 100644 --- a/migrations/versions/9ca5901af374_cleanup.py +++ b/migrations/versions/9ca5901af374_cleanup.py @@ -29,7 +29,7 @@ def upgrade(): def downgrade(): # ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('note', schema=None) as batch_op: - batch_op.add_column(sa.Column('tags', sa.VARCHAR(), nullable=True)) - batch_op.add_column(sa.Column('projects', sa.VARCHAR(), nullable=True)) + batch_op.add_column(sa.Column('tags', sa.VARCHAR(128), nullable=True)) + batch_op.add_column(sa.Column('projects', sa.VARCHAR(128), nullable=True)) # ### end Alembic commands ### diff --git a/migrations/versions/a477f34dbaa4_initial_config.py b/migrations/versions/a477f34dbaa4_initial_config.py index 6bfa698..a9e4e12 100644 --- a/migrations/versions/a477f34dbaa4_initial_config.py +++ b/migrations/versions/a477f34dbaa4_initial_config.py @@ -29,10 +29,10 @@ def upgrade(): op.create_index(op.f('ix_user_uuid'), 'user', ['uuid'], unique=True) op.create_table('note', sa.Column('uuid', app.model_types.GUID(), nullable=False), - sa.Column('tags', sa.String(), nullable=True), - sa.Column('projects', sa.String(), nullable=True), + sa.Column('tags', sa.String(128), nullable=True), + sa.Column('projects', sa.String(128), nullable=True), sa.Column('user_id', app.model_types.GUID(), nullable=False), - sa.Column('data', sa.String(), nullable=True), + sa.Column('data', sa.Text(), nullable=True), sa.Column('title', sa.String(length=128), nullable=False), sa.Column('date', sa.DateTime(timezone=True), server_default=sa.text(u'(CURRENT_TIMESTAMP)'), nullable=True), sa.Column('is_date', sa.Boolean(), nullable=True), diff --git a/requirements.txt b/requirements.txt index 44e4823..12b2dc5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ flask-migrate flask-jwt-extended flask-argon2 python-frontmatter -pycrypto \ No newline at end of file +pycrypto +PyMySQL +psycopg2-binary \ No newline at end of file