   `frozen_at` timestamp NULL DEFAULT NULL,
   `checked_positions` varchar(10) CHARACTER SET utf8 NOT NULL DEFAULT 'T',
   `shortcodes` tinyint(4) NOT NULL DEFAULT '0',
-  `css` text COLLATE utf8_bin NOT NULL,
-  PRIMARY KEY (`id`),
-  UNIQUE KEY `slug` (`slug`),
-  KEY `language_id` (`language_id`),
-  KEY `parent_firm_id` (`firm_parent_id`,`create_date`),
-  KEY `shortcodes` (`shortcodes`)
-) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1327 ;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `tbl_firm_language`
---
-
-CREATE TABLE IF NOT EXISTS `tbl_firm_language` (
+  `css` text COLLATE utf8_bin NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Struttura della tabella `tbl_firm_language`
+--
+
+CREATE TABLE `tbl_firm_language` (
   `firm_id` int(11) NOT NULL,
-  `language_id` int(11) NOT NULL,
-  PRIMARY KEY (`firm_id`,`language_id`),
-  KEY `firm_id` (`firm_id`),
-  KEY `language_id` (`language_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `tbl_firm_user`
---
-
-CREATE TABLE IF NOT EXISTS `tbl_firm_user` (
+  `language_id` int(11) NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8;
+
+-- --------------------------------------------------------
+
+--
+-- Struttura della tabella `tbl_firm_user`
+--
+
+CREATE TABLE `tbl_firm_user` (
   `firm_id` int(11) NOT NULL,
   `user_id` int(11) NOT NULL,
-  `role` char(1) COLLATE utf8_bin NOT NULL,
-  PRIMARY KEY (`firm_id`,`user_id`),
-  KEY `user_id` (`user_id`),
-  KEY `firm_id` (`firm_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
-
--- --------------------------------------------------------
-
---
--- Table structure for table `tbl_journalentry`
---
-
-CREATE TABLE IF NOT EXISTS `tbl_journalentry` (
-  `id` int(11) NOT NULL AUTO_INCREMENT,
+  `role` char(1) COLLATE utf8_bin NOT NULL
+) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
+
+-- --------------------------------------------------------
+
+--
+-- Struttura della tabella `tbl_journalentry`
+--
+
+CREATE TABLE `tbl_journalentry` (
+  `id` int(11) NOT NULL,
   `firm_id` int(11) NOT NULL,
   `date` date NOT NULL,
   `description` varchar(255) CHARACTER SET utf8 NOT NULL,

  `is_adjustment` tinyint(1) NOT NULL DEFAULT '0',
  `is_included` tinyint(1) NOT NULL DEFAULT '1',
  `rank` int(11) NOT NULL,
  `transaction_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `firm_rank` (`firm_id`,`rank`),
  KEY `firm_id` (`firm_id`),
  KEY `is_confirmed` (`is_confirmed`),
  KEY `is_closing` (`is_closing`),
  KEY `firm_is_confirmed` (`firm_id`,`is_confirmed`),
  KEY `firm_is_closing` (`firm_id`,`is_closing`),
  KEY `is_included` (`is_included`),
  KEY `transaction_id` (`transaction_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_language`
--

CREATE TABLE IF NOT EXISTS `tbl_language` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `language_code` char(3) CHARACTER SET utf8 NOT NULL,
  `country_code` char(3) CHARACTER SET utf8 DEFAULT NULL,
  `english_name` varchar(64) CHARACTER SET utf8 NOT NULL,
  `native_name` varchar(64) CHARACTER SET utf8 NOT NULL,
  `is_default` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  KEY `is_default` (`is_default`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=9 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_migration`
--

CREATE TABLE IF NOT EXISTS `tbl_migration` (
  `version` varchar(255) NOT NULL,
  `apply_time` int(11) DEFAULT NULL,
  PRIMARY KEY (`version`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_posting`
--

CREATE TABLE IF NOT EXISTS `tbl_posting` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `account_id` int(11) NOT NULL,
  `journalentry_id` int(11) NOT NULL,
  `amount` decimal(16,2) NOT NULL COMMENT 'positive if Debit, negative if Credit',
  `rank` int(11) NOT NULL,
  `comment` varchar(100) CHARACTER SET utf8 NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `journalentryrank` (`journalentry_id`,`rank`),
  KEY `account_id` (`account_id`),
  KEY `journalentry_id` (`journalentry_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_bin AUTO_INCREMENT=70408 ;

-- --------------------------------------------------------

--
-- Table structure for table `tbl_profiles`
--

CREATE TABLE IF NOT EXISTS `tbl_profiles` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `last_name` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
  `school` varchar(255) COLLATE utf8_bin NOT NULL DEFAULT '',

  `superuser` int(1) NOT NULL DEFAULT '0',
  `status` int(1) NOT NULL DEFAULT '0',
  `create_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `lastvisit_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;

-- --------------------------------------------------------

--
-- Struttura della tabella `tbl_user_oauth`
--

CREATE TABLE `tbl_user_oauth` (
  `user_id` int(11) NOT NULL,
  `provider` varchar(45) NOT NULL,
  `identifier` varchar(64) NOT NULL,
  `profile_cache` text,
  `session_data` text
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

--
-- Indici per le tabelle scaricate
--

--
-- Indici per le tabelle `tbl_account`
--
ALTER TABLE `tbl_account`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `firm_code` (`firm_id`,`code`),
  ADD KEY `account_parent_id` (`account_parent_id`),
  ADD KEY `firm_id` (`firm_id`);

--
-- Indici per le tabelle `tbl_challenge`
--
ALTER TABLE `tbl_challenge`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `user_id_2` (`user_id`,`exercise_id`),
  ADD KEY `mark` (`rate`),
  ADD KEY `assigned_at` (`assigned_at`),
  ADD KEY `instructor_id` (`instructor_id`),
  ADD KEY `user_id` (`user_id`),
  ADD KEY `firm_id` (`firm_id`),
  ADD KEY `exercise_id` (`exercise_id`),
  ADD KEY `suspended_at` (`suspended_at`),
  ADD KEY `transaction_id` (`transaction_id`),
  ADD KEY `checked_at` (`checked_at`),
  ADD KEY `session` (`session`);

--
-- Indici per le tabelle `tbl_event`
--
ALTER TABLE `tbl_event`
  ADD PRIMARY KEY (`id`),
  ADD KEY `firm_id` (`firm_id`),
  ADD KEY `user_id` (`user_id`);

--
-- Indici per le tabelle `tbl_exercise`
--
ALTER TABLE `tbl_exercise`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `slug` (`slug`),
  ADD KEY `user_id` (`user_id`),
  ADD KEY `firm_id` (`firm_id`);

--
-- Indici per le tabelle `tbl_firm`
--
ALTER TABLE `tbl_firm`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `slug` (`slug`),
  ADD KEY `language_id` (`language_id`),
  ADD KEY `parent_firm_id` (`firm_parent_id`,`create_date`),
  ADD KEY `shortcodes` (`shortcodes`);

--
-- Indici per le tabelle `tbl_firm_language`
--
ALTER TABLE `tbl_firm_language`
  ADD PRIMARY KEY (`firm_id`,`language_id`),
  ADD KEY `firm_id` (`firm_id`),
  ADD KEY `language_id` (`language_id`);

--
-- Indici per le tabelle `tbl_firm_user`
--
ALTER TABLE `tbl_firm_user`
  ADD PRIMARY KEY (`firm_id`,`user_id`),
  ADD KEY `user_id` (`user_id`),
  ADD KEY `firm_id` (`firm_id`);

--
-- Indici per le tabelle `tbl_journalentry`
--
ALTER TABLE `tbl_journalentry`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `firm_rank` (`firm_id`,`rank`),
  ADD KEY `firm_id` (`firm_id`),
  ADD KEY `is_confirmed` (`is_confirmed`),
  ADD KEY `is_closing` (`is_closing`),
  ADD KEY `firm_is_confirmed` (`firm_id`,`is_confirmed`),
  ADD KEY `firm_is_closing` (`firm_id`,`is_closing`),
  ADD KEY `is_included` (`is_included`),
  ADD KEY `transaction_id` (`transaction_id`);

--
-- Indici per le tabelle `tbl_language`
--
ALTER TABLE `tbl_language`
  ADD PRIMARY KEY (`id`),
  ADD KEY `is_default` (`is_default`);

--
-- Indici per le tabelle `tbl_migration`
--
ALTER TABLE `tbl_migration`
  ADD PRIMARY KEY (`version`);

--
-- Indici per le tabelle `tbl_posting`
--
ALTER TABLE `tbl_posting`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `journalentryrank` (`journalentry_id`,`rank`),
  ADD KEY `account_id` (`account_id`),
  ADD KEY `journalentry_id` (`journalentry_id`);

--
-- Indici per le tabelle `tbl_profiles`
--
ALTER TABLE `tbl_profiles`
  ADD PRIMARY KEY (`user_id`),
  ADD KEY `email_notices` (`email_notices`);

--
-- Indici per le tabelle `tbl_profiles_fields`
--
ALTER TABLE `tbl_profiles_fields`
  ADD PRIMARY KEY (`id`);

--
-- Indici per le tabelle `tbl_template`
--
ALTER TABLE `tbl_template`
  ADD PRIMARY KEY (`id`),
  ADD KEY `firm_id` (`firm_id`);

--
-- Indici per le tabelle `tbl_transaction`
--
ALTER TABLE `tbl_transaction`
  ADD PRIMARY KEY (`id`),
  ADD KEY `exercise_id` (`exercise_id`),
  ADD KEY `event_date` (`event_date`,`rank`);

--
-- Indici per le tabelle `tbl_users`
--
ALTER TABLE `tbl_users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `user_username` (`username`),
  ADD UNIQUE KEY `user_email` (`email`);

--
-- Indici per le tabelle `tbl_user_oauth`
--
ALTER TABLE `tbl_user_oauth`
  ADD PRIMARY KEY (`provider`,`identifier`),
  ADD UNIQUE KEY `unic_user_id_name` (`user_id`,`provider`),
  ADD KEY `oauth_user_id` (`user_id`);

--
-- AUTO_INCREMENT per le tabelle scaricate
--

--
-- AUTO_INCREMENT per la tabella `tbl_account`
--
ALTER TABLE `tbl_account`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=579927;
--
-- AUTO_INCREMENT per la tabella `tbl_challenge`
--
ALTER TABLE `tbl_challenge`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=118;
--
-- AUTO_INCREMENT per la tabella `tbl_event`
--
ALTER TABLE `tbl_event`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=112417;
--
-- AUTO_INCREMENT per la tabella `tbl_exercise`
--
ALTER TABLE `tbl_exercise`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT per la tabella `tbl_firm`
--
ALTER TABLE `tbl_firm`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2837;
--
-- AUTO_INCREMENT per la tabella `tbl_journalentry`
--
ALTER TABLE `tbl_journalentry`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=38636;
--
-- AUTO_INCREMENT per la tabella `tbl_language`
--
ALTER TABLE `tbl_language`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
--
-- AUTO_INCREMENT per la tabella `tbl_posting`
--
ALTER TABLE `tbl_posting`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=259201;
--
-- AUTO_INCREMENT per la tabella `tbl_profiles`
--
ALTER TABLE `tbl_profiles`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=722;
--
-- AUTO_INCREMENT per la tabella `tbl_profiles_fields`
--
ALTER TABLE `tbl_profiles_fields`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10;
--
-- AUTO_INCREMENT per la tabella `tbl_template`
--
ALTER TABLE `tbl_template`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=553;
--
-- AUTO_INCREMENT per la tabella `tbl_transaction`
--
ALTER TABLE `tbl_transaction`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=26;
--
-- AUTO_INCREMENT per la tabella `tbl_users`
--
ALTER TABLE `tbl_users`
  MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=722;
--
-- Limiti per le tabelle scaricate
--

--
-- Limiti per la tabella `tbl_account`
--
ALTER TABLE `tbl_account`
  ADD CONSTRAINT `tbl_account_ibfk_1` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`) ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_challenge`
--
ALTER TABLE `tbl_challenge`
  ADD CONSTRAINT `tbl_challenge_ibfk_1` FOREIGN KEY (`instructor_id`) REFERENCES `tbl_users` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_challenge_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_challenge_ibfk_3` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_challenge_ibfk_5` FOREIGN KEY (`exercise_id`) REFERENCES `tbl_exercise` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_challenge_ibfk_6` FOREIGN KEY (`transaction_id`) REFERENCES `tbl_transaction` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_event`
--
ALTER TABLE `tbl_event`
  ADD CONSTRAINT `tbl_event_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_event_ibfk_2` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`) ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_exercise`
--
ALTER TABLE `tbl_exercise`
  ADD CONSTRAINT `tbl_exercise_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_exercise_ibfk_2` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`);

--
-- Limiti per la tabella `tbl_firm_user`
--
ALTER TABLE `tbl_firm_user`
  ADD CONSTRAINT `tbl_firm_user_ibfk_1` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_firm_user_ibfk_2` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`) ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_journalentry`
--
ALTER TABLE `tbl_journalentry`
  ADD CONSTRAINT `tbl_journalentry_ibfk_1` FOREIGN KEY (`firm_id`) REFERENCES `tbl_firm` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_journalentry_ibfk_2` FOREIGN KEY (`transaction_id`) REFERENCES `tbl_transaction` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_posting`
--
ALTER TABLE `tbl_posting`
  ADD CONSTRAINT `tbl_posting_ibfk_1` FOREIGN KEY (`account_id`) REFERENCES `tbl_account` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `tbl_posting_ibfk_2` FOREIGN KEY (`journalentry_id`) REFERENCES `tbl_journalentry` (`id`) ON UPDATE CASCADE;

--
-- Limiti per la tabella `tbl_profiles`
--
ALTER TABLE `tbl_profiles`
  ADD CONSTRAINT `user_profile_id` FOREIGN KEY (`user_id`) REFERENCES `tbl_users` (`id`) ON DELETE CASCADE;

--
-- Limiti per la tabella `tbl_transaction`
--
ALTER TABLE `tbl_transaction`
  ADD CONSTRAINT `tbl_transaction_ibfk_1` FOREIGN KEY (`exercise_id`) REFERENCES `tbl_exercise` (`id`) ON UPDATE CASCADE;
