--
-- Sequence preparation start
--

-- Prepares all sequences for PostgreSQL


SELECT setval( sequence_name, max(id)) from table_name;

select 'select setval(\'' || relname || '_s\',max(id)) from ' || relname || ';' as query from pg_class where relname in (  select  substring(relname FROM '^(.*)_s$') from pg_class where relname like 'ez%\_s' and  relname != 'ezcontentobject_tree_s'  and relkind='S' );

SELECT setval('ezapprove_items_s', max(id)) , 'ezapprove_items' as tablename FROM ezapprove_items;
SELECT setval('ezbasket_s', max(id)) , 'ezbasket' as tablename FROM ezbasket;
SELECT setval('ezcollab_group_s', max(id)) , 'ezcollab_group' as tablename FROM ezcollab_group;
SELECT setval('ezcollab_item_s', max(id)) , 'ezcollab_item' as tablename FROM ezcollab_item;
SELECT setval('ezcollab_item_message_link_s', max(id)) , 'ezcollab_item_message_link' as tablename FROM ezcollab_item_message_link;
SELECT setval('ezcollab_notification_rule_s', max(id)) , 'ezcollab_notification_rule' as tablename FROM ezcollab_notification_rule;
SELECT setval('ezcollab_profile_s', max(id)) , 'ezcollab_profile' as tablename FROM ezcollab_profile;
SELECT setval('ezcollab_simple_message_s', max(id)) , 'ezcollab_simple_message' as tablename FROM ezcollab_simple_message;
SELECT setval('ezcontent_translation_s', max(id)) , 'ezcontent_translation' as tablename FROM ezcontent_translation;
SELECT setval('ezcontentbrowsebookmark_s', max(id)) , 'ezcontentbrowsebookmark' as tablename FROM ezcontentbrowsebookmark;
SELECT setval('ezcontentbrowserecent_s', max(id)) , 'ezcontentbrowserecent' as tablename FROM ezcontentbrowserecent;
SELECT setval('ezcontentclass_s', max(id)) , 'ezcontentclass' as tablename FROM ezcontentclass;
SELECT setval('ezcontentclass_attribute_s', max(id)) , 'ezcontentclass_attribute' as tablename FROM ezcontentclass_attribute;
SELECT setval('ezcontentclassgroup_s', max(id)) , 'ezcontentclassgroup' as tablename FROM ezcontentclassgroup;
SELECT setval('ezcontentobject_s', max(id)) , 'ezcontentobject' as tablename FROM ezcontentobject;
SELECT setval('ezcontentobject_attribute_s', max(id)) , 'ezcontentobject_attribute' as tablename FROM ezcontentobject_attribute;
SELECT setval('ezcontentobject_link_s', max(id)) , 'ezcontentobject_link' as tablename FROM ezcontentobject_link;
SELECT setval('ezcontentobject_tree_s', max(node_id)) , 'ezcontentobject_tree' as tablename FROM ezcontentobject_tree;
SELECT setval('ezcontentobject_version_s', max(id)) , 'ezcontentobject_version' as tablename FROM ezcontentobject_version;
SELECT setval('ezdiscountrule_s', max(id)) , 'ezdiscountrule' as tablename FROM ezdiscountrule;
SELECT setval('ezdiscountsubrule_s', max(id)) , 'ezdiscountsubrule' as tablename FROM ezdiscountsubrule;
SELECT setval('ezenumvalue_s', max(id)) , 'ezenumvalue' as tablename FROM ezenumvalue;
SELECT setval('ezforgot_password_s', max(id)) , 'ezforgot_password' as tablename FROM ezforgot_password;
SELECT setval('ezgeneral_digest_user_settings_s', max(id)) , 'ezgeneral_digest_user_settings' as tablename FROM ezgeneral_digest_user_settings;
SELECT setval('ezinfocollection_s', max(id)) , 'ezinfocollection' as tablename FROM ezinfocollection;
SELECT setval('ezinfocollection_attribute_s', max(id)) , 'ezinfocollection_attribute' as tablename FROM ezinfocollection_attribute;
SELECT setval('ezkeyword_s', max(id)) , 'ezkeyword' as tablename FROM ezkeyword;
SELECT setval('ezkeyword_attribute_link_s', max(id)) , 'ezkeyword_attribute_link' as tablename FROM ezkeyword_attribute_link;
SELECT setval('ezmessage_s', max(id)) , 'ezmessage' as tablename FROM ezmessage;
SELECT setval('ezmodule_run_s', max(id)) , 'ezmodule_run' as tablename FROM ezmodule_run;
SELECT setval('eznode_assignment_s', max(id)) , 'eznode_assignment' as tablename FROM eznode_assignment;
SELECT setval('eznotificationcollection_s', max(id)) , 'eznotificationcollection' as tablename FROM eznotificationcollection;
SELECT setval('eznotificationcollection_item_s', max(id)) , 'eznotificationcollection_item' as tablename FROM eznotificationcollection_item;
SELECT setval('eznotificationevent_s', max(id)) , 'eznotificationevent' as tablename FROM eznotificationevent;
SELECT setval('ezoperation_memento_s', max(id)) , 'ezoperation_memento' as tablename FROM ezoperation_memento;
SELECT setval('ezorder_s', max(id)) , 'ezorder' as tablename FROM ezorder;
SELECT setval('ezorder_item_s', max(id)) , 'ezorder_item' as tablename FROM ezorder_item;
SELECT setval('ezpolicy_s', max(id)) , 'ezpolicy' as tablename FROM ezpolicy;
SELECT setval('ezpolicy_limitation_s', max(id)) , 'ezpolicy_limitation' as tablename FROM ezpolicy_limitation;
SELECT setval('ezpolicy_limitation_value_s', max(id)) , 'ezpolicy_limitation_value' as tablename FROM ezpolicy_limitation_value;
SELECT setval('ezpreferences_s', max(id)) , 'ezpreferences' as tablename FROM ezpreferences;
SELECT setval('ezproductcollection_s', max(id)) , 'ezproductcollection' as tablename FROM ezproductcollection;
SELECT setval('ezproductcollection_item_s', max(id)) , 'ezproductcollection_item' as tablename FROM ezproductcollection_item;
SELECT setval('ezproductcollection_item_opt_s', max(id)) , 'ezproductcollection_item_opt' as tablename FROM ezproductcollection_item_opt;
SELECT setval('ezrole_s', max(id)) , 'ezrole' as tablename FROM ezrole;
SELECT setval('ezsearch_object_word_link_s', max(id)) , 'ezsearch_object_word_link' as tablename FROM ezsearch_object_word_link;
SELECT setval('ezsearch_return_count_s', max(id)) , 'ezsearch_return_count' as tablename FROM ezsearch_return_count;
SELECT setval('ezsearch_search_phrase_s', max(id)) , 'ezsearch_search_phrase' as tablename FROM ezsearch_search_phrase;
SELECT setval('ezsearch_word_s', max(id)) , 'ezsearch_word' as tablename FROM ezsearch_word;
SELECT setval('ezsection_s', max(id)) , 'ezsection' as tablename FROM ezsection;
SELECT setval('ezsubtree_notification_rule_s', max(id)) , 'ezsubtree_notification_rule' as tablename FROM ezsubtree_notification_rule;
SELECT setval('eztrigger_s', max(id)) , 'eztrigger' as tablename FROM eztrigger;
SELECT setval('ezurl_s', max(id)) , 'ezurl' as tablename FROM ezurl;
SELECT setval('ezurlalias_s', max(id)) , 'ezurlalias' as tablename FROM ezurlalias;
SELECT setval('ezuser_accountkey_s', max(id)) , 'ezuser_accountkey' as tablename FROM ezuser_accountkey;
SELECT setval('ezuser_discountrule_s', max(id)) , 'ezuser_discountrule' as tablename FROM ezuser_discountrule;
SELECT setval('ezuser_role_s', max(id)) , 'ezuser_role' as tablename FROM ezuser_role;
SELECT setval('ezvattype_s', max(id)) , 'ezvattype' as tablename FROM ezvattype;
SELECT setval('ezwaituntildatevalue_s', max(id)) , 'ezwaituntildatevalue' as tablename FROM ezwaituntildatevalue;
SELECT setval('ezwishlist_s', max(id)) , 'ezwishlist' as tablename FROM ezwishlist;
SELECT setval('ezworkflow_s', max(id)) , 'ezworkflow' as tablename FROM ezworkflow;
SELECT setval('ezworkflow_assign_s', max(id)) , 'ezworkflow_assign' as tablename FROM ezworkflow_assign;
SELECT setval('ezworkflow_event_s', max(id)) , 'ezworkflow_event' as tablename FROM ezworkflow_event;
SELECT setval('ezworkflow_group_s', max(id)) , 'ezworkflow_group' as tablename FROM ezworkflow_group;
SELECT setval('ezworkflow_process_s', max(id)) , 'ezworkflow_process' as tablename FROM ezworkflow_process;
SELECT setval('ezurlwildcard_s', max(id)) , 'ezurlwildcard' as tablename FROM ezurlwildcard;

--
-- Sequence preparation end
-- command to generate sequence queries
--  echo "select 'select setval(\'' || relname || '_s\',max(id)) from ' || relname || ';' as query from pg_class where relname in (  select trim(  trailing '_s' from relname) from pg_class where relname like 'ez%\_s' and  relname != 'ezcontentobject_tree_s'  and relkind='S' );" | psql -q -t  sp
