cubicweb/misc/migration/3.8.5_Any.py
author Denis Laxalde <denis.laxalde@logilab.fr>
Sat, 16 Jan 2016 13:48:51 +0100
changeset 11057 0b59724cb3f2
parent 10589 misc/migration/3.8.5_Any.py@7c23b7de2b8d
permissions -rw-r--r--
Reorganize source tree to have a "cubicweb" top-level package Basically: mkdir cubicweb hg mv *.py -X setup.py cubicweb hg mv dataimport devtools entities etwist ext hooks i18n misc schemas server skeleton sobjects test web wsgi cubicweb Other changes: * adjust path to cubicweb-ctl in devtools tests * update setup.py to avoid importing __pkginfo__ (exec it instead), replace os.path.walk by os.walk and prepend `modname` here and there * update tox.ini to account for new test locations * update doc/conf.py so that it still finds __pkginfo__.py and CWDIR in doc/Makefile
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     1
from __future__ import print_function
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
     2
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     3
def migrate_varchar_to_nvarchar():
9460
a2a0bc984863 [config] cleanup/refactor server sources file values handling
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 5832
diff changeset
     4
    dbdriver  = config.system_source_config['db-driver']
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     5
    if dbdriver != "sqlserver2005":
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     6
        return
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
     7
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     8
    introspection_sql = """\
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
     9
SELECT table_schema, table_name, column_name, is_nullable, character_maximum_length
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    10
FROM information_schema.columns
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    11
WHERE data_type = 'VARCHAR' and table_name <> 'SYSDIAGRAMS'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    12
"""
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    13
    has_index_sql = """\
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    14
SELECT i.name AS index_name,
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    15
       i.type_desc,
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    16
       i.is_unique,
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    17
       i.is_unique_constraint
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    18
FROM sys.indexes AS i, sys.index_columns as j, sys.columns as k
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    19
WHERE is_hypothetical = 0 AND i.index_id <> 0
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    20
AND i.object_id = j.object_id
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    21
AND i.index_id = j.index_id
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    22
AND i.object_id = OBJECT_ID('%(table)s')
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    23
AND k.name = '%(col)s'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    24
AND k.object_id=i.object_id
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    25
AND j.column_id = k.column_id;"""
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    26
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    27
    generated_statements = []
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    28
    for schema, table, column, is_nullable, length in sql(introspection_sql, ask_confirm=False):
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    29
        qualified_table = '[%s].[%s]' % (schema, table)
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    30
        rset = sql(has_index_sql % {'table': qualified_table, 'col':column},
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    31
                   ask_confirm = False)
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    32
        drops = []
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    33
        creates = []
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    34
        for idx_name, idx_type, idx_unique, is_unique_constraint in rset:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    35
            if is_unique_constraint:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    36
                drops.append('ALTER TABLE %s DROP CONSTRAINT %s' % (qualified_table, idx_name))
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    37
                creates.append('ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)' % (qualified_table, idx_name, column))
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    38
            else:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    39
                drops.append('DROP INDEX %s ON %s' % (idx_name, qualified_table))
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    40
                if idx_unique:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    41
                    unique = 'UNIQUE'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    42
                else:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    43
                    unique = ''
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    44
                creates.append('CREATE %s %s INDEX %s ON %s(%s)' % (unique, idx_type, idx_name, qualified_table, column))
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    45
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    46
        if length == -1:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    47
            length = 'max'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    48
        if is_nullable == 'YES':
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    49
            not_null = 'NULL'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    50
        else:
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    51
            not_null = 'NOT NULL'
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    52
        alter_sql = 'ALTER TABLE %s ALTER COLUMN %s NVARCHAR(%s) %s' % (qualified_table, column, length, not_null)
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    53
        generated_statements+= drops + [alter_sql] + creates
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    54
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    55
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    56
    for statement in generated_statements:
10589
7c23b7de2b8d [py3k] print function
Samuel Trégouët <samuel.tregouet@logilab.fr>
parents: 9460
diff changeset
    57
        print(statement)
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    58
        sql(statement, ask_confirm=False)
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    59
    commit()
5832
16a4235e031f delete trailing whitespace
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5831
diff changeset
    60
5831
561014b6d70a Had thought this could be done in cube, but it really needs to be done earlier in the migration process
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
diff changeset
    61
migrate_varchar_to_nvarchar()