doc/book/devrepo/migration.rst
author Denis Laxalde <denis.laxalde@logilab.fr>
Mon, 24 Oct 2016 09:29:08 +0200
changeset 11743 48d70d143dc1
parent 10994 ebd586aa5b00
child 12196 b6b87c36beed
permissions -rw-r--r--
[test] Use `with open` syntax in cwconfig "prefix" tests
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     1
.. -*- coding: utf-8 -*-
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     2
282
fe06f5e828c5 [doc] Add internal links.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 229
diff changeset
     3
.. _migration:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     4
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     5
Migration
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     6
=========
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
     7
2535
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2479
diff changeset
     8
One of the main design goals of *CubicWeb* was to support iterative and agile
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2479
diff changeset
     9
development. For this purpose, multiple actions are provided to facilitate the
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2479
diff changeset
    10
improvement of an instance, and in particular to handle the changes to be
c7b736929a58 [doc] a bit of rewriting
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2479
diff changeset
    11
applied to the data model, without loosing existing data.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    12
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    13
The current version of a cube (and of cubicweb itself) is provided in the file
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    14
`__pkginfo__.py` as a tuple of 3 integers.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    15
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    16
Migration scripts management
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    17
----------------------------
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    18
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    19
Migration scripts has to be located in the directory `migration` of your
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    20
cube and named accordingly:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    21
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    22
::
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    23
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    24
  <version n° X.Y.Z>[_<description>]_<mode>.py
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    25
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    26
in which :
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    27
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    28
* X.Y.Z is the model version number to which the script enables to migrate.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    29
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    30
* *mode* (between the last "_" and the extension ".py") is used for
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    31
  distributed installation. It indicates to which part
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    32
  of the application (RQL server, web server) the script applies.
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    33
  Its value could be :
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    34
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    35
  * `common`, applies to the RQL server as well as the web server and updates
