Skip to content

Commit

Permalink
Merge 1d38f71 into 8a56891
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkiljo committed Nov 10, 2019
2 parents 8a56891 + 1d38f71 commit 7039daa
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 13 deletions.
4 changes: 2 additions & 2 deletions nflow-engine/src/main/resources/scripts/db/h2.create.ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ create table if not exists nflow_workflow_action (
foreign key (workflow_id) references nflow_workflow(id) on delete cascade
);

alter table nflow_workflow add constraint fk_workflow_parent
alter table nflow_workflow add constraint if not exists fk_workflow_parent
foreign key (parent_workflow_id, parent_action_id) references nflow_workflow_action (workflow_id, id) on delete cascade;

alter table nflow_workflow add constraint fk_workflow_root
alter table nflow_workflow add constraint if not exists fk_workflow_root
foreign key (root_workflow_id) references nflow_workflow (id) on delete cascade;

create table if not exists nflow_workflow_state (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
-- production tables

if not exists (select 1 from sys.tables where name='nflow_workflow')
create table nflow_workflow (
id int not null identity(1,1) primary key,
status varchar(32) not null,
Expand All @@ -23,15 +24,18 @@ create table nflow_workflow (
constraint nflow_workflow_uniq unique (type, external_id, executor_group)
);

if not exists (select 1 from sys.triggers where name='nflow_workflow_modified_trigger')
exec dbo.sp_executesql @statement = N'
create trigger nflow_workflow_modified_trigger on nflow_workflow after update as
begin
update nflow_workflow set modified = SYSDATETIMEOFFSET()
from nflow_workflow wf inner join inserted i on wf.id = i.id
end;
end';

drop index nflow_workflow_activation;
if not exists (select 1 from sys.indexes where name='nflow_workflow_activation')
create index nflow_workflow_activation on nflow_workflow(next_activation, modified);

if not exists (select 1 from sys.tables where name='nflow_workflow_action')
create table nflow_workflow_action (
id int not null identity(1,1) primary key,
workflow_id int not null,
Expand All @@ -46,12 +50,15 @@ create table nflow_workflow_action (
constraint nflow_workflow_action_uniq unique (workflow_id, id)
);

if not exists (select 1 from sys.foreign_keys where name='fk_workflow_parent')
alter table nflow_workflow add constraint fk_workflow_parent
foreign key (parent_workflow_id, parent_action_id) references nflow_workflow_action (workflow_id, id) on update no action;

if not exists (select 1 from sys.foreign_keys where name='fk_workflow_root')
alter table nflow_workflow add constraint fk_workflow_root
foreign key (root_workflow_id) references nflow_workflow (id) on update no action;

if not exists (select 1 from sys.tables where name='nflow_workflow_state')
create table nflow_workflow_state (
workflow_id int not null,
action_id int not null,
Expand All @@ -61,6 +68,7 @@ create table nflow_workflow_state (
foreign key (workflow_id) references nflow_workflow(id) on delete cascade
);

if not exists (select 1 from sys.tables where name='nflow_executor')
create table nflow_executor (
id int not null identity(1,1) primary key,
host varchar(253) not null,
Expand All @@ -72,6 +80,7 @@ create table nflow_executor (
stopped datetimeoffset(3)
);

if not exists (select 1 from sys.tables where name='nflow_workflow_definition')
create table nflow_workflow_definition (
type varchar(64) not null,
definition_sha1 varchar(40) not null,
Expand All @@ -83,11 +92,13 @@ create table nflow_workflow_definition (
primary key (type, executor_group)
);

if not exists (select 1 from sys.triggers where name='nflow_workflow_definition_modified_trigger')
exec dbo.sp_executesql @statement = N'
create trigger nflow_workflow_definition_modified_trigger on nflow_workflow_definition after update as
begin
update nflow_workflow_definition set modified = SYSDATETIMEOFFSET()
from nflow_workflow_definition df inner join inserted i on df.type = i.type and df.executor_group = i.executor_group
end;
end';


-- Archive tables
Expand All @@ -97,6 +108,7 @@ end;
-- - same indexes and constraints as production tables
-- - remove recursive foreign keys

if not exists (select 1 from sys.tables where name='nflow_archive_workflow')
create table nflow_archive_workflow (
id int not null primary key,
status varchar(32) not null,
Expand All @@ -120,6 +132,7 @@ create table nflow_archive_workflow (
constraint nflow_archive_workflow_uniq unique (type, external_id, executor_group)
);

if not exists (select 1 from sys.tables where name='nflow_archive_workflow_action')
create table nflow_archive_workflow_action (
id int not null primary key,
workflow_id int not null,
Expand All @@ -134,6 +147,7 @@ create table nflow_archive_workflow_action (
constraint nflow_archive_workflow_action_uniq unique (workflow_id, id)
);

if not exists (select 1 from sys.tables where name='nflow_archive_workflow_state')
create table nflow_archive_workflow_state (
workflow_id int not null,
action_id int not null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
alter table nflow_workflow add started timestamp;
alter table nflow_archive_workflow add started timestamp;

update nflow_workflow w, (select workflow_id, min(execution_start) as started from nflow_workflow_action group by workflow_id) a
set w.started = a.started, w.modified = w.modified where w.id = a.workflow_id;
update nflow_archive_workflow w, (select workflow_id, min(execution_start) as started from nflow_archive_workflow_action group by workflow_id) a
set w.started = a.started, w.modified = w.modified where w.id = a.workflow_id;
update nflow_workflow w set
started=(select min(execution_start) from nflow_workflow_action a where a.workflow_id = w.id group by a.workflow_id),
modified=w.modified;

update nflow_archive_workflow w set
started=(select min(execution_start) from nflow_archive_workflow_action a where a.workflow_id = w.id group by a.workflow_id),
modified=w.modified;
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
alter table nflow_workflow add started datetimeoffset(3);
alter table nflow_archive_workflow add started datetimeoffset(3);

update nflow_workflow w, (select workflow_id, min(execution_start) as started from nflow_workflow_action group by workflow_id) a
set w.started = a.started, w.modified = w.modified where w.id = a.workflow_id;
update nflow_archive_workflow w, (select workflow_id, min(execution_start) as started from nflow_archive_workflow_action group by workflow_id) a
set w.started = a.started, w.modified = w.modified where w.id = a.workflow_id;
go

update w set
w.started = a.started,
w.modified = w.modified
from
nflow_workflow w,
(select workflow_id, min(execution_start) as started from nflow_workflow_action group by workflow_id) a
where w.id = a.workflow_id;

update w set
w.started = a.started,
w.modified = w.modified
from
nflow_archive_workflow w,
(select workflow_id, min(execution_start) as started from nflow_archive_workflow_action group by workflow_id) a
where w.id = a.workflow_id;

0 comments on commit 7039daa

Please sign in to comment.