public static function getInstance() { $basepath = realpath(__DIR__ . '/../../'); $db = new ExtendedPdo('sqlite:' . $basepath . '/weights.db'); $db->exec("CREATE TABLE IF NOT EXISTS persons (id INTEGER PRIMARY KEY, timeadded INTEGER, currentweight REAL, telegramid INTEGER, firstname TEXT, username TEXT, token TEXT)"); $db->exec("CREATE TABLE IF NOT EXISTS weights (id INTEGER PRIMARY KEY, timestamp INTEGER, weight REAL, personid INTEGER)"); return $db; }
public static function setUpBeforeClass() { $extendedPdo = new ExtendedPdo('sqlite::memory:'); $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`changelog` (\n `id` integer PRIMARY KEY AUTOINCREMENT,\n `hash` char(40) NOT NULL,\n `message` text,\n `message_short` varchar(100),\n `datetime` datetime NOT NULL,\n `author` varchar(50) NOT NULL,\n `commit_link` varchar(100) NOT NULL,\n `created_at` datetime,\n `updated_at` datetime\n )"); self::$connection = new ConnectionLocator(function () use($extendedPdo) { return $extendedPdo; }); }
public static function setUpBeforeClass() { $extendedPdo = new ExtendedPdo('sqlite::memory:'); $extendedPdo->exec("ATTACH DATABASE `jpemeric_stream.db` AS `jpemeric_stream`"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_stream`.`activity` (\n `id` integer PRIMARY KEY AUTOINCREMENT,\n `message` text NOT NULL,\n `message_long` text NOT NULL,\n `datetime` datetime NOT NULL,\n `metadata` text NOT NULL,\n `type` varchar(10) NOT NULL,\n `type_id` integer NOT NULL,\n `created_at` datetime,\n `updated_at` datetime\n )"); self::$connection = new ConnectionLocator(function () use($extendedPdo) { return $extendedPdo; }); }
public static function setUpBeforeClass() { $extendedPdo = new ExtendedPdo('sqlite::memory:'); $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`introduction` (\n `id` integer PRIMARY KEY AUTOINCREMENT,\n `type` varchar(10) NOT NULL,\n `value` varchar(25) NOT NULL,\n `title` varchar(100) NOT NULL,\n `content` text NOT NULL,\n `image` image\n )"); self::$connection = new ConnectionLocator(function () use($extendedPdo) { return $extendedPdo; }); }
public static function setUpBeforeClass() { $extendedPdo = new ExtendedPdo('sqlite::memory:'); $extendedPdo->exec("ATTACH DATABASE `jpemeric_blog.db` AS `jpemeric_blog`"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`post` (\n `id` integer PRIMARY KEY AUTOINCREMENT,\n `title` varchar(60) NOT NULL,\n `path` varchar(60) NOT NULL,\n `category` varchar(15) NOT NULL,\n `date` datetime,\n `body` text,\n `display` integer(1) NOT NULL\n )"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series` (\n `id` integer PRIMARY KEY AUTOINCREMENT,\n `title` text NOT NULL,\n `description` text NOT NULL\n )"); $extendedPdo->exec("\n CREATE TABLE IF NOT EXISTS `jpemeric_blog`.`series_post` (\n `series` integer NOT NULL,\n `post` integer NOT NULL,\n `order` integer(1) NOT NULL\n )"); self::$connection = new ConnectionLocator(function () use($extendedPdo) { return $extendedPdo; }); }
/** * @return ExtendedPdo */ public static function getInstance() { $dbopts = parse_url(getenv('DATABASE_URL')); $db = new ExtendedPdo("pgsql:host={$dbopts["host"]};port={$dbopts["port"]};dbname=" . ltrim($dbopts["path"], '/'), $dbopts['user'], $dbopts['pass']); $db->exec("CREATE TABLE IF NOT EXISTS users\n(\n id serial NOT NULL,\n username character varying(50) NOT NULL,\n registered_by integer NOT NULL,\n created timestamp with time zone NOT NULL DEFAULT now(),\n CONSTRAINT user_pk PRIMARY KEY (id),\n CONSTRAINT users_username_uk UNIQUE (username)\n);"); return $db; }
<?php require '../vendor/autoload.php'; use Aura\Sql\ExtendedPdo; $pdo = new ExtendedPdo('sqlite:../database.sqlite'); $pdo->exec('CREATE TABLE records (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, animal_type TEXT, animal_name TEXT, animal_age INTEGER, timestamp NUMERIC);'); if (file_exists('../database.sqlite')) { echo 'Database and tables created.'; } else { echo 'Database was not created. Please check to make sure this directory is writeable.'; }
public static function postInstall(Event $oEvent) { // Initialize $sAppDirPath = __DIR__ . '/../../../app'; $sLocalDistConfigPath = sprintf('%s/%s', $sAppDirPath, 'install/local.php.dist'); $sLocalConfigPath = sprintf('%s/%s', $sAppDirPath, 'config/local.php'); // Create file manager $oFileManager = new FileManager([]); $oFileManager->addHandler('local', 'PHP', []); // Make sure console is executable $sOutput = 'Make sure the deployment manager console is executable'; $sErrorMessage = ''; try { ExtendedShell::exec(sprintf('chmod +x %s', sprintf('%s/%s', $sAppDirPath, 'console'))); } catch (Exception $oException) { // Update error message $sErrorMessage = $oException->getMessage(); } // No error if ($sErrorMessage === '') { $sResult = 'OK'; } else { $sResult = 'KO'; } // Output $oEvent->getIO()->write(sprintf("\n%s: %s", $sOutput, $sResult)); // Copy dist config $sOutput = 'Copy the local dist config'; $sErrorMessage = ''; try { $oFileManager->copy($sLocalDistConfigPath, $sLocalConfigPath); } catch (Exception $oException) { // Update error message $sErrorMessage = $oException->getMessage(); } // Error if ($sErrorMessage === '') { // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Explain $oEvent->getIO()->write("\nTo install the manager, you need a valid UTF-8 database as well as a user with " . "read/write privileges on it. Once you have it, please fill in the information below:\n"); // Set ouput $sOutput = 'Update local config parameters'; // Get local config content $sLocalConfigContent = $oFileManager->read($sLocalConfigPath); // Get values to ask $aValuesToAsk = ['%DATASOURCE_HOSTNAME%' => ['label' => 'database host', 'default' => 'localhost', 'mandatory' => true], '%DATASOURCE_DATABASE%' => ['label' => 'database name', 'default' => 'deployment', 'mandatory' => true], '%DATASOURCE_USERNAME%' => ['label' => 'database user name', 'mandatory' => true], '%DATASOURCE_PASSWORD%' => ['label' => 'database user password', 'mandatory' => false], '%BUILD_nb_backups_per_project%' => ['label' => 'number of backups kept per project', 'default' => '2', 'mandatory' => true], '%BUILD_BIN_COMPOSER%' => ['label' => 'full path to composer binary', 'default' => '/usr/bin/composer', 'mandatory' => true, 'binary' => ['check_command' => '%s -v', 'name' => 'composer']], '%BUILD_BIN_GIT%' => ['label' => 'full path to git binary', 'default' => '/usr/local/bin/git', 'mandatory' => true, 'binary' => ['check_command' => '%s --version', 'name' => 'git']], '%BUILD_BIN_PHP%' => ['label' => 'full path to php binary', 'default' => '/usr/bin/php', 'mandatory' => true, 'binary' => ['check_command' => '%s -v', 'name' => 'php']]]; // Loop through values to ask foreach ($aValuesToAsk as $sKeyToReplace => $aValueToAsk) { // Initialize $sDefault = isset($aValueToAsk['default']) ? $aValueToAsk['default'] : null; // Binary path if (isset($aValueToAsk['binary'])) { // Get value $sValue = ExtendedComposer::askBinaryPath($oEvent, $aValueToAsk['label'], $aValueToAsk['binary']['name'], $aValueToAsk['binary']['check_command'], $sDefault, true, $aValueToAsk['mandatory']); } else { // Get value $sValue = ExtendedComposer::askString($oEvent, $aValueToAsk['label'], $sDefault, $aValueToAsk['mandatory']); } // Replace config $sLocalConfigContent = preg_replace(sprintf('/%s/', ExtendedString::pregQuote($sKeyToReplace)), $sValue, $sLocalConfigContent); } // Put local config content $oFileManager->write($sLocalConfigContent, $sLocalConfigPath, WriteMethod::OVERWRITE); // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Get config $aConfig = ExtendedArray::extendWithDefaultValues(require __DIR__ . '/../../../app/config/local.php', require __DIR__ . '/../../../app/config/global.php'); // Build extended PDO $aDatasourceConfig = $aConfig['datasources']['write']['deployment']; $oExtendedPDO = new ExtendedPdo("mysql:host={$aDatasourceConfig['hostname']};" . "dbname={$aDatasourceConfig['database']};", $aDatasourceConfig['username'], $aDatasourceConfig['password'], $aConfig['pdo_options']); // Execute SQL commands $sOutput = 'Execute SQL commands'; $sErrorMessage = ''; try { // Get SQL files $aSQLFiles = $oFileManager->explore(__DIR__ . '/../../../sql', OrderField::BASENAME); // Loop through SQL files /** @var $oSQLFile \Asticode\FileManager\Entity\File */ foreach ($aSQLFiles as $oSQLFile) { // Split statements $aStatements = explode(';', $oFileManager->read($oSQLFile->getPath())); // Loop through statements foreach ($aStatements as $sStatement) { if ($sStatement !== '') { $oExtendedPDO->exec($sStatement); } } } } catch (Exception $oException) { // Get error message $sErrorMessage = $oException->getMessage(); } // Error if ($sErrorMessage === '') { // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Create dirs $sOutput = 'Create directories'; $aDirsToCreate = ['backups', 'gits', 'tmp']; // Check keys exist ExtendedArray::checkRequiredKeys($aConfig['build']['dirs'], $aDirsToCreate); // Loop through dirs to create foreach ($aDirsToCreate as $sDirToCreate) { // Create dir $oFileManager->createDir($aConfig['build']['dirs'][$sDirToCreate]); } // Output $oEvent->getIO()->write(sprintf("\n%s: OK", $sOutput)); // Conclude $oEvent->getIO()->write("\n\nInstallation successful!\n\n\nYou can now add a new project with\n\n" . " \$ <your path>/app/console project:add\n\nOr remove a project with\n\n" . " \$ <your path>/app/console project:remove\n"); } else { // Output $oEvent->getIO()->write(sprintf("\n%s: KO\n", $sOutput)); // Throw exception throw new RuntimeException($sErrorMessage); } } else { // Output $oEvent->getIO()->write(sprintf("\n%s: KO\n", $sOutput)); // Throw exception throw new RuntimeException($sErrorMessage); } }