Add migration for Planet
This commit is contained in:
parent
1f98a8875c
commit
38dfe42087
52
alembic/versions/20250325_130505_4bf2ca14_create_planet.py
Normal file
52
alembic/versions/20250325_130505_4bf2ca14_create_planet.py
Normal file
@ -0,0 +1,52 @@
|
||||
"""create planets table
|
||||
|
||||
Revision ID: 1234567890ab
|
||||
Revises:
|
||||
Create Date: 2023-05-25 10:00:00.000000
|
||||
|
||||
"""
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
# revision identifiers, used by Alembic
|
||||
revision = '1234567890ab'
|
||||
down_revision = None
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Create planets table
|
||||
op.create_table(
|
||||
'planets',
|
||||
sa.Column('id', sa.Integer, primary_key=True, index=True),
|
||||
sa.Column('name', sa.String, unique=True, nullable=False),
|
||||
sa.Column('description', sa.String, nullable=True),
|
||||
sa.Column('diameter', sa.Integer, nullable=False),
|
||||
sa.Column('mass', sa.Integer, nullable=False),
|
||||
sa.Column('distance_from_sun', sa.Integer, nullable=False),
|
||||
sa.Column('created_at', sa.Integer, default=func.now()),
|
||||
sa.Column('updated_at', sa.Integer, default=func.now())
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
# Drop planets table
|
||||
op.drop_table('planets')
|
||||
```
|
||||
|
||||
This Alembic migration script creates a new table named 'planets' with the following columns:
|
||||
|
||||
- `id` (Integer, Primary Key, Indexed)
|
||||
- `name` (String, Unique, Not Nullable)
|
||||
- `description` (String, Nullable)
|
||||
- `diameter` (Integer, Not Nullable)
|
||||
- `mass` (Integer, Not Nullable)
|
||||
- `distance_from_sun` (Integer, Not Nullable)
|
||||
- `created_at` (Integer, Default value from `func.now()`)
|
||||
- `updated_at` (Integer, Default value from `func.now()`)
|
||||
|
||||
The `upgrade()` function creates the table using the `op.create_table()` operation, while the `downgrade()` function drops the table using `op.drop_table()`.
|
||||
|
||||
Note that the revision ID, revision identifiers, and create date are included as comments at the top of the script.
|
Loading…
x
Reference in New Issue
Block a user