tarantool / ddl

The DDL module enables you to describe data schema in a declarative YAML-based format.
BSD 2-Clause "Simplified" License
12 stars 6 forks source link

Expanding schema by adding nullable field #104

Open AnaNek opened 1 year ago

AnaNek commented 1 year ago

The ddl module is responsible for applying the schema on the cluster. This module does not allow to modify the schema after applying it.

In Tarantool, there are two types of schema migration that do not require data migration:

To make it easier to write migrations, we should add the ability to expand the schema by adding nullable field to the end of the space.

Example of migrations:

Migration 1:

return {
    up = function()
        local ddl = require('ddl')
        local schema = {
            spaces = {
                keyval = {
                    engine = 'memtx',
                    is_local = false,
                    temporary = false,
                    format = {
                        { name = 'bucket_id', type = 'unsigned', is_nullable = false },
                        { name = 'key', type = 'string', is_nullable = false },
                        { name = 'value', type = 'string', is_nullable = false }
                    },
                    indexes = {{
                        name = 'pk',
                        type = 'TREE',
                        unique = true,
                        parts = {
                            { path = 'key', type = 'string', is_nullable = false }
                        }
                    }, {
                        name = 'bucket_id',
                        type = 'TREE',
                        unique = false,
                        parts = {
                            { path = 'bucket_id', type = 'unsigned', is_nullable = false }
                        }
                    }},
                    sharding_key = { 'key' }
                }
            }
        }

        assert(ddl.check_schema(schema))
        ddl.set_schema(schema)
    end
}

Migration 2:

return {
    up = function()
        local ddl = require('ddl')
        local schema = {
            spaces = {
                keyval = {
                    engine = 'memtx',
                    is_local = false,
                    temporary = false,
                    format = {
                        { name = 'bucket_id', type = 'unsigned', is_nullable = false },
                        { name = 'key', type = 'string', is_nullable = false },
                        { name = 'value', type = 'string', is_nullable = false },
                        { name = 'state', type = 'map', is_nullable = true }
                    },
                    indexes = {{
                        name = 'pk',
                        type = 'TREE',
                        unique = true,
                        parts = {
                            { path = 'key', type = 'string', is_nullable = false }
                        }
                    }, {
                        name = 'bucket_id',
                        type = 'TREE',
                        unique = false,
                        parts = {
                            { path = 'bucket_id', type = 'unsigned', is_nullable = false }
                        }
                    }},
                    sharding_key = { 'key' }
                }
            }
        }

        assert(ddl.check_schema(schema))
        ddl.set_schema(schema)
    end
}