Skip to content

Commit

Permalink
Carrega dump do banco de dados se estiver sem a tabela system_role
Browse files Browse the repository at this point in the history
  • Loading branch information
lpirola committed Aug 1, 2024
1 parent 056da4e commit eecb885
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dev
!dev/db/dump.sql
var/*/*
# **/*.log
# # **/*.md
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Add default env values
run: |
cp .env.sample .env
cp .env.sample app
cp .env.sample app/.env
- uses: hoverkraft-tech/compose-action@v1.5.1
with:
Expand Down Expand Up @@ -79,7 +79,7 @@ jobs:
- name: Add default env values
run: |
cp .env.sample .env
cp .env.sample app
cp .env.sample app/.env
- uses: hoverkraft-tech/compose-action@v1.5.1
with:
Expand Down
50 changes: 49 additions & 1 deletion src/core/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,13 +443,61 @@ function init(array $config) {

$this->_initStorage();

if(defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE))
if(defined('DB_UPDATES_FILE') && file_exists(DB_UPDATES_FILE)) {
$this->_firstDbUpdate();
$this->_dbUpdates();
}

$this->applyHookBoundTo($this, 'app.init:after');
return $this;
}

function _firstDbUpdate()
{
try {
// Conectar ao banco de dados usando Doctrine DBAL
$connection = $this->em->getConnection();

// Verificar a existência da tabela system_role
$schemaManager = $connection->createSchemaManager();
$tables = $schemaManager->listTableNames();

if (in_array('system_role', $tables)) {
echo 'A tabela "system_role" já existe. Nenhuma ação necessária.';
} else {
// Ler o arquivo SQL
$sqlFile = PROTECTED_PATH . 'dev/db/dump.sql';
$sql = file_get_contents($sqlFile);

if ($sql === false) {
throw new Exception("Erro ao ler o arquivo SQL.");
}

// Filtrar linhas específicas do psql
// $sqlLines = explode("\n", $sql);
// $filteredSqlLines = array_filter($sqlLines, function ($line) {
// // Remover linhas que começam com '\'
// return !preg_match('/^\\\/', trim($line));
// });

// $filteredSql = implode("\n", $filteredSqlLines);

// Registrar SQL filtrado para depuração
// file_put_contents('filtered_sql.sql', $filteredSql);

// Dividir o SQL por ';' para assegurar a execução correta de múltiplas queries
$connection->executeStatement($sql);
echo 'Arquivo SQL executado com sucesso.';
}
} catch (Exception $e) {
// Reverter a transação em caso de erro
if (isset($connection) && $connection->isTransactionActive()) {
$connection->rollBack();
}
echo 'Erro ao executar o arquivo SQL: ' . $e->getMessage();
}
}

/**
* Executa a aplicação
*
Expand Down

0 comments on commit eecb885

Please sign in to comment.