mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 03:57:45 +00:00
Migration with procedures (#1451)
DB Migration with helper stored procedures (for MySql and SQL Server)
This commit is contained in:
committed by
Rob Bygrave
parent
7d38933e9d
commit
5c99986476
@@ -314,24 +314,28 @@ public class DefaultDbMigration implements DbMigration {
|
||||
private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform) throws IOException {
|
||||
|
||||
if (dbPlatform != null) {
|
||||
ExtraDdl extraDdl = ExtraDdlXmlReader.read("/extra-ddl.xml");
|
||||
if (extraDdl != null) {
|
||||
List<DdlScript> ddlScript = extraDdl.getDdlScript();
|
||||
for (DdlScript script : ddlScript) {
|
||||
if (!script.isDrop() && ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) {
|
||||
generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin());
|
||||
generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.read());
|
||||
}
|
||||
}
|
||||
|
||||
private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl) throws IOException {
|
||||
if (extraDdl != null) {
|
||||
List<DdlScript> ddlScript = extraDdl.getDdlScript();
|
||||
for (DdlScript script : ddlScript) {
|
||||
if (!script.isDrop() && ExtraDdlXmlReader.matchPlatform(dbPlatform.getName(), script.getPlatforms())) {
|
||||
writeExtraDdl(migrationDir, script);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write (or override) the "repeatable" migration script.
|
||||
*/
|
||||
private void writeExtraDdl(File migrationDir, DdlScript script) throws IOException {
|
||||
|
||||
String fullName = repeatableMigrationName(script.getName());
|
||||
String fullName = repeatableMigrationName(script.isInit(), script.getName());
|
||||
|
||||
logger.info("writing repeatable script {}", fullName);
|
||||
|
||||
@@ -342,8 +346,18 @@ public class DefaultDbMigration implements DbMigration {
|
||||
}
|
||||
}
|
||||
|
||||
private String repeatableMigrationName(String scriptName) {
|
||||
return "R__" + scriptName.replace(' ', '_') + migrationConfig.getApplySuffix();
|
||||
|
||||
|
||||
private String repeatableMigrationName(boolean init, String scriptName) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (init) {
|
||||
sb.append("I__");
|
||||
} else {
|
||||
sb.append("R__");
|
||||
}
|
||||
sb.append(scriptName.replace(' ', '_'));
|
||||
sb.append(migrationConfig.getApplySuffix());
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,6 +38,16 @@ public class MySqlDdl extends PlatformDdl {
|
||||
return "alter table " + tableName + " drop foreign key " + maxConstraintName(fkName);
|
||||
}
|
||||
|
||||
/**
|
||||
* It is rather complex to delete a column on MySql as there must not exist any foreign keys.
|
||||
* That's why we call a user stored procedure here
|
||||
*/
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException {
|
||||
|
||||
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String alterTableDropConstraint(String tableName, String constraintName) {
|
||||
// drop constraint not supported in MySQL 5.7 and 8.0 but starting with MariaDB 10.2.1 CHECK is evaluated
|
||||
@@ -61,7 +71,7 @@ public class MySqlDdl extends PlatformDdl {
|
||||
@Override
|
||||
public String alterColumnDefaultValue(String tableName, String columnName, String defaultValue) {
|
||||
|
||||
String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + defaultValue;
|
||||
String suffix = DdlHelp.isDropDefault(defaultValue) ? columnDropDefault : columnSetDefault + " " + convertDefaultValue(defaultValue);
|
||||
|
||||
// use alter
|
||||
return "alter table " + tableName + " alter " + columnName + " " + suffix;
|
||||
|
||||
+43
-22
@@ -3,7 +3,9 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
|
||||
import io.ebean.annotation.ConstraintMode;
|
||||
import io.ebean.config.dbplatform.DatabasePlatform;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
|
||||
import io.ebeaninternal.dbmigration.migration.AlterColumn;
|
||||
import io.ebeaninternal.server.persist.platform.MultiValueBind;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -137,13 +139,7 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
// a rather complex statement.
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (DdlHelp.isDropDefault(defaultValue)) {
|
||||
sb.append("delimiter $$\n");
|
||||
sb.append("DECLARE @Tmp nvarchar(200);");
|
||||
sb.append("select @Tmp = t1.name from sys.default_constraints t1\n");
|
||||
sb.append(" join sys.columns t2 on t1.object_id = t2.default_object_id\n");
|
||||
sb.append(" where t1.parent_object_id = OBJECT_ID('").append(tableName)
|
||||
.append("') and t2.name = '").append(columnName).append("';\n");
|
||||
sb.append("if @Tmp is not null EXEC('alter table ").append(tableName).append(" drop constraint ' + @Tmp)$$");
|
||||
sb.append("EXEC usp_ebean_drop_default_constraint ").append(tableName).append(", ").append(columnName);
|
||||
} else {
|
||||
sb.append("alter table ").append(tableName);
|
||||
sb.append(" add default ").append(convertDefaultValue(defaultValue)).append(" for ").append(columnName);
|
||||
@@ -200,25 +196,50 @@ public class SqlServerDdl extends PlatformDdl {
|
||||
|
||||
/**
|
||||
* It is rather complex to delete a column on SqlServer as there must not exist any references
|
||||
* (constraints, default values, indices and foreign keys). The list is not yet complete, as
|
||||
* indices over multiple columns will not yet deleted.
|
||||
* (This may be changed to delete all refering objects by using the sys.* tables later)
|
||||
* (constraints, default values, indices and foreign keys). That's why we call a user stored procedure here
|
||||
*/
|
||||
@Override
|
||||
public void alterTableDropColumn(DdlBuffer buffer, String tableName, String columnName) throws IOException {
|
||||
buffer.append("-- drop column ").append(tableName).append(".").append(columnName).endOfStatement();
|
||||
|
||||
buffer.append(alterTableDropUniqueConstraint(tableName, naming.uniqueConstraintName(tableName, columnName)));
|
||||
buffer.endOfStatement();
|
||||
buffer.append(alterColumnDefaultValue(tableName, columnName, DdlHelp.DROP_DEFAULT));
|
||||
buffer.endOfStatement();
|
||||
buffer.append(alterTableDropConstraint(tableName, naming.checkConstraintName(tableName, columnName)));
|
||||
buffer.endOfStatement();
|
||||
buffer.append(dropIndex(naming.indexName(tableName, columnName), tableName));
|
||||
buffer.endOfStatement();
|
||||
buffer.append(alterTableDropForeignKey(tableName, naming.foreignKeyConstraintName(tableName, columnName)));
|
||||
buffer.endOfStatement();
|
||||
super.alterTableDropColumn(buffer, tableName, columnName);
|
||||
buffer.append("EXEC usp_ebean_drop_column ").append(tableName).append(", ").append(columnName).endOfStatement();
|
||||
}
|
||||
|
||||
/**
|
||||
* This writes the multi value datatypes needed for {@link MultiValueBind}
|
||||
*/
|
||||
@Override
|
||||
public void generateProlog(DdlWrite write) throws IOException {
|
||||
super.generateProlog(write);
|
||||
|
||||
generateTVPDefinitions(write, "bigint");
|
||||
generateTVPDefinitions(write, "float");
|
||||
generateTVPDefinitions(write, "bit");
|
||||
generateTVPDefinitions(write, "date");
|
||||
generateTVPDefinitions(write, "time");
|
||||
//generateTVPDefinitions(write, "datetime2");
|
||||
generateTVPDefinitions(write, "uniqueidentifier");
|
||||
generateTVPDefinitions(write, "nvarchar(max)");
|
||||
|
||||
}
|
||||
|
||||
private void generateTVPDefinitions(DdlWrite write, String definition) throws IOException {
|
||||
int pos = definition.indexOf('(');
|
||||
String name = pos == -1 ? definition : definition.substring(0, pos);
|
||||
|
||||
dropTVP(write.dropAll(), name);
|
||||
//TVPs are included in "I__create_procs.sql"
|
||||
//createTVP(write.apply(), name, definition);
|
||||
}
|
||||
|
||||
private void dropTVP(DdlBuffer ddl, String name) throws IOException {
|
||||
ddl.append("if exists (select name from sys.types where name = 'ebean_").append(name)
|
||||
.append("_tvp') drop type ebean_").append(name).append("_tvp").endOfStatement();
|
||||
}
|
||||
|
||||
private void createTVP(DdlBuffer ddl, String name, String definition) throws IOException {
|
||||
ddl.append("if not exists (select name from sys.types where name = 'ebean_").append(name)
|
||||
.append("_tvp') create type ebean_").append(name).append("_tvp as table (c1 ").append(definition).append(")")
|
||||
.endOfStatement();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,8 +9,12 @@ import io.ebeaninternal.dbmigration.migration.ChangeSet;
|
||||
import io.ebeaninternal.dbmigration.model.build.ModelBuildBeanVisitor;
|
||||
import io.ebeaninternal.dbmigration.model.build.ModelBuildContext;
|
||||
import io.ebeaninternal.dbmigration.model.visitor.VisitAllUsing;
|
||||
import io.ebeaninternal.extraddl.model.DdlScript;
|
||||
import io.ebeaninternal.extraddl.model.ExtraDdl;
|
||||
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Reads EbeanServer bean descriptors to build the current model.
|
||||
@@ -111,6 +115,19 @@ public class CurrentModel {
|
||||
if (header != null && !header.isEmpty()) {
|
||||
ddl.append(header).append('\n');
|
||||
}
|
||||
|
||||
ExtraDdl extraDdl = ExtraDdlXmlReader.readBuiltin();
|
||||
if (extraDdl != null) {
|
||||
List<DdlScript> ddlScript = extraDdl.getDdlScript();
|
||||
for (DdlScript script : ddlScript) {
|
||||
if (script.isInit() && ExtraDdlXmlReader.matchPlatform(server.getDatabasePlatform().getName(), script.getPlatforms())) {
|
||||
ddl.append("-- init script " + script.getName()).append('\n');
|
||||
ddl.append(script.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
ddl.append(write.apply().getBuffer());
|
||||
ddl.append(write.applyForeignKeys().getBuffer());
|
||||
ddl.append(write.applyHistoryView().getBuffer());
|
||||
|
||||
+2
-1
@@ -258,12 +258,13 @@ public class ModelBuildPropertyVisitor extends BaseTablePropertyVisitor {
|
||||
}
|
||||
} else {
|
||||
col.setDefaultValue(p.getDbColumnDefault());
|
||||
col.setDbMigrationInfos(p.getDbMigrationInfos());
|
||||
if (!p.isNullable() || p.isDDLNotNull()) {
|
||||
col.setNotnull(true);
|
||||
}
|
||||
}
|
||||
|
||||
col.setDbMigrationInfos(p.getDbMigrationInfos());
|
||||
|
||||
if (p.isUnique() && !p.isId()) {
|
||||
col.setUnique(determineUniqueConstraintName(col.getName()));
|
||||
indexSetAdd(col.getName());
|
||||
|
||||
@@ -39,7 +39,8 @@ public class DdlScript {
|
||||
protected String platforms;
|
||||
@XmlAttribute(name = "drop")
|
||||
protected boolean drop;
|
||||
|
||||
@XmlAttribute(name = "init")
|
||||
protected boolean init;
|
||||
/**
|
||||
* Gets the value of the value property.
|
||||
*
|
||||
@@ -114,4 +115,17 @@ public class DdlScript {
|
||||
this.drop = drop;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return if this a init script.
|
||||
*/
|
||||
public boolean isInit() {
|
||||
return init;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets that this is a init script.
|
||||
*/
|
||||
public void setInit(boolean init) {
|
||||
this.init = init;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,10 +83,24 @@ public class ExtraDdlXmlReader {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the builtin extra ddl. (Stored procedures, tvp types etc)
|
||||
*/
|
||||
public static ExtraDdl readBuiltin() {
|
||||
return read("/io/ebeaninternal/dbmigration/builtin-extra-ddl.xml");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the extra ddl.
|
||||
*/
|
||||
public static ExtraDdl read() {
|
||||
return read("/extra-ddl.xml");
|
||||
}
|
||||
|
||||
/**
|
||||
* Read and return a ExtraDdl from an xml document at the given resource path.
|
||||
*/
|
||||
public static ExtraDdl read(String resourcePath) {
|
||||
private static ExtraDdl read(String resourcePath) {
|
||||
|
||||
try (InputStream is = ExtraDdlXmlReader.class.getResourceAsStream(resourcePath)) {
|
||||
if (is == null) {
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
<?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);
|
||||
if not exists (select name from sys.types where name = 'ebean_float_tvp') create type ebean_float_tvp as table (c1 float);
|
||||
if not exists (select name from sys.types where name = 'ebean_bit_tvp') create type ebean_bit_tvp as table (c1 bit);
|
||||
if not exists (select name from sys.types where name = 'ebean_date_tvp') create type ebean_date_tvp as table (c1 date);
|
||||
if not exists (select name from sys.types where name = 'ebean_time_tvp') create type ebean_time_tvp as table (c1 time);
|
||||
if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp') create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier);
|
||||
if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp') create type ebean_nvarchar_tvp as table (c1 nvarchar(max));
|
||||
|
||||
delimiter $$
|
||||
-----------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
--------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
--------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
-------------------------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
</ddl-script>
|
||||
|
||||
<ddl-script name="create procs" platforms="mysql" init="true">-- Inital script to create stored procedures etc for mysql platform
|
||||
DROP PROCEDURE IF EXISTS usp_ebean_drop_foreign_keys;
|
||||
|
||||
delimiter $$
|
||||
------------------------------------------------------------------------------
|
||||
-- PROCEDURE: usp_ebean_drop_foreign_keys TABLE, COLUMN
|
||||
-- deletes all constraints and foreign keys referring to TABLE.COLUMN
|
||||
------------------------------------------------------------------------------
|
||||
CREATE PROCEDURE usp_ebean_drop_foreign_keys(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
|
||||
BEGIN
|
||||
DECLARE done INT DEFAULT FALSE;
|
||||
DECLARE c_fk_name CHAR(255);
|
||||
DECLARE curs CURSOR FOR SELECT CONSTRAINT_NAME from information_schema.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = DATABASE() and TABLE_NAME = p_table_name and COLUMN_NAME = p_column_name
|
||||
AND REFERENCED_TABLE_NAME IS NOT NULL;
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
|
||||
|
||||
OPEN curs;
|
||||
|
||||
read_loop: LOOP
|
||||
FETCH curs INTO c_fk_name;
|
||||
IF done THEN
|
||||
LEAVE read_loop;
|
||||
END IF;
|
||||
SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP FOREIGN KEY ', c_fk_name);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
END LOOP;
|
||||
|
||||
CLOSE curs;
|
||||
END
|
||||
$$
|
||||
|
||||
DROP PROCEDURE IF EXISTS usp_ebean_drop_column;
|
||||
|
||||
delimiter $$
|
||||
-------------------------------------------------------------------------------------
|
||||
-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN
|
||||
-- deletes the column and ensures that all indices and constraints are dropped first
|
||||
-------------------------------------------------------------------------------------
|
||||
CREATE PROCEDURE usp_ebean_drop_column(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
|
||||
BEGIN
|
||||
CALL usp_ebean_drop_foreign_keys(p_table_name, p_column_name);
|
||||
SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP COLUMN ', p_column_name);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
END
|
||||
$$
|
||||
</ddl-script>
|
||||
</extra-ddl>
|
||||
@@ -7,6 +7,7 @@ import io.ebean.SqlUpdate;
|
||||
import io.ebean.Transaction;
|
||||
import io.ebean.annotation.IgnorePlatform;
|
||||
import io.ebean.annotation.Platform;
|
||||
import io.ebean.migration.MigrationConfig;
|
||||
import io.ebean.migration.ddl.DdlRunner;
|
||||
import io.ebeaninternal.dbmigration.ddlgeneration.Helper;
|
||||
|
||||
@@ -16,7 +17,6 @@ import javax.persistence.PersistenceException;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
@@ -84,6 +84,9 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
"migtest_oto_child",
|
||||
"migtest_oto_master");
|
||||
|
||||
if (isSqlServer() || isMySql()) {
|
||||
runScript(false, "I__create_procs.sql");
|
||||
}
|
||||
|
||||
runScript(false, "1.0__initial.sql");
|
||||
|
||||
@@ -103,7 +106,6 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
assertThat(server().execute(update)).isEqualTo(2);
|
||||
}
|
||||
|
||||
|
||||
createHistoryEntities();
|
||||
|
||||
// Run migration
|
||||
@@ -121,7 +123,7 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
|
||||
assertThat(row.getString("new_string_field")).isEqualTo("foo'bar");
|
||||
assertThat(row.getBoolean("new_boolean_field2")).isTrue();
|
||||
assertThat(row.getTimestamp("some_date")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00
|
||||
//assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 86_000); // allow 1 minute delta
|
||||
|
||||
row = result.get(1);
|
||||
assertThat(row.getInteger("id")).isEqualTo(2);
|
||||
@@ -130,15 +132,10 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
|
||||
assertThat(row.getString("new_string_field")).isEqualTo("foo'bar");
|
||||
assertThat(row.getBoolean("new_boolean_field2")).isTrue();
|
||||
assertThat(row.getTimestamp("some_date")).isEqualTo(new Timestamp(100, 0, 1, 0, 0, 0, 0)); // = 2000-01-01T00:00:00
|
||||
//assertThat(row.getTimestamp("some_date")).isCloseTo(new Date(), 60_000); // allow 1 minute delta
|
||||
|
||||
// Run migration & drops
|
||||
if (isMySql()) {
|
||||
return; // TODO: mysql cannot drop table (need stored procedure for drop column)
|
||||
}
|
||||
runScript(false, "1.2__dropsFor_1.1.sql");
|
||||
|
||||
|
||||
// Oracle caches the statement and does not detect schema change. It fails with
|
||||
// an ORA-01007
|
||||
if (isOracle()) {
|
||||
@@ -191,7 +188,7 @@ public class DbMigrationTest extends BaseTestCase {
|
||||
update = server().createSqlUpdate("update migtest_e_history5 set test_number = 45 where id = 1");
|
||||
assertThat(server().execute(update)).isEqualTo(1);
|
||||
|
||||
update = server().createSqlUpdate("insert into migtest_e_history6 (id, test_number2) values (1, 7)");
|
||||
update = server().createSqlUpdate("insert into migtest_e_history6 (id, test_number1, test_number2) values (1, 2, 7)");
|
||||
assertThat(server().execute(update)).isEqualTo(1);
|
||||
update = server().createSqlUpdate("update migtest_e_history6 set test_number2 = 45 where id = 1");
|
||||
assertThat(server().execute(update)).isEqualTo(1);
|
||||
|
||||
+1
-1
@@ -185,7 +185,7 @@ public class PlatformDdl_AlterColumnTest {
|
||||
assertEquals("alter table mytab alter acol drop default", sql);
|
||||
|
||||
sql = sqlServerDdl.alterColumnDefaultValue("mytab", "acol", "DROP DEFAULT");
|
||||
assertThat(sql).startsWith("delimiter $$").endsWith("$$");
|
||||
assertEquals("EXEC usp_ebean_drop_default_constraint mytab, acol", sql);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -12,7 +12,7 @@ public class ExtraDdlXmlReaderTest {
|
||||
@Test
|
||||
public void read(){
|
||||
|
||||
ExtraDdl read = ExtraDdlXmlReader.read("/extra-ddl.xml");
|
||||
ExtraDdl read = ExtraDdlXmlReader.read();
|
||||
assertNotNull(read);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.validation.constraints.Size;
|
||||
@Embeddable
|
||||
public class CKeyParentId {
|
||||
|
||||
@Size(max=127)
|
||||
Integer oneKey;
|
||||
|
||||
@Size(max=127)
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.validation.constraints.Size;
|
||||
@Embeddable
|
||||
public class CKeyParentId {
|
||||
|
||||
@Size(max=127)
|
||||
Integer oneKey;
|
||||
|
||||
@Size(max=127)
|
||||
|
||||
@@ -62,8 +62,10 @@ public class EBasic {
|
||||
@Size(max=127)
|
||||
String description;
|
||||
|
||||
@NotNull
|
||||
@DbDefault("2000-01-01T00:00:00")
|
||||
//@NotNull
|
||||
//@DbDefault("2000-01-01T00:00:00") //- date time literals do not work for each platform yet
|
||||
//@DbDefault("now") //- now does not work for mariaDb
|
||||
// MariaDb requires: ALTER TABLE `migtest_e_basic` CHANGE `some_date` `some_date` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP;
|
||||
Timestamp someDate;
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -4,6 +4,7 @@ package misc.migration.v1_1;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import io.ebean.annotation.DbDefault;
|
||||
import io.ebean.annotation.History;
|
||||
@@ -14,18 +15,21 @@ import io.ebean.annotation.NotNull;
|
||||
@Table(name = "migtest_e_history2")
|
||||
@History
|
||||
public class EHistory2 {
|
||||
|
||||
|
||||
@Id
|
||||
Integer id;
|
||||
|
||||
|
||||
@NotNull
|
||||
@DbDefault("unknown")
|
||||
String testString;
|
||||
|
||||
@HistoryExclude
|
||||
String testString2;
|
||||
|
||||
|
||||
@NotNull
|
||||
@DbDefault("unknown")
|
||||
String testString3;
|
||||
|
||||
@Size(max = 20)
|
||||
String newColumn;
|
||||
}
|
||||
|
||||
@@ -6,7 +6,6 @@ import javax.validation.constraints.Size;
|
||||
@Embeddable
|
||||
public class CKeyParentId {
|
||||
|
||||
@Size(max=127)
|
||||
Integer oneKey;
|
||||
|
||||
@Size(max=127)
|
||||
|
||||
@@ -39,10 +39,6 @@ alter table migtest_e_basic add constraint ck_mgtst__bsc_stts check ( status in
|
||||
-- rename all collisions;
|
||||
-- NOT SUPPORTED alter table migtest_e_basic add constraint uq_mgtst__b_vs45xo unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic alter column some_date set not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_mgtst__bsc_sr_d foreign key (user_id) references migtest_e_user (id) on delete restrict;
|
||||
alter table migtest_e_basic alter column user_id set null;
|
||||
@@ -72,6 +68,7 @@ alter table migtest_e_history2 alter column test_string set default 'unknown';
|
||||
alter table migtest_e_history2 alter column test_string set not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number bigint;
|
||||
alter table migtest_e_history5 add column test_boolean boolean default false not null;
|
||||
|
||||
@@ -19,8 +19,6 @@ alter table migtest_e_basic alter column status drop default;
|
||||
alter table migtest_e_basic alter column status set null;
|
||||
alter table migtest_e_basic add constraint ck_mgtst__bsc_stts check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_mgtst__b_vs45xo;
|
||||
alter table migtest_e_basic alter column some_date drop default;
|
||||
alter table migtest_e_basic alter column some_date set null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop constraint fk_mgtst__bsc_sr_d;
|
||||
|
||||
@@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2;
|
||||
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
|
||||
@@ -13,7 +13,7 @@ create table migtest_ckey_detail (
|
||||
);
|
||||
|
||||
create table migtest_ckey_parent (
|
||||
one_key integer(127) not null,
|
||||
one_key integer not null,
|
||||
two_key varchar(127) not null,
|
||||
name varchar(255),
|
||||
version integer not null,
|
||||
|
||||
@@ -23,7 +23,7 @@ create table migtest_mtm_m_migtest_mtm_c (
|
||||
constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id)
|
||||
);
|
||||
|
||||
alter table migtest_ckey_detail add column one_key integer(127);
|
||||
alter table migtest_ckey_detail add column one_key integer;
|
||||
alter table migtest_ckey_detail add column two_key varchar(127);
|
||||
|
||||
alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key) on delete restrict on update restrict;
|
||||
@@ -45,10 +45,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic alter column some_date set not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict;
|
||||
alter table migtest_e_basic alter column user_id set null;
|
||||
@@ -78,8 +74,10 @@ alter table migtest_e_history2 alter column test_string set default 'unknown';
|
||||
alter table migtest_e_history2 alter column test_string set not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
alter table migtest_e_history2_history add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown';
|
||||
alter table migtest_e_history2_history add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number bigint;
|
||||
alter table migtest_e_history4_history alter column test_number bigint;
|
||||
@@ -136,7 +134,7 @@ create view migtest_e_history4_with_history as select * from migtest_e_history4
|
||||
create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history;
|
||||
|
||||
create trigger migtest_e_history_history_upd before update,delete on migtest_e_history for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger";
|
||||
-- changes: [add test_string2, add test_string3]
|
||||
-- changes: [add test_string2, add test_string3, add new_column]
|
||||
drop trigger migtest_e_history2_history_upd;
|
||||
create trigger migtest_e_history2_history_upd before update,delete on migtest_e_history2 for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger";
|
||||
-- changes: [exclude test_string]
|
||||
|
||||
@@ -24,8 +24,6 @@ alter table migtest_e_basic alter column status drop default;
|
||||
alter table migtest_e_basic alter column status set null;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic alter column some_date drop default;
|
||||
alter table migtest_e_basic alter column some_date set null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id;
|
||||
|
||||
@@ -32,6 +32,9 @@ alter table migtest_e_history2_history drop column test_string2;
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
alter table migtest_e_history2_history drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
alter table migtest_e_history2_history drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
alter table migtest_e_history5_history drop column test_boolean;
|
||||
|
||||
@@ -47,7 +50,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2
|
||||
|
||||
create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history;
|
||||
|
||||
-- changes: [drop test_string2, drop test_string3]
|
||||
-- changes: [drop test_string2, drop test_string3, drop new_column]
|
||||
drop trigger migtest_e_history2_history_upd;
|
||||
create trigger migtest_e_history2_history_upd before update,delete on migtest_e_history2 for each row call "io.ebean.config.dbplatform.h2.H2HistoryTrigger";
|
||||
-- changes: [drop test_boolean]
|
||||
|
||||
@@ -39,10 +39,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic alter column some_date set not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict;
|
||||
alter table migtest_e_basic alter column user_id set null;
|
||||
@@ -72,6 +68,7 @@ alter table migtest_e_history2 alter column test_string set default 'unknown';
|
||||
alter table migtest_e_history2 alter column test_string set not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number bigint;
|
||||
alter table migtest_e_history5 add column test_boolean boolean default false not null;
|
||||
|
||||
@@ -19,8 +19,6 @@ alter table migtest_e_basic alter column status drop default;
|
||||
alter table migtest_e_basic alter column status set null;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic alter column some_date drop default;
|
||||
alter table migtest_e_basic alter column some_date set null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id;
|
||||
|
||||
@@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2;
|
||||
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<column name="something" type="varchar"/>
|
||||
</createTable>
|
||||
<createTable name="migtest_ckey_parent" pkName="pk_migtest_ckey_parent">
|
||||
<column name="one_key" type="integer(127)" primaryKey="true"/>
|
||||
<column name="one_key" type="integer" primaryKey="true"/>
|
||||
<column name="two_key" type="varchar(127)" primaryKey="true"/>
|
||||
<column name="name" type="varchar"/>
|
||||
<column name="version" type="integer" notnull="true"/>
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
|
||||
<changeSet type="apply">
|
||||
<addColumn tableName="migtest_ckey_detail">
|
||||
<column name="one_key" type="integer(127)"/>
|
||||
<column name="one_key" type="integer"/>
|
||||
<column name="two_key" type="varchar(127)"/>
|
||||
</addColumn>
|
||||
<alterForeignKey name="fk_migtest_ckey_detail_parent" columnNames="one_key,two_key" refColumnNames="one_key,two_key" refTableName="migtest_ckey_parent" indexName="ix_migtest_ckey_detail_parent" tableName="migtest_ckey_detail"/>
|
||||
@@ -17,7 +17,6 @@
|
||||
<alterColumn columnName="description" tableName="migtest_e_basic" unique="uq_migtest_e_basic_description">
|
||||
<before>-- rename all collisions</before>
|
||||
</alterColumn>
|
||||
<alterColumn columnName="some_date" tableName="migtest_e_basic" currentType="timestamp" defaultValue="'2000-01-01T00:00:00'" notnull="true" currentNotnull="false"/>
|
||||
<alterColumn columnName="user_id" tableName="migtest_e_basic" currentType="integer" notnull="false" currentNotnull="true" references="migtest_e_user.id" foreignKeyName="fk_migtest_e_basic_user_id" foreignKeyIndex="ix_migtest_e_basic_user_id">
|
||||
<before>insert into migtest_e_user (id) select distinct user_id from migtest_e_basic</before>
|
||||
</alterColumn>
|
||||
@@ -46,6 +45,7 @@
|
||||
<addColumn tableName="migtest_e_history2" withHistory="true">
|
||||
<column name="test_string2" type="varchar" historyExclude="true"/>
|
||||
<column name="test_string3" type="varchar" defaultValue="'unknown'" notnull="true"/>
|
||||
<column name="new_column" type="varchar(20)"/>
|
||||
</addColumn>
|
||||
<alterColumn columnName="test_string" tableName="migtest_e_history3" withHistory="true" historyExclude="true"/>
|
||||
<alterColumn columnName="test_number" tableName="migtest_e_history4" withHistory="true" type="bigint" currentType="integer" currentNotnull="false"/>
|
||||
|
||||
@@ -8,7 +8,6 @@
|
||||
<alterColumn columnName="one_id" tableName="migtest_fk_set_null" references="migtest_fk_one.id" foreignKeyName="fk_migtest_fk_set_null_one_id" foreignKeyIndex="ix_migtest_fk_set_null_one_id" foreignKeyOnDelete="SET_NULL" foreignKeyOnUpdate="RESTRICT" dropForeignKey="fk_migtest_fk_set_null_one_id" dropForeignKeyIndex="ix_migtest_fk_set_null_one_id"/>
|
||||
<alterColumn columnName="status" tableName="migtest_e_basic" currentType="varchar(1)" defaultValue="DROP DEFAULT" notnull="false" currentNotnull="true" checkConstraint="check ( status in ('N','A','I'))" checkConstraintName="ck_migtest_e_basic_status"/>
|
||||
<alterColumn columnName="description" tableName="migtest_e_basic" dropUnique="uq_migtest_e_basic_description"/>
|
||||
<alterColumn columnName="some_date" tableName="migtest_e_basic" currentType="timestamp" defaultValue="DROP DEFAULT" notnull="false" currentNotnull="true"/>
|
||||
<alterColumn columnName="user_id" tableName="migtest_e_basic" currentType="integer" defaultValue="23" notnull="true" currentNotnull="false" dropForeignKey="fk_migtest_e_basic_user_id" dropForeignKeyIndex="ix_migtest_e_basic_user_id"/>
|
||||
<addColumn tableName="migtest_e_basic">
|
||||
<column name="old_boolean" type="boolean" defaultValue="false" notnull="true"/>
|
||||
@@ -55,6 +54,7 @@
|
||||
<dropHistoryTable baseTable="migtest_e_history"/>
|
||||
<dropColumn columnName="test_string2" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="test_string3" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="new_column" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="test_boolean" tableName="migtest_e_history5" withHistory="true"/>
|
||||
<dropColumn columnName="deleted" tableName="migtest_e_softdelete"/>
|
||||
<dropColumn columnName="master_id" tableName="migtest_oto_child"/>
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
<dropHistoryTable baseTable="migtest_e_history"/>
|
||||
<dropColumn columnName="test_string2" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="test_string3" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="new_column" tableName="migtest_e_history2" withHistory="true"/>
|
||||
<dropColumn columnName="test_boolean" tableName="migtest_e_history5" withHistory="true"/>
|
||||
<dropColumn columnName="deleted" tableName="migtest_e_softdelete"/>
|
||||
<dropColumn columnName="master_id" tableName="migtest_oto_child"/>
|
||||
|
||||
@@ -13,7 +13,7 @@ create table migtest_ckey_detail (
|
||||
);
|
||||
|
||||
create table migtest_ckey_parent (
|
||||
one_key integer(127) not null,
|
||||
one_key integer not null,
|
||||
two_key varchar(127) not null,
|
||||
name varchar(255),
|
||||
version integer not null,
|
||||
|
||||
@@ -23,7 +23,7 @@ create table migtest_mtm_m_migtest_mtm_c (
|
||||
constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id)
|
||||
);
|
||||
|
||||
alter table migtest_ckey_detail add column one_key integer(127);
|
||||
alter table migtest_ckey_detail add column one_key integer;
|
||||
alter table migtest_ckey_detail add column two_key varchar(127);
|
||||
|
||||
alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key) on delete restrict on update restrict;
|
||||
@@ -44,10 +44,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic modify some_date datetime(6) not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict;
|
||||
alter table migtest_e_basic modify user_id integer;
|
||||
@@ -75,8 +71,10 @@ alter table migtest_e_history2 alter test_string set default 'unknown';
|
||||
alter table migtest_e_history2 modify test_string varchar(255) not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
alter table migtest_e_history2_history add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown';
|
||||
alter table migtest_e_history2_history add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 modify test_number bigint;
|
||||
alter table migtest_e_history4_history modify test_number bigint;
|
||||
@@ -142,17 +140,17 @@ create trigger migtest_e_history_history_del before delete on migtest_e_history
|
||||
insert into migtest_e_history_history (sys_period_start,sys_period_end,id, test_string) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string);
|
||||
end$$
|
||||
lock tables migtest_e_history2 write, migtest_e_history3 write, migtest_e_history4 write, migtest_e_history5 write;
|
||||
-- changes: [add test_string2, add test_string3]
|
||||
-- changes: [add test_string2, add test_string3, add new_column]
|
||||
drop trigger migtest_e_history2_history_upd;
|
||||
drop trigger migtest_e_history2_history_del;
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
set NEW.sys_period_start = now(6);
|
||||
end$$
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
end$$
|
||||
-- changes: [exclude test_string]
|
||||
drop trigger migtest_e_history3_history_upd;
|
||||
|
||||
@@ -3,17 +3,17 @@
|
||||
drop view if exists migtest_e_history2_with_history;
|
||||
|
||||
-- apply changes
|
||||
alter table migtest_e_basic drop column old_boolean;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean');
|
||||
|
||||
alter table migtest_e_basic drop column old_boolean2;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'old_boolean2');
|
||||
|
||||
alter table migtest_e_basic drop column eref_id;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'eref_id');
|
||||
|
||||
alter table migtest_e_history2 drop column obsolete_string1;
|
||||
alter table migtest_e_history2_history drop column obsolete_string1;
|
||||
CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string1');
|
||||
CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string1');
|
||||
|
||||
alter table migtest_e_history2 drop column obsolete_string2;
|
||||
alter table migtest_e_history2_history drop column obsolete_string2;
|
||||
CALL usp_ebean_drop_column('migtest_e_history2', 'obsolete_string2');
|
||||
CALL usp_ebean_drop_column('migtest_e_history2_history', 'obsolete_string2');
|
||||
|
||||
drop table if exists migtest_e_ref;
|
||||
create view migtest_e_history2_with_history as select * from migtest_e_history2 union all select * from migtest_e_history2_history;
|
||||
@@ -24,11 +24,11 @@ drop trigger migtest_e_history2_history_upd;
|
||||
drop trigger migtest_e_history2_history_del;
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column);
|
||||
set NEW.sys_period_start = now(6);
|
||||
end$$
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column);
|
||||
end$$
|
||||
unlock tables;
|
||||
|
||||
@@ -22,7 +22,6 @@ alter table migtest_fk_set_null add constraint fk_migtest_fk_set_null_one_id for
|
||||
alter table migtest_e_basic alter status drop default;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop index uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic alter some_date drop default;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop foreign key fk_migtest_e_basic_user_id;
|
||||
@@ -73,12 +72,12 @@ drop trigger migtest_e_history2_history_upd;
|
||||
drop trigger migtest_e_history2_history_del;
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_upd before update on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column);
|
||||
set NEW.sys_period_start = now(6);
|
||||
end$$
|
||||
delimiter $$
|
||||
create trigger migtest_e_history2_history_del before delete on migtest_e_history2 for each row begin
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period_start,sys_period_end,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (OLD.sys_period_start, now(6),OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column);
|
||||
end$$
|
||||
-- changes: [include test_string]
|
||||
drop trigger migtest_e_history3_history_upd;
|
||||
|
||||
@@ -11,34 +11,37 @@ drop view if exists migtest_e_history2_with_history;
|
||||
drop view if exists migtest_e_history5_with_history;
|
||||
|
||||
-- apply changes
|
||||
alter table migtest_ckey_detail drop column one_key;
|
||||
CALL usp_ebean_drop_column('migtest_ckey_detail', 'one_key');
|
||||
|
||||
alter table migtest_ckey_detail drop column two_key;
|
||||
CALL usp_ebean_drop_column('migtest_ckey_detail', 'two_key');
|
||||
|
||||
alter table migtest_ckey_parent drop column assoc_id;
|
||||
CALL usp_ebean_drop_column('migtest_ckey_parent', 'assoc_id');
|
||||
|
||||
alter table migtest_e_basic drop column new_string_field;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'new_string_field');
|
||||
|
||||
alter table migtest_e_basic drop column new_boolean_field;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field');
|
||||
|
||||
alter table migtest_e_basic drop column new_boolean_field2;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'new_boolean_field2');
|
||||
|
||||
alter table migtest_e_basic drop column progress;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'progress');
|
||||
|
||||
alter table migtest_e_basic drop column new_integer;
|
||||
CALL usp_ebean_drop_column('migtest_e_basic', 'new_integer');
|
||||
|
||||
alter table migtest_e_history2 drop column test_string2;
|
||||
alter table migtest_e_history2_history drop column test_string2;
|
||||
CALL usp_ebean_drop_column('migtest_e_history2', 'test_string2');
|
||||
CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string2');
|
||||
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
alter table migtest_e_history2_history drop column test_string3;
|
||||
CALL usp_ebean_drop_column('migtest_e_history2', 'test_string3');
|
||||
CALL usp_ebean_drop_column('migtest_e_history2_history', 'test_string3');
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
alter table migtest_e_history5_history drop column test_boolean;
|
||||
CALL usp_ebean_drop_column('migtest_e_history2', 'new_column');
|
||||
CALL usp_ebean_drop_column('migtest_e_history2_history', 'new_column');
|
||||
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
CALL usp_ebean_drop_column('migtest_e_history5', 'test_boolean');
|
||||
CALL usp_ebean_drop_column('migtest_e_history5_history', 'test_boolean');
|
||||
|
||||
alter table migtest_oto_child drop column master_id;
|
||||
CALL usp_ebean_drop_column('migtest_e_softdelete', 'deleted');
|
||||
|
||||
CALL usp_ebean_drop_column('migtest_oto_child', 'master_id');
|
||||
|
||||
drop table if exists migtest_e_user;
|
||||
drop table if exists migtest_mtm_c_migtest_mtm_m;
|
||||
@@ -48,7 +51,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2
|
||||
create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history;
|
||||
|
||||
lock tables migtest_e_history2 write, migtest_e_history5 write;
|
||||
-- changes: [drop test_string2, drop test_string3]
|
||||
-- changes: [drop test_string2, drop test_string3, drop new_column]
|
||||
drop trigger migtest_e_history2_history_upd;
|
||||
drop trigger migtest_e_history2_history_del;
|
||||
delimiter $$
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
-- Inital script to create stored procedures etc for mysql platform
|
||||
DROP PROCEDURE IF EXISTS usp_ebean_drop_foreign_keys;
|
||||
|
||||
delimiter $$
|
||||
------------------------------------------------------------------------------
|
||||
-- PROCEDURE: usp_ebean_drop_foreign_keys TABLE, COLUMN
|
||||
-- deletes all constraints and foreign keys referring to TABLE.COLUMN
|
||||
------------------------------------------------------------------------------
|
||||
CREATE PROCEDURE usp_ebean_drop_foreign_keys(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
|
||||
BEGIN
|
||||
DECLARE done INT DEFAULT FALSE;
|
||||
DECLARE c_fk_name CHAR(255);
|
||||
DECLARE curs CURSOR FOR SELECT CONSTRAINT_NAME from information_schema.KEY_COLUMN_USAGE
|
||||
WHERE TABLE_SCHEMA = DATABASE() and TABLE_NAME = p_table_name and COLUMN_NAME = p_column_name
|
||||
AND REFERENCED_TABLE_NAME IS NOT NULL;
|
||||
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
|
||||
|
||||
OPEN curs;
|
||||
|
||||
read_loop: LOOP
|
||||
FETCH curs INTO c_fk_name;
|
||||
IF done THEN
|
||||
LEAVE read_loop;
|
||||
END IF;
|
||||
SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP FOREIGN KEY ', c_fk_name);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
END LOOP;
|
||||
|
||||
CLOSE curs;
|
||||
END
|
||||
$$
|
||||
|
||||
DROP PROCEDURE IF EXISTS usp_ebean_drop_column;
|
||||
|
||||
delimiter $$
|
||||
-------------------------------------------------------------------------------------
|
||||
-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN
|
||||
-- deletes the column and ensures that all indices and constraints are dropped first
|
||||
-------------------------------------------------------------------------------------
|
||||
CREATE PROCEDURE usp_ebean_drop_column(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
|
||||
BEGIN
|
||||
CALL usp_ebean_drop_foreign_keys(p_table_name, p_column_name);
|
||||
SET @sql = CONCAT('ALTER TABLE ', p_table_name, ' DROP COLUMN ', p_column_name);
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
END
|
||||
$$
|
||||
@@ -15,7 +15,7 @@ create table migtest_ckey_detail (
|
||||
create sequence migtest_ckey_detail_seq;
|
||||
|
||||
create table migtest_ckey_parent (
|
||||
one_key number(127) not null,
|
||||
one_key number(10) not null,
|
||||
two_key varchar2(127) not null,
|
||||
name varchar2(255),
|
||||
version number(10) not null,
|
||||
|
||||
@@ -18,7 +18,7 @@ create table migtest_mtm_m_migtest_mtm_c (
|
||||
constraint pk_migtest_mtm_m_migtest_mtm_c primary key (migtest_mtm_m_id,migtest_mtm_c_id)
|
||||
);
|
||||
|
||||
alter table migtest_ckey_detail add column one_key number(127);
|
||||
alter table migtest_ckey_detail add column one_key number(10);
|
||||
alter table migtest_ckey_detail add column two_key varchar2(127);
|
||||
|
||||
alter table migtest_ckey_detail add constraint fk_migtest_ckey_detail_parent foreign key (one_key,two_key) references migtest_ckey_parent (one_key,two_key);
|
||||
@@ -40,10 +40,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
-- NOT YET IMPLEMENTED: alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic modify some_date default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic modify some_date not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id);
|
||||
alter table migtest_e_basic modify user_id null;
|
||||
@@ -73,6 +69,7 @@ alter table migtest_e_history2 modify test_string default 'unknown';
|
||||
alter table migtest_e_history2 modify test_string not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar2(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar2(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar2(20);
|
||||
|
||||
alter table migtest_e_history4 modify test_number number(19);
|
||||
alter table migtest_e_history5 add column test_boolean number(1) default 0 not null;
|
||||
|
||||
@@ -20,8 +20,6 @@ alter table migtest_e_basic modify status drop default;
|
||||
alter table migtest_e_basic modify status null;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic modify some_date drop default;
|
||||
alter table migtest_e_basic modify some_date null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop constraint fk_migtest_e_basic_user_id;
|
||||
|
||||
@@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2;
|
||||
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
|
||||
@@ -45,10 +45,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic alter column some_date set not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id) on delete restrict on update restrict;
|
||||
alter table migtest_e_basic alter column user_id drop not null;
|
||||
@@ -80,8 +76,10 @@ alter table migtest_e_history2 alter column test_string set default 'unknown';
|
||||
alter table migtest_e_history2 alter column test_string set not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
alter table migtest_e_history2_history add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2_history add column test_string3 varchar(255) default 'unknown';
|
||||
alter table migtest_e_history2_history add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number type bigint;
|
||||
alter table migtest_e_history4_history alter column test_number type bigint;
|
||||
@@ -153,7 +151,7 @@ create trigger migtest_e_history_history_upd
|
||||
before update or delete on migtest_e_history
|
||||
for each row execute procedure migtest_e_history_history_version();
|
||||
|
||||
-- changes: [add test_string2, add test_string3]
|
||||
-- changes: [add test_string2, add test_string3, add new_column]
|
||||
create or replace function migtest_e_history2_history_version() returns trigger as $$
|
||||
declare
|
||||
lowerTs timestamptz;
|
||||
@@ -162,11 +160,11 @@ begin
|
||||
lowerTs = lower(OLD.sys_period);
|
||||
upperTs = greatest(lowerTs + '1 microsecond',current_timestamp);
|
||||
if (TG_OP = 'UPDATE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
NEW.sys_period = tstzrange(upperTs,null);
|
||||
return new;
|
||||
elsif (TG_OP = 'DELETE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column, obsolete_string1, obsolete_string2) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column, OLD.obsolete_string1, OLD.obsolete_string2);
|
||||
return old;
|
||||
end if;
|
||||
end;
|
||||
|
||||
@@ -28,11 +28,11 @@ begin
|
||||
lowerTs = lower(OLD.sys_period);
|
||||
upperTs = greatest(lowerTs + '1 microsecond',current_timestamp);
|
||||
if (TG_OP = 'UPDATE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column);
|
||||
NEW.sys_period = tstzrange(upperTs,null);
|
||||
return new;
|
||||
elsif (TG_OP = 'DELETE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.test_string3, OLD.new_column);
|
||||
return old;
|
||||
end if;
|
||||
end;
|
||||
|
||||
@@ -24,8 +24,6 @@ alter table migtest_e_basic alter column status drop default;
|
||||
alter table migtest_e_basic alter column status drop not null;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic alter column some_date drop default;
|
||||
alter table migtest_e_basic alter column some_date drop not null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table if exists migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id;
|
||||
@@ -83,11 +81,11 @@ begin
|
||||
lowerTs = lower(OLD.sys_period);
|
||||
upperTs = greatest(lowerTs + '1 microsecond',current_timestamp);
|
||||
if (TG_OP = 'UPDATE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column);
|
||||
NEW.sys_period = tstzrange(upperTs,null);
|
||||
return new;
|
||||
elsif (TG_OP = 'DELETE') then
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3);
|
||||
insert into migtest_e_history2_history (sys_period,id, test_string, obsolete_string2, test_string2, test_string3, new_column) values (tstzrange(lowerTs,upperTs), OLD.id, OLD.test_string, OLD.obsolete_string2, OLD.test_string2, OLD.test_string3, OLD.new_column);
|
||||
return old;
|
||||
end if;
|
||||
end;
|
||||
|
||||
@@ -33,6 +33,9 @@ alter table migtest_e_history2_history drop column test_string2;
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
alter table migtest_e_history2_history drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
alter table migtest_e_history2_history drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
alter table migtest_e_history5_history drop column test_boolean;
|
||||
|
||||
@@ -48,7 +51,7 @@ create view migtest_e_history2_with_history as select * from migtest_e_history2
|
||||
|
||||
create view migtest_e_history5_with_history as select * from migtest_e_history5 union all select * from migtest_e_history5_history;
|
||||
|
||||
-- changes: [drop test_string2, drop test_string3]
|
||||
-- changes: [drop test_string2, drop test_string3, drop new_column]
|
||||
create or replace function migtest_e_history2_history_version() returns trigger as $$
|
||||
declare
|
||||
lowerTs timestamptz;
|
||||
|
||||
@@ -13,7 +13,7 @@ create table migtest_ckey_detail (
|
||||
);
|
||||
|
||||
create table migtest_ckey_parent (
|
||||
one_key integer(127) not null,
|
||||
one_key integer not null,
|
||||
two_key varchar(127) not null,
|
||||
name varchar(255),
|
||||
version integer not null,
|
||||
|
||||
@@ -21,7 +21,7 @@ create table migtest_mtm_m_migtest_mtm_c (
|
||||
foreign key (migtest_mtm_c_id) references migtest_mtm_c (id) on delete restrict on update restrict
|
||||
);
|
||||
|
||||
alter table migtest_ckey_detail add column one_key integer(127);
|
||||
alter table migtest_ckey_detail add column one_key integer;
|
||||
alter table migtest_ckey_detail add column two_key varchar(127);
|
||||
|
||||
alter table migtest_ckey_parent add column assoc_id integer;
|
||||
@@ -38,10 +38,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
alter table migtest_e_basic add constraint uq_migtest_e_basic_description unique (description);
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic alter column some_date set default '2000-01-01T00:00:00';
|
||||
alter table migtest_e_basic alter column some_date set not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic alter column user_id set null;
|
||||
alter table migtest_e_basic add column new_string_field varchar(255) default 'foo''bar' not null;
|
||||
@@ -68,6 +64,7 @@ alter table migtest_e_history2 alter column test_string set default 'unknown';
|
||||
alter table migtest_e_history2 alter column test_string set not null;
|
||||
alter table migtest_e_history2 add column test_string2 varchar(255);
|
||||
alter table migtest_e_history2 add column test_string3 varchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add column new_column varchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number integer;
|
||||
alter table migtest_e_history5 add column test_boolean int default 0 not null;
|
||||
|
||||
@@ -17,8 +17,6 @@ alter table migtest_e_basic alter column status drop default;
|
||||
alter table migtest_e_basic alter column status set null;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
alter table migtest_e_basic alter column some_date drop default;
|
||||
alter table migtest_e_basic alter column some_date set null;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
alter table migtest_e_basic drop constraint if exists fk_migtest_e_basic_user_id;
|
||||
|
||||
@@ -20,6 +20,8 @@ alter table migtest_e_history2 drop column test_string2;
|
||||
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
|
||||
alter table migtest_e_history2 drop column new_column;
|
||||
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
|
||||
@@ -40,10 +40,6 @@ alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( sta
|
||||
-- rename all collisions;
|
||||
create unique nonclustered index uq_migtest_e_basic_description on migtest_e_basic(description) where description is not null;
|
||||
|
||||
update migtest_e_basic set some_date = '2000-01-01T00:00:00' where some_date is null;
|
||||
alter table migtest_e_basic add default '2000-01-01T00:00:00' for some_date;
|
||||
alter table migtest_e_basic alter column some_date datetime2 not null;
|
||||
|
||||
insert into migtest_e_user (id) select distinct user_id from migtest_e_basic;
|
||||
alter table migtest_e_basic add constraint fk_migtest_e_basic_user_id foreign key (user_id) references migtest_e_user (id);
|
||||
alter table migtest_e_basic alter column user_id integer;
|
||||
@@ -73,6 +69,7 @@ alter table migtest_e_history2 add default 'unknown' for test_string;
|
||||
alter table migtest_e_history2 alter column test_string nvarchar(255) not null;
|
||||
alter table migtest_e_history2 add test_string2 nvarchar(255);
|
||||
alter table migtest_e_history2 add test_string3 nvarchar(255) default 'unknown' not null;
|
||||
alter table migtest_e_history2 add new_column nvarchar(20);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number numeric(19);
|
||||
alter table migtest_e_history5 add test_boolean bit default 0 not null;
|
||||
|
||||
@@ -1,69 +1,14 @@
|
||||
-- Migrationscripts for ebean unittest
|
||||
-- apply changes
|
||||
-- drop column migtest_e_basic.old_boolean;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_old_boolean', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_old_boolean;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_old_boolean') drop index uq_migtest_e_basic_old_boolean ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'old_boolean';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_old_boolean', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_old_boolean;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_old_boolean') drop index ix_migtest_e_basic_old_boolean ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_old_boolean', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_old_boolean;
|
||||
alter table migtest_e_basic drop column old_boolean;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, old_boolean;
|
||||
|
||||
-- drop column migtest_e_basic.old_boolean2;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_old_boolean2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_old_boolean2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_old_boolean2') drop index uq_migtest_e_basic_old_boolean2 ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'old_boolean2';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_old_boolean2', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_old_boolean2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_old_boolean2') drop index ix_migtest_e_basic_old_boolean2 ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_old_boolean2', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_old_boolean2;
|
||||
alter table migtest_e_basic drop column old_boolean2;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, old_boolean2;
|
||||
|
||||
-- drop column migtest_e_basic.eref_id;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_eref_id', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_eref_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_eref_id') drop index uq_migtest_e_basic_eref_id ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'eref_id';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_eref_id', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_eref_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_eref_id') drop index ix_migtest_e_basic_eref_id ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_eref_id', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_eref_id;
|
||||
alter table migtest_e_basic drop column eref_id;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, eref_id;
|
||||
|
||||
-- drop column migtest_e_history2.obsolete_string1;
|
||||
IF (OBJECT_ID('uq_migtest_e_history2_obsolete_string1', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_obsolete_string1;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_obsolete_string1') drop index uq_migtest_e_history2_obsolete_string1 ON migtest_e_history2;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history2') and t2.name = 'obsolete_string1';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_history2_obsolete_string1', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_obsolete_string1;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_obsolete_string1') drop index ix_migtest_e_history2_obsolete_string1 ON migtest_e_history2;
|
||||
IF OBJECT_ID('fk_migtest_e_history2_obsolete_string1', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_obsolete_string1;
|
||||
alter table migtest_e_history2 drop column obsolete_string1;
|
||||
EXEC usp_ebean_drop_column migtest_e_history2, obsolete_string1;
|
||||
|
||||
-- drop column migtest_e_history2.obsolete_string2;
|
||||
IF (OBJECT_ID('uq_migtest_e_history2_obsolete_string2', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_obsolete_string2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_obsolete_string2') drop index uq_migtest_e_history2_obsolete_string2 ON migtest_e_history2;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history2') and t2.name = 'obsolete_string2';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_history2_obsolete_string2', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_obsolete_string2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_obsolete_string2') drop index ix_migtest_e_history2_obsolete_string2 ON migtest_e_history2;
|
||||
IF OBJECT_ID('fk_migtest_e_history2_obsolete_string2', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_obsolete_string2;
|
||||
alter table migtest_e_history2 drop column obsolete_string2;
|
||||
EXEC usp_ebean_drop_column migtest_e_history2, obsolete_string2;
|
||||
|
||||
IF OBJECT_ID('migtest_e_ref', 'U') IS NOT NULL drop table migtest_e_ref;
|
||||
IF OBJECT_ID('migtest_e_ref_seq', 'SO') IS NOT NULL drop sequence migtest_e_ref_seq;
|
||||
|
||||
@@ -16,19 +16,10 @@ IF OBJECT_ID('fk_migtest_fk_none_via_join_one_id', 'F') IS NOT NULL alter table
|
||||
IF OBJECT_ID('fk_migtest_fk_set_null_one_id', 'F') IS NOT NULL alter table migtest_fk_set_null drop constraint fk_migtest_fk_set_null_one_id;
|
||||
alter table migtest_fk_set_null add constraint fk_migtest_fk_set_null_one_id foreign key (one_id) references migtest_fk_one (id) on delete set null on update set null;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_status', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_status;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'status';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
EXEC usp_ebean_drop_default_constraint migtest_e_basic, status;
|
||||
alter table migtest_e_basic add constraint ck_migtest_e_basic_status check ( status in ('N','A','I'));
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_description', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_description;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_description') drop index uq_migtest_e_basic_description ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'some_date';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
|
||||
update migtest_e_basic set user_id = 23 where user_id is null;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_user_id', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_user_id;
|
||||
@@ -50,20 +41,12 @@ create unique nonclustered index uq_migtest_e_basic_indextest2 on migtest_e_basi
|
||||
create unique nonclustered index uq_migtest_e_basic_indextest6 on migtest_e_basic(indextest6) where indextest6 is not null;
|
||||
IF (OBJECT_ID('ck_migtest_e_enum_test_status', 'C') IS NOT NULL) alter table migtest_e_enum drop constraint ck_migtest_e_enum_test_status;
|
||||
alter table migtest_e_enum add constraint ck_migtest_e_enum_test_status check ( test_status in ('N','A','I'));
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history2') and t2.name = 'test_string';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$;
|
||||
EXEC usp_ebean_drop_default_constraint migtest_e_history2, test_string;
|
||||
alter table migtest_e_history2 add obsolete_string1 nvarchar(255);
|
||||
alter table migtest_e_history2 add obsolete_string2 nvarchar(255);
|
||||
|
||||
alter table migtest_e_history4 alter column test_number integer;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history6') and t2.name = 'test_number1';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history6 drop constraint ' + @Tmp)$$;
|
||||
EXEC usp_ebean_drop_default_constraint migtest_e_history6, test_number1;
|
||||
|
||||
-- NOTE: table has @History - special migration may be necessary
|
||||
update migtest_e_history6 set test_number2 = 7 where test_number2 is null;
|
||||
|
||||
@@ -1,189 +1,40 @@
|
||||
-- Migrationscripts for ebean unittest
|
||||
-- apply changes
|
||||
-- drop column migtest_ckey_detail.one_key;
|
||||
IF (OBJECT_ID('uq_migtest_ckey_detail_one_key', 'UQ') IS NOT NULL) alter table migtest_ckey_detail drop constraint uq_migtest_ckey_detail_one_key;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'uq_migtest_ckey_detail_one_key') drop index uq_migtest_ckey_detail_one_key ON migtest_ckey_detail;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_ckey_detail') and t2.name = 'one_key';
|
||||
if @Tmp is not null EXEC('alter table migtest_ckey_detail drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_ckey_detail_one_key', 'C') IS NOT NULL) alter table migtest_ckey_detail drop constraint ck_migtest_ckey_detail_one_key;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'ix_migtest_ckey_detail_one_key') drop index ix_migtest_ckey_detail_one_key ON migtest_ckey_detail;
|
||||
IF OBJECT_ID('fk_migtest_ckey_detail_one_key', 'F') IS NOT NULL alter table migtest_ckey_detail drop constraint fk_migtest_ckey_detail_one_key;
|
||||
alter table migtest_ckey_detail drop column one_key;
|
||||
EXEC usp_ebean_drop_column migtest_ckey_detail, one_key;
|
||||
|
||||
-- drop column migtest_ckey_detail.two_key;
|
||||
IF (OBJECT_ID('uq_migtest_ckey_detail_two_key', 'UQ') IS NOT NULL) alter table migtest_ckey_detail drop constraint uq_migtest_ckey_detail_two_key;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'uq_migtest_ckey_detail_two_key') drop index uq_migtest_ckey_detail_two_key ON migtest_ckey_detail;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_ckey_detail') and t2.name = 'two_key';
|
||||
if @Tmp is not null EXEC('alter table migtest_ckey_detail drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_ckey_detail_two_key', 'C') IS NOT NULL) alter table migtest_ckey_detail drop constraint ck_migtest_ckey_detail_two_key;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_detail','U') AND name = 'ix_migtest_ckey_detail_two_key') drop index ix_migtest_ckey_detail_two_key ON migtest_ckey_detail;
|
||||
IF OBJECT_ID('fk_migtest_ckey_detail_two_key', 'F') IS NOT NULL alter table migtest_ckey_detail drop constraint fk_migtest_ckey_detail_two_key;
|
||||
alter table migtest_ckey_detail drop column two_key;
|
||||
EXEC usp_ebean_drop_column migtest_ckey_detail, two_key;
|
||||
|
||||
-- drop column migtest_ckey_parent.assoc_id;
|
||||
IF (OBJECT_ID('uq_migtest_ckey_parent_assoc_id', 'UQ') IS NOT NULL) alter table migtest_ckey_parent drop constraint uq_migtest_ckey_parent_assoc_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_parent','U') AND name = 'uq_migtest_ckey_parent_assoc_id') drop index uq_migtest_ckey_parent_assoc_id ON migtest_ckey_parent;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_ckey_parent') and t2.name = 'assoc_id';
|
||||
if @Tmp is not null EXEC('alter table migtest_ckey_parent drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_ckey_parent_assoc_id', 'C') IS NOT NULL) alter table migtest_ckey_parent drop constraint ck_migtest_ckey_parent_assoc_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_ckey_parent','U') AND name = 'ix_migtest_ckey_parent_assoc_id') drop index ix_migtest_ckey_parent_assoc_id ON migtest_ckey_parent;
|
||||
IF OBJECT_ID('fk_migtest_ckey_parent_assoc_id', 'F') IS NOT NULL alter table migtest_ckey_parent drop constraint fk_migtest_ckey_parent_assoc_id;
|
||||
alter table migtest_ckey_parent drop column assoc_id;
|
||||
EXEC usp_ebean_drop_column migtest_ckey_parent, assoc_id;
|
||||
|
||||
-- drop column migtest_e_basic.new_string_field;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_new_string_field', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_string_field;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_string_field') drop index uq_migtest_e_basic_new_string_field ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'new_string_field';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_new_string_field', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_string_field;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_string_field') drop index ix_migtest_e_basic_new_string_field ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_new_string_field', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_string_field;
|
||||
alter table migtest_e_basic drop column new_string_field;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, new_string_field;
|
||||
|
||||
-- drop column migtest_e_basic.new_boolean_field;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_new_boolean_field', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_boolean_field;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_boolean_field') drop index uq_migtest_e_basic_new_boolean_field ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'new_boolean_field';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_new_boolean_field', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_boolean_field;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_boolean_field') drop index ix_migtest_e_basic_new_boolean_field ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_new_boolean_field', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_boolean_field;
|
||||
alter table migtest_e_basic drop column new_boolean_field;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, new_boolean_field;
|
||||
|
||||
-- drop column migtest_e_basic.new_boolean_field2;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_new_boolean_field2', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_boolean_field2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_boolean_field2') drop index uq_migtest_e_basic_new_boolean_field2 ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'new_boolean_field2';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_new_boolean_field2', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_boolean_field2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_boolean_field2') drop index ix_migtest_e_basic_new_boolean_field2 ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_new_boolean_field2', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_boolean_field2;
|
||||
alter table migtest_e_basic drop column new_boolean_field2;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, new_boolean_field2;
|
||||
|
||||
-- drop column migtest_e_basic.progress;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_progress', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_progress;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_progress') drop index uq_migtest_e_basic_progress ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'progress';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_progress', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_progress;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_progress') drop index ix_migtest_e_basic_progress ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_progress', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_progress;
|
||||
alter table migtest_e_basic drop column progress;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, progress;
|
||||
|
||||
-- drop column migtest_e_basic.new_integer;
|
||||
IF (OBJECT_ID('uq_migtest_e_basic_new_integer', 'UQ') IS NOT NULL) alter table migtest_e_basic drop constraint uq_migtest_e_basic_new_integer;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'uq_migtest_e_basic_new_integer') drop index uq_migtest_e_basic_new_integer ON migtest_e_basic;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_basic') and t2.name = 'new_integer';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_basic drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_basic_new_integer', 'C') IS NOT NULL) alter table migtest_e_basic drop constraint ck_migtest_e_basic_new_integer;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_basic','U') AND name = 'ix_migtest_e_basic_new_integer') drop index ix_migtest_e_basic_new_integer ON migtest_e_basic;
|
||||
IF OBJECT_ID('fk_migtest_e_basic_new_integer', 'F') IS NOT NULL alter table migtest_e_basic drop constraint fk_migtest_e_basic_new_integer;
|
||||
alter table migtest_e_basic drop column new_integer;
|
||||
EXEC usp_ebean_drop_column migtest_e_basic, new_integer;
|
||||
|
||||
-- drop column migtest_e_history2.test_string2;
|
||||
IF (OBJECT_ID('uq_migtest_e_history2_test_string2', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_test_string2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_test_string2') drop index uq_migtest_e_history2_test_string2 ON migtest_e_history2;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history2') and t2.name = 'test_string2';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_history2_test_string2', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_test_string2;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_test_string2') drop index ix_migtest_e_history2_test_string2 ON migtest_e_history2;
|
||||
IF OBJECT_ID('fk_migtest_e_history2_test_string2', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_test_string2;
|
||||
alter table migtest_e_history2 drop column test_string2;
|
||||
EXEC usp_ebean_drop_column migtest_e_history2, test_string2;
|
||||
|
||||
-- drop column migtest_e_history2.test_string3;
|
||||
IF (OBJECT_ID('uq_migtest_e_history2_test_string3', 'UQ') IS NOT NULL) alter table migtest_e_history2 drop constraint uq_migtest_e_history2_test_string3;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'uq_migtest_e_history2_test_string3') drop index uq_migtest_e_history2_test_string3 ON migtest_e_history2;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history2') and t2.name = 'test_string3';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history2 drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_history2_test_string3', 'C') IS NOT NULL) alter table migtest_e_history2 drop constraint ck_migtest_e_history2_test_string3;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history2','U') AND name = 'ix_migtest_e_history2_test_string3') drop index ix_migtest_e_history2_test_string3 ON migtest_e_history2;
|
||||
IF OBJECT_ID('fk_migtest_e_history2_test_string3', 'F') IS NOT NULL alter table migtest_e_history2 drop constraint fk_migtest_e_history2_test_string3;
|
||||
alter table migtest_e_history2 drop column test_string3;
|
||||
EXEC usp_ebean_drop_column migtest_e_history2, test_string3;
|
||||
|
||||
-- drop column migtest_e_history5.test_boolean;
|
||||
IF (OBJECT_ID('uq_migtest_e_history5_test_boolean', 'UQ') IS NOT NULL) alter table migtest_e_history5 drop constraint uq_migtest_e_history5_test_boolean;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history5','U') AND name = 'uq_migtest_e_history5_test_boolean') drop index uq_migtest_e_history5_test_boolean ON migtest_e_history5;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history5') and t2.name = 'test_boolean';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history5 drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_history5_test_boolean', 'C') IS NOT NULL) alter table migtest_e_history5 drop constraint ck_migtest_e_history5_test_boolean;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_history5','U') AND name = 'ix_migtest_e_history5_test_boolean') drop index ix_migtest_e_history5_test_boolean ON migtest_e_history5;
|
||||
IF OBJECT_ID('fk_migtest_e_history5_test_boolean', 'F') IS NOT NULL alter table migtest_e_history5 drop constraint fk_migtest_e_history5_test_boolean;
|
||||
alter table migtest_e_history5 drop column test_boolean;
|
||||
EXEC usp_ebean_drop_column migtest_e_history2, new_column;
|
||||
|
||||
-- drop column migtest_e_softdelete.deleted;
|
||||
IF (OBJECT_ID('uq_migtest_e_softdelete_deleted', 'UQ') IS NOT NULL) alter table migtest_e_softdelete drop constraint uq_migtest_e_softdelete_deleted;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_softdelete','U') AND name = 'uq_migtest_e_softdelete_deleted') drop index uq_migtest_e_softdelete_deleted ON migtest_e_softdelete;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_softdelete') and t2.name = 'deleted';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_softdelete drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_e_softdelete_deleted', 'C') IS NOT NULL) alter table migtest_e_softdelete drop constraint ck_migtest_e_softdelete_deleted;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_e_softdelete','U') AND name = 'ix_migtest_e_softdelete_deleted') drop index ix_migtest_e_softdelete_deleted ON migtest_e_softdelete;
|
||||
IF OBJECT_ID('fk_migtest_e_softdelete_deleted', 'F') IS NOT NULL alter table migtest_e_softdelete drop constraint fk_migtest_e_softdelete_deleted;
|
||||
alter table migtest_e_softdelete drop column deleted;
|
||||
EXEC usp_ebean_drop_column migtest_e_history5, test_boolean;
|
||||
|
||||
-- drop column migtest_oto_child.master_id;
|
||||
IF (OBJECT_ID('uq_migtest_oto_child_master_id', 'UQ') IS NOT NULL) alter table migtest_oto_child drop constraint uq_migtest_oto_child_master_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_oto_child','U') AND name = 'uq_migtest_oto_child_master_id') drop index uq_migtest_oto_child_master_id ON migtest_oto_child;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_oto_child') and t2.name = 'master_id';
|
||||
if @Tmp is not null EXEC('alter table migtest_oto_child drop constraint ' + @Tmp)$$;
|
||||
IF (OBJECT_ID('ck_migtest_oto_child_master_id', 'C') IS NOT NULL) alter table migtest_oto_child drop constraint ck_migtest_oto_child_master_id;
|
||||
IF EXISTS (SELECT name FROM sys.indexes WHERE object_id = OBJECT_ID('migtest_oto_child','U') AND name = 'ix_migtest_oto_child_master_id') drop index ix_migtest_oto_child_master_id ON migtest_oto_child;
|
||||
IF OBJECT_ID('fk_migtest_oto_child_master_id', 'F') IS NOT NULL alter table migtest_oto_child drop constraint fk_migtest_oto_child_master_id;
|
||||
alter table migtest_oto_child drop column master_id;
|
||||
EXEC usp_ebean_drop_column migtest_e_softdelete, deleted;
|
||||
|
||||
EXEC usp_ebean_drop_column migtest_oto_child, master_id;
|
||||
|
||||
IF OBJECT_ID('migtest_e_user', 'U') IS NOT NULL drop table migtest_e_user;
|
||||
IF OBJECT_ID('migtest_e_user_seq', 'SO') IS NOT NULL drop sequence migtest_e_user_seq;
|
||||
IF OBJECT_ID('migtest_mtm_c_migtest_mtm_m', 'U') IS NOT NULL drop table migtest_mtm_c_migtest_mtm_m;
|
||||
IF OBJECT_ID('migtest_mtm_m_migtest_mtm_c', 'U') IS NOT NULL drop table migtest_mtm_m_migtest_mtm_c;
|
||||
-- dropping history support for migtest_e_history;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history') and t2.name = 'sys_periodFrom';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history drop constraint ' + @Tmp)$$;
|
||||
delimiter $$
|
||||
DECLARE @Tmp nvarchar(200);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('migtest_e_history') and t2.name = 'sys_periodTo';
|
||||
if @Tmp is not null EXEC('alter table migtest_e_history drop constraint ' + @Tmp)$$;
|
||||
EXEC usp_ebean_drop_default_constraint migtest_e_history, sys_periodFrom;
|
||||
EXEC usp_ebean_drop_default_constraint migtest_e_history, sys_periodTo;
|
||||
alter table migtest_e_history set (system_versioning = off);
|
||||
alter table migtest_e_history drop period for system_time;
|
||||
alter table migtest_e_history drop column sys_periodFrom;
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
-- 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);
|
||||
if not exists (select name from sys.types where name = 'ebean_float_tvp') create type ebean_float_tvp as table (c1 float);
|
||||
if not exists (select name from sys.types where name = 'ebean_bit_tvp') create type ebean_bit_tvp as table (c1 bit);
|
||||
if not exists (select name from sys.types where name = 'ebean_date_tvp') create type ebean_date_tvp as table (c1 date);
|
||||
if not exists (select name from sys.types where name = 'ebean_time_tvp') create type ebean_time_tvp as table (c1 time);
|
||||
if not exists (select name from sys.types where name = 'ebean_uniqueidentifier_tvp') create type ebean_uniqueidentifier_tvp as table (c1 uniqueidentifier);
|
||||
if not exists (select name from sys.types where name = 'ebean_nvarchar_tvp') create type ebean_nvarchar_tvp as table (c1 nvarchar(max));
|
||||
|
||||
delimiter $$
|
||||
-----------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
--------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
--------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
|
||||
delimiter $$
|
||||
-------------------------------------------------------------------------------------
|
||||
-- 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
|
||||
$$
|
||||
Reference in New Issue
Block a user