From 2bda70f73f5b4132be2a1430cae1db6576ffa4d5 Mon Sep 17 00:00:00 2001 From: Tan Jiang Date: Sun, 18 Sep 2016 18:54:30 +0800 Subject: [PATCH] db migration script for 0.4.0 --- Deploy/db/registry.sql | 4 +- migration/changelog.md | 3 +- migration/db_meta.py | 15 ++++++ migration/migration_harbor/versions/0_4_0.py | 54 ++++++++++++++++++++ 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 migration/migration_harbor/versions/0_4_0.py diff --git a/Deploy/db/registry.sql b/Deploy/db/registry.sql index 991df9b4e..ca30ce855 100644 --- a/Deploy/db/registry.sql +++ b/Deploy/db/registry.sql @@ -123,8 +123,6 @@ create table repository ( creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, primary key (repository_id), - FOREIGN KEY (owner_id) REFERENCES user(user_id), - FOREIGN KEY (project_id) REFERENCES project(project_id), UNIQUE (name) ); @@ -184,4 +182,4 @@ CREATE TABLE IF NOT EXISTS `alembic_version` ( `version_num` varchar(32) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -insert into alembic_version values ('0.3.0'); +insert into alembic_version values ('0.4.0'); diff --git a/migration/changelog.md b/migration/changelog.md index 55b8a560f..696faf6b9 100644 --- a/migration/changelog.md +++ b/migration/changelog.md @@ -26,12 +26,13 @@ Changelog for harbor database schema - alter column `repo_name` on table `access_log` - alter column `email` on table `user` -## TODO +## 0.4.0 - add index `pid_optime (project_id, op_time)` on table `access_log` - add index `poid_uptime (policy_id, update_time)` on table `replication_job` - add column `deleted` to table `replication_policy` - alter column `username` on table `user`: varchar(15)->varchar(32) + - alter column `password` on table `replication_target`: varchar(40)->varchar(128) - alter column `email` on table `user`: varchar(128)->varchar(255) - alter column `name` on table `project`: varchar(30)->varchar(41) - create table `repository` diff --git a/migration/db_meta.py b/migration/db_meta.py index af8f444b0..c928edc17 100644 --- a/migration/db_meta.py +++ b/migration/db_meta.py @@ -125,3 +125,18 @@ class ReplicationJob(Base): update_time = sa.Column(mysql.TIMESTAMP, server_default = sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) __table_args__ = (sa.Index('policy', "policy_id"),) + +class Repository(Base): + __tablename__ = "repository" + + repository_id = sa.Column(sa.Integer, primary_key=True) + name = sa.Column(sa.String(255), nullable=False, unique=True) + project_id = sa.Column(sa.Integer, nullable=False) + owner_id = sa.Column(sa.Integer, nullable=False) + description = sa.Column(sa.Text) + pull_count = sa.Column(sa.Integer,server_default=sa.text("'0'"), nullable=False) + star_count = sa.Column(sa.Integer,server_default=sa.text("'0'"), nullable=False) + creation_time = sa.Column(mysql.TIMESTAMP, server_default = sa.text("CURRENT_TIMESTAMP")) + update_time = sa.Column(mysql.TIMESTAMP, server_default = sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) + + diff --git a/migration/migration_harbor/versions/0_4_0.py b/migration/migration_harbor/versions/0_4_0.py new file mode 100644 index 000000000..ddd96a806 --- /dev/null +++ b/migration/migration_harbor/versions/0_4_0.py @@ -0,0 +1,54 @@ +# Copyright (c) 2008-2016 VMware, Inc. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""0.3.0 to 0.4.0 + +Revision ID: 0.3.0 +Revises: + +""" + +# revision identifiers, used by Alembic. +revision = '0.4.0' +down_revision = '0.3.0' +branch_labels = None +depends_on = None + +from alembic import op +from db_meta import * + +from sqlalchemy.dialects import mysql + +def upgrade(): + """ + update schema&data + """ + bind = op.get_bind() + #alter column user.username, alter column user.email, project.name and add column replication_policy.deleted + op.alter_column('user', 'username', type_=sa.String(32), existing_type=sa.String(15)) + op.alter_column('user', 'email', type_=sa.String(255), existing_type=sa.String(128)) + op.alter_column('project', 'name', type_=sa.String(41), existing_type=sa.String(30), nullable=False) + op.alter_column('replication_target', 'password', type_=sa.String(128), existing_type=sa.String(40)) + op.add_column('replication_policy', sa.Column('deleted', mysql.TINYINT(1), nullable=False, server_default=sa.text("'0'"))) + #create index pid_optime (project_id, op_time) on table access_log, poid_uptime (policy_id, update_time) on table replication_job + op.create_index('pid_optime', 'access_log', ['project_id', 'op_time']) + op.create_index('poid_uptime', 'replication_job', ['policy_id', 'update_time']) + #create tables: repository + Repository.__table__.create(bind) + +def downgrade(): + """ + Downgrade has been disabled. + """ + pass