File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ class Forge extends \CodeIgniter\Database\Forge
4949 *
5050 * @var string
5151 */
52- protected $ createDatabaseStr = 'CREATE DATABASE %s CHARACTER SET %s COLLATE %s ' ;
52+ protected $ createDatabaseStr = 'CREATE DATABASE IF NOT EXISTS %s CHARACTER SET %s COLLATE %s ' ;
5353
5454 /**
5555 * DROP CONSTRAINT statement
Original file line number Diff line number Diff line change 4444class Forge extends \CodeIgniter \Database \Forge
4545{
4646
47+ /**
48+ * CHECK DATABASE EXIST statement
49+ *
50+ * @var string
51+ */
52+ private $ checkDatabaseExist = 'SELECT 1 FROM pg_database WHERE datname = %s ' ;
53+
4754 /**
4855 * DROP CONSTRAINT statement
4956 *
@@ -77,6 +84,26 @@ class Forge extends \CodeIgniter\Database\Forge
7784
7885 //--------------------------------------------------------------------
7986
87+ /**
88+ * Create database
89+ *
90+ * @param string $db_name
91+ *
92+ * @return boolean
93+ * @throws \CodeIgniter\Database\Exceptions\DatabaseException
94+ */
95+ public function createDatabase (string $ db_name ): bool
96+ {
97+ if ($ this ->db ->query (sprintf ($ this ->checkDatabaseExist , $ this ->db ->escape ($ db_name )))->getRow () !== null )
98+ {
99+ return true ;
100+ }
101+
102+ return parent ::createDatabase ($ db_name );
103+ }
104+
105+ //--------------------------------------------------------------------
106+
80107 /**
81108 * CREATE TABLE attributes
82109 *
You can’t perform that action at this time.
0 commit comments