"""initial migration Revision ID: 001 Revises: Create Date: 2024-02-11 00:00:00 """ from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql # revision identifiers, used by Alembic. revision = '001' down_revision = None branch_labels = None depends_on = None def upgrade(): # Create users table op.create_table( 'users', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('email', sa.String(length=255), nullable=False), sa.Column('hashed_password', sa.String(length=255), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column('is_active', sa.Boolean(), server_default=sa.true(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('email', name='uq_users_email') ) op.create_index('idx_users_email', 'users', ['email'], unique=True) # Create courses table op.create_table( 'courses', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('title', sa.String(length=255), nullable=False), sa.Column('description', sa.Text(), nullable=False), sa.Column('level', sa.String(length=50), nullable=False), sa.Column('duration', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('author_id', sa.Integer(), nullable=False), sa.Column('is_published', sa.Boolean(), server_default=sa.false(), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.ForeignKeyConstraint(['author_id'], ['users.id'], ondelete='CASCADE') ) op.create_index('idx_courses_author_id', 'courses', ['author_id']) op.create_index('idx_courses_is_published', 'courses', ['is_published']) # Create course_modules table op.create_table( 'course_modules', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('course_id', sa.Integer(), nullable=False), sa.Column('title', sa.String(length=255), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('duration', sa.String(length=100), nullable=True), sa.Column('order_index', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.ForeignKeyConstraint(['course_id'], ['courses.id'], ondelete='CASCADE') ) op.create_index('idx_course_modules_course_id', 'course_modules', ['course_id']) # Create lessons table op.create_table( 'lessons', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('module_id', sa.Integer(), nullable=False), sa.Column('title', sa.String(length=255), nullable=False), sa.Column('description', sa.Text(), nullable=True), sa.Column('order_index', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.ForeignKeyConstraint(['module_id'], ['course_modules.id'], ondelete='CASCADE') ) op.create_index('idx_lessons_module_id', 'lessons', ['module_id']) # Create lesson_steps table op.create_table( 'lesson_steps', sa.Column('id', sa.Integer(), autoincrement=True, nullable=False), sa.Column('lesson_id', sa.Integer(), nullable=False), sa.Column('step_type', sa.String(length=50), nullable=False), sa.Column('title', sa.String(length=255), nullable=True), sa.Column('content', sa.Text(), nullable=True), sa.Column('order_index', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('id'), sa.ForeignKeyConstraint(['lesson_id'], ['lessons.id'], ondelete='CASCADE') ) op.create_index('idx_lesson_steps_lesson_id', 'lesson_steps', ['lesson_id']) # Create favorite_courses table op.create_table( 'favorite_courses', sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('course_id', sa.Integer(), nullable=False), sa.Column('added_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('user_id', 'course_id'), sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['course_id'], ['courses.id'], ondelete='CASCADE') ) # Create progress table op.create_table( 'progress', sa.Column('user_id', sa.Integer(), nullable=False), sa.Column('course_id', sa.Integer(), nullable=False), sa.Column('completed_lessons', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('total_lessons', sa.Integer(), server_default=sa.text('0'), nullable=False), sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.PrimaryKeyConstraint('user_id', 'course_id'), sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'), sa.ForeignKeyConstraint(['course_id'], ['courses.id'], ondelete='CASCADE') ) # Insert seed data # Test user (password: Test1234) op.execute(""" INSERT INTO users (email, hashed_password, is_active) VALUES ('test@example.com', '$2b$12$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW', true) """) # Sample courses op.execute(""" INSERT INTO courses (title, description, level, duration, author_id, is_published) VALUES ('Python Fundamentals', 'Learn the basics of Python programming language including syntax, data types, and control structures.', 'beginner', 20, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('Web Development with FastAPI', 'Build modern web applications using FastAPI framework with Python.', 'intermediate', 30, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('Docker for Developers', 'Master containerization with Docker and Docker Compose for application deployment.', 'intermediate', 25, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('Advanced Algorithms', 'Deep dive into complex algorithms and data structures for technical interviews.', 'advanced', 40, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('JavaScript Essentials', 'Learn JavaScript from scratch including ES6+ features and DOM manipulation.', 'beginner', 25, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('Database Design', 'Learn relational database design principles and SQL optimization techniques.', 'intermediate', 35, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('Machine Learning Basics', 'Introduction to machine learning concepts and practical implementations.', 'advanced', 45, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('DevOps Practices', 'Learn continuous integration, deployment, and infrastructure as code.', 'intermediate', 30, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('React Frontend Development', 'Build modern user interfaces with React library and hooks.', 'intermediate', 35, (SELECT id FROM users WHERE email = 'test@example.com'), true), ('System Architecture', 'Design scalable and maintainable software system architectures.', 'advanced', 50, (SELECT id FROM users WHERE email = 'test@example.com'), true) """) def downgrade(): # Delete seed data first op.execute("DELETE FROM favorite_courses") op.execute("DELETE FROM progress") op.execute("DELETE FROM lesson_steps") op.execute("DELETE FROM lessons") op.execute("DELETE FROM course_modules") op.execute("DELETE FROM courses WHERE author_id = (SELECT id FROM users WHERE email = 'test@example.com')") op.execute("DELETE FROM users WHERE email = 'test@example.com'") # Drop tables op.drop_table('progress') op.drop_table('favorite_courses') op.drop_index('idx_lesson_steps_lesson_id', table_name='lesson_steps') op.drop_table('lesson_steps') op.drop_index('idx_lessons_module_id', table_name='lessons') op.drop_table('lessons') op.drop_index('idx_course_modules_course_id', table_name='course_modules') op.drop_table('course_modules') op.drop_index('idx_courses_is_published', table_name='courses') op.drop_index('idx_courses_author_id', table_name='courses') op.drop_table('courses') op.drop_index('idx_users_email', table_name='users') op.drop_table('users')