数据库升级脚本制作

最近由于公司走了一个负责数据库方面的人,一些有关数据库的任务没人做了,leader就找到了我。这次的任务是负责新版本的数据库升级脚本,难点是我对数据表结构不是很熟悉,而且新版本的功能也不是有我来负责的,还有就是数据表结构变化比较大,要保证用户原本数据不能丢失。200多张表里面还是有数据的,如果人工做的话不死人才怪。于是查找工具,先从自己熟悉的powerdesigner开始,先用反向工程分别生成新老两个数据库的模型,然后点击 菜单中database->modify database,再按照步骤做,没啥难度。完工后生成了一个很长很长的sql, 由于是机器生成,看起来超麻烦,所以一狠心,直接拿了备份升级下试试看。执行好后,报出一堆错,看看表是改了,但数据都丢失了,也不确定是不是所以的表都升级了。唉,这对于表变化比较大或原有数据比较多的情况根本行不通。

注意图中的 Use alter statements when possible选项,说的太模棱两可。我试了下,大多数只要表变化一复杂,他就开始偷懒不用alter来搞表,直接create然后把原表的数据导入。先不说效率,万一丢失了数据可不好。

如果你的表和数据都变化比较少,那PD也算个好方法,毕竟方便,高效。如果不是,下面介绍个好工具SQL Delta,这个用来对比数据库的确好用。

进入系统,设置好要升级后的数据库和升级前的数据库,然后点compare databases来比较表结构

大家可以发现字段变化看的很清楚,有多的有少的,也有改变类型的。一般建议升级时多余的字段不要删除,只增加和修改。

大家可以在右边看到工具建议的升级脚本,由于是一个表一个表查看,这样虽然工作量大了,但比较放心,而且脚本可读性较好。

这里也要注意对于表变化比较大的情况,这工具还是采用以下方法(先建立临时表,导入原表数据,然后删除原表,临时表更名成原表的名字)

