{"id":261530,"date":"2015-07-15T17:06:02","date_gmt":"2015-07-15T13:06:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=261530"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=261530","title":{"rendered":"\u0410\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 Peewee"},"content":{"rendered":"<p>             \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0442\u0430\u043a\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0439\u0448\u0435\u0439 ORM, \u043a\u0430\u043a peewee. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043b\u0451\u0433\u043a\u0430\u044f, \u0431\u044b\u0441\u0442\u0440\u0430\u044f, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u0443 Django ORM, \u043e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0442\u0435\u043c SQL \u043a\u043e\u0434\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435.<\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u043d\u0430\u0434 Python \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u043c\u0441\u044f \u0441 \u0411\u0414, \u0432\u044b\u0431\u043e\u0440 \u043f\u0430\u043b \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u0431\u044b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043b\u043b\u0435\u0433\u0438 \u0432 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Django, \u043d\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0434\u0435\u043b\u0430\u043b\u0430 \u0431\u044b application \u0438\u0437\u043b\u0438\u0448\u043d\u0435 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c (\u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e \u0432 \u0435\u0433\u043e requirements \u0438 \u0442\u0430\u043a \u0437\u043d\u0430\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439).<\/p>\n<p>  \u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u0434\u0435\u043b\u044c\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c, \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u043b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0439 \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u043e\u0437\u043d\u0438\u043a \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c migrate. \u041c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 peewee \u0435\u0441\u0442\u044c. \u0438\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043e\u043f\u0438\u0441\u0430\u043d <a href=\"http:\/\/peewee.readthedocs.org\/en\/latest\/peewee\/playhouse.html#migrate\">\u0442\u0443\u0442<\/a>. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u0438 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u2014 \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0410\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u043e \u0447\u0438\u0441\u0442\u044b\u0439, \u0434\u0435\u0432\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u0430\u0433\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0435\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a, \u043a\u0430\u043a \u0437\u0430\u0445\u043e\u0442\u0438\u043c \u0441\u0430\u043c\u0438. \u041f\u043e\u0436\u0430\u043b\u0443\u0439, \u043f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u043d\u0430 \u0443\u043c \u2014 \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0435\u0451 \u043f\u0440\u0438 \u043a\u0430\u0436\u0434\u043e\u043c \u0441\u0442\u0430\u0440\u0442\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f. \u0422\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u043c\u043e\u043c\u0435\u043d\u0442\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0435\u0430\u043a\u0446\u0438\u0438 \u0438\u043b\u0438 \u043c\u043e\u043b\u043d\u0438\u0435\u043d\u043e\u0441\u043d\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u041f\u0435\u0440\u0435\u0434 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043b\u043e\u0433\u0438\u043a\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439, \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u043e\u0431\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e\u0431\u0449\u0438\u0435 \u0443\u0441\u043b\u043e\u0432\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u044b:<\/p>\n<ol>\n<li> \u0421\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435 \u0441 \u0431\u0430\u0437\u043e\u0439 \u0436\u0438\u0432\u0451\u0442 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u043c Borg \u043a\u043b\u0430\u0441\u0441\u0435 \u0441 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u043c \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u043c \u2014 \u0441\u0441\u044b\u043b\u043a\u043e\u0439 \u043d\u0430 \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435.<\/li>\n<li> \u041e\u0431 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0438 \u0444\u0430\u0439\u043b\u043e\u0432 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u043d\u0435 \u0438\u0434\u0451\u0442 \u0438 \u0440\u0435\u0447\u0438. \u0422\u043e\u043b\u044c\u043a\u043e \u043e\u0431 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u043d\u0435\u043d\u0438\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0439.<\/li>\n<li> \u0412\u0442\u043e\u0440\u043e\u0439 \u043f\u0443\u043d\u043a\u0442 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043c \u0434\u0435\u043b\u0430\u0442\u044c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u043a\u0430\u043a \u043c\u044b \u0445\u043e\u0442\u0438\u043c, \u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u043d\u043e\u0432\u043e\u0439 \u0432\u0435\u0440\u0441\u0438\u0438 \u043d\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043b\u044e\u0431\u0443\u044e \u043b\u043e\u0433\u0438\u043a\u0443 \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445.<\/li>\n<\/ol>\n<p>  \u0418\u0442\u0430\u043a, \u043d\u0430\u0447\u043d\u0451\u043c \u0441 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0410\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0434\u043b\u044f \u043c\u0438\u0433\u0440\u0430\u0442\u043e\u0440\u0430<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">import abc from playhouse.migrate import (migrate, MySQLMigrator)  class Migrator(object):     &quot;&quot;&quot;     Migration interface     &quot;&quot;&quot;      __metaclass__ = abc.ABCMeta      connection = db_connection.connection                      # db_connection is a Borg instance     migrator = MySQLMigrator(db_connection.connection)      @abc.abstractproperty     def migrations(self):         &quot;&quot;&quot;         List of the migrations dictionaries         :param self: class instance         :return: list         &quot;&quot;&quot;         return [             {'statement': 1 != 2, 'migration': ['list', 'of', 'migration', 'options'],              'migration_kwargs': {}, 'pre_migrations': list(), 'post_migrations': list()}         ]          # Just an example      def migrate(self):         &quot;&quot;&quot;         Run migrations         &quot;&quot;&quot;         for migration in self.migrations:             if migration['statement']:                 # Run scripts before the migration                 pre_migrations = migration.get('pre_migrations', list())                 for pre_m in pre_migrations:                     pre_m()                 # Migrate                 with db_connection.connection.transaction():                     migration_kwargs = migration.get('migration_kwargs', {})                     migrate(*migration['migration'], **migration_kwargs)                 # Run scripts after the migration                 post_migrations = migration.get('post_migrations', list())                 for post_m in post_migrations:                     post_m() <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043a\u043b\u0430\u0441\u0441 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u0442\u0440\u0451\u0445 \u0447\u0430\u0441\u0442\u0435\u0439: \u043f\u0440\u0435\u0434\u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0451\u043d\u043d\u044b\u0445 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0441\u0441\u044b\u043b\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043a\u043e\u043d\u043d\u0435\u043a\u0442\u043e\u0440 \u0441 \u0411\u0414, \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 migrations \u2014 \u0441\u043f\u0438\u0441\u043a\u0430 \u0441\u043b\u043e\u0432\u0430\u0440\u0435\u0439 \u0434\u043b\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438, \u0430 \u0442\u0430\u043a\u0436\u0435, \u0444-\u0438\u0438 migrate, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442 \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u0441\u0435 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b, \u043f\u0440\u0435\u0434\u0432\u0430\u0440\u044f\u044e\u0449\u0438\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e, \u043f\u043e\u0442\u043e\u043c \u0441\u0430\u043c\u0443 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u044e, \u0430 \u043f\u043e\u0442\u043e\u043c \u0444-\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043a\u0430\u043a\u043e\u0439-\u043d\u0438\u0431\u0443\u0434\u044c \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u0438\u0441\u043a\u0430\u0442\u044c \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0438\u0445.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0431\u0440\u0430\u0437\u0435\u0446 \u0430\u0432\u0442\u043e\u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">import sys import re   def get_migration_modules(packages=[]):     &quot;&quot;&quot;     Get python modules with migrations     :param packages: iterable - list or tuple with packages names for the searching     :return: list - ('module.path', 'module_name')     &quot;&quot;&quot;     # List of the modules to migrate     migration_modules = list()     for pack in packages:         migration_module = __import__(pack, globals(), locals(), fromlist=[str('migrations')])         try:             # Check, that imported object is module             if inspect.ismodule(migration_module.migrations):                 # Find submodules inside the module                 for importer, modname, ispkg in pkgutil.iter_modules(migration_module.migrations.__path__):                     if re.match(r'^\\d{3,}_migration_[\\d\\w_]+$', modname) and not ispkg:                         migration_modules.append((migration_module.migrations.__name__, modname))             # Unregister module             sys.modules.pop(migration_module.__name__)         except AttributeError:             pass     return migration_modules  def get_migration_classes(migration_modules):     &quot;&quot;&quot;     Get list of the migration classes     :type migration_modules: iterable     :param migration_modules: array with a migration modules     :return: list     &quot;&quot;&quot;     migration_classes = list()     for mig_mod, m in migration_modules:         mig = __import__(mig_mod, globals(), locals(), fromlist=[m])         try:             target_module = mig.__getattribute__(m)             # Check, that imported object is module             if inspect.ismodule(target_module):                 for name, obj in inspect.getmembers(target_module):                     # Get all containing elements                     if inspect.isclass(obj) and issubclass(obj, Migrator) and obj != Migrator:                         # Save this elements                         migration_classes.append(obj)             # Remove imported module from the stack             sys.modules.pop(mig.__name__)         except AttributeError:             pass     return migration_classes <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u0442\u0441\u044f \u0430\u0432\u0442\u043e\u043f\u043e\u0438\u0441\u043a \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0410\u0432\u0442\u043e\u043f\u043e\u0438\u0441\u043a \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\"># Get modules with migrations m_mods = get_migration_modules(packages=['package_1', 'package_2', 'package_3']) # Get migration classes m_classes = get_migration_classes(m_mods)  # Execute migrations for m_class in m_classes:     mig = m_class()     mig.migrate() <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0443 r&#8217;^\\d{3,}_migration_[\\d\\w_]+$&#8217; \u2014 \u043e\u043d\u0430 \u043d\u0443\u0436\u043d\u0430 \u0434\u043b\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u043f\u043e \u0448\u0430\u0431\u043b\u043e\u043d\u0443. \u0417\u043d\u0430\u0447\u0438\u0442, \u043f\u043e \u044d\u0442\u043e\u043c\u0443 \u0448\u0430\u0431\u043b\u043e\u043d\u0443 \u043d\u0430\u043c \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0444\u0430\u0439\u043b\u044b \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439.<\/p>\n<p>  \u0412 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 package_1, package_2 \u0438 package_3 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043f\u0430\u043a\u0435\u0442\u044b (\u0441 __init__.py) migrations. \u0418, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 package_1.migrations \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043c\u043e\u0434\u0443\u043b\u044c 001_migration_add_first_fields.py<\/p>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0438\u0437\u043e\u0431\u0440\u0430\u0437\u0438\u0442\u044c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0435 \u044d\u0442\u043e\u0433\u043e \u0444\u0430\u0439\u043b\u0430:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">001_migration_add_first_fields.py<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">from controllers.migrator import Migrator from package_1.models import FirstModel   class AddNewFields(Migrator):     &quot;&quot;&quot;     Append new fields to the FirstModel     &quot;&quot;&quot;      table_name = FirstModel._meta.db_table    # Get name of the table for target model      def __field_not_exists(self):         &quot;&quot;&quot;         Check, that new field does not exists         :return: bool         &quot;&quot;&quot;         q = 'SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_NAME = \\'{0}\\' AND COLUMN_NAME = \\'my_new_field\\''.format(self.table_name)         cursor = self.connection.execute_sql(q)         result = int(cursor.fetchone()[0])         return result == 0      @property     def migrations(self):         return [             # add my_new_field column             {                 'statement': self.__field_not_exists(),                 'migration': [self.migrator.add_column(self.table_name, 'my_new_field', FirstModel.my_new_field)],             }         ] <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u0412\u0441\u0451! \u0422\u0435\u043f\u0435\u0440\u044c, \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 \u0430\u0432\u0442\u043e\u043f\u043e\u0438\u0441\u043a\u0430, \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u043b\u0435\u0440 \u043d\u0430\u0439\u0434\u0451\u0442 AddNewFields \u0438, \u0435\u0441\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f statement, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442 migration.<\/p>\n<p>  \u0412\u043e\u043e\u0431\u0449\u0435, \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u043a\u0440\u0430\u0442\u043a\u043e, \u0438, \u043a\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043d\u044f\u0442\u044c, \u0438\u043c\u0435\u0435\u0442 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u043e\u0439 \u0437\u0430\u043f\u0430\u0441 \u043c\u043e\u0449\u043d\u043e\u0441\u0442\u0438:<\/p>\n<ol>\n<li> \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u0434\u043e \u0438\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 (\u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0431\u044d\u043a\u0430\u043f\u0438\u0442\u044c \u0438 \u0440\u0430\u0437\u0432\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u0431\u044d\u043a\u0430\u043f\u0430).<\/li>\n<li> \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043f\u0440\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439.<\/li>\n<li> \u041c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c NULL \u043f\u043e\u043b\u044f \u0431\u0435\u0437 \u0443\u043a\u0430\u0437\u0430\u043d\u0438\u044f defaults.<\/li>\n<\/ol>\n<p>  \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u044e\u044e \u0444\u0438\u0448\u043a\u0443 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435:<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u0435\u043c def migrations()<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"python\">@property def migrations(self):     # Modify NULL field     my_new_field = FirstModel.my_new_field     my_new_field.default = 'Rewrite me'     return [         # add my_new_field column         {             'statement': self.__field_not_exists(),             'migration': [self.migrator.add_column(self.table_name, 'my_new_field', FirstModel.my_new_field)],         }     ] <\/code><\/pre>\n<\/div>\n<\/div>\n<p>  \u041d\u0443 \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0441\u043b\u0435 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0439 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441\u0444\u043e\u0440\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f, \u043f\u0440\u043e\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0432 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u044b\u0439 \u0441\u043b\u043e\u0432\u0430\u0440\u044c \u044d\u043b\u0435\u043c\u0435\u043d\u0442 &#8216;post_migrations&#8217; \u0441\u043e \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u044f\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044f.             <\/p>\n<div class=\"clear\"><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/262697\/\"> http:\/\/habrahabr.ru\/post\/262697\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>             \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u0442\u044c \u043e \u0442\u0430\u043a\u043e\u0439 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u0435\u0439\u0448\u0435\u0439 ORM, \u043a\u0430\u043a peewee. \u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043b\u0451\u0433\u043a\u0430\u044f, \u0431\u044b\u0441\u0442\u0440\u0430\u044f, \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435, \u0447\u0435\u043c \u0443 Django ORM, \u043e\u0434\u043d\u0430\u043a\u043e \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u043e\u0442\u0435\u043d\u0446\u0438\u0430\u043b\u044c\u043d\u043e \u0441\u043b\u0435\u0434\u0438\u0442\u044c \u0437\u0430 \u0442\u0435\u043c SQL \u043a\u043e\u0434\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0432\u044b\u0445\u043e\u0434\u0435.<\/p>\n<p>  \u041f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u044f \u0440\u0430\u0431\u043e\u0442\u0430\u043b \u043d\u0430\u0434 Python \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435\u043c, \u0441\u043e\u0435\u0434\u0438\u043d\u044f\u044e\u0449\u0438\u043c\u0441\u044f \u0441 \u0411\u0414, \u0432\u044b\u0431\u043e\u0440 \u043f\u0430\u043b \u043d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b\u043e \u0431\u044b \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043e\u0431\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u0414\u043e \u044d\u0442\u043e\u0433\u043e \u043a\u043e\u043b\u043b\u0435\u0433\u0438 \u0432 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0445 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f\u0445 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b\u0438 Django, \u043d\u043e \u0435\u0433\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0434\u0435\u043b\u0430\u043b\u0430 \u0431\u044b application \u0438\u0437\u043b\u0438\u0448\u043d\u0435 \u0433\u0440\u043e\u043c\u043e\u0437\u0434\u043a\u0438\u043c (\u0442\u0435\u043c \u0431\u043e\u043b\u0435\u0435, \u0447\u0442\u043e \u0432 \u0435\u0433\u043e requirements \u0438 \u0442\u0430\u043a \u0437\u043d\u0430\u0447\u0438\u043b\u043e\u0441\u044c \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043c\u043d\u043e\u0433\u043e \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439).<\/p>\n<p>  \u0427\u0435\u0440\u0435\u0437 \u043d\u0435\u0434\u0435\u043b\u044c\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u043d\u0430\u0434 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c, \u0440\u0443\u043a\u043e\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c \u043f\u043e\u043f\u0440\u043e\u0441\u0438\u043b \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0432 \u0431\u0430\u0437\u0443 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u043b\u0435\u0439 \u0438, \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u043e\u0437\u043d\u0438\u043a \u0432\u043e\u043f\u0440\u043e\u0441: \u043a\u0430\u043a \u0434\u0435\u043b\u0430\u0442\u044c migrate. \u041c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u0432 peewee \u0435\u0441\u0442\u044c. \u0438\u0445 \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u043e\u043f\u0438\u0441\u0430\u043d <a href=\"http:\/\/peewee.readthedocs.org\/en\/latest\/peewee\/playhouse.html#migrate\">\u0442\u0443\u0442<\/a>. \u041e\u0434\u043d\u0430\u043a\u043e, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043d\u0430\u043c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u044d\u0442\u0438 \u043c\u0438\u0433\u0440\u0430\u0446\u0438\u0438 \u2014 \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e.  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-261530","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/261530","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=261530"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/261530\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=261530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=261530"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=261530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}