On Tue, Aug 8, 2017 at 6:14 AM, Sandeep Srinivasa <san...@gmail.com> wrote: > hi guys, > I'm just taking over a legacy piece of code where tables were hand created > (SQL). > > I'm trying to migrate this over to alembic. I dont have (want to use) any > python models, so this is alembic by itself. > > I'm unsure of two things: > 1. How do I create the first migration - for my first migration, I want to > snapshot the existing structure of the database. Most of the autogenerate > takes place only if there is an existing set of python models. How do I do > this in my case
that is a fantastic question and I don't think anyone has asked that before. So I worked up an example, we should add it to the "Cookbook" documentation. This will print out just the python code you'd stick inside of upgrade() by hand, with some more it could write the whole migration file out at once but I think this will do what you need: from sqlalchemy import Column, Integer, ForeignKey, create_engine, \ String, Index, MetaData from sqlalchemy.ext.declarative import declarative_base from alembic.operations import ops from alembic.autogenerate import render_python_code def make_a_database(engine): Base = declarative_base() class A1(Base): __tablename__ = 'a1' id = Column(Integer, primary_key=True) class A2(Base): __tablename__ = 'a2' id = Column(Integer, primary_key=True) class B(Base): __tablename__ = 'b' id = Column(Integer, primary_key=True) a1_id = Column(ForeignKey('a1.id')) a2_id = Column(ForeignKey('a2.id')) class C(Base): __tablename__ = 'c' foo = Column(String(50), primary_key=True) bar = Column(String(10)) Index("c_bar", C.bar) Base.metadata.create_all(engine) def metadata_to_migration(metadata): migration_script = ops.MigrationScript( 'initial_rev', ops.UpgradeOps( ops=[ ops.CreateTableOp.from_table(table) for table in reflected_metadata.sorted_tables ] + [ ops.CreateIndexOp.from_index(index) for table in reflected_metadata.sorted_tables for index in table.indexes ] ), ops.DowngradeOps(), ) print(render_python_code(migration_script.upgrade_ops)) engine = create_engine("sqlite://") make_a_database(engine) reflected_metadata = MetaData() reflected_metadata.reflect(engine) metadata_to_migration(reflected_metadata) > 2. How do I apply the migration ... without applying the migration. Since I > need alembic to start managing my db, I realize I need to get it to create a > "migrate_version" . However, I cant really apply the migration. How do I do > this ? For this use case you use the "alembic stamp" command, the section at http://alembic.zzzcomputing.com/en/latest/cookbook.html#building-an-up-to-date-database-from-scratch talks about this command. > > Thanks guys! > regards > sandeep > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy-alembic" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy-alembic+unsubscr...@googlegroups.com. > For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alembic+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.