IF @@TRANCOUNT = 1 CREATE TABLE [dbo].[tmp_TICKET] ( [TICKET_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [TICKET_NUM] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [TICKET_SUBJECT] [varchar] (200) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [TICKET_DESC] [varchar] (2500) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [PRIORITY] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [SEVERITY] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [STATUS_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [CHANNEL_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [MACHINE_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [REQUESTER_CONTACT_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [OWNER_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [ASSIGNEE_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ESCALATED_TO] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ITEM_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [RESOLUTION_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [FOLLOWUP_FLAG] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [REPORT_TIME] [datetime] NOT NULL, [CREATE_TIME] [datetime] NOT NULL, [DISPATCH_TIME] [datetime] NULL, [ASSIGN_TIME] [datetime] NULL, [CONFIRM_TIME] [datetime] NULL, [RESPONSE_TIME] [datetime] NULL, [START_WORKING_TIME] [datetime] NULL, [SLA_RESP_EXPECTED] [datetime] NULL, [SLA_RESP_HOURS] [decimal] (4, 0) NULL, [RESPONSE_TIME_EXPECTED] [datetime] NULL, [RESP_TIME_ACTUAL_VAL] [decimal] (10, 4) NULL, [RESP_TIMELINE_TYPE] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [CHRONIC_ACTION] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [RECOVER_TIME] [datetime] NULL, [FIXED_TIME] [datetime] NULL, [SLA_FIXED_EXPECTED] [datetime] NULL, [FIXED_TIME_EXPECTED] [datetime] NULL, [FIXED_TIME_ACTUAL_VAL] [decimal] (10, 4) NULL, [FIXED_TIMELINE_TYPE] [char] (3) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [SUSPEND_TIME] [datetime] NULL, [CLOSED_TIME] [datetime] NULL, [CONTRACT_FLAG] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [PARENT_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [PARENT_FLAG] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [GROUP_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [OWNER_GROUP_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [PREVIOUS_GROUP_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [SP_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [CUSTOMER_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [CLOSED_BY] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [ASSIGNMENT_FLAG] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [IS_AUTO_CREATED] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [IS_AUTO_FIXED] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [AUTO_FIX_ENABLE] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF__TICKET__AUTO_FIX__0F624AF8] DEFAULT ('1'), [AUTO_RESPONSE_ENABLE] [char] (1) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF__TICKET__AUTO_RES__10566F31] DEFAULT ('1'), [REOPEN_TIMES] [int] NULL, [RESPONSE_TIME_MANUAL] [datetime] NULL, [FIXED_TIME_MANUAL] [datetime] NULL, [HISTORY_ID] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, [LAST_UPDATED] [datetime] NOT NULL, [FIX_EXCLUDE_TIME_VAL] [decimal] (10, 4) NULL, [TICKET_DOMNTIME] [decimal] (10, 4) NULL, [EXTENAL_REFERENCE_NO] [varchar] (32) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) GO IF @@ERROR <> 0 IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION GO IF @@TRANCOUNT = 1 INSERT INTO [dbo].[tmp_TICKET] ([TICKET_ID], [TICKET_NUM], [TICKET_SUBJECT], [TICKET_DESC], [PRIORITY], [SEVERITY], [STATUS_ID], [CHANNEL_ID], [MACHINE_ID], [REQUESTER_CONTACT_ID], [OWNER_ID], [ASSIGNEE_ID], [ESCALATED_TO], [ITEM_ID], [RESOLUTION_ID], [FOLLOWUP_FLAG], [REPORT_TIME], [CREATE_TIME], [DISPATCH_TIME], [ASSIGN_TIME], [CONFIRM_TIME], [RESPONSE_TIME], [START_WORKING_TIME], [SLA_RESP_EXPECTED], [SLA_RESP_HOURS], [RESPONSE_TIME_EXPECTED], [RESP_TIME_ACTUAL_VAL], [RESP_TIMELINE_TYPE], [CHRONIC_ACTION], [RECOVER_TIME], [FIXED_TIME], [SLA_FIXED_EXPECTED], [FIXED_TIME_EXPECTED], [FIXED_TIME_ACTUAL_VAL], [FIXED_TIMELINE_TYPE], [SUSPEND_TIME], [CLOSED_TIME], [CONTRACT_FLAG], [PARENT_ID], [PARENT_FLAG], [GROUP_ID], [OWNER_GROUP_ID], [PREVIOUS_GROUP_ID], [SP_ID], [CUSTOMER_ID], [CLOSED_BY], [ASSIGNMENT_FLAG], [IS_AUTO_CREATED], [IS_AUTO_FIXED], [AUTO_FIX_ENABLE], [REOPEN_TIMES], [RESPONSE_TIME_MANUAL], [FIXED_TIME_MANUAL], [HISTORY_ID], [LAST_UPDATED], [FIX_EXCLUDE_TIME_VAL], [TICKET_DOMNTIME], [EXTENAL_REFERENCE_NO]) SELECT [TICKET_ID], [TICKET_NUM], [TICKET_SUBJECT], [TICKET_DESC], [PRIORITY], [SEVERITY], [STATUS_ID], [CHANNEL_ID], [MACHINE_ID], [REQUESTER_CONTACT_ID], [OWNER_ID], [ASSIGNEE_ID], [ESCALATED_TO], [ITEM_ID], [RESOLUTION_ID], [FOLLOWUP_FLAG], [REPORT_TIME], [CREATE_TIME], [DISPATCH_TIME], [ASSIGN_TIME], [CONFIRM_TIME], [RESPONSE_TIME], [START_WORKING_TIME], [SLA_RESP_EXPECTED], [SLA_RESP_HOURS], [RESPONSE_TIME_EXPECTED], [RESP_TIME_ACTUAL_VAL], [RESP_TIMELINE_TYPE], NULL, [RECOVER_TIME], [FIXED_TIME], [SLA_FIXED_EXPECTED], [FIXED_TIME_EXPECTED], [FIXED_TIME_ACTUAL_VAL], [FIXED_TIMELINE_TYPE], [SUSPEND_TIME], [CLOSED_TIME], [CONTRACT_FLAG], [PARENT_ID], [PARENT_FLAG], [GROUP_ID], [OWNER_GROUP_ID], [PREVIOUS_GROUP_ID], [SP_ID], [CUSTOMER_ID], [CLOSED_BY], [ASSIGNMENT_FLAG], [IS_AUTO_CREATED], [IS_AUTO_FIXED], [AUTO_FIX_ENABLE], NULL, [RESPONSE_TIME_MANUAL], [FIXED_TIME_MANUAL], [HISTORY_ID], [LAST_UPDATED], NULL, NULL, NULL FROM [dbo].[TICKET] GO IF @@ERROR <> 0 IF @@TRANCOUNT = 1 ROLLBACK TRANSACTION GO IF @@TRANCOUNT = 1 DROP TABLE [dbo].[TICKET] GO sp_rename N'[dbo].[tmp_TICKET]', N'TICKET'

接下来比较表内的数据,按菜单栏中的data 然后点击wizard

选中所要比较数据的表,一般是些配置用表,用户的数据不要丢失就行。

大家可以观察A,B来知道数据的变化,想在升级脚本中体现的话,就选中不想生效的就去掉勾。最后点击Sync进入后save一把,脚本就生成出来了。这个数据库升级的DDL和DML都有了,主要生成好还是要多测试下,毕竟升级有风险,升级前请做好充足的备份。

你可能感兴趣的:(数据库基础)