SET CLIENT_ENCODING TO UTF8;
BEGIN;

/* mcconfig_service */
DROP TABLE IF EXISTS "mcconfig_service" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_service_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_service_id" CASCADE;

CREATE SEQUENCE "mcconfig_service_id_seq" ;

CREATE TABLE  "mcconfig_service" (
 "id" numeric   DEFAULT nextval('"mcconfig_service_id_seq"')  NOT NULL,
 "name"   text, 
 "source"   text NOT NULL, 
 "type"   integer NOT NULL, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_service_id" ON "mcconfig_service" (id);


/* mcconfig_serviceoption */
DROP TABLE IF EXISTS "mcconfig_serviceoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_serviceoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_serviceoption_service_id" CASCADE;

CREATE SEQUENCE "mcconfig_serviceoption_id_seq" ;

CREATE TABLE  "mcconfig_serviceoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_serviceoption_id_seq"')  NOT NULL,
 "service_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_serviceoption ADD CONSTRAINT fk_mcconfig_serviceoption_service FOREIGN KEY (service_id) REFERENCES mcconfig_service (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_serviceoption_service_id" ON "mcconfig_serviceoption" (service_id);


/* mcconfig_datastore  */
DROP TABLE IF EXISTS "mcconfig_datastore" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_datastore_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_datastore_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_datastore_service_id" CASCADE;

CREATE SEQUENCE "mcconfig_datastore_id_seq" ;

CREATE TABLE  "mcconfig_datastore" (
 "id" numeric   DEFAULT nextval('"mcconfig_datastore_id_seq"')  NOT NULL,
 "service_id"   INTEGER NOT NULL, 
 "name"   text, 
 "layers"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_datastore ADD CONSTRAINT fk_mcconfig_datastore_service FOREIGN KEY (service_id) REFERENCES mcconfig_service (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_datastore_id" ON "mcconfig_datastore" (id);
CREATE INDEX "idx_mcconfig_datastore_service_id" ON "mcconfig_datastore" (service_id);


/* mcconfig_datastoreoption */
DROP TABLE IF EXISTS "mcconfig_datastoreoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_datastoreoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_datastoreoption_datastore_id" CASCADE;

CREATE SEQUENCE "mcconfig_datastoreoption_id_seq" ;

CREATE TABLE  "mcconfig_datastoreoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_datastoreoption_id_seq"')  NOT NULL,
 "datastore_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_datastoreoption ADD CONSTRAINT fk_mcconfig_datastoreoption_datastore FOREIGN KEY (datastore_id) REFERENCES mcconfig_datastore (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_datastoreoption_datastore_id" ON "mcconfig_datastoreoption" (datastore_id);


/* mcconfig_resource  */
DROP TABLE IF EXISTS "mcconfig_resource" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_resource_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resource_id" CASCADE;

CREATE SEQUENCE "mcconfig_resource_id_seq" ;

CREATE TABLE  "mcconfig_resource" (
 "id" numeric   DEFAULT nextval('"mcconfig_resource_id_seq"')  NOT NULL,
 "name"   text, 
 "key"   text, 
 "domain"   text, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_resource_id" ON "mcconfig_resource" (id);


/* mcconfig_resourceoption */
DROP TABLE IF EXISTS "mcconfig_resourceoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_resourceoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourceoption_resource_id" CASCADE;

CREATE SEQUENCE "mcconfig_resourceoption_id_seq" ;

CREATE TABLE  "mcconfig_resourceoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_resourceoption_id_seq"')  NOT NULL,
 "resource_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 "key"   text, 
 "domain"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_resourceoption ADD CONSTRAINT fk_mcconfig_resourceoption_resource FOREIGN KEY (resource_id) REFERENCES mcconfig_resource (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_resourceoption_resource_id" ON "mcconfig_resourceoption" (resource_id);


/* mcconfig_resourcedatastore */
DROP TABLE IF EXISTS "mcconfig_resourcedatastore" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_resourcedatastore_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourcedatastore_resource_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourcedatastore_datastore_id" CASCADE;

CREATE SEQUENCE "mcconfig_resourcedatastore_id_seq" ;

CREATE TABLE  "mcconfig_resourcedatastore" (
 "id" numeric   DEFAULT nextval('"mcconfig_resourcedatastore_id_seq"')  NOT NULL,
 "resource_id"   INTEGER NOT NULL, 
 "datastore_id"   INTEGER NOT NULL, 
 primary key ("id")
);

ALTER TABLE mcconfig_resourcedatastore ADD CONSTRAINT fk_mcconfig_resourcedatastore_resource FOREIGN KEY (resource_id) REFERENCES mcconfig_resource (id) MATCH FULL ON DELETE CASCADE;
ALTER TABLE mcconfig_resourcedatastore ADD CONSTRAINT fk_mcconfig_resourcedatastore_datastore FOREIGN KEY (datastore_id) REFERENCES mcconfig_datastore (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_resourcedatastore_resource_id" ON "mcconfig_resourcedatastore" (resource_id);
CREATE INDEX "idx_mcconfig_resourcedatastore_datastore_id" ON "mcconfig_resourcedatastore" (datastore_id);


/* mcconfig_widget  */
DROP TABLE IF EXISTS "mcconfig_widget" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_widget_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_widget_id" CASCADE;

CREATE SEQUENCE "mcconfig_widget_id_seq" ;

CREATE TABLE  "mcconfig_widget" (
 "id" numeric   DEFAULT nextval('"mcconfig_widget_id_seq"')  NOT NULL,
 "name"   text, 
 "type"   integer NOT NULL, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_widget_id" ON "mcconfig_widget" (id);


/* mcconfig_widgetoption */
DROP TABLE IF EXISTS "mcconfig_widgetoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_widgetoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_widgetoption_widget_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_widgetoption_order" CASCADE;

CREATE SEQUENCE "mcconfig_widgetoption_id_seq" ;

CREATE TABLE  "mcconfig_widgetoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_widgetoption_id_seq"')  NOT NULL,
 "widget_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 "order"  INTEGER, 
 primary key ("id")
);

ALTER TABLE mcconfig_widgetoption ADD CONSTRAINT fk_mcconfig_widgetoption_widget FOREIGN KEY (widget_id) REFERENCES mcconfig_widget (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_widgetoption_widget_id" ON "mcconfig_widgetoption" (widget_id);
CREATE INDEX "idx_mcconfig_widgetoption_order" ON "mcconfig_widgetoption" ("order");


/* mcconfig_mapcontext  */
DROP TABLE IF EXISTS "mcconfig_mapcontext" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_mapcontext_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_mapcontext_id" CASCADE;

CREATE SEQUENCE "mcconfig_mapcontext_id_seq" ;

CREATE TABLE  "mcconfig_mapcontext" (
 "id" numeric   DEFAULT nextval('"mcconfig_mapcontext_id_seq"')  NOT NULL,
 "name"   text, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_mapcontext_id" ON "mcconfig_mapcontext" (id);


/* mcconfig_mapcontextoption */
DROP TABLE IF EXISTS "mcconfig_mapcontextoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_mapcontextoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_mapcontextoption_mapcontext_id" CASCADE;

CREATE SEQUENCE "mcconfig_mapcontextoption_id_seq" ;

CREATE TABLE  "mcconfig_mapcontextoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_mapcontextoption_id_seq"')  NOT NULL,
 "mapcontext_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_mapcontextoption ADD CONSTRAINT fk_mcconfig_mapcontextoption_mapcontext FOREIGN KEY (mapcontext_id) REFERENCES mcconfig_mapcontext (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_mapcontextoption_mapcontext_id" ON "mcconfig_mapcontextoption" (mapcontext_id);

/* mcconfig_mapcontextresource */
DROP TABLE IF EXISTS "mcconfig_mapcontextresource" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_mapcontextresource_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_mapcontextresource_mapcontext_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_mapcontextresource_resource_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_mapcontextresource_order" CASCADE;

CREATE SEQUENCE "mcconfig_mapcontextresource_id_seq" ;

CREATE TABLE  "mcconfig_mapcontextresource" (
 "id" numeric   DEFAULT nextval('"mcconfig_mapcontextresource_id_seq"')  NOT NULL,
 "mapcontext_id"   INTEGER NOT NULL, 
 "resource_id"   INTEGER NOT NULL, 
 "order"   INTEGER NOT NULL, 
 primary key ("id")
);

ALTER TABLE mcconfig_mapcontextresource ADD CONSTRAINT fk_mcconfig_mapcontextresource_mapcontext FOREIGN KEY (mapcontext_id) REFERENCES mcconfig_mapcontext (id) MATCH FULL ON DELETE CASCADE;
ALTER TABLE mcconfig_mapcontextresource ADD CONSTRAINT fk_mcconfig_mapcontextresource_resource FOREIGN KEY (resource_id) REFERENCES mcconfig_resource (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_mapcontextresource_mapcontext_id" ON "mcconfig_mapcontextresource" (mapcontext_id);
CREATE INDEX "idx_mcconfig_mapcontextresource_resource_id" ON "mcconfig_mapcontextresource" (resource_id);
CREATE INDEX "idx_mcconfig_mapcontextresource_order" ON "mcconfig_mapcontextresource" ("order");


/* mcconfig_application  */
DROP TABLE IF EXISTS "mcconfig_application" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_application_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_application_id" CASCADE;

CREATE SEQUENCE "mcconfig_application_id_seq" ;

CREATE TABLE  "mcconfig_application" (
 "id" numeric   DEFAULT nextval('"mcconfig_application_id_seq"')  NOT NULL,
 "name"   text, 
 "template"  text NOT NULL, 
 "type"  integer NOT NULL, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_application_id" ON "mcconfig_application" (id);


/* mcconfig_applicationwidget */
DROP TABLE IF EXISTS "mcconfig_applicationwidget" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_applicationwidget_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_applicationwidget_application_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_applicationwidget_widget_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_applicationwidget_order" CASCADE;

CREATE SEQUENCE "mcconfig_applicationwidget_id_seq" ;

CREATE TABLE  "mcconfig_applicationwidget" (
 "id" numeric   DEFAULT nextval('"mcconfig_applicationwidget_id_seq"')  NOT NULL,
 "application_id"   INTEGER NOT NULL, 
 "widget_id"   INTEGER NOT NULL, 
 "order"   INTEGER NOT NULL, 
 primary key ("id")
);

ALTER TABLE mcconfig_applicationwidget ADD CONSTRAINT fk_mcconfig_applicationwidget_application FOREIGN KEY (application_id) REFERENCES mcconfig_application (id) MATCH FULL ON DELETE CASCADE;
ALTER TABLE mcconfig_applicationwidget ADD CONSTRAINT fk_mcconfig_applicationwidget_widget FOREIGN KEY (widget_id) REFERENCES mcconfig_widget (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_applicationwidget_application_id" ON "mcconfig_applicationwidget" (application_id);
CREATE INDEX "idx_mcconfig_applicationwidget_widget_id" ON "mcconfig_applicationwidget" (widget_id);
CREATE INDEX "idx_mcconfig_applicationwidget_order" ON "mcconfig_applicationwidget" ("order");


/* mcconfig_session */
DROP TABLE IF EXISTS "mcconfig_session" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_session_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_session_application_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_session_mapcontext_id" CASCADE;

CREATE SEQUENCE "mcconfig_session_id_seq" ;

CREATE TABLE  "mcconfig_session" (
 "id" numeric   DEFAULT nextval('"mcconfig_session_id_seq"')  NOT NULL,
 "application_id"   INTEGER NOT NULL, 
 "mapcontext_id"   INTEGER NOT NULL, 
 primary key ("id")
);

ALTER TABLE mcconfig_session ADD CONSTRAINT fk_mcconfig_session_application FOREIGN KEY (application_id) REFERENCES mcconfig_application (id) MATCH FULL ON DELETE CASCADE;
ALTER TABLE mcconfig_session ADD CONSTRAINT fk_mcconfig_session_mapcontext FOREIGN KEY (mapcontext_id) REFERENCES mcconfig_mapcontext (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_session_application_id" ON "mcconfig_session" (application_id);
CREATE INDEX "idx_mcconfig_session_mapcontext_id" ON "mcconfig_session" (mapcontext_id);


/* mcconfig_field  */
DROP TABLE IF EXISTS "mcconfig_field" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_field_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_field_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_field_resource_id" CASCADE;

CREATE SEQUENCE "mcconfig_field_id_seq" ;

CREATE TABLE  "mcconfig_field" (
 "id" numeric   DEFAULT nextval('"mcconfig_field_id_seq"')  NOT NULL,
 "name"   text, 
 "title"   text, 
 "key"   text, 
 "domain"   text, 
 primary key ("id")
);

CREATE INDEX "idx_mcconfig_field_id" ON "mcconfig_field" (id);


/* mcconfig_fieldoption */
DROP TABLE IF EXISTS "mcconfig_fieldoption" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_fieldoption_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_fieldoption_field_id" CASCADE;

CREATE SEQUENCE "mcconfig_fieldoption_id_seq" ;

CREATE TABLE  "mcconfig_fieldoption" (
 "id" numeric   DEFAULT nextval('"mcconfig_fieldoption_id_seq"')  NOT NULL,
 "field_id"   INTEGER NOT NULL, 
 "name"   text, 
 "value"   text, 
 primary key ("id")
);

ALTER TABLE mcconfig_fieldoption ADD CONSTRAINT fk_mcconfig_fieldoption_field FOREIGN KEY (field_id) REFERENCES mcconfig_field (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_fieldoption_field_id" ON "mcconfig_fieldoption" (field_id);


/* mcconfig_resourcefield */
DROP TABLE IF EXISTS "mcconfig_resourcefield" CASCADE;
DROP SEQUENCE IF EXISTS "mcconfig_resourcefield_id_seq" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourcefield_resource_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourcefield_field_id" CASCADE;
DROP INDEX IF EXISTS "idx_mcconfig_resourcefield_order" CASCADE;

CREATE SEQUENCE "mcconfig_resourcefield_id_seq" ;

CREATE TABLE  "mcconfig_resourcefield" (
 "id" numeric   DEFAULT nextval('"mcconfig_resourcefield_id_seq"')  NOT NULL,
 "resource_id"   INTEGER NOT NULL, 
 "field_id"   INTEGER NOT NULL, 
 "order"  INTEGER, 
 primary key ("id")
);

ALTER TABLE mcconfig_resourcefield ADD CONSTRAINT fk_mcconfig_resourcefield_resource FOREIGN KEY (resource_id) REFERENCES mcconfig_resource (id) MATCH FULL ON DELETE CASCADE;
ALTER TABLE mcconfig_resourcefield ADD CONSTRAINT fk_mcconfig_resourcefield_field FOREIGN KEY (field_id) REFERENCES mcconfig_field (id) MATCH FULL ON DELETE CASCADE;

CREATE INDEX "idx_mcconfig_resourcefield_resource_id" ON "mcconfig_resourcefield" (resource_id);
CREATE INDEX "idx_mcconfig_resourcefield_field_id" ON "mcconfig_resourcefield" (field_id);
CREATE INDEX "idx_mcconfig_resourcefield_order" ON "mcconfig_resourcefield" ("order");

END;