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

User Management backend - Base - DB migrations #46

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,6 @@ _pycache_/

# Python
*.pyc
*.orig
*.pyc.*
simzacks marked this conversation as resolved.
Show resolved Hide resolved
/.cache
17 changes: 0 additions & 17 deletions Vagrantfile
Original file line number Diff line number Diff line change
@@ -1,21 +1,4 @@
db_file_exists = "test -f /vagrant/edison/db.sql"
restore_db = "sudo -u postgres psql edison < /vagrant/edison/db.sql"
db_restored_msg = "echo \"Database restored.\""
db_not_exists_msg = "echo \"db.sql not exists.\""
try_restore_db = "bash -c '#{db_file_exists} && #{restore_db} && #{db_restored_msg} || #{db_not_exists_msg}'"
save_db_data = "sudo -u postgres pg_dump edison > /vagrant/edison/db.sql"

Vagrant.configure("2") do |config|
config.trigger.before :destroy do |trigger|
trigger.info = "Saving database data inside synced folder..."
trigger.run_remote = {inline: "#{save_db_data}"}
end

config.trigger.after :up do |trigger|
trigger.info = "Trying to restore database from /vagrant/edison/db.sql..."
trigger.run_remote = {inline: "#{try_restore_db}"}
end

config.vm.box = "ubuntu/bionic64"
config.vm.provision :shell, path: "setup.sh"
config.vm.network "private_network", type: "dhcp"
Expand Down
2 changes: 2 additions & 0 deletions edison/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
import edison.models

from flask import render_template
from flask_migrate import Migrate


app = edison.app
db = edison.db
migrate = Migrate(app, db)

# Creates all tables defined in the database models and the only ones that are not created yet.
# If there's any change in the database models you should perform a migration to apply this change in the database itself.
Expand Down
Empty file removed edison/db.sql
Empty file.
43 changes: 43 additions & 0 deletions edison/migrations/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
This information and more can be found at - https://flask-migrate.readthedocs.io/

The content of edison/migrations folder is auto-created by Flask-Migrate extension.
Flask-Migrate is an extension that configures Alembic in the proper way to work with Flask and Flask-SQLAlchemy application.
In terms of the actual database migrations, everything is handled by Alembic so you get exactly the same functionality.

FAQ

1. Why to use database migrations ?

Database migrations basically track granular changes to our database schema.
These granular changes are typically reflected as separate scripted files.
Every time we make a change to our database schema we should perform a migration.
This migration creates a new file that describes our current database schema inside edison/migrations/versions.
That way we could downgrade or upgrade our database schema to different versions if needed.

2. When to use database migration ?

You should perform a database migration on any change you make to the database models.
When a migration is performed it applies those changes to the database itself.

3. How to perform a database migration ?