229
767ff7f5d5a7 [doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 197
diff changeset
    36
    files on the hard drive (configuration files migration for example).
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    37
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    38
  * `web`, applies only to the web server and updates files on the hard drive.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    39
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    40
  * `repository`, applies only to the RQL server and updates files on the
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    41
    hard drive.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    42
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    43
  * `Any`, applies only to the RQL server and updates data in the database
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    44
    (schema and data migration for example).
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    45
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    46
Again in the directory `migration`, the file `depends.map` allows to indicate
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    47
that for the migration to a particular model version, you always have to first
2175
16d3c37c5d28 [doc] improvements
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2172
diff changeset
    48
migrate to a particular *CubicWeb* version. This file can contain comments (lines
9304
ff61b10e7415 [book] English fix
Dimitri Papadopoulos <dimitri.papadopoulos@cea.fr>
parents: 9295
diff changeset
    49
starting with `#`) and a dependency is listed as follows: ::
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    50
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    51
  <model version n° X.Y.Z> : <cubicweb version n° X.Y.Z>
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    52
229
767ff7f5d5a7 [doc] Replace all : s/by example/for example/
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 197
diff changeset
    53
For example: ::
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    54
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    55
  0.12.0: 2.26.0
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    56
  0.13.0: 2.27.0
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    57
  # 0.14 works with 2.27 <= cubicweb <= 2.28 at least
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    58
  0.15.0: 2.28.0
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    59
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    60
Base context
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    61
------------
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    62
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
    63
The following identifiers are pre-defined in migration scripts:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    64
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    65
* `config`, instance configuration
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    66
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    67
* `interactive_mode`, boolean indicating that the script is executed in
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    68
  an interactive mode or not
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    69
2479
1c697cc6dd31 fix versions_map doc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    70
* `versions_map`, dictionary of migrated versions  (key are cubes
1c697cc6dd31 fix versions_map doc
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2476
diff changeset
    71
  names, including 'cubicweb', values are (from version, to version)
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    72
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
    73
* `confirm(question)`, function asking the user and returning true
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    74
  if the user answers yes, false otherwise (always returns true in
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    75
  non-interactive mode)
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    76
5231
40f53867e332 [doc/book] misc fixes/enhancements & notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    77
* `_()` is equivalent to `unicode` allowing to flag the strings to
40f53867e332 [doc/book] misc fixes/enhancements & notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    78
  internationalize in the migration scripts.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    79
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
    80
In the `repository` scripts, the following identifiers are also defined:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    81
5231
40f53867e332 [doc/book] misc fixes/enhancements & notes
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents: 5150
diff changeset
    82
* `commit(ask_confirm=True)`, request confirming and executing a "commit"
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    83
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    84
* `schema`, instance schema (readen from the database)
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    85
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    86
* `fsschema`, installed schema on the file system (e.g. schema of
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
    87
  the updated model and cubicweb)
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    88
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    89
* `repo`, repository object
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    90
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
    91
* `session`, repository session object
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
    92
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
    93
6244
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    94
New cube dependencies
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    95
---------------------
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    96
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    97
If your code depends on some new cubes, you have to add them in a migration
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    98
script by using:
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
    99
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   100
* `add_cube(cube, update_database=True)`, add a cube.
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   101
* `add_cubes(cubes, update_database=True)`, add a list of cubes.
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   102
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   103
The `update_database` parameter is telling if the database schema
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   104
should be updated or if only the relevant persistent property should be
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   105
inserted (for the case where a new cube has been extracted from an
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   106
existing one, so the new cube schema is actually already in there).
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   107
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   108
If some of the added cubes are already used by an instance, they'll simply be
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   109
silently skipped.
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   110
10994
ebd586aa5b00 [doc] add documentation about drop_cube
Arthur Lutz <arthur.lutz@logilab.fr>
parents: 10491
diff changeset
   111
To remove a cube use `drop_cube(cube, removedeps=False)`. 
6244
1c4ac1626f3c [book - #1251197] add a section in migration page for add_cube and add_cubes command
Stephanie Marcu <stephanie.marcu@logilab.fr>
parents: 6059
diff changeset
   112
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   113
Schema migration
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   114
----------------
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   115
The following functions for schema migration are available in `repository`
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   116
scripts:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   117
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   118
* `add_attribute(etype, attrname, attrtype=None, commit=True)`, adds a new
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   119
  attribute to an existing entity type. If the attribute type is not specified,
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   120
  then it is extracted from the updated schema.
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   121
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   122
* `drop_attribute(etype, attrname, commit=True)`, removes an attribute from an
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   123
  existing entity type.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   124
1168
ebf7328c251e improve migration doc
Emile Anclin <emile.anclin@logilab.fr>
parents: 287
diff changeset
   125
* `rename_attribute(etype, oldname, newname, commit=True)`, renames an attribute
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   126
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   127
* `add_entity_type(etype, auto=True, commit=True)`, adds a new entity type.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   128
  If `auto` is True, all the relations using this entity type and having a known
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   129
  entity type on the other hand will automatically be added.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   130
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   131
* `drop_entity_type(etype, commit=True)`, removes an entity type and all the
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   132
  relations using it.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   133
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   134
* `rename_entity_type(oldname, newname, commit=True)`, renames an entity type
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   135
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   136
* `add_relation_type(rtype, addrdef=True, commit=True)`, adds a new relation
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   137
  type. If `addrdef` is True, all the relations definitions of this type will
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   138
  be added.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   139
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   140
* `drop_relation_type(rtype, commit=True)`, removes a relation type and all the
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   141
  definitions of this type.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   142
8700
9064df1c2f99 [book] fix typo.
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents: 8467
diff changeset
   143
* `rename_relation_type(oldname, newname, commit=True)`, renames a relation type.
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   144
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   145
* `add_relation_definition(subjtype, rtype, objtype, commit=True)`, adds a new
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   146
  relation definition.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   147
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   148
* `drop_relation_definition(subjtype, rtype, objtype, commit=True)`, removes
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   149
  a relation definition.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   150
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2535
diff changeset
   151
* `sync_schema_props_perms(ertype=None, syncperms=True, syncprops=True, syncrdefs=True, commit=True)`,
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
   152
  synchronizes properties and/or permissions on:
2544
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2535
diff changeset
   153
  - the whole schema if ertype is None
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2535
diff changeset
   154
  - an entity or relation type schema if ertype is a string
282261b26774 [doc] fixed some dangling internal links
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 2535
diff changeset
   155
  - a relation definition  if ertype is a 3-uple (subject, relation, object)
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   156
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   157
* `change_relation_props(subjtype, rtype, objtype, commit=True, **kwargs)`, changes
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   158
  properties of a relation definition by using the named parameters of the properties
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   159
  to change.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   160
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   161
* `set_widget(etype, rtype, widget, commit=True)`, changes the widget used for the
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   162
  relation <rtype> of entity type <etype>.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   163
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   164
* `set_size_constraint(etype, rtype, size, commit=True)`, changes the size constraints
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   165
  for the relation <rtype> of entity type <etype>.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   166
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   167
Data migration
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   168
--------------
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   169
The following functions for data migration are available in `repository` scripts:
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   170
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   171
* `rql(rql, kwargs=None, cachekey=None, ask_confirm=True)`, executes an arbitrary RQL
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   172
  query, either to interrogate or update. A result set object is returned.
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   173
9295
df68a28c5735 [book] Typo fix and rephrasing in devrepo/migration
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8700
diff changeset
   174
* `add_entity(etype, *args, **kwargs)`, adds a new entity of the given type.
df68a28c5735 [book] Typo fix and rephrasing in devrepo/migration
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8700
diff changeset
   175
  The attribute and relation values are specified as named positional
df68a28c5735 [book] Typo fix and rephrasing in devrepo/migration
Denis Laxalde <denis.laxalde@logilab.fr>
parents: 8700
diff changeset
   176
  arguments.
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   177
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   178
Workflow creation
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   179
-----------------
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   180
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   181
The following functions for workflow creation are available in `repository`
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   182
scripts:
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   183
4447
a55fb2745644 add_state/add_transition are now deprecated, only add_workflow remaining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4442
diff changeset
   184
* `add_workflow(label, workflowof, initial=False, commit=False, **kwargs)`, adds a new workflow
a55fb2745644 add_state/add_transition are now deprecated, only add_workflow remaining
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 4442
diff changeset
   185
  for a given type(s)
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   186
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   187
You can find more details about workflows in the chapter :ref:`Workflow` .
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   188
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   189
Configuration migration
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   190
-----------------------
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   191
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   192
The following functions for configuration migration are available in all
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   193
scripts:
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   194
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   195
* `option_renamed(oldname, newname)`, indicates that an option has been renamed
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   196
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   197
* `option_group_change(option, oldgroup, newgroup)`, indicates that an option does not
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   198
  belong anymore to the same group.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   199
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   200
* `option_added(oldname, newname)`, indicates that an option has been added.
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   201
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   202
* `option_removed(oldname, newname)`, indicates that an option has been deleted.
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   203
6059
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   204
The `config` variable is an object which can be used to access the
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   205
configuration values, for reading and updating, with a dictionary-like
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   206
syntax. 
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   207
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   208
Example 1: migration script changing the variable 'sender-addr' in
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   209
all-in-one.conf. The script also checks that in that the instance is
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   210
configured with a known value for that variable, and only updates the
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   211
value in that case.
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   212
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   213
.. sourcecode:: python
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   214
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   215
 wrong_addr = 'cubicweb@loiglab.fr' # known wrong address
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   216
 fixed_addr = 'cubicweb@logilab.fr'
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   217
 configured_addr = config.get('sender-addr')
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   218
 # check that the address has not been hand fixed by a sysadmin
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   219
 if configured_addr == wrong_addr: 
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   220
     config['sender-addr'] = fixed-addr
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   221
     config.save()
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   222
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   223
Example 2: checking the value of the database backend driver, which
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   224
can be useful in case you need to issue backend-dependent raw SQL
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   225
queries in a migration script.
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   226
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   227
.. sourcecode:: python
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   228
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   229
 dbdriver  = config.sources()['system']['db-driver']
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   230
 if dbdriver == "sqlserver2005":
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   231
     # this is now correctly handled by CW :-)
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   232
     sql('ALTER TABLE cw_Xxxx ALTER COLUMN cw_name varchar(64) NOT NULL;')
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   233
     commit()
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   234
 else: # postgresql
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   235
     sync_schema_props_perms(ertype=('Xxxx', 'name', 'String'),
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   236
     syncperms=False)
47f84adcd676 added tip on config file migration in scripts
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents: 5394
diff changeset
   237
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   238
197
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   239
Others migration functions
1632e01a58a9 [doc] Translation to english.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 127
diff changeset
   240
--------------------------
2172
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   241
Those functions are only used for low level operations that could not be
cf8f9180e63e delete-trailing-whitespace
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents: 1714
diff changeset
   242
accomplished otherwise or to repair damaged databases during interactive
1195
79c1abc64304 doc improvements
Emile Anclin <emile.anclin@logilab.fr>
parents: 1168
diff changeset
   243
session. They are available in `repository` scripts:
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   244
2476
1294a6bdf3bf application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents: 2175
diff changeset
   245
* `sql(sql, args=None, ask_confirm=True)`, executes an arbitrary SQL query on the system source
93
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   246
* `add_entity_type_table(etype, commit=True)`
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   247
* `add_relation_type_table(rtype, commit=True)`
9c919a47e140 [doc] total file reorganisation - phase 1 complete
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
diff changeset
   248
* `uninline_relation(rtype, commit=True)`
1440
4fc48f2a1748 [doc] Typos and minor changes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1195
diff changeset
   249
4fc48f2a1748 [doc] Typos and minor changes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1195
diff changeset
   250
4fc48f2a1748 [doc] Typos and minor changes.
Sandrine Ribeau <sandrine.ribeau@logilab.fr>
parents: 1195
diff changeset
   251
[FIXME] Add explanation on how to use cubicweb-ctl shell