{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"never","results":{"codes":[]},"params":[]},"next":{"description":"","pages":[]},"title":"RDB Migrations","type":"basic","slug":"rdb-migrations","excerpt":"","body":"[block:api-header]\n{\n \"type\": \"basic\",\n \"title\": \"glim-migrations\"\n}\n[/block]\nglim-migrations is an extension of glim framework for bringing up rdb migrations. An rdb migrations can be defined as a change in the database of an application.\n\n# Installation\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"# install glim-extensions from pip\\npip install glim-extensions\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\n# Configuration\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"config = {\\n 'extensions' : {\\n 'migration' : {\\n 'db' : 'default' # the connection alias\\n }\\n },\\n\\n 'db' : {\\n 'default' : {\\n 'driver' : 'mysql',\\n 'host' : 'localhost',\\n 'schema' : 'test',\\n 'user' : 'root',\\n 'password' : ''\\n }\\n }\\n\\n # it is required to have at least one db connection\\n}\",\n \"language\": \"python\"\n }\n ]\n}\n[/block]\n# Initializing Migration Extension\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:init\\n# this will create a glim_migrations table using rdb connection\\n# also, it will create app/migrations.py file\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\n# Syncing models into db\nSuppose that you have the following model in your models;\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"# app/models.py\\nfrom glim.db import Model\\nfrom sqlalchemy import Column, Integer, String\\n\\nclass User(Model):\\n __tablename__ = 'users'\\n id = Column(Integer, primary_key=True)\\n name = Column(String(255))\\n fullname = Column(String(255))\\n password = Column(String(255))\\n\",\n \"language\": \"python\"\n }\n ]\n}\n[/block]\nYou can easily sync with db using following;\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:sync --name User\\n# OR\\nglim:migration:sync # syncs all your db models\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\n\n[block:callout]\n{\n \"type\": \"warning\",\n \"body\": \"This command will not sync if db table already exists.\",\n \"title\": \"Won't sync if db table exists\"\n}\n[/block]\n# Create a migration\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"# app/migrations.py\\nfrom app.models import User\\nfrom ext.migration.migration import Migration\\n\\nclass AddUserMigration(Migration):\\n\\n description = 'creates a sample user in users table'\\n\\n def run(self):\\n user = User(id=5, name='Aras')\\n self.orm.add(user)\\n self.orm.commit()\\n return True\\n\\n def rollback(self):\\n user = self.orm.query(User).filter_by(id=5).first()\\n if user is not None:\\n self.orm.delete(user)\\n self.orm.commit()\",\n \"language\": \"python\"\n }\n ]\n}\n[/block]\nTo migrate this run the following;\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:run --name AddUserMigration\\n# output\\n# Migrating AddUserMigration..\\n# Done.\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\nOptionally, you can migrate all with the following command;\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:run # migrates all defined migrations\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\nThis command will add a sample user & add a row into `glim_migrations` table.\n\n# Rollback a migration\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:rollback --name AddUserMigration\\n# output\\n# Rolling back AddUserMigration\\n# Done.\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\nOptionally, you can rollback all migrations ordered by `created_at desc`;\n[block:code]\n{\n \"codes\": [\n {\n \"code\": \"glim migration:rollback # rollbacks all migrations\",\n \"language\": \"shell\"\n }\n ]\n}\n[/block]\n\n[block:callout]\n{\n \"type\": \"warning\",\n \"title\": \"Do not change anything in glim_migrations\",\n \"body\": \"Please do not change anything in glim_migrations table manually if you don't want the migrations extension to crash.\"\n}\n[/block]","updates":[],"order":4,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"553d394e9660ba0d00569d0b","category":{"sync":{"isSync":false,"url":""},"pages":["553d394e9660ba0d00569d0a","553d394e9660ba0d00569d0b","553d394e9660ba0d00569d0c","553d394e9660ba0d00569d0d","553d394e9660ba0d00569d0e","553d394e9660ba0d00569d0f","553d394e9660ba0d00569d10","553d394e9660ba0d00569d11","553d394e9660ba0d00569d12"],"title":"EXTENSIONS","slug":"extensions","order":1,"from_sync":false,"reference":false,"_id":"553d394d9660ba0d00569d09","project":"543467200ef9c00800164ecc","__v":1,"version":"553d394d9660ba0d00569d07","createdAt":"2014-10-15T18:17:35.811Z"},"githubsync":"","createdAt":"2014-10-15T18:17:55.671Z","project":"543467200ef9c00800164ecc","user":"543466ea0e8e2b0e00341818","__v":0,"version":{"version":"0.11.2","version_clean":"0.11.2","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["553d394d9660ba0d00569d08","553d394d9660ba0d00569d09"],"_id":"553d394d9660ba0d00569d07","project":"543467200ef9c00800164ecc","forked_from":"54bedb2fffb79c320030db1f","__v":1,"createdAt":"2015-04-26T19:15:25.245Z","releaseDate":"2015-04-26T19:15:25.245Z"}}