Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add RENAME CONSTRAINT #222

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This repo is mainly unmaintained. But if you found a bug and create a pull reque
* Add support for CREATE TYPE (Karol Rybak)
* Add support for CREATE EXTENSION (Átila Camurça Alves)
* Add basic support for CREATE FOREIGN TABLE (Bruno Almeida)
* Add support for ALTER TABLE ... RENAME CONSTRAINT .. TO .. (Shem Pasamba);

#### Fixes
* Added hint to use "CREATE TABLE ... CONSTRAINT name PRIMARY KEY/UNIQUE ..."
Expand Down
34 changes: 31 additions & 3 deletions src/main/java/cz/startnet/utils/pgdiff/PgDiffConstraints.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ public static void createConstraints(final PrintWriter writer,
getNewConstraints(oldTable, newTable, primaryKey)) {
searchPathHelper.outputSearchPath(writer);
writer.println();
writer.println(constraint.getCreationSQL());
if (constraint.wasRenamed())
writer.println(constraint.getRenameSQL());
else
writer.println(constraint.getCreationSQL());
}
}
}
Expand Down Expand Up @@ -110,7 +113,8 @@ private static List<PgConstraint> getDropConstraints(final PgTable oldTable,
if (constraint.isPrimaryKeyConstraint() == primaryKey
&& (!newTable.containsConstraint(constraint.getName())
|| !newTable.getConstraint(constraint.getName()).equals(
constraint))) {
constraint))
&& !foundARename(constraint, newTable, primaryKey)) {
list.add(constraint);
}
}
Expand All @@ -119,6 +123,29 @@ private static List<PgConstraint> getDropConstraints(final PgTable oldTable,
return list;
}

/**
* Returns true of a constraint can be found that it was renamed
*
* @param oldTable original Table
* @param newTable new table
* @param oldName old constraint name
* @return boolean
*/
private static boolean foundARename(PgConstraint constraint,
PgTable searchTable,
boolean primaryKey)
{
if (constraint != null && searchTable != null) {
for (final PgConstraint search_constraint : searchTable.getConstraints()) {
if (constraint.renamed(search_constraint) &&
constraint.isPrimaryKeyConstraint() == primaryKey) {
return true;
}
}
}
return false;
}

/**
* Returns list of constraints that should be added.
*
Expand All @@ -145,7 +172,8 @@ private static List<PgConstraint> getNewConstraints(final PgTable oldTable,
} else {
for (final PgConstraint constraint :
newTable.getConstraints()) {
if ((constraint.isPrimaryKeyConstraint() == primaryKey)
if (foundARename(constraint, oldTable, primaryKey)
|| (constraint.isPrimaryKeyConstraint() == primaryKey)
&& (!oldTable.containsConstraint(
constraint.getName())
|| !oldTable.getConstraint(constraint.getName()).
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgConstraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ public class PgConstraint {
*/
private String comment;

/**
* Flag to test say if this constraint is a renamed object
*/
private boolean wasRenamed;

/**
* String to store the renamed column
*/
private String renamedFrom;

/**
* Creates a new PgConstraint object.
*
* @param name {@link #name}
*/
public PgConstraint(String name) {
this.name = name;
this.wasRenamed = false;
this.renamedFrom = null;
}

/**
Expand Down Expand Up @@ -77,6 +89,37 @@ public String getCreationSQL() {
return sbSQL.toString();
}

/**
* Creates and returns SQL for rename of the constraint.
* @return created SQL
*/
public String getRenameSQL() {
final StringBuilder sbSQL = new StringBuilder(100);
sbSQL.append("ALTER TABLE ");
sbSQL.append(PgDiffUtils.getDropIfExists());
sbSQL.append(PgDiffUtils.getQuotedName(getTableName()));
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append("\tRENAME CONSTRAINT ");
sbSQL.append(PgDiffUtils.getQuotedName(this.renamedFrom));
sbSQL.append(" TO ");
sbSQL.append(PgDiffUtils.getQuotedName(getName()));
sbSQL.append(';');

if (comment != null && !comment.isEmpty()) {
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append("COMMENT ON CONSTRAINT ");
sbSQL.append(PgDiffUtils.getQuotedName(name));
sbSQL.append(" ON ");
sbSQL.append(PgDiffUtils.getQuotedName(tableName));
sbSQL.append(" IS ");
sbSQL.append(comment);
sbSQL.append(';');
}

return sbSQL.toString();
}

/**
* Getter for {@link #comment}.
*
Expand Down Expand Up @@ -198,6 +241,39 @@ public boolean equals(final Object object) {
return equals;
}

/**
* {@inheritDoc}
*
* @param object {@inheritDoc}
*
* @return {@inheritDoc}
*/
public boolean renamed(final Object object) {
this.wasRenamed = false;

if (this == object) {
this.wasRenamed = false;
} else if (object instanceof PgConstraint) {
final PgConstraint constraint = (PgConstraint) object;
this.wasRenamed = definition.equals(constraint.getDefinition())
&& !name.equals(constraint.getName())
&& tableName.equals(constraint.getTableName());
if (this.wasRenamed)
this.renamedFrom = constraint.getName();
}

return this.wasRenamed;
}

