-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to Create Migration #90
Comments
Are you referring to a create-table migration? just create a normal migration file: <?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateFlightsTable extends Migration
{
public function up()
{
// follow dynamodb PHP sdk docs http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#createtable
}
public function down()
{
// follow http://docs.aws.amazon.com/aws-sdk-php/v3/api/api-dynamodb-2012-08-10.html#deletetable
}
} |
DynamoDb does not require a schema, so the traditional migration pattern does not apply. You can use CloudFormation templates, and/or the AWS CLI, to create your DynamoDb tables programmatically. I wrote a small artisan command that uses the AWS SDK and allows me to run a handful of commands against DynamoDB. One of these commands is "create-table" which takes the name(s) of a model and then loads up the JSON definition of the table from my database folder and creates the table, it also uses the models "version" property (another additive) to make sure that I have version controlled tables. I can share this command later today when I have access to my workstation. |
This is just a rough console command I wrote, it has a few conveniences built into it. I didn't write it with the intent to share it, so it's pretty rough around the edges.
And in my
The model looks something like this
|
I agree with @zoul0813 , that is why I get confused on how to create the migration using how about adding an example migration in the |
@dashawk I'm not sure what part of my comments you're agreeing with. I agree with @baopham, if you want to use the migration logic in Laravel. I proposed an alternative solution by providing a custom artisan command I wrote for managing my DynamoDB resources. Here's a sample migration class, this uses a copy of the code from my 'create-table' block above, with a local PHP Array representing the table schema. It also includes a 'deleteTable' in the Refer to the DynamoDB API Documentation for more options. http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_CreateTable.html If the schema has a "Tags" key in the array, then the code will wait 5s to give AWS enough time to create the resource before calling the
@baopham feel free to clean this sample up and provide a reference to it in the README.md |
Thanks @zoul0813! There are many good points in this discussion so I'll just reference it in the FAQ :) |
This is not ready for the README.md but could go into the wiki. First consider installing the following:
|
Any suggest how to make migration works ?? follow @zoul0813 , didn't works. |
@JuniYadi , the error you have is because is trying to create a migration table in your default database, which is a MySQL database. Migrations require a database to register which migrations have been run already, and which ones need to be run. So you need to configure one or use another method to create your table. |
I gonna put this here as just an example of how I'm using it. <?php
use BaoPham\DynamoDb\DynamoDbClientInterface;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
return new class extends Migration
{
protected DynamoDbClientInterface $dynamoDbClient;
public function __construct()
{
$this->dynamoDbClient = App::make(DynamoDbClientInterface::class);
}
/**
* Run the migrations.
*/
public function up(): void
{
$this->dynamoDbClient->getClient()->createTable([
'TableName' => 'user',
'AttributeDefinitions' => [
[
'AttributeName' => 'id',
'AttributeType' => 'S'
]
],
'KeySchema' => [
[
'AttributeName' => 'id',
'KeyType' => 'HASH'
]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
]
]);
$this->dynamoDbClient->getClient()->waitUntil('TableExists', [
'TableName' => 'user'
]);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
$this->dynamoDbClient->getClient()->deleteTable([
'TableName' => 'user'
]);
$this->dynamoDbClient->getClient()->waitUntil('TableNotExists', [
'TableName' => 'user'
]);
}
}; |
Thank you for your good idea @allanzi . I also created a command to create a table using the code below to make the table easier, maybe it will help others. Please create DynamoDBMakeCommand.php file: <?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Filesystem\Filesystem;
class DynamoDBMakeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dynamodb:make {name}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Make an Table Class';
/**
* Filesystem instance
* @var Filesystem
*/
protected $files;
/**
* Create a new command instance.
* @param Filesystem $files
*/
public function __construct(Filesystem $files)
{
parent::__construct();
$this->files = $files;
}
/**
* Execute the console command.
*/
public function handle(): void
{
$path = $this->getSourceFilePath();
$this->makeDirectory(dirname($path));
$contents = $this->getSourceFile();
if (!$this->files->exists($path)) {
$this->files->put($path, $contents);
$this->components->info(sprintf('Migration [%s] created successfully.', $path));
} else {
$this->components->warn(sprintf('Migration [%s] already exits', $path));
}
}
/**
* Return the stub file path
* @return string
*
*/
public function getStubPath(): string
{
return base_path('stubs/dynamodb.stub');
}
/**
**
* Map the stub variables present in stub to its value
*
* @return array
*
*/
public function getStubVariables(): array
{
return [
'table' => $this->argument('name'),
];
}
/**
* Get the stub path and the stub variables
*
* @return bool|mixed|string
*
*/
public function getSourceFile(): mixed
{
return $this->getStubContents($this->getStubPath(), $this->getStubVariables());
}
/**
* Replace the stub variables(key) with the desire value
*
* @param $stub
* @param array $stubVariables
* @return string|array|bool
*/
public function getStubContents($stub, array $stubVariables = []): string|array|bool
{
$contents = file_get_contents($stub);
foreach ($stubVariables as $search => $replace) {
$contents = str_replace(['{{ table }}', '{{table}}'], $replace, $contents);
}
return $contents;
}
/**
* Get the full path of generate class
*
* @return string
*/
public function getSourceFilePath(): string
{
$prefixDate=date('Y_m_d_His');
$fileName = "{$prefixDate}_create_{$this->argument('name')}_table.php";
return database_path('migrations') . '/' . $fileName;
}
/**
* Build the directory for the class if necessary.
*
* @param string $path
* @return string
*/
protected function makeDirectory(string $path): string
{
if (!$this->files->isDirectory($path)) {
$this->files->makeDirectory($path, 0777, true, true);
}
return $path;
}
} also create folder stubs in root project and put on dynamodb.stub file: <?php
use BaoPham\DynamoDb\DynamoDbClientInterface;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\App;
return new class extends Migration
{
protected DynamoDbClientInterface $dynamoDbClient;
public function __construct()
{
$this->dynamoDbClient = App::make(DynamoDbClientInterface::class);
}
/**
* Run the migrations.
*/
public function up(): void
{
$this->dynamoDbClient->getClient()->createTable([
'TableName' => '{{ table }}',
'AttributeDefinitions' => [
[
'AttributeName' => 'id',
'AttributeType' => 'S'
]
],
'KeySchema' => [
[
'AttributeName' => 'id',
'KeyType' => 'HASH'
]
],
'ProvisionedThroughput' => [
'ReadCapacityUnits' => 10,
'WriteCapacityUnits' => 20
]
]);
$this->dynamoDbClient->getClient()->waitUntil('TableExists', [
'TableName' => '{{ table }}'
]);
}
/**
* Reverse the migrations.
*/
public function down(): void
{
$this->dynamoDbClient->getClient()->deleteTable([
'TableName' => '{{ table }}'
]);
$this->dynamoDbClient->getClient()->waitUntil('TableNotExists', [
'TableName' => '{{ table }}'
]);
}
}; |
How can we create migrations using this package?
The text was updated successfully, but these errors were encountered: