Skip to content

Commit ea15ce0

Browse files
authored
Merge pull request #1 from enaguero/main
Remove mysql description and dependencies and add postgres documentation
2 parents d093cbe + e9323f8 commit ea15ce0

File tree

8 files changed

+342
-153
lines changed

8 files changed

+342
-153
lines changed

Pipfile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ flask-migrate = "*"
1313
flask-swagger = "*"
1414
psycopg2-binary = "*"
1515
python-dotenv = "*"
16-
mysql-connector-python = "*"
1716
flask-cors = "*"
1817
gunicorn = "*"
19-
mysqlclient = "*"
2018
cloudinary = "*"
2119
flask-admin = "*"
2220

Pipfile.lock

Lines changed: 110 additions & 142 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/DATABASE.md

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,51 @@
1-
# Creating and/or Accessing the MySQL Database
1+
# Database configuration
22

3-
1. Log in to mysql terminal:
3+
In this boilerplate, you can use either postgres or sqlite as database engine. Verify your .env file to specify which one you would like to use.
4+
You can use the env var `DATABASE_URL` for this purpose.
5+
6+
# Creating and/or Accessing the Postgres Database
7+
8+
1. Log in to postgres terminal:
49
```sh
5-
$ mysql
10+
$ psql
611
```
7-
2. Once inside, check if you have the database already created:
12+
2. Once inside, list all the databases and check if you have the database already created:
813
```sql
9-
SHOW databases;
14+
\l
1015
```
16+
Note: If you are using GitPod, check the file `docs/assets/reset_migrations.bash`. Basically, you are creating a database from scratch call `example`.
17+
1118
3. If you don't see the example database create it by typing:
1219
```sql
1320
CREATE DATABASE example;
1421
```
1522
Note: Make sure to update the `DB_CONNECTION_STRING` on the `.env` file with the correct database name.
1623

1724
3. If your database is already created, get inside of it by typing:
25+
26+
*Command*
1827
```sql
19-
USE example;
28+
\c example;
2029
```
21-
4. Now you can type any SQL command you like, for example:
30+
31+
*Result*
2232
```sql
23-
SHOW TABLES;
33+
postgres=# \c example;
34+
You are now connected to database "example" as user "gitpod".
2435
```
25-
Note: type `exit;` to quit.
36+
4. Now you could want to see all the tables availables:
37+
```sql
38+
\dt
39+
```
40+
41+
5. Also you can execute, all the SQL queries you want. For example, assuming you have a `users` table:
42+
```
43+
select * from users;
44+
```
45+
46+
Note: Type `exit` if you want to exit from the postgres terminal.
2647

48+
More commands, you can check this [amazing summary](https://www.postgresqltutorial.com/postgresql-cheat-sheet/).
2749

2850
# Querying data using Python and SQL Alchemy (SELECT)
2951

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

migrations/alembic.ini

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# template used to generate migration files
5+
# file_template = %%(rev)s_%%(slug)s
6+
7+
# set to 'true' to run the environment during
8+
# the 'revision' command, regardless of autogenerate
9+
# revision_environment = false
10+
11+
12+
# Logging configuration
13+
[loggers]
14+
keys = root,sqlalchemy,alembic
15+
16+
[handlers]
17+
keys = console
18+
19+
[formatters]
20+
keys = generic
21+
22+
[logger_root]
23+
level = WARN
24+
handlers = console
25+
qualname =
26+
27+
[logger_sqlalchemy]
28+
level = WARN
29+
handlers =
30+
qualname = sqlalchemy.engine
31+
32+
[logger_alembic]
33+
level = INFO
34+
handlers =
35+
qualname = alembic
36+
37+
[handler_console]
38+
class = StreamHandler
39+
args = (sys.stderr,)
40+
level = NOTSET
41+
formatter = generic
42+
43+
[formatter_generic]
44+
format = %(levelname)-5.5s [%(name)s] %(message)s
45+
datefmt = %H:%M:%S

migrations/env.py

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
from __future__ import with_statement
2+
3+
import logging
4+
from logging.config import fileConfig
5+
6+
from sqlalchemy import engine_from_config
7+
from sqlalchemy import pool
8+
9+
from alembic import context
10+
11+
# this is the Alembic Config object, which provides
12+
# access to the values within the .ini file in use.
13+
config = context.config
14+
15+
# Interpret the config file for Python logging.
16+
# This line sets up loggers basically.
17+
fileConfig(config.config_file_name)
18+
logger = logging.getLogger('alembic.env')
19+
20+
# add your model's MetaData object here
21+
# for 'autogenerate' support
22+
# from myapp import mymodel
23+
# target_metadata = mymodel.Base.metadata
24+
from flask import current_app
25+
config.set_main_option(
26+
'sqlalchemy.url',
27+
str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
28+
target_metadata = current_app.extensions['migrate'].db.metadata
29+
30+
# other values from the config, defined by the needs of env.py,
31+
# can be acquired:
32+
# my_important_option = config.get_main_option("my_important_option")
33+
# ... etc.
34+
35+
36+
def run_migrations_offline():
37+
"""Run migrations in 'offline' mode.
38+
39+
This configures the context with just a URL
40+
and not an Engine, though an Engine is acceptable
41+
here as well. By skipping the Engine creation
42+
we don't even need a DBAPI to be available.
43+
44+
Calls to context.execute() here emit the given string to the
45+
script output.
46+
47+
"""
48+
url = config.get_main_option("sqlalchemy.url")
49+
context.configure(
50+
url=url, target_metadata=target_metadata, literal_binds=True
51+
)
52+
53+
with context.begin_transaction():
54+
context.run_migrations()
55+
56+
57+
def run_migrations_online():
58+
"""Run migrations in 'online' mode.
59+
60+
In this scenario we need to create an Engine
61+
and associate a connection with the context.
62+
63+
"""
64+
65+
# this callback is used to prevent an auto-migration from being generated
66+
# when there are no changes to the schema
67+
# reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
68+
def process_revision_directives(context, revision, directives):
69+
if getattr(config.cmd_opts, 'autogenerate', False):
70+
script = directives[0]
71+
if script.upgrade_ops.is_empty():
72+
directives[:] = []
73+
logger.info('No changes in schema detected.')
74+
75+
connectable = engine_from_config(
76+
config.get_section(config.config_ini_section),
77+
prefix='sqlalchemy.',
78+
poolclass=pool.NullPool,
79+
)
80+
81+
with connectable.connect() as connection:
82+
context.configure(
83+
connection=connection,
84+
target_metadata=target_metadata,
85+
process_revision_directives=process_revision_directives,
86+
**current_app.extensions['migrate'].configure_args
87+
)
88+
89+
with context.begin_transaction():
90+
context.run_migrations()
91+
92+
93+
if context.is_offline_mode():
94+
run_migrations_offline()
95+
else:
96+
run_migrations_online()

migrations/script.py.mako

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""empty message
2+
3+
Revision ID: 30fb1b056366
4+
Revises:
5+
Create Date: 2021-02-10 00:20:37.009332
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '30fb1b056366'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('user',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('email', sa.String(length=120), nullable=False),
24+
sa.Column('password', sa.String(length=80), nullable=False),
25+
sa.Column('is_active', sa.Boolean(), nullable=False),
26+
sa.PrimaryKeyConstraint('id'),
27+
sa.UniqueConstraint('email')
28+
)
29+
# ### end Alembic commands ###
30+
31+
32+
def downgrade():
33+
# ### commands auto generated by Alembic - please adjust! ###
34+
op.drop_table('user')
35+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)