/**
* Returns if constraint was renamed
*
* @return boolean
*/
public boolean wasRenamed() {
return this.wasRenamed;
}

/**
* {@inheritDoc}
*
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/cz/startnet/utils/pgdiff/schema/PgSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class PgSequence {

/**
* Value for AS or null if no value is specified.
*/
private String as;
/**
* Value for CACHE or null if no value is specified.
*/
Expand Down Expand Up @@ -73,6 +77,24 @@ public PgSequence(final String name) {
this.name = name;
}

/**
* Setter for {@link #as}.
*
* @param as {@link #as}
*/
public void setAs(final String as) {
this.as = as;
}

/**
* Getter for {@link #as}.
*
* @return {@link #as}
*/
public String getAs() {
return as;
}

/**
* Setter for {@link #cache}.
*
Expand Down Expand Up @@ -129,6 +151,12 @@ public String getCreationSQL() {

}

if (as != null) {
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append("\tAS ");
sbSQL.append(as);
}

if (startWith != null) {
sbSQL.append(System.getProperty("line.separator"));
sbSQL.append("\tSTART WITH ");
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/cz/startnet/utils/pgdiff/PgDiffTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ public static Collection<?> parameters() {
{"modify_constraint", false, false, false, false},
// Tests scenario where TABLE CONSTRAINT is dropped.
{"drop_constraint", false, false, false, false},
// Tests scenario where TABLE CONSTRAINT is renamed.
{"rename_constraint", false, false, false, false},
// Tests scenario where UNIQUE TABLE CONSTRAINT is added.
{"add_unique_constraint", false, false, false, true},
// Tests reading of TABLE with INHERITS.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE IF EXISTS addresses
RENAME CONSTRAINT addresses_pkey TO addresses_id_pkey;

ALTER TABLE IF EXISTS testtable
RENAME CONSTRAINT field4check TO field4check_renamed;

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--

COMMENT ON SCHEMA public IS 'Standard public schema';


SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

CREATE TABLE "addresses" (
"id" SERIAL NOT NULL,
"address" TEXT,
"created_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
"updated_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
"deleted_at" TIMESTAMP WITHOUT TIME ZONE,
"user_id" INTEGER NOT NULL
);

--
-- Name: testtable; Type: TABLE; Schema: public; Owner: fordfrog; Tablespace:
--

CREATE TABLE testtable (
field1 integer,
field2 integer,
field3 character varying(150) DEFAULT 'none'::character varying,
field4 double precision,
"full" timestamp with time zone DEFAULT '2006-11-10 00:00:00+01'::timestamp with time zone NOT NULL,
CONSTRAINT field4check_renamed CHECK ((field4 > (0.0)::double precision))
);
ALTER TABLE ONLY testtable ALTER COLUMN "full" SET STATISTICS 200;


ALTER TABLE public.testtable OWNER TO fordfrog;

--
-- Name: testindex; Type: INDEX; Schema: public; Owner: fordfrog; Tablespace:
--

CREATE INDEX testindex ON testtable USING btree (field3);

ALTER TABLE "addresses" ADD CONSTRAINT "addresses_id_pkey" PRIMARY KEY (id);

--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
--
-- PostgreSQL database dump
--

SET client_encoding = 'UTF8';
SET check_function_bodies = false;
SET client_min_messages = warning;

--
-- Name: SCHEMA public; Type: COMMENT; Schema: -; Owner: postgres
--

COMMENT ON SCHEMA public IS 'Standard public schema';


SET search_path = public, pg_catalog;

SET default_tablespace = '';

SET default_with_oids = false;

CREATE TABLE "addresses" (
"id" SERIAL NOT NULL,
"address" TEXT,
"created_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
"updated_at" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
"deleted_at" TIMESTAMP WITHOUT TIME ZONE,
"user_id" INTEGER NOT NULL
);


--
-- Name: testtable; Type: TABLE; Schema: public; Owner: fordfrog; Tablespace:
--

CREATE TABLE testtable (
field1 integer,
field2 integer,
field3 character varying(150) DEFAULT 'none'::character varying,
field4 double precision,
"full" timestamp with time zone DEFAULT '2006-11-10 00:00:00+01'::timestamp with time zone NOT NULL,
CONSTRAINT field4check CHECK ((field4 > (0.0)::double precision))
);
ALTER TABLE ONLY testtable ALTER COLUMN "full" SET STATISTICS 200;


ALTER TABLE public.testtable OWNER TO fordfrog;


--
-- Name: testindex; Type: INDEX; Schema: public; Owner: fordfrog; Tablespace:
--

CREATE INDEX testindex ON testtable USING btree (field3);


ALTER TABLE ONLY addresses
ADD CONSTRAINT addresses_pkey PRIMARY KEY (id);


--
-- Name: public; Type: ACL; Schema: -; Owner: postgres
--

REVOKE ALL ON SCHEMA public FROM PUBLIC;
REVOKE ALL ON SCHEMA public FROM postgres;
GRANT ALL ON SCHEMA public TO postgres;
GRANT ALL ON SCHEMA public TO PUBLIC;


--
-- PostgreSQL database dump complete
--