Exemplo n.º 1
0
def migration_v10(db, migrator: SchemaMigrator):
    with db.atomic():
        migrate(
            migrator.add_column("jamentry", "title",
                                TextField(null=True, default=None)),
            migrator.add_column("jamentry", "url", CharField(null=True)),
        )
Exemplo n.º 2
0
def _rename_columns():
    column_rename = {
        'contributors': {
            'person_id': 'user_id'
        },
        'institutionuser': {
            'person_id': 'user_id'
        },
        'instrumentuser': {
            'custodian_id': 'user_id'
        },
        'projectuser': {
            'person_id': 'user_id'
        },
        'transactionuser': {
            'authorized_person_id': 'user_id'
        }
    }
    migrator = SchemaMigrator(DB)
    for table_name, column_tre in column_rename.items():
        for old_name, new_name in column_tre.items():
            migrate(migrator.rename_column(table_name, old_name, new_name))
            old_fkey_name = '_'.join([table_name, old_name, 'fkey'])
            new_fkey_name = '_'.join([table_name, new_name, 'fkey'])
            DB.execute_sql('alter table {} rename constraint {} TO {}'.format(
                table_name, old_fkey_name, new_fkey_name))
            old_index_name = '_'.join([table_name, old_name])
            new_index_name = '_'.join([table_name, new_name])
            DB.execute_sql('alter index {} rename to {}'.format(
                old_index_name, new_index_name))
Exemplo n.º 3
0
def migration_v6(db, migrator: SchemaMigrator):
    with db.atomic():
        migrate(
            migrator.drop_column("jam", "short_description"),
            migrator.add_column("jamcriteria", "order",
                                IntegerField(default=0)),
        )
Exemplo n.º 4
0
def upgrade(migrator: SchemaMigrator):
    expanded_score_field = FloatField(null=True)
    migrate(
        migrator.drop_not_null(Karma._meta.db_table, 'score'),
        migrator.add_column(Karma._meta.db_table, 'expanded_score',
                            expanded_score_field),
    )
Exemplo n.º 5
0
def update_schema():
    """Update schema from 2.0 to 2.1."""
    DB.execute_sql(
        'alter table projects rename constraint proposals_pkey to projects_pkey'
    )
    rename_indexes = {
        'atoolproject_proposal_id': 'atoolproject_project_id',
        'citationproject_proposal_id': 'citationproject_project_id',
        'projectgroup_proposal_id': 'projectgroup_project_id',
        'projectinstrument_proposal_id': 'projectinstrument_project_id',
        'projectparticipant_proposal_id': 'projectparticipant_project_id',
        'proposals_accepted_date': 'projects_accepted_date',
        'proposals_actual_end_date': 'projects_actual_end_date',
        'proposals_actual_start_date': 'projects_actual_start_date',
        'proposals_closed_date': 'projects_closed_date',
        'proposals_created': 'projects_created',
        'proposals_deleted': 'projects_deleted',
        'proposals_short_name': 'projects_short_name',
        'proposals_submitted_date': 'projects_submitted_date',
        'proposals_suspense_date': 'projects_suspense_date',
        'proposals_title': 'projects_title',
        'proposals_updated': 'projects_updated',
        'transsap_proposal_id': 'transsap_project_id',
        'transsip_proposal_id': 'transsip_project_id'
    }
    for old_index, new_index in rename_indexes.items():
        DB.execute_sql('alter index {} rename to {}'.format(
            old_index, new_index))
    migrator = SchemaMigrator(DB)
    for table_name in ['transsip', 'transsap']:
        for col_name in ['created', 'deleted', 'updated']:
            migrate(migrator.add_index(table_name, (col_name, ), False))
Exemplo n.º 6
0
 def update_0_0_to_1_0(cls):
     """Update by creating the table."""
     if not IngestState.table_exists():
         IngestState.create_table()
     col_names = [col_md.name for col_md in DB.get_columns('ingeststate')]
     if 'complete' in col_names:
         migrator = SchemaMigrator(DB)
         migrate(migrator.drop_column('ingeststate', 'complete'))
