dev-master
9999999-devMigrations for Couchbase in a Symfony bundle.
The Requires
- php >=7.0
- symfony/console 3.4.*
- symfony/symfony 3.4.*
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Migrations for Couchbase in a Symfony bundle.
With this Symfony bundle you can generate and execute migrations on a Couchbase database. It works kind of like Doctrine Migrations. * Generate blank migrations and fill them to e.g. make new indexes on buckets or upsert/remove documents. * It keeps in check which migrations that have already been executed and which still need to be done. * Usable via Symfony commands., (*1)
Install the bundle via composer., (*2)
composer require bowlofsoup/couchbase-migrations-bundle
Add the bundle to your AppKernel.php., (*3)
$bundles = [
...
new \BowlOfSoup\CouchbaseMigrationsBundle\CouchbaseMigrationsBundle()
...
];
Create a CouchbaseMigrations directory in your app directory. This will contain all the generated blank migrations., (*4)
$ mkdir CouchbaseMigrations
Add the config/packages/couchbase_migrations_bundle.yaml., (*5)
couchbase_migrations:
host: '%env(COUCHBASE_MIGRATIONS_HOST)%'
user: '%env(COUCHBASE_MIGRATIONS_USER)%'
password: '%env(COUCHBASE_MIGRATIONS_PASSWORD)%'
bucket_migrations: '%env(COUCHBASE_MIGRATIONS_BUCKET_MIGRATIONS)%'
bucket_default: '%env(COUCHBASE_MIGRATIONS_DEFAULT_BUCKET)%'
Optional you can also configure the port if it is not standard., (*6)
couchbase_migrations:
port: '%env(COUCHBASE_MIGRATIONS_PORT)%'
In your .env file, define the above values:, (*7)
COUCHBASE_MIGRATIONS_HOST="127.0.0.1" COUCHBASE_MIGRATIONS_USER="couchbase_user" COUCHBASE_MIGRATIONS_PASSWORD="couchbase_password" COUCHBASE_MIGRATIONS_BUCKET_MIGRATIONS="default" COUCHBASE_MIGRATIONS_DEFAULT_BUCKET="default"
bucket_migrations must hold the bucket in which you want to store which migration has already been done.
Not mandatory, but then a bucket named migrations must be accessible.bucket_default must hold your default bucket.
If you do not have a default bucket, use %couchbase_migrations.bucket_migrations% as option.bin/console couchbase:migrations:generate
This will generate a blank migration file in CouchbaseMigrations/ for you to fill., (*8)
bin/console couchbase:migrations:migrate
This will execute all migrations that still need to be done., (*9)
The configured bucket (bucket_migrations in the config file) will contain a document (migrations::versions) which contains the already migrated., (*10)
bin/console couchbase:migrations:execute VERSION_NUMBER [--no-verbose] [--down]
This will execute the given version (file in CouchbaseMigrations/).
Replace VERSION_NUMBER with the version (date-time part of the file) you want to execute.
You can execute a version indefinitely: will not be kept track of., (*11)
bin/console couchbase:migrations:flush-data BUCKET_NAME
Flushes all the data in a bucket, if flushing is enabled for that bucket, except the migrations version document.
Replace BUCKET_NAME with the name of the bucket for which you want the data to be flushed. Defaults to the configured bucket., (*12)
Can be handy if you want to reset all data in a bucket, but do not want to lose your migrations., (*13)
bin/console couchbase:migrations:create-bucket [BUCKET_NAME] [--index]
Creates a bucket, optional name (takes default configured) and optionally creating a primary index., (*14)
bin/console couchbase:migrations:remove-bucket [BUCKET_NAME]
Creates a bucket, optional name (takes default configured)., (*15)
When you have generated a migration, open the file and use the up function. Example:, (*16)
public function up()
{
// Use only if you want to select a different bucket than the one configured.
$this->selectBucket('some-other-bucket-name');
$this->bucketRepository->query(
'CREATE INDEX `i_someindexname` ON `bucketname`(`propertyname`) WHERE (`someotherpropertyname` = "propertycontent")'
);
}
or, (*17)
public function up()
{
// Use only if you want to select a different bucket than the one configured.
$bucket = $this->selectBucket('some-other-bucket-name');
$result = $bucket->get('some-document-key');
$documentContent = $result->value;
$bucket->insert('some-other-document-key', $documentContent);
}
Downgrading is also supported for the execute command, just add a method down() to the migration., (*18)
public function down()
{
$this->bucketRepository->query(
'the opposite of the up() query'
);
}
So:
* The $this->bucketRepository can be used to make it easier to do queries on a bucket (like named parameters).
* You can also directly do actions on the bucket by using the return value of $this->selectBucket()., (*19)
Note: The Bucket returned by selectBucket() and the result returned by $bucket->get() are both small backwards-
compatible classes to not break old migrations defined with the previous version of this bundle. If you need the actual
Couchbase Bucket, you can use $bucket->getBucket(), if you need the actual Couchbase result you can get that by calling
$result->getResult()., (*20)
You can use this bundle as standalone application, so, not use it within a Symfony installation. This is also perfect for development., (*21)
app directory within the src directory.src/CouchbaseMigrations directory.src/app/parameters.yml which holds the config parameters.Fill the src/app/parameters.yml file with:, (*22)
parameters: kernel.project_dir: couchbase_bucket: couchbase_migrations.bucket_migrations: couchbase_migrations.host: couchbase_migrations.user: couchbase_migrations.password:
kernel.project_dir option must hold the full path the the src directory.You are more then welcome to fork this repository, make changes and create a pull request back., (*23)
vendor/bin/php-cs-fixer fix before you commit code changes. This will make the changed code adhere to the coding standards.When you're upgrading from version 1.0, you need to do two things:, (*24)
app/ directory is gone in new Symfony versions, the CouchbaseMigrations folder should now be in the root of your projectMigrations for Couchbase in a Symfony bundle.