- First, we need to initialize our migrations folder (If it's not already created).
To do so you should first make sure to define the Flask app path inside FLASK_APP environment variable,
and then execute the following command:
flask db init
Executing this command will create the migrations folder inside /home/vagrant/migrations,
which is not good for us because we want it to be created in our project directory.
So we simply need to add the directory flag:
flask db init --directory /vagrant/edison/migrations

- Then, we should perform the migration as follow:
flask db migrate -m "<migration message>" --directory /vagrant/edison/migrations
It will create a new version file of the database schema inside edison/migrations/versions.

- Last, we need to apply this migration on the database itself by executing:
flask db upgrade --directory /vagrant/edison/migrations.

4. How to downgrade the database schema ?

Simply execute the following command:
flask db downgrade --directory /vagrant/edison/migrations.
45 changes: 45 additions & 0 deletions edison/migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
96 changes: 96 additions & 0 deletions edison/migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
from __future__ import with_statement

import logging
from logging.config import fileConfig

from sqlalchemy import engine_from_config
from sqlalchemy import pool

from alembic import context

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option(
'sqlalchemy.url',
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""

# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')

connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool,
)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions edison/migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
Flask==1.1.1
flask-sqlalchemy==2.4.1
psycopg2-binary==2.8.5
Flask-Migrate==2.5.3
pytest==3.3.2
33 changes: 33 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
import psycopg2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to use sqlalchemy to test that the database was created properly, so that your tests are uniform with your code. Either way works the same, one difference is if you switch database engines (one of the benefits of using an ORM) you would have to rewrite this code.



@pytest.fixture
def db_connection():
connection = psycopg2.connect(
host="localhost",
database="edison",
user="postgres",
password="edison")

yield connection
connection.close()

@pytest.fixture
def cursor(db_connection):
cursor = db_connection.cursor()
yield cursor
db_connection.rollback()

@pytest.fixture
def build_users_table(cursor):
cursor.execute("""
INSERT INTO users (username) VALUES (%s);
""",
("ahinoam",))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume you don't want to save this data because its a test. If you did, you would need to call commit


@pytest.fixture
def reset_users_primary_key(cursor):
Copy link
Collaborator Author

@ahinoamta ahinoamta May 19, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @ifireball 😄
I wrote a few tests for checking the DB like you asked me in the last PR.
Can you please help me to find a way to reset the primary key sequence after the tests will be finished?
I tried to enter this fixture in the 'cursor' fixture (right after the 'yield'), but it didn't work, I assume it happened because after the 'yield' the cursor is not available anymore?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why would you want to reset the primary key sequence? It should have no special meaning, so it won't matter if the first user is 1 or 140

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In any case, probably the reason it didn't reset your sequence is because you didn't commit the transaction.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One last comment. The tests that you wrote here verify basic database functionality, which doesn't need to be tested.
What you should probably test is:

  • the database exists
  • you can connect to it
  • the tables you created exist.

Any other tests are really redundant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. I want to reset the primary key in order to clean up my database after the tests and return it to the initial state. I think it will make more sense when the database show you data that begins with id 1.
  2. About the commit after the execute, I used the “cursor.execute” without commit after it but still the query was executed. So why in this case I should add the commit?
    Furthermore, I don’t know where to call this fixture, because if I inject it to “cursor” it will activate it in the beginning of the “cursor” fixture, no?
  3. Ack

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 - the alter sequence statement is correct and it should work. Try to remember, in the lifetime of an app the database sequence will very rarely stay sequential.
2 - Have you tried executing this code and then going into the database using psql and seeing if the data you added was still there? As far as i know pyscopg does not have automated commits. One possibly confusing thing about sequences and transactions is when you get a new sequence that ignores the transaction, so if you insert and then rollback the transaction, the sequence is still at the next number.
On line 20 you have db_connection.rollback() when is this code called? A rollback means erase everything that I've done during this current transaction.

Copy link
Collaborator Author

@ahinoamta ahinoamta May 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried executing this code and then going into the database using psql and seeing if the data you added was still there?

I thought that cursor.execute is enough because when I ran the test def test_username(cursor, build_users_table) it was passed, so the row 'ahinoam' did enter to the DB.
But when I checked it in the DB itself with psql, it didn't appear there (and it's not because db_connection.rollback() because I put it in a comment).
Do you have any idea what is the reason for that?
Maybe it's because I remain in the same transaction?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 20 you have db_connection.rollback() when is this code called? A rollback means erase everything that I've done during this current transaction.

I want it to be called after the cursor will finish his job every test, therefore I put db_connection.rollback() right after the yield cursor statement. But I have to be honest I don't understand the yield statements into their depth...

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way the database works is that during the same transaction everything looks like it is saved, but it really isn't until you call commit.
If you don't manually call commit, it is the same as calling rollback.

cursor.execute("""
ALTER SEQUENCE users_id_seq RESTART;
""")
17 changes: 17 additions & 0 deletions tests/db_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
import psycopg2


def test_users_table_rows_count(cursor, build_users_table):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this cursor defined? The only place I see it is in conftest.py and this script doesn't import that one.
It also might make sense to have one database test file and not split it up.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if I understood the code in the conftest.py correctly, you always call rollback, which erases all of your changes. So if you are expecting the data to be here because of that execution, it might not be.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is this cursor defined? The only place I see it is in conftest.py and this script doesn't import that one.
It also might make sense to have one database test file and not split it up.

The cursor is defined in conftest.py file that aggregate all the fixtures in one place. I tried to keep the db_test.py file short.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, if I understood the code in the conftest.py correctly, you always call rollback, which erases all of your changes. So if you are expecting the data to be here because of that execution, it might not be.

I call rollback after the cursor finish his job so the data still in the DB in this point. This test was passed.

cursor.execute("""
SELECT * FROM users;
""")
result = cursor.fetchall()
assert len(result) == 1

def test_username(cursor, build_users_table):
cursor.execute("""
SELECT username FROM users;
""")
result = cursor.fetchone()
assert result[0] == "ahinoam"
11 changes: 0 additions & 11 deletions tests/init_test.py

This file was deleted.