Exemplo n.º 7
0
 def update_0_1_to_1_0(cls):
     """Update by adding the boolean column."""
     migrator = SchemaMigrator(DB)
     migrate(
         migrator.add_column(
             'cart',
             'bundle',
             BooleanField(default=False, null=True)
         )
     )
Exemplo n.º 8
0
def upgrade(migrator: SchemaMigrator):
    total_playtime = IntegerField(default=0, null=False)
    total_donations = IntegerField(default=0, null=False)

    migrate(
        migrator.add_column(Player._meta.db_table, 'total_playtime',
                            total_playtime),
        migrator.add_column(Player._meta.db_table, 'total_donations',
                            total_donations),
    )
def _rename_column():
    table_name = 'usergroup'
    old_name = 'person_id'
    new_name = 'user_id'
    migrator = SchemaMigrator(DB)
    migrate(migrator.rename_column(table_name, old_name, new_name))
    old_fkey_name = '_'.join([table_name, old_name, 'fkey'])
    new_fkey_name = '_'.join([table_name, new_name, 'fkey'])
    DB.execute_sql('alter table {} rename constraint {} TO {}'.format(
        table_name, old_fkey_name, new_fkey_name))
    old_index_name = '_'.join([table_name, old_name])
    new_index_name = '_'.join([table_name, new_name])
    DB.execute_sql('alter index {} rename to {}'.format(
        old_index_name, new_index_name))
Exemplo n.º 10
0
def update_schema():
    """Update schema from 3.0 to 4.0."""
    migrator = SchemaMigrator(DB)
    for table_name in ['keys', 'values', 'groups']:
        migrate(
            migrator.add_column(table_name, 'display_name',
                                CharField(default='', index=True)),
            migrator.add_column(table_name, 'description',
                                TextField(default='')))
    new_rel_list = [{
        'name':
        'upload_required',
        'display_name':
        'Required for Upload',
        'description':
        'This relationship means that the objects are required for upload to be asserted.'
    }, {
        'name':
        'search_required',
        'display_name':
        'Required for Search',
        'description':
        'This relationship means that the objects are required for search to be asserted.'
    }, {
        'name':
        'co_principal_investigator',
        'display_name':
        'Co-Principal Investigator',
        'description':
        'subject is the co-principal investigator of the object'
    }]
    for new_rel in new_rel_list:
        Relationships.get_or_create(**new_rel)
    rel_obj = Relationships.get(name='upload_required')
    migrate(
        migrator.add_column(
            'instrumentkeyvalue', 'relationship_id',
            ForeignKeyField(Relationships,
                            field=Relationships.uuid,
                            default=rel_obj.uuid)))
    DB.execute_sql(
        'alter table instrumentkeyvalue drop constraint instrumentkeyvalue_pkey'
    )
    DB.execute_sql('''
        alter table
            instrumentkeyvalue
        add constraint
            instrumentkeyvalue_pkey primary key (instrument_id, key_id, value_id, relationship_id)
    ''')
Exemplo n.º 11
0
def upgrade(migrator: SchemaMigrator):
    try:
        migrate(
            migrator.drop_index(MapFolder._meta.db_table, 'mapfolder_name'), )
    except Exception as e:
        print(str(e))
        print('Migration failed but silencing error!')
Exemplo n.º 12
0
    def __init__(self, url, bare_fields=False):
        if isinstance(url, Database):
            self._url = None
            self._database = url
            self._database_path = self._database.database
        else:
            self._url = url
            parse_result = urlparse(url)
            self._database_path = parse_result.path[1:]

            # Connect to the database.
            self._database = connect(url)

        self._database.connect()

        # Introspect the database and generate models.
        self._introspector = Introspector.from_database(self._database)
        self._models = self._introspector.generate_models(
            skip_invalid=True,
            literal_column_names=True,
            bare_fields=bare_fields)
        self._migrator = SchemaMigrator.from_database(self._database)

        class BaseModel(Model):
            class Meta:
                database = self._database
        self._base_model = BaseModel
        self._export_formats = self.get_export_formats()
        self._import_formats = self.get_import_formats()
