{"_id":"553d394e9660ba0d00569d0b","category":{"_id":"553d394d9660ba0d00569d09","pages":["553d394e9660ba0d00569d0a","553d394e9660ba0d00569d0b","553d394e9660ba0d00569d0c","553d394e9660ba0d00569d0d","553d394e9660ba0d00569d0e","553d394e9660ba0d00569d0f","553d394e9660ba0d00569d10","553d394e9660ba0d00569d11","553d394e9660ba0d00569d12"],"project":"543467200ef9c00800164ecc","__v":1,"version":"553d394d9660ba0d00569d07","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-15T18:17:35.811Z","from_sync":false,"order":1,"slug":"extensions","title":"EXTENSIONS"},"githubsync":"","project":"543467200ef9c00800164ecc","user":"543466ea0e8e2b0e00341818","__v":0,"version":{"_id":"553d394d9660ba0d00569d07","project":"543467200ef9c00800164ecc","forked_from":"54bedb2fffb79c320030db1f","__v":1,"createdAt":"2015-04-26T19:15:25.245Z","releaseDate":"2015-04-26T19:15:25.245Z","categories":["553d394d9660ba0d00569d08","553d394d9660ba0d00569d09"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"0.11.2","version":"0.11.2"},"metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-15T18:17:55.671Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":4,"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]","excerpt":"","slug":"rdb-migrations","type":"basic","title":"RDB Migrations"}
[block:api-header] { "type": "basic", "title": "glim-migrations" } [/block] glim-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. # Installation [block:code] { "codes": [ { "code": "# install glim-extensions from pip\npip install glim-extensions", "language": "shell" } ] } [/block] # Configuration [block:code] { "codes": [ { "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}", "language": "python" } ] } [/block] # Initializing Migration Extension [block:code] { "codes": [ { "code": "glim migration:init\n# this will create a glim_migrations table using rdb connection\n# also, it will create app/migrations.py file", "language": "shell" } ] } [/block] # Syncing models into db Suppose that you have the following model in your models; [block:code] { "codes": [ { "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", "language": "python" } ] } [/block] You can easily sync with db using following; [block:code] { "codes": [ { "code": "glim migration:sync --name User\n# OR\nglim:migration:sync # syncs all your db models", "language": "shell" } ] } [/block] [block:callout] { "type": "warning", "body": "This command will not sync if db table already exists.", "title": "Won't sync if db table exists" } [/block] # Create a migration [block:code] { "codes": [ { "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()", "language": "python" } ] } [/block] To migrate this run the following; [block:code] { "codes": [ { "code": "glim migration:run --name AddUserMigration\n# output\n# Migrating AddUserMigration..\n# Done.", "language": "shell" } ] } [/block] Optionally, you can migrate all with the following command; [block:code] { "codes": [ { "code": "glim migration:run # migrates all defined migrations", "language": "shell" } ] } [/block] This command will add a sample user & add a row into `glim_migrations` table. # Rollback a migration [block:code] { "codes": [ { "code": "glim migration:rollback --name AddUserMigration\n# output\n# Rolling back AddUserMigration\n# Done.", "language": "shell" } ] } [/block] Optionally, you can rollback all migrations ordered by `created_at desc`; [block:code] { "codes": [ { "code": "glim migration:rollback # rollbacks all migrations", "language": "shell" } ] } [/block] [block:callout] { "type": "warning", "title": "Do not change anything in glim_migrations", "body": "Please do not change anything in glim_migrations table manually if you don't want the migrations extension to crash." } [/block]