def run_migration(): recipes_db = SqliteDatabase(os.path.join(SCRIPT_DIR, '../recipes.db')) migrator = SqliteMigrator(recipes_db) new_picture_field = TextField(null=True) updated_pictures = [(recipe_id, update_picture(picture)) for ( recipe_id, picture) in recipes_db.execute_sql('SELECT id, picture FROM recipe')] migrate( migrator.drop_column('recipe', 'picture'), migrator.add_column('recipe', 'picture', new_picture_field), ) for (recipe_id, picture) in updated_pictures: if picture: recipes_db.execute_sql('UPDATE recipe SET picture=? WHERE id=?', (picture, recipe_id))
def apply(): database = SqliteDatabase(settings.DATABASE) migrator = SqliteMigrator(database) with database.transaction(): database.execute_sql('CREATE TABLE user (pk INTEGER PRIMARY KEY)') database.execute_sql('CREATE TABLE snippet (pk INTEGER PRIMARY KEY)') for field in ( User.created_at, User.updated_at, User.name, User.passhash, Snippet.created_at, Snippet.updated_at, Snippet.syntax, Snippet.raw, Snippet.html, ): field.null = True migrate( # user table migrator.add_column('user', 'created_at', User.created_at), migrator.add_column('user', 'updated_at', User.updated_at), migrator.add_column('user', 'name', User.name), migrator.add_column('user', 'passhash', User.passhash), migrator.add_index('user', ('name', ), True), migrator.add_index('user', ('updated_at', ), False), # snippet table migrator.add_column('snippet', 'created_at', Snippet.created_at), migrator.add_column('snippet', 'updated_at', Snippet.updated_at), migrator.add_column('snippet', 'author_id', Snippet.author), migrator.add_column('snippet', 'name', Snippet.name), migrator.add_column('snippet', 'syntax', Snippet.syntax), migrator.add_column('snippet', 'raw', Snippet.raw), migrator.add_column('snippet', 'html', Snippet.html), migrator.add_index('snippet', ('updated_at', ), False), )
self.created_at = None self.user_name = None self.user_screen_name = None self.link_url = '' self.media_list = [] for k in kwargs: setattr(self, k, kwargs[k]) # Create tables for t in (TwitterUser, TelegramChat, Subscription): t.create_table(fail_silently=True) # Migrate new fields. TODO: think of some better migration mechanism db = SqliteDatabase('peewee.db', timeout=10) migrator = SqliteMigrator(db) operations = [ migrator.add_column('telegramchat', 'twitter_request_token', TelegramChat.twitter_request_token), migrator.add_column('telegramchat', 'twitter_token', TelegramChat.twitter_token), migrator.add_column('telegramchat', 'twitter_secret', TelegramChat.twitter_secret), migrator.add_column('telegramchat', 'timezone_name', TelegramChat.timezone_name), migrator.add_column('telegramchat', 'delete_soon', TelegramChat.delete_soon), migrator.add_column('telegramchat', 'last_tweet_id', TelegramChat.last_tweet_id), ]
#!/usr/bin/env python3 # -*- coding: utf-8 -*- __author__ = 'ipetrash' # SOURCE: http://docs.peewee-orm.com/en/latest/peewee/playhouse.html#schema-migrations from playhouse.migrate import SqliteDatabase, SqliteMigrator, migrate, IntegerField from config import DB_FILE_NAME db = SqliteDatabase(DB_FILE_NAME) migrator = SqliteMigrator(db) with db.atomic(): migrate( migrator.add_column('settings', 'filter_quote_by_max_length_text', IntegerField(null=True)), )
if "db_type" in config_o2m: db_type = config_o2m["db_type"] if db_type == "mysql": db_username = config_o2m["db_username"] db_password = config_o2m["db_password"] db_host = config_o2m["db_host"] db_port = config_o2m["db_port"] db_name = config_o2m["db_name"] db = ReconnectMySQLDatabase(db_name, host=db_host, user=db_username, password=db_password) elif db_type == "sqlite": database_path = config_o2m["db_sqlite_path"] db = SqliteDatabase(database_path) # Si rien n'est spécifié -> base par défaut if db == None: db = SqliteDatabase("data.db") """ MODEL STRUCTURE """ class BaseModel(Model): class Meta: database = db class Tag(BaseModel):
def rollback(): database = SqliteDatabase(settings.DATABASE) with database.transaction(): database.execute_sql('DROP TABLE user') database.execute_sql('DROP TABLE snippet')
class Tweet(Model): tw_id = BigIntegerField(unique=True) known_at = DateTimeField(default=datetime.datetime.now) text = TextField() created_at = DateTimeField() twitter_user = ForeignKeyField(TwitterUser, related_name='tweets') photo_url = TextField(default='') @property def screen_name(self): return self.twitter_user.screen_name @property def name(self): return self.twitter_user.name # Migrate new fields. TODO: think of some better migration mechanism db = SqliteDatabase('peewee.db') migrator = SqliteMigrator(db) operations = [ migrator.add_column('tweet', 'photo_url', Tweet.photo_url), migrator.add_column('twitteruser', 'last_fetched', TwitterUser.last_fetched) ] for op in operations: try: migrate(op) except OperationalError: pass
import argparse from playhouse.migrate import SqliteMigrator, SqliteDatabase, CharField, migrate parser = argparse.ArgumentParser(description='Migration: add ip_address field to ConnectionInfo.') parser.add_argument("dbfile") args = parser.parse_args() my_db = SqliteDatabase(args.dbfile) migrator = SqliteMigrator(my_db) ipfield = CharField(default='') with my_db.transaction(): migrate( migrator.add_column('ConnectionInfo', 'ip_address', ipfield), )