#2074 - Refactor extract ebean-ddlgen module. In future this module would expected to be a test only dependency

This commit is contained in:
rob bygrave committed 2020-10-10 12:28:02 +13:00
1 parent b9047bf280
commit b22bffe770
426 files changed
+5533 -229

No files matched your search

@@ -1 +0,0 @@
io.ebeaninternal.dbmigration.DefaultDbMigration
@@ -1 +0,0 @@
io.ebeaninternal.dbmigration.DdlGeneratorProvider
@@ -1,374 +0,0 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://ebean-orm.github.io/xml/ns/dbmigration"
targetNamespace="http://ebean-orm.github.io/xml/ns/dbmigration" elementFormDefault="qualified">
<!-- =========================================================== -->
<!-- CHANGE LOG -->
<!-- =========================================================== -->
<!-- Root level type : migration -->
<xsd:element name="migration">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="changeSet" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="changeSet">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:group ref="changeSetChildren" minOccurs="0" maxOccurs="unbounded"/>
</xsd:choice>
</xsd:sequence>
<xsd:attribute name="type" type="changeSetType" use="required"/>
<xsd:attribute name="dropsFor" type="xsd:string"/>
<xsd:attribute name="suppressDropsForever" type="xsd:boolean"/>
<xsd:attribute name="generated" type="xsd:boolean"/>
<xsd:attribute name="author" type="xsd:string"/>
<xsd:attribute name="comment" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="changeSetType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="apply"/>
<xsd:enumeration value="pendingDrops"/>
<xsd:enumeration value="baseline"/>
<xsd:enumeration value="drop"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:complexType name="ddl-script">
<xsd:sequence>
<xsd:element name="ddl" type="xsd:string" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="platforms" type="xsd:string"/>
</xsd:complexType>
<!-- =========================================================== -->
<!-- CHANGE SET CHILDREN -->
<!-- =========================================================== -->
<xsd:element name="sql">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="apply" minOccurs="1" maxOccurs="1"/>
<xsd:element ref="rollback" minOccurs="1" maxOccurs="1"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="apply">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<xsd:element name="rollback">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
<!-- CONFIGURATION -->
<xsd:element name="configuration">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="defaultTablespace"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="defaultTablespace">
<xsd:complexType>
<xsd:attribute name="tables" type="xsd:string"/>
<xsd:attribute name="indexes" type="xsd:string"/>
<xsd:attribute name="history" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<!-- TABLE -->
<xsd:element name="createTable">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="column" minOccurs="1" maxOccurs="unbounded"/>
<xsd:element ref="uniqueConstraint" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element ref="foreignKey" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="partitionMode" type="xsd:string"/>
<xsd:attribute name="partitionColumn" type="xsd:string"/>
<xsd:attribute name="withHistory" type="xsd:boolean"/>
<xsd:attribute name="draft" type="xsd:boolean"/>
<xsd:attribute name="identityType" type="identityType"/>
<xsd:attribute name="identityStart" type="xsd:positiveInteger"/>
<xsd:attribute name="identityIncrement" type="xsd:positiveInteger"/>
<xsd:attribute name="identityCache" type="xsd:positiveInteger"/>
<xsd:attribute name="identityGenerated" type="xsd:string"/>
<xsd:attribute name="sequenceName" type="xsd:string"/>
<xsd:attribute name="sequenceInitial" type="xsd:positiveInteger"/>
<xsd:attribute name="sequenceAllocate" type="xsd:positiveInteger"/>
<xsd:attribute name="pkName" type="xsd:string"/>
<xsd:attribute name="storageEngine" type="xsd:string"/>
<xsd:attributeGroup ref="tablespaceAttributes"/>
</xsd:complexType>
</xsd:element>
<xsd:simpleType name="identityType">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="identity"/>
<xsd:enumeration value="sequence"/>
<xsd:enumeration value="generator"/>
<xsd:enumeration value="external"/>
<xsd:enumeration value="default"/>
</xsd:restriction>
</xsd:simpleType>
<!-- Only expected to be used for compound unique constraint -->
<xsd:element name="uniqueConstraint">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="columnNames" type="xsd:string" use="required"/>
<xsd:attribute name="oneToOne" type="xsd:boolean"/>
<xsd:attribute name="nullableColumns" type="xsd:string"/>
<xsd:attribute name="platforms" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="addUniqueConstraint">
<xsd:complexType>
<xsd:attribute name="constraintName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="columnNames" type="xsd:string" use="required"/>
<xsd:attribute name="oneToOne" type="xsd:boolean"/>
<xsd:attribute name="nullableColumns" type="xsd:string"/>
<xsd:attribute name="platforms" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<!-- Only expected to be used for compound foreign keys -->
<xsd:element name="foreignKey">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="columnNames" type="xsd:string" use="required"/>
<xsd:attribute name="refColumnNames" type="xsd:string" use="required"/>
<xsd:attribute name="refTableName" type="xsd:string" use="required"/>
<xsd:attribute name="indexName" type="xsd:string"/>
<xsd:attribute name="onDelete" type="xsd:string"/>
<xsd:attribute name="onUpdate" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="alterForeignKey">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="columnNames" type="xsd:string"/>
<xsd:attribute name="refColumnNames" type="xsd:string"/>
<xsd:attribute name="refTableName" type="xsd:string"/>
<xsd:attribute name="indexName" type="xsd:string"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="onDelete" type="xsd:string"/>
<xsd:attribute name="onUpdate" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="dropTable">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="sequenceCol" type="xsd:string"/>
<xsd:attribute name="sequenceName" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="renameTable">
<xsd:complexType>
<xsd:attribute name="oldName" type="xsd:string" use="required"/>
<xsd:attribute name="newName" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="addTableComment">
<xsd:complexType>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="comment" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<!-- HISTORY -->
<xsd:element name="addHistoryTable">
<xsd:complexType>
<xsd:attribute name="baseTable" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="alterHistoryTable">
<xsd:complexType>
<xsd:attribute name="baseTable" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="dropHistoryTable">
<xsd:complexType>
<xsd:attribute name="baseTable" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
<!-- COLUMN -->
<xsd:element name="addColumn">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="column" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="withHistory" type="xsd:boolean"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="dropColumn">
<xsd:complexType>
<xsd:attribute name="columnName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="withHistory" type="xsd:boolean"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="alterColumn">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="before" minOccurs="0" maxOccurs="unbounded" type="ddl-script"/>
<xsd:element name="after" minOccurs="0" maxOccurs="unbounded" type="ddl-script"/>
</xsd:sequence>
<xsd:attribute name="columnName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="withHistory" type="xsd:boolean"/>
<xsd:attribute name="type" type="xsd:string"/>
<xsd:attribute name="currentType" type="xsd:string"/>
<xsd:attribute name="defaultValue" type="xsd:string"/>
<xsd:attribute name="currentDefaultValue" type="xsd:string"/>
<xsd:attribute name="notnull" type="xsd:boolean"/>
<xsd:attribute name="currentNotnull" type="xsd:boolean"/>
<xsd:attribute name="comment" type="xsd:string"/>
<xsd:attribute name="historyExclude" type="xsd:boolean"/>
<xsd:attribute name="checkConstraint" type="xsd:string"/>
<xsd:attribute name="checkConstraintName" type="xsd:string"/>
<xsd:attribute name="dropCheckConstraint" type="xsd:string"/>
<xsd:attribute name="unique" type="xsd:string"/>
<xsd:attribute name="uniqueOneToOne" type="xsd:string"/>
<xsd:attribute name="dropUnique" type="xsd:string"/>
<xsd:attribute name="references" type="xsd:string"/>
<xsd:attribute name="foreignKeyName" type="xsd:string"/>
<xsd:attribute name="foreignKeyIndex" type="xsd:string"/>
<xsd:attribute name="foreignKeyOnDelete" type="xsd:string"/>
<xsd:attribute name="foreignKeyOnUpdate" type="xsd:string"/>
<xsd:attribute name="dropForeignKey" type="xsd:string"/>
<xsd:attribute name="dropForeignKeyIndex" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="renameColumn">
<xsd:complexType>
<xsd:attribute name="oldName" type="xsd:string" use="required"/>
<xsd:attribute name="newName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="dataType" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<!-- INDEXES -->
<xsd:element name="createIndex">
<xsd:complexType>
<xsd:attribute name="indexName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="columns" type="xsd:string" use="required"/>
<xsd:attribute name="unique" type="xsd:boolean"/>
<xsd:attribute name="concurrent" type="xsd:boolean"/>
<xsd:attribute name="definition" type="xsd:string"/>
<xsd:attribute name="platforms" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:element name="dropIndex">
<xsd:complexType>
<xsd:attribute name="indexName" type="xsd:string" use="required"/>
<xsd:attribute name="tableName" type="xsd:string" use="required"/>
<xsd:attribute name="concurrent" type="xsd:boolean"/>
<xsd:attribute name="platforms" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<!-- ============================================ -->
<xsd:element name="column">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="before" minOccurs="0" maxOccurs="unbounded" type="ddl-script"/>
<xsd:element name="after" minOccurs="0" maxOccurs="unbounded" type="ddl-script"/>
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="type" type="xsd:string" use="required"/>
<xsd:attribute name="defaultValue" type="xsd:string"/>
<xsd:attribute name="notnull" type="xsd:boolean"/>
<xsd:attribute name="historyExclude" type="xsd:boolean"/>
<xsd:attribute name="primaryKey" type="xsd:boolean"/>
<xsd:attribute name="identity" type="xsd:boolean"/>
<xsd:attribute name="checkConstraint" type="xsd:string"/>
<xsd:attribute name="checkConstraintName" type="xsd:string"/>
<xsd:attribute name="unique" type="xsd:string"/>
<xsd:attribute name="uniqueOneToOne" type="xsd:string"/>
<xsd:attribute name="references" type="xsd:string"/>
<xsd:attribute name="foreignKeyName" type="xsd:string"/>
<xsd:attribute name="foreignKeyIndex" type="xsd:string"/>
<xsd:attribute name="foreignKeyOnDelete" type="xsd:string"/>
<xsd:attribute name="foreignKeyOnUpdate" type="xsd:string"/>
<xsd:attribute name="comment" type="xsd:string"/>
</xsd:complexType>
</xsd:element>
<xsd:attributeGroup name="tablespaceAttributes">
<xsd:attribute name="tablespace" type="xsd:string"/>
<xsd:attribute name="indexTablespace" type="xsd:string"/>
<xsd:attribute name="comment" type="xsd:string"/>
</xsd:attributeGroup>
<!-- Children for changeSet -->
<xsd:group name="changeSetChildren">
<xsd:choice>
<xsd:element ref="configuration" maxOccurs="1"/>
<xsd:element ref="sql" maxOccurs="unbounded"/>
<xsd:element ref="createTable" maxOccurs="unbounded"/>
<xsd:element ref="dropTable" maxOccurs="unbounded"/>
<xsd:element ref="renameTable" maxOccurs="unbounded"/>
<xsd:element ref="addTableComment" maxOccurs="unbounded"/>
<xsd:element ref="addUniqueConstraint" maxOccurs="unbounded"/>
<xsd:element ref="addHistoryTable" maxOccurs="unbounded"/>
<xsd:element ref="dropHistoryTable" maxOccurs="unbounded"/>
<xsd:element ref="alterForeignKey" maxOccurs="unbounded"/>
<xsd:element ref="addColumn" maxOccurs="unbounded"/>
<xsd:element ref="dropColumn" maxOccurs="unbounded"/>
<xsd:element ref="alterColumn" maxOccurs="unbounded"/>
<xsd:element ref="renameColumn" maxOccurs="unbounded"/>
<xsd:element ref="createIndex" maxOccurs="unbounded"/>
<xsd:element ref="dropIndex" maxOccurs="unbounded"/>
</xsd:choice>
</xsd:group>
</xsd:schema>
@@ -1,28 +0,0 @@
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://ebean-orm.github.io/xml/ns/extraddl"
targetNamespace="http://ebean-orm.github.io/xml/ns/extraddl" elementFormDefault="qualified">
<!-- Root level type : extra-ddl -->
<xsd:element name="extra-ddl">
<xsd:complexType>
<xsd:sequence>
<xsd:element ref="ddl-script" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ddl-script">
<xsd:complexType>
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="platforms" type="xsd:string"/>
<xsd:attribute name="init" type="xsd:boolean"/>
<xsd:attribute name="drop" type="xsd:boolean"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:schema>
@@ -1,202 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<extra-ddl xmlns="http://ebean-orm.github.io/xml/ns/extraddl">
<ddl-script name="partition help" init="true" platforms="postgres">
-- partitioning helper functions (UTC based)
------------------------------------------------------------------------------------
-- Type: partition_meta
--
-- Type used to hold common partitioning parameters such as period start and end etc
------------------------------------------------------------------------------------
do $$
begin
if not exists (select 1 from pg_type where typname = 'partition_meta') THEN
create type partition_meta as
(
period_start timestamptz,
period_end timestamptz,
period_name text,
base_name text,
part_name text
);
end if;
end$$;
------------------------------------------------------------------------------------
-- Function: _partition_create
--
-- Internal helper method to create a partition given meta data
------------------------------------------------------------------------------------
create or replace function _partition_create(meta partition_meta)
returns text
language plpgsql
set timezone to 'UTC'
as $$
begin
execute format('create table if not exists %I partition of %I for values from (''%s'') TO (''%s'')', meta.part_name, meta.base_name, meta.period_start, meta.period_end);
return meta.part_name;
end;
$$;
------------------------------------------------------------------------------------
-- Function: _partition_meta
--
-- Internal helper method to create and return meta data used to create a partition.
-- Helps work out start and end periods for day, week, month and year partitions.
------------------------------------------------------------------------------------
create or replace function _partition_meta(
mode text,
asOf date,
baseName text)
returns partition_meta
language plpgsql
set timezone to 'UTC'
as $$
declare
partName text;
meta partition_meta;
asOfUtc timestamptz;
begin
asOfUtc = timezone('utc', asOf);
if (mode = 'day') then
asOfUtc = date_trunc('day', asOfUtc);
partName = to_char(asOfUtc, 'YYYY_MM_DD');
select asOfUtc, asOfUtc + interval '1 days' into meta.period_start, meta.period_end;
elseif (mode = 'week') then
asOfUtc = date_trunc('week', asOfUtc);
partName = format('%s_w%s', extract(ISOYEAR FROM asOfUtc), extract(WEEK FROM asOfUtc));
select asOfUtc, asOfUtc + interval '7 days' into meta.period_start, meta.period_end;
elseif (mode = 'year') then
asOfUtc = date_trunc('year', asOfUtc);
partName = to_char(date_trunc('year', asOfUtc), 'YYYY');
select asOfUtc, asOfUtc + interval '1 year' into meta.period_start, meta.period_end;
else
asOfUtc = date_trunc('month', asOfUtc);
partName = to_char(asOfUtc, 'YYYY_MM');
select asOfUtc, asOfUtc + interval '1 month' into meta.period_start, meta.period_end;
end if;
select partName, baseName, format('%s_%s', baseName, partName)
into meta.period_name, meta.base_name, meta.part_name;
return meta;
end;
$$;
------------------------------------------------------------------------------------
-- Function: _partition_over
--
-- Internal helper method to return a set/table of dates to ensure partitions exists for.
-- Typically we want to ensure some future partitions exist and this helps return dates
-- for which we loop to create partitions.
------------------------------------------------------------------------------------
create or replace function _partition_over(
mode text,
fromDate date default current_date,
_count integer default 0)
returns TABLE(of_date date)
language plpgsql
as $$
declare
endDate date;
begin
if (mode = 'day') then
endDate = fromDate + (interval '1 day' * _count);
fromDate = fromDate - interval '1 day'; -- allow for timezone
return query select s::date from generate_series(fromDate, endDate, '1 day') s;
elseif (mode = 'week') then
fromDate = date_trunc('week', fromDate);
endDate = fromDate + (interval '1 week' * _count);
return query select s::date from generate_series(fromDate, endDate, '1 week') s;
elseif (mode = 'year') then
fromDate = date_trunc('year', fromDate);
endDate = fromDate + (interval '1 year' * _count);
return query select s::date from generate_series(fromDate, endDate, '1 year') s;
else
fromDate = date_trunc('month', fromDate);
endDate = fromDate + (interval '1 month' * _count);
return query select s::date from generate_series(fromDate, endDate, '1 month') s;
end if;
end;
$$;
------------------------------------------------------------------------------------
-- Function: partition
--
-- Helper to ensure we create partitions into the future as needed for day, week, month
-- and year based partitioning. Typically we call this periodically (e.g. every day).
--
-- Examples:
--
-- select partition('week', 'trip', 4);
-- select partition('month', 'event', 1);
--
------------------------------------------------------------------------------------
create or replace function partition(
mode text, -- one of 'day','week','month','year'
baseName text, -- base table name
partitionCount integer default 0, -- number of additional partitions
fromDate date default current_date) -- date to create first partition for
returns text
language plpgsql
set timezone to 'UTC'
as $$
begin
perform _partition_create(_partition_meta(mode, poDate, baseName))
from _partition_over(mode, fromDate, partitionCount) poDate;
return 'done';
end;
$$;
------------------------------------------------------------------------------------
-- Function: partition_init
--
-- Similar to partition but allows the first partition to be bigger with an explicit
-- initDate typically to allow back dated rows to go into the initial partition.
--
-- Examples:
--
-- select partition_init(date '2001-01-01', 'week', 'event');
--
------------------------------------------------------------------------------------
create or replace function partition_init(
initDate date, -- first partition period start date
mode text, -- one of 'day','week','month','year'
baseName text, -- base table name
partitionCount integer default 0, -- number of additional partitions
fromDate date default current_date) -- date to create first partition for
returns text
language plpgsql
set timezone to 'UTC'
as $$
declare
meta partition_meta;
begin
-- override the period start for the first partition
meta = _partition_meta(mode, fromDate, baseName);
meta.period_start = initDate;
perform _partition_create(meta);
if (partitionCount > 0) then
-- create additional partitions normally
fromDate = fromDate + interval '1 day';
perform _partition_create(_partition_meta(mode, poDate, baseName))
from _partition_over(mode, fromDate, partitionCount) poDate;
end if;
return 'done';
end;
$$;
</ddl-script>
</extra-ddl>
@@ -1,116 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<extra-ddl xmlns="http://ebean-orm.github.io/xml/ns/extraddl">
<ddl-script name="create procs" platforms="sqlserver" init="true">-- Initial script to create stored procedures etc for sqlserver platform
-- create table-value-parameters
if not exists (select name from sys.types where name = 'ebean_bigint_tvp') create type ebean_bigint_tvp as table (c1 bigint)
GO
if not exists (select name from sys.types where name = 'ebean_float_tvp') create type ebean_float_tvp as table (c1 float)
GO
if not exists (select name from sys.types where name = 'ebean_bit_tvp') create type ebean_bit_tvp as table (c1 bit)
GO
if not exists (select name from sys.types where name = 'ebean_date_tvp') create type ebean_date_tvp as table (c1 date)
GO
if not exists (select name from sys.types where name = 'ebean_time_tvp') create type ebean_time_tvp as table (c1 time)
GO
if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp') create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier)
GO
if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp') create type ebean_nvarchar_tvp as table (c1 nvarchar(max))
GO
--
-- PROCEDURE: usp_ebean_drop_indices TABLE, COLUMN
-- deletes all indices referring to TABLE.COLUMN
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_indices @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
declare @sql nvarchar(1000)
declare @indexName nvarchar(255)
BEGIN
DECLARE index_cursor CURSOR FOR SELECT i.name from sys.indexes i
join sys.index_columns ic on ic.object_id = i.object_id and ic.index_id = i.index_id
join sys.columns c on c.object_id = ic.object_id and c.column_id = ic.column_id
where i.object_id = OBJECT_ID(@tableName) AND c.name = @columnName;
OPEN index_cursor
FETCH NEXT FROM index_cursor INTO @indexName
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = 'drop index ' + @indexName + ' on ' + @tableName;
EXECUTE(@sql);
FETCH NEXT FROM index_cursor INTO @indexName
END;
CLOSE index_cursor;
DEALLOCATE index_cursor;
END
GO
--
-- PROCEDURE: usp_ebean_drop_default_constraint TABLE, COLUMN
-- deletes the default constraint, which has a random name
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_default_constraint @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
declare @tmp nvarchar(1000)
BEGIN
select @tmp = t1.name from sys.default_constraints t1
join sys.columns t2 on t1.object_id = t2.default_object_id
where t1.parent_object_id = OBJECT_ID(@tableName) and t2.name = @columnName;
if @tmp is not null EXEC('alter table ' + @tableName +' drop constraint ' + @tmp);
END
GO
--
-- PROCEDURE: usp_ebean_drop_constraints TABLE, COLUMN
-- deletes constraints and foreign keys refering to TABLE.COLUMN
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_constraints @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
declare @sql nvarchar(1000)
declare @constraintName nvarchar(255)
BEGIN
DECLARE name_cursor CURSOR FOR
SELECT cc.name from sys.check_constraints cc
join sys.columns c on c.object_id = cc.parent_object_id and c.column_id = cc.parent_column_id
where parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName
UNION SELECT fk.name from sys.foreign_keys fk
join sys.foreign_key_columns fkc on fkc.constraint_object_id = fk.object_id
and fkc.parent_object_id = fk.parent_object_id
join sys.columns c on c.object_id = fkc.parent_object_id and c.column_id = fkc.parent_column_id
where fkc.parent_object_id = OBJECT_ID(@tableName) AND c.name = @columnName;
OPEN name_cursor
FETCH NEXT FROM name_cursor INTO @constraintName
WHILE @@FETCH_STATUS = 0
BEGIN
set @sql = 'alter table ' + @tableName + ' drop constraint ' + @constraintName;
EXECUTE(@sql);
FETCH NEXT FROM name_cursor INTO @constraintName
END;
CLOSE name_cursor;
DEALLOCATE name_cursor;
END
GO
--
-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN
-- deletes the column annd ensures that all indices and constraints are dropped first
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_column @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
declare @sql nvarchar(1000)
BEGIN
EXEC usp_ebean_drop_indices @tableName, @columnName;
EXEC usp_ebean_drop_default_constraint @tableName, @columnName;
EXEC usp_ebean_drop_constraints @tableName, @columnName;
set @sql = 'alter table ' + @tableName + ' drop column ' + @columnName;
EXECUTE(@sql);
END
GO
</ddl-script>
</extra-ddl>