Exemplo n.º 13
0
    def __init__(self, url, bare_fields=False):
        if isinstance(url, Database):
            self._url = None
            self._database = url
            self._database_path = self._database.database
        else:
            self._url = url
            parse_result = urlparse(url)
            self._database_path = parse_result.path[1:]

            # Connect to the database.
            self._database = connect(url)

        self._database.connect()

        # Introspect the database and generate models.
        self._introspector = Introspector.from_database(self._database)
        self._models = self._introspector.generate_models(
            skip_invalid=True,
            literal_column_names=True,
            bare_fields=bare_fields)
        self._migrator = SchemaMigrator.from_database(self._database)

        class BaseModel(Model):
            class Meta:
                database = self._database
        self._base_model = BaseModel
        self._export_formats = self.get_export_formats()
        self._import_formats = self.get_import_formats()
Exemplo n.º 14
0
def migrate_schema(db):
    print("Starting migration")

    from src.model.models import DatabaseInfo

    if isinstance(db, Proxy):
        db = db.obj

    migrator = SchemaMigrator.from_database(db)

    if migrator is None:
        raise Exception("Migrator is broken")

    info = DatabaseInfo.get_or_none(id=0)
    if info is None:
        info = DatabaseInfo.create(id=0, version=0)

    print("Current database version is %d" % info.version)

    for i in range(info.version, len(migrations)):
        print(f"Applying migration v{i+1}")

        migrations[i](db, migrator)

        info.version = i + 1
        info.save()
Exemplo n.º 15
0
def migration_v3(db, migrator: SchemaMigrator):
    import magic

    from src.model.models import Content

    with db.atomic():
        migrate(
            migrator.add_column("content", "mime", CharField(default="")),
            migrator.add_column("content", "size", BigIntegerField(default=0)),
        )
    query = Content.select()

    for c in query:
        c.mime = magic.from_file(c.path, mime=True)
        c.size = os.stat(c.path).st_size
        c.save()
Exemplo n.º 16
0
    def __init__(self, url, include_views=False, **kwargs):
        if isinstance(url, Database):
            self._url = None
            self._database = url
            self._database_path = self._database.database
        else:
            self._url = url
            parse_result = urlparse(url)
            self._database_path = parse_result.path[1:]

            # Connect to the database.
            self._database = connect(url)

        # Open a connection if one does not already exist.
        self._database.connect(reuse_if_open=True)

        # Introspect the database and generate models.
        self._introspector = Introspector.from_database(self._database)
        self._include_views = include_views
        self._models = self._introspector.generate_models(
            skip_invalid=True,
            literal_column_names=True,
            include_views=self._include_views,
            **kwargs)
        self._migrator = SchemaMigrator.from_database(self._database)

        class BaseModel(Model):
            class Meta:
                database = self._database

        self._base_model = BaseModel
        self._export_formats = self.get_export_formats()
        self._import_formats = self.get_import_formats()
Exemplo n.º 17
0
def migration_v1(db, migrator: SchemaMigrator):
    from src.model.models import Post

    with db.atomic():
        migrate(
            migrator.add_column("post", "has_cut",
                                BooleanField(default=False)),
            migrator.add_column("post", "cut_name",
                                CharField(default=None, null=True)),
        )

    query = Post.select()

    for p in query:
        p.has_cut = "<cut>" in p.text
        p.save()
 def up(self, migrator: SchemaMigrator):
     Person._meta.database = migrator.database
     Person.create_table()
     return [
         migrator.add_column('sample_person_table', 'test1', CharField(default='sample_test2_value')),
         migrator.add_column('sample_person_table', 'test2', CharField(default='sample_test2_value'))
     ]
Exemplo n.º 19
0
def _rename_tables():
    table_rename = {
        'institutionperson': 'institutionuser',
        'instrumentcustodian': 'instrumentuser',
        'projectparticipant': 'projectuser',
        'transactionrelease': 'transactionuser'
    }
    migrator = SchemaMigrator(DB)
    for old_table, new_table in table_rename.items():
        migrate(migrator.rename_table(old_table, new_table))
        for index_meta in DB.get_indexes(new_table):
            new_index_name = '{}{}'.format(new_table, index_meta.name[len(old_table):])
            DB.execute_sql('alter index {} rename to {}'.format(index_meta.name, new_index_name))
        for fkey_meta in DB.get_foreign_keys(new_table):
            old_name = '_'.join([old_table, fkey_meta.column, 'fkey'])
            new_name = '_'.join([new_table, fkey_meta.column, 'fkey'])
            DB.execute_sql('alter table {} rename constraint {} TO {}'.format(new_table, old_name, new_name))
Exemplo n.º 20
0
    def __init__(self, database):
        if isinstance(database, pw.Proxy):
            database = database.obj

        self.database = database
        self.orm = dict()
        self.ops = list()
        self.migrator = SchemaMigrator.from_database(self.database)
Exemplo n.º 21
0
def main():
    for cls in models.Category, models.Feed, models.Entry:
        try:
            cls.create_table()
        except peewee.OperationalError as ex:
            print ex

    migrator = SchemaMigrator(models.DATABASE)

    try:
        try:
            migrate(migrator.drop_index('entry_feed_id_published_at'))
        except:
            pass

        migrate(migrator.add_index('entry', ('feed_id', 'published_at'), False))
    except peewee.OperationalError as ex:
        print ex
Exemplo n.º 22
0
def migration_v13(db, migrator: SchemaMigrator):
    with db.atomic():
        migrate(
            migrator.add_column("comment", "object_type",
                                CharField(null=True)),
            migrator.add_column("comment", "object_id",
                                IntegerField(default=0)),
        )

        from src.model.models import Comment

        for c in Comment.select():
            c.object_type = "post"
            c.object_id = c.post.id
            c.save()

        migrate(
            # migrator.drop_foreign_key_constraint("comment", "post"),
            migrator.drop_column("comment", "post_id"), )
Exemplo n.º 23
0
    def test_fk_migration(self):
        migrator = SchemaMigrator.from_database(self.database)
        migrate(
            migrator.drop_column(FKMB._meta.table_name, FKMB.fkma.column_name))

        migrate(
            migrator.add_column(FKMB._meta.table_name, FKMB.fkma.column_name,
                                FKMB.fkma))

        fa = FKMA.create(name='fa')
        FKMB.create(name='fb', fkma=fa)
        obj = FKMB.select().first()
        self.assertEqual(obj.name, 'fb')
Exemplo n.º 24
0
def migration_v9(db, migrator: SchemaMigrator):
    from src.model.models import Jam

    with db.atomic():
        migrate(
            migrator.add_column(
                "jamentry",
                "jam_id",
                ForeignKeyField(Jam,
                                Jam.id,
                                default=None,
                                null=True,
                                index=False),
            ), )
Exemplo n.º 25
0
def migration_v4(db, migrator: SchemaMigrator):
    from src.model.models import Token

    with db.atomic():
        migrate(
            migrator.add_column("token", "token_type",
                                CharField(default="")), )

    query = Token.select()
    for t in query:
        if t.is_refresh_token:
            t.token_type = "refresh"
        else:
            t.token_type = "access"
        t.save()
Exemplo n.º 26
0
    def __init__(self, url):
        self._url = url
        parse_result = urlparse(url)
        self._database_path = parse_result.path[1:]

        # Connect to the database.
        self._database = connect(url)
        self._database.connect()

        # Introspect the database and generate models.
        self._introspector = Introspector.from_database(self._database)
        self._models = self._introspector.generate_models()
        self._migrator = SchemaMigrator.from_database(self._database)

        class BaseModel(Model):
            class Meta:
                database = self._database
        self._base_model = BaseModel
        self._export_formats = self.get_export_formats()
Exemplo n.º 27
0
    def __init__(self, url):
        self._url = url
        parse_result = urlparse(url)
        self._database_path = parse_result.path[1:]

        # Connect to the database.
        self._database = connect(url)
        self._database.connect()

        # Introspect the database and generate models.
        self._introspector = Introspector.from_database(self._database)
        self._models = self._introspector.generate_models(skip_invalid=True)
        self._migrator = SchemaMigrator.from_database(self._database)

        class BaseModel(Model):
            class Meta:
                database = self._database
        self._base_model = BaseModel
        self._export_formats = self.get_export_formats()
        self._import_formats = self.get_import_formats()
Exemplo n.º 28
0
    def update_0_1_to_1_0(cls):
        """Update from 0.1 to 1.0."""
        migrator = SchemaMigrator(DB)
        TransSIP.create_table()
        TransSAP.create_table()

        class OldTrans(Model):
            """This is the old transactions."""

            submitter = ForeignKeyField(Users, backref='transactions')
            instrument = ForeignKeyField(Instruments, backref='transactions')
            proposal = ForeignKeyField(Proposals, backref='transactions')
            created = DateTimeField()
            updated = DateTimeField()
            deleted = DateTimeField(null=True)

            class Meta(object):
                """This is the meta class for OldTrans."""

                database = DB
                table_name = 'transactions'

        migrate(
            migrator.add_column('transactions', 'description',
                                TextField(null=True)))
        for old_trans in OldTrans.select():
            transsip = TransSIP()
            for attr in [
                    'submitter', 'instrument', 'proposal', 'created',
                    'updated', 'deleted'
            ]:
                setattr(transsip, attr, getattr(old_trans, attr))
            setattr(transsip, 'id',
                    Transactions.get(Transactions.id == old_trans.id))
            transsip.save(force_insert=True)
        migrate(migrator.drop_column('transactions', 'submitter_id'),
                migrator.drop_column('transactions', 'instrument_id'),
                migrator.drop_column('transactions', 'proposal_id'))
Exemplo n.º 29
0
def update_schema():
    """Update from 0.1 to 1.0."""
    migrator = SchemaMigrator(DB)

    OldTransSIP.create_table()
    OldTransSAP.create_table()

    migrate(
        migrator.add_column('transactions', 'description',
                            TextField(null=True)))
    for old_trans in OldTrans.select():
        transsip = OldTransSIP()
        for attr in [
                'submitter', 'instrument', 'proposal', 'created', 'updated',
                'deleted'
        ]:
            setattr(transsip, attr, getattr(old_trans, attr))
        setattr(transsip, 'id',
                Transactions.get(Transactions.id == old_trans.id))
        transsip.save(force_insert=True)
    migrate(migrator.drop_column('transactions', 'submitter_id'),
            migrator.drop_column('transactions', 'instrument_id'),
            migrator.drop_column('transactions', 'proposal_id'))
Exemplo n.º 30
0
def commands(migrator: SchemaMigrator):
    migrate(migrator.add_column("ingredient", "calories", peewee.FloatField(default=0)))
Exemplo n.º 31
0
 def __init__(self, database):
     self.database = database
     self.migrator = SchemaMigrator.from_database(self.database)
     self.models = []
Exemplo n.º 32
0
 def __init__(self, database):
     self.database = database
     self.migrator = SchemaMigrator.from_database(self.database)
     self.models = []
Exemplo n.º 33
0
def upgrade(migrator: SchemaMigrator):
    migrate(
        migrator.add_column(TestModel._meta.db_table, 'sample', sample_field))
Exemplo n.º 34
0
 def __init__(self, db: Database, migration_path: str = 'migrations'):
     self.db = db
     self.migrator = SchemaMigrator.from_database(self.db)
     MigrationModel._meta.database = db
     __import__(migration_path, locals=locals(), globals=globals())
Exemplo n.º 35
0
 def update_1_0_to_2_0(cls):
     """Update by adding the auth column."""
     migrator = SchemaMigrator(DB)
     migrate(migrator.add_column('eventmatch', 'auth',
                                 TextField(null=True)))
Exemplo n.º 36
0
def migrate(db):
    migrator = SchemaMigrator.from_database(db)
    from database import model_factory
    models = model_factory(db)

    migrate_001(migrator, models)
Exemplo n.º 37
0
 def __init__(self, db):
     self.db = db
     self.migrator = SchemaMigrator.from_database(self.db)