Skip to content

Commit 1f79857

Browse files
authored
Merge pull request #2 from 4GeeksAcademy/data-base
Data base Models
2 parents 648f983 + 5226879 commit 1f79857

File tree

7 files changed

+262
-64
lines changed

7 files changed

+262
-64
lines changed

Pipfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ verify_ssl = true
77

88
[packages]
99
flask = "*"
10-
sqlalchemy = "==1.4.46"
10+
sqlalchemy = "*"
1111
flask-sqlalchemy = "*"
1212
flask-migrate = "*"
1313
flask-swagger = "*"

Pipfile.lock

+63-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

migrations/versions/56991a87e963_.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
"""empty message
2+
3+
Revision ID: 56991a87e963
4+
Revises: 6b562a354979
5+
Create Date: 2025-03-01 15:09:33.474450
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '56991a87e963'
14+
down_revision = '6b562a354979'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
with op.batch_alter_table('goals', schema=None) as batch_op:
22+
batch_op.add_column(sa.Column('projects_id', sa.Integer(), nullable=False))
23+
batch_op.drop_constraint('goals_habits_id_fkey', type_='foreignkey')
24+
batch_op.create_foreign_key(None, 'projects', ['projects_id'], ['id'])
25+
batch_op.drop_column('habits_id')
26+
27+
with op.batch_alter_table('habits', schema=None) as batch_op:
28+
batch_op.add_column(sa.Column('goals_id', sa.Integer(), nullable=False))
29+
batch_op.create_foreign_key(None, 'goals', ['goals_id'], ['id'])
30+
31+
with op.batch_alter_table('notes', schema=None) as batch_op:
32+
batch_op.add_column(sa.Column('projects_id', sa.Integer(), nullable=False))
33+
batch_op.create_foreign_key(None, 'projects', ['projects_id'], ['id'])
34+
35+
with op.batch_alter_table('projects', schema=None) as batch_op:
36+
batch_op.drop_constraint('projects_goals_id_fkey', type_='foreignkey')
37+
batch_op.drop_constraint('projects_notes_id_fkey', type_='foreignkey')
38+
batch_op.drop_constraint('projects_habits_id_fkey', type_='foreignkey')
39+
batch_op.drop_column('notes_id')
40+
batch_op.drop_column('goals_id')
41+
batch_op.drop_column('habits_id')
42+
43+
# ### end Alembic commands ###
44+
45+
46+
def downgrade():
47+
# ### commands auto generated by Alembic - please adjust! ###
48+
with op.batch_alter_table('projects', schema=None) as batch_op:
49+
batch_op.add_column(sa.Column('habits_id', sa.INTEGER(), autoincrement=False, nullable=False))
50+
batch_op.add_column(sa.Column('goals_id', sa.INTEGER(), autoincrement=False, nullable=False))
51+
batch_op.add_column(sa.Column('notes_id', sa.INTEGER(), autoincrement=False, nullable=False))
52+
batch_op.create_foreign_key('projects_habits_id_fkey', 'habits', ['habits_id'], ['id'])
53+
batch_op.create_foreign_key('projects_notes_id_fkey', 'notes', ['notes_id'], ['id'])
54+
batch_op.create_foreign_key('projects_goals_id_fkey', 'goals', ['goals_id'], ['id'])
55+
56+
with op.batch_alter_table('notes', schema=None) as batch_op:
57+
batch_op.drop_constraint(None, type_='foreignkey')
58+
batch_op.drop_column('projects_id')
59+
60+
with op.batch_alter_table('habits', schema=None) as batch_op:
61+
batch_op.drop_constraint(None, type_='foreignkey')
62+
batch_op.drop_column('goals_id')
63+
64+
with op.batch_alter_table('goals', schema=None) as batch_op:
65+
batch_op.add_column(sa.Column('habits_id', sa.INTEGER(), autoincrement=False, nullable=False))
66+
batch_op.drop_constraint(None, type_='foreignkey')
67+
batch_op.create_foreign_key('goals_habits_id_fkey', 'habits', ['habits_id'], ['id'])
68+
batch_op.drop_column('projects_id')
69+
70+
# ### end Alembic commands ###

migrations/versions/6b562a354979_.py

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"""empty message
2+
3+
Revision ID: 6b562a354979
4+
Revises: 0050c23e095d
5+
Create Date: 2025-02-28 11:35:30.713498
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '6b562a354979'
14+
down_revision = '0050c23e095d'
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('habits',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('name', sa.String(length=100), nullable=False),
24+
sa.Column('description', sa.String(length=1000), nullable=False),
25+
sa.Column('ready', sa.Boolean(), nullable=False),
26+
sa.Column('user_id', sa.Integer(), nullable=False),
27+
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
28+
sa.PrimaryKeyConstraint('id')
29+
)
30+
op.create_table('notes',
31+
sa.Column('id', sa.Integer(), nullable=False),
32+
sa.Column('title', sa.String(length=150), nullable=False),
33+
sa.Column('description', sa.String(length=2000), nullable=False),
34+
sa.Column('user_id', sa.Integer(), nullable=False),
35+
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
36+
sa.PrimaryKeyConstraint('id')
37+
)
38+
op.create_table('goals',
39+
sa.Column('id', sa.Integer(), nullable=False),
40+
sa.Column('target', sa.String(length=500), nullable=False),
41+
sa.Column('ready', sa.Boolean(), nullable=False),
42+
sa.Column('user_id', sa.Integer(), nullable=False),
43+
sa.Column('habits_id', sa.Integer(), nullable=False),
44+
sa.ForeignKeyConstraint(['habits_id'], ['habits.id'], ),
45+
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
46+
sa.PrimaryKeyConstraint('id')
47+
)
48+
op.create_table('projects',
49+
sa.Column('id', sa.Integer(), nullable=False),
50+
sa.Column('name', sa.String(length=100), nullable=False),
51+
sa.Column('description', sa.String(length=1500), nullable=False),
52+
sa.Column('user_id', sa.Integer(), nullable=False),
53+
sa.Column('notes_id', sa.Integer(), nullable=False),
54+
sa.Column('habits_id', sa.Integer(), nullable=False),
55+
sa.Column('goals_id', sa.Integer(), nullable=False),
56+
sa.ForeignKeyConstraint(['goals_id'], ['goals.id'], ),
57+
sa.ForeignKeyConstraint(['habits_id'], ['habits.id'], ),
58+
sa.ForeignKeyConstraint(['notes_id'], ['notes.id'], ),
59+
sa.ForeignKeyConstraint(['user_id'], ['user.id'], ),
60+
sa.PrimaryKeyConstraint('id')
61+
)
62+
with op.batch_alter_table('user', schema=None) as batch_op:
63+
batch_op.add_column(sa.Column('name', sa.String(length=120), nullable=False))
64+
65+
# ### end Alembic commands ###
66+
67+
68+
def downgrade():
69+
# ### commands auto generated by Alembic - please adjust! ###
70+
with op.batch_alter_table('user', schema=None) as batch_op:
71+
batch_op.drop_column('name')
72+
73+
op.drop_table('projects')
74+
op.drop_table('goals')
75+
op.drop_table('notes')
76+
op.drop_table('habits')
77+
# ### end Alembic commands ###

migrations/versions/88d24bf205f8_.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""empty message
2+
3+
Revision ID: 88d24bf205f8
4+
Revises: 56991a87e963
5+
Create Date: 2025-03-01 16:59:06.640063
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '88d24bf205f8'
14+
down_revision = '56991a87e963'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
with op.batch_alter_table('goals', schema=None) as batch_op:
22+
batch_op.add_column(sa.Column('description', sa.String(length=1000), nullable=False))
23+
24+
# ### end Alembic commands ###
25+
26+
27+
def downgrade():
28+
# ### commands auto generated by Alembic - please adjust! ###
29+
with op.batch_alter_table('goals', schema=None) as batch_op:
30+
batch_op.drop_column('description')
31+
32+
# ### end Alembic commands ###

src/api/admin.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
import os
33
from flask_admin import Admin
4-
from .models import db, User
4+
from .models import db, User, Notes, Habits, Goals, Projects
55
from flask_admin.contrib.sqla import ModelView
66

77
def setup_admin(app):
@@ -12,6 +12,10 @@ def setup_admin(app):
1212

1313
# Add your models here, for example this is how we add a the User model to the admin
1414
admin.add_view(ModelView(User, db.session))
15+
admin.add_view(ModelView(Notes, db.session))
16+
admin.add_view(ModelView(Habits, db.session))
17+
admin.add_view(ModelView(Goals, db.session))
18+
admin.add_view(ModelView(Projects, db.session))
1519

1620
# You can duplicate that line to add mew models
1721
# admin.add_view(ModelView(YourModelName, db.session))

0 commit comments

Comments